Skip to content

Commit

Permalink
Allocate Node objects on arena
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhsnn committed Dec 21, 2024
1 parent 35fdac6 commit c7963b4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct Page {
Page *next;
};

Arena node_arena;
Arena pp_arena;
bool free_alloc;

Expand Down
10 changes: 8 additions & 2 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ bool is_const_var(Obj *var) {
}

static Node *new_node(NodeKind kind, Token *tok) {
Node *node = calloc(1, sizeof(Node));
Node *node = arena_calloc(&node_arena, sizeof(Node));
node->kind = kind;
node->tok = tok;
return node;
Expand Down Expand Up @@ -313,7 +313,7 @@ Node *new_cast(Node *expr, Type *ty) {
return expr;
}
}
Node *node = malloc(sizeof(Node));
Node *node = arena_malloc(&node_arena, sizeof(Node));
*node = tmp_node;
return node;
}
Expand Down Expand Up @@ -4575,15 +4575,19 @@ Obj *parse(Token *tok) {
Obj head = {0};
Obj *cur = &head;
while (tok->kind != TK_EOF) {
arena_on(&node_arena);

if (equal(tok, "_Static_assert") || equal_kw(tok, "static_assert")) {
static_assertion(&tok, tok->next);
arena_off(&node_arena);
continue;
}

if (equal_kw(tok, "asm") || equal(tok, "__asm") || equal(tok, "__asm__")) {
cur = cur->next = calloc(1, sizeof(Obj));
cur->asm_str = str_tok(&tok, skip(tok->next, "("));
tok = skip(tok, ")");
arena_off(&node_arena);
continue;
}

Expand All @@ -4593,11 +4597,13 @@ Obj *parse(Token *tok) {
// Typedef
if (attr.is_typedef) {
parse_typedef(&tok, tok, basety, &attr);
arena_off(&node_arena);
continue;
}

// Global declarations
tok = global_declaration(tok, basety, &attr);
arena_off(&node_arena);
}

for (Obj *var = globals; var; var = var->next)
Expand Down
4 changes: 4 additions & 0 deletions preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,13 @@ static int64_t eval_const_expr(Token *tok) {
if (tok->kind == TK_EOF)
error_tok(start, "no expression");

arena_on(&node_arena);

Token *end;
int64_t val = const_expr(&end, tok);

arena_off(&node_arena);

if (end->kind != TK_EOF)
error_tok(end, "extra token");
return val;
Expand Down
1 change: 1 addition & 0 deletions slimcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void *arena_calloc(Arena *a, size_t sz);
void *arena_malloc(Arena *a, size_t sz);
bool check_mem_usage(void);

extern Arena node_arena;
extern Arena pp_arena;
extern bool free_alloc;

Expand Down

0 comments on commit c7963b4

Please sign in to comment.