scc

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

commit 88b40779476d653a5777d0376e78fed4d1e2a938
parent 2fa266a3a26606a1a1bee7328157b5072302d638
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 12 Dec 2016 10:32:06 +0100

Revert "[cc2] Minimal fix for type symbols"

This reverts commit 5c3445da52cc656a9c415d1dcb8142ab9beb7400.

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 | 25++++++++++++++++---------
6 files changed, 22 insertions(+), 15 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, 1); + sym = getsym(TMPSYM); 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, 1); + sym = getsym(TMPSYM); 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, int islocal); +extern Symbol *getsym(unsigned id); 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, 1); + Symbol *sym = getsym(TMPSYM); 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), 0); + sym = getsym(atoi(token+1)); 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), 1); + Symbol *sym = getsym(atoi(token+1)); sclass = u.op >> 8; np->u.sym = sym; diff --git a/cc2/symbol.c b/cc2/symbol.c @@ -33,24 +33,31 @@ pushctx(void) void popctx(void) { - Symbol *sym, *next, **psym; + Symbol *sym, *next; infunction = 0; for (sym = locals; sym; sym = next) { next = sym->next; - if (sym->id != TMPSYM) { - psym = &symtab[sym->id & NR_SYMHASH-1]; - while (*psym != sym) - psym = &(*psym)->next; - *psym = sym->h_next; - } + /* + * Symbols are inserted in the hash in the inverted + * order they are found in locals and it is impossible + * to have a global over a local, because a local is + * any symbol defined in the body of a function, + * even if it has extern linkage. + * For this reason when we raich a symbol in the + * locals list we know that it is the head of it + * collision list and we can remove it assigning + * it h_next to the hash table position + */ + if (sym->id != TMPSYM) + symtab[sym->id & NR_SYMHASH-1] = sym->h_next; freesym(sym); } curlocal = locals = NULL; } Symbol * -getsym(unsigned id, int islocal) +getsym(unsigned id) { Symbol **htab, *sym; static unsigned short num; @@ -68,7 +75,7 @@ getsym(unsigned id, int islocal) sym->id = id; if ((sym->numid = ++num) == 0) error(EIDOVER); - if (infunction && islocal) { + if (infunction) { if (!locals) locals = sym; if (curlocal)