fela

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 99c1d0c53ab240683b7715c1e6065dbb6fa78296
parent 2cfda5fdf657c12a49ff6ac35c78a906e4f6414e
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Mon,  3 Nov 2025 22:44:32 +0000

switched to an arena allocator/ctx allocator

Diffstat:
MMakefile | 4++--
Mast.c | 5+++--
Mast.h | 3++-
Mconfig.mk | 4++--
Mparse.y | 21++++++++-------------
Mtest.fe | 4++--
Autil.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mutil.h | 29++++++++++++-----------------
8 files changed, 103 insertions(+), 39 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,12 +1,12 @@ include config.mk -SRC = parse.c lex.c types.c ast.c +SRC = parse.c lex.c types.c ast.c util.c OBJ = ${SRC:.c=.o} all: fela fela: ${OBJ} - ${CC} -o fela ${OBJ} ${LIBS} + ${CC} -o fela ${OBJ} ${LIBS} .c.o: ${CC} ${CFLAGS} -c $< diff --git a/ast.c b/ast.c @@ -1,10 +1,11 @@ #include <string.h> #include <stdlib.h> #include "ast.h" +#include "util.h" struct ast_args * append_arg(struct ast_args *args, struct ast_arg *arg) { - args->args = realloc(args->args, ++args->argc * sizeof(struct ast_arg)); - memcpy(&args->args[args->argc], arg, sizeof(struct ast_arg)); + args->args = reallocz(args->args, ++args->argc * sizeof(struct ast_arg)); + memcpy(&args->args[args->argc - 1], arg, sizeof(struct ast_arg)); return args; } diff --git a/ast.h b/ast.h @@ -28,7 +28,8 @@ struct ast_expr { int child_count; struct ast_expr *children; }; - struct { + struct { /* for function decs */ + struct ast_args *args; struct ast_exprs *exprs; struct ast_decs *decs; }; diff --git a/config.mk b/config.mk @@ -1,7 +1,7 @@ -LIBS = -lfl +LIBS = -lfl -fsanitize=address CC = cc -CFLAGS = -ggdb -Werror -Wpedantic +CFLAGS = -ggdb -Werror -Wpedantic # bison YACC = bison diff --git a/parse.y b/parse.y @@ -114,16 +114,17 @@ exprs : exprs expr ';' { } ; -func : '(' ')' RETTYPE type '{' decs exprs '}' { +func : '(' args ')' RETTYPE type '{' decs exprs '}' { $$ = alloczt(struct ast_expr); $$->op = FUNCTION; - $$->decs = $6; - $$->exprs = $7; + $$->args = $2; + $$->decs = $7; + $$->exprs = $8; } - | '(' ')' RETTYPE type '{' exprs '}' { + | '(' args ')' RETTYPE type '{' exprs '}' { $$ = alloczt(struct ast_expr); $$->op = FUNCTION; - $$->exprs = $6; + $$->exprs = $7; } ; @@ -197,18 +198,11 @@ type : NAME { $$->t = BASIC_T; $$->type = strslice($1, yyleng); } - | FUNC '(' args ')' RETTYPE type { - $$ = alloczt(struct ast_type); - $$->t = FUNCTION_T; - $$->ret = $6; - $$->args = $3; - } | FUNC '(' ')' RETTYPE type { $$ = alloczt(struct ast_type); $$->t = FUNCTION_T; $$->ret = $5; } - ; args : args ',' arg { @@ -239,6 +233,7 @@ yyerror(const char *msg) { int main() { + gctx = newctx(); yyparse(); - typecheck(body); + freectx(gctx); } diff --git a/test.fe b/test.fe @@ -1,4 +1,4 @@ -func(int a) -> int -main = () -> int { +func() -> int +main = (int a) -> int { rand(); }; diff --git a/util.c b/util.c @@ -0,0 +1,72 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <signal.h> + +#define allocztn(type, n) allocz(sizeof(type) * n) +#define alloczt(type) allocz(sizeof(type)) + +typedef struct mctx { + int ptrc; + void **ptrs; +} mctx; + +mctx *gctx; + +void * +alloczctx(mctx *ctx, const size_t size) { + ctx->ptrs = realloc(ctx->ptrs, sizeof(void *) * (ctx->ptrc + 1)); + ctx->ptrs[ctx->ptrc] = malloc(size); + memset(ctx->ptrs[ctx->ptrc], 0, size); + return ctx->ptrs[ctx->ptrc++]; +} + +void * +realloczctx(mctx *ctx, void *ptr, const size_t size) { + if (!ptr) + return alloczctx(ctx, size); + + for (int i = 0; i < ctx->ptrc; i++) { + if (ctx->ptrs[i] == ptr) { + ctx->ptrs[i] = realloc(ctx->ptrs[i], size); + memset(ctx->ptrs[i], 0, size); + return ctx->ptrs[i]; + } + } + kill(getpid(), SIGSEGV); +} + +void * +reallocz(void *ptr, const size_t size) { + return realloczctx(gctx, ptr, size); +} + +void * +allocz(const size_t size) { + return alloczctx(gctx, size); +} + +mctx * +newctx() { + mctx *ctx = malloc(sizeof(mctx)); + memset(ctx, 0, sizeof(mctx)); + return ctx; +} + +void +freectx(mctx *ctx) { + for (int i = 0; i < ctx->ptrc; i++) + free(ctx->ptrs[i]); + free(ctx->ptrs); + free(ctx); +} + +char * +strslice(char *s, int len) { + char *slice = allocz(len + 1); + memcpy(slice, s, len); + return slice; +} + +/* TODO make this allocate to an arena that can be freed */ diff --git a/util.h b/util.h @@ -1,22 +1,17 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - #define allocztn(type, n) allocz(sizeof(type) * n) #define alloczt(type) allocz(sizeof(type)) -void * -allocz(const size_t size) { - void *p = malloc(size); - memset(p, 0, size); - return p; -} +typedef struct mctx { + int ptrc; + void **ptrs; +} mctx; -char * -strslice(char *s, int len) { - char *slice = allocz(len + 1); - memcpy(slice, s, len); - return slice; -} +extern mctx *gctx; -/* TODO make this allocate to an arena that can be freed */ +void *alloczctx(mctx *ctx, const size_t size); +void *realloczctx(mctx *ctx, void *ptr, const size_t size); +void *reallocz(void *ptr, const size_t size); +void *allocz(const size_t size); +mctx *newctx(); +void freectx(mctx *ctx); +char *strslice(char *s, int len);