Skip to content

Commit

Permalink
Free parsed tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhsnn committed Dec 22, 2024
1 parent cae73be commit 49a5a39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3589,6 +3589,8 @@ static void struct_members(Token **rest, Token *tok, Type *ty) {
Member *mem = ast_arena_calloc(sizeof(Member));
mem->alt_align = attr.align;
mem->ty = declarator2(&tok, tok, basety, &mem->name, &mem->alt_align);
if (mem->name && !current_fn)
mem->name->is_live = true;

for (Type *t = mem->ty; t; t = t->base)
if (t->kind == TY_VLA)
Expand Down Expand Up @@ -4558,16 +4560,40 @@ static Token *global_declaration(Token *tok, Type *basety, VarAttr *attr) {
return tok;
}

static Token *free_parsed_tok(Token *tok, Token *end) {
while (tok != end) {
for (Token *t = tok->attr_next; t;) {
Token *nxt_attr = t->attr_next;
while (t) {
Token *nxt_t = t->next;
free(t);
t = nxt_t;
}
t = nxt_attr;
}
Token *nxt = tok->next;
if (!tok->is_live)
free(tok);
tok = nxt;
}
return end;
}

// program = (typedef | function-definition | global-variable)*
Obj *parse(Token *tok) {
globals = NULL;

Token *free_head = tok;
Obj head = {0};
Obj *cur = &head;
while (tok->kind != TK_EOF) {
if (free_alloc)
free_head = free_parsed_tok(free_head, tok);

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, "("));
cur->asm_str->is_live = true;
tok = skip(tok, ")");
continue;
}
Expand Down
1 change: 1 addition & 0 deletions slimcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ struct Token {
bool dont_expand; // True if a macro token is encountered during the macro's expansion
bool is_incl_guard;
bool is_root;
bool is_live;
Token *origin; // If this is expanded from a macro, the original token
Token *attr_next;
Token *alloc_next;
Expand Down

0 comments on commit 49a5a39

Please sign in to comment.