scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 5c3445da52cc656a9c415d1dcb8142ab9beb7400
parent a63a49c2947aff8282f161ebcf30e64103f505a1
Author: Quentin Carbonneaux <quentin@c9x.me>
Date:   Sun, 11 Dec 2016 21:19:37 -0500

[cc2] Minimal fix for type symbols

This also fixes a hash removal bug in popctx().

Diffstat:
Mcc2/arch/qbe/cgen.c | 2+-
Mcc2/arch/qbe/optm.c | 2+-
Mcc2/cc2.h | 2+-
Mcc2/code.c | 2+-
Mcc2/parser.c | 4++--
Mcc2/symbol.c | 14+++++++++-----
6 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c @@ -102,7 +102,7 @@ tmpnode(Node *np, Type *tp) if (!np) np = newnode(OTMP); - sym = getsym(TMPSYM); + sym = getsym(TMPSYM, 1); sym->type = np->type = *tp; sym->kind = STMP; np->u.sym = sym; diff --git a/cc2/arch/qbe/optm.c b/cc2/arch/qbe/optm.c @@ -41,7 +41,7 @@ optm_dep(Node *np) break; case OBRANCH: if (!next->label) { - sym = getsym(TMPSYM); + sym = getsym(TMPSYM, 1); sym->kind = SLABEL; next->label = sym; } diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -236,7 +236,7 @@ extern Node *nextstmt(void); /* symbol.c */ #define TMPSYM 0 -extern Symbol *getsym(unsigned id); +extern Symbol *getsym(unsigned id, int islocal); extern void popctx(void); extern void pushctx(void); extern void freesym(Symbol *sym); diff --git a/cc2/code.c b/cc2/code.c @@ -61,7 +61,7 @@ addr(Node *np, Addr *addr) Symbol * newlabel(void) { - Symbol *sym = getsym(TMPSYM); + Symbol *sym = getsym(TMPSYM, 1); sym->kind = SLABEL; return sym; diff --git a/cc2/parser.c b/cc2/parser.c @@ -178,7 +178,7 @@ composed(char *token, union tokenop u) { Symbol *sym; - sym = getsym(atoi(token+1)); + sym = getsym(atoi(token+1), 0); push(&sym->type); } @@ -192,7 +192,7 @@ static void symbol(char *token, union tokenop u) { Node *np = newnode(u.op & 0xFF); - Symbol *sym = getsym(atoi(token+1)); + Symbol *sym = getsym(atoi(token+1), 1); sclass = u.op >> 8; np->u.sym = sym; diff --git a/cc2/symbol.c b/cc2/symbol.c @@ -33,20 +33,24 @@ pushctx(void) void popctx(void) { - Symbol *sym, *next; + Symbol *sym, *next, **psym; infunction = 0; for (sym = locals; sym; sym = next) { next = sym->next; - if (sym->id != TMPSYM) - symtab[sym->id & NR_SYMHASH-1] = sym->h_next; + if (sym->id != TMPSYM) { + psym = &symtab[sym->id & NR_SYMHASH-1]; + while (*psym != sym) + psym = &(*psym)->next; + *psym = sym->h_next; + } freesym(sym); } curlocal = locals = NULL; } Symbol * -getsym(unsigned id) +getsym(unsigned id, int islocal) { Symbol **htab, *sym; static unsigned short num; @@ -64,7 +68,7 @@ getsym(unsigned id) sym->id = id; if ((sym->numid = ++num) == 0) error(EIDOVER); - if (infunction) { + if (infunction && islocal) { if (!locals) locals = sym; if (curlocal)