scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 49121885af59183def0534697a2414733ac881cf
parent 8b151b2043335063de44fa993d8bfd48bdbb3a99
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 22 Dec 2016 15:02:21 +0100

[cc1] Make symbol table an ordered hash table again

Since cpp symbols were allocated in the main hash table we had to deal
with symbols inserted out of order, because cpp symbols should stay
in the table forever. This is not needed anymore since we are inserting
cpp symbols in a different table we can get rid of this code.

Diffstat:
Mcc1/cc1.h | 2+-
Mcc1/code.c | 2+-
Mcc1/decl.c | 20++++++--------------
Mcc1/expr.c | 2+-
Mcc1/fold.c | 4++--
Mcc1/init.c | 2+-
Mcc1/lex.c | 4++--
Mcc1/symbol.c | 92++++++++++++++++++++++++++-----------------------------------------------------
8 files changed, 44 insertions(+), 84 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -371,7 +371,7 @@ extern void dumpstab(char *msg); extern Symbol *lookup(int ns, char *name, int alloc); extern Symbol *nextsym(Symbol *sym, int ns); extern Symbol *install(int ns, Symbol *sym); -extern Symbol *newsym(int ns); +extern Symbol *newsym(int ns, char *name); extern void pushctx(void), popctx(void); extern void killsym(Symbol *sym); extern Symbol *newlabel(void); diff --git a/cc1/code.c b/cc1/code.c @@ -501,7 +501,7 @@ sizeofnode(Type *tp) { Symbol *sym; - sym = newsym(NS_IDEN); + sym = newsym(NS_IDEN, NULL); sym->type = sizettype; sym->u.i = tp->size; return constnode(sym); diff --git a/cc1/decl.c b/cc1/decl.c @@ -334,9 +334,11 @@ directdcl(struct declarators *dp, unsigned ns) } else { if (yytoken == IDEN || yytoken == TYPEIDEN) { sym = yylval.sym; + if (sym->ctx != curctx) + sym = newsym(ns, yytext); next(); } else { - sym = newsym(ns); + sym = newsym(ns, NULL); } push(dp, IDEN, sym); } @@ -481,7 +483,7 @@ newtag(void) next(); break; default: - sym = newsym(NS_TAG); + sym = newsym(NS_TAG, NULL); break; } if (!sym->type) { @@ -760,20 +762,10 @@ identifier(struct decl *dcl) errorp("declared variable '%s' of incomplete type", name); } - if (tp->op != FTN) { - sym = install(NS_IDEN, sym); - } else { + sym = install(NS_IDEN, sym); + if (tp->op == FTN) { if (sclass == NOSCLASS) sclass = EXTERN; - /* - * Ugly workaround to solve function declarations. - * A new context is added for the parameters, - * so at this point curctx is incremented by - * one since sym was parsed. - */ - --curctx; - sym = install(NS_IDEN, sym); - ++curctx; if (!strcmp(name, "main") && tp->type != inttype) { errorp("main shall be defined with a return type of int"); errorp("please contact __20h__ on irc.freenode.net (#bitreich-en) via IRC"); diff --git a/cc1/expr.c b/cc1/expr.c @@ -637,7 +637,7 @@ notdefined(Symbol *sym) expect(')'); isdef = (sym->flags & SDECLARED) != 0; - sym = newsym(NS_IDEN); + sym = newsym(NS_IDEN, NULL); sym->type = inttype; sym->flags |= SCONSTANT; sym->u.i = isdef; diff --git a/cc1/fold.c b/cc1/fold.c @@ -301,7 +301,7 @@ foldconst(int type, int op, Type *tp, Symbol *ls, Symbol *rs) return NULL; break; } - sym = newsym(NS_IDEN); + sym = newsym(NS_IDEN, NULL); sym->type = tp; sym->u = aux.u; return constnode(sym); @@ -578,7 +578,7 @@ castcode(Node *np, Type *newtp) goto noconstant; } - sym = newsym(NS_IDEN); + sym = newsym(NS_IDEN, NULL); np->type = sym->type = newtp; np->sym = sym; sym->u = aux.u; diff --git a/cc1/init.c b/cc1/init.c @@ -170,7 +170,7 @@ mkcompound(Init *ip) } } - sym = newsym(NS_IDEN); + sym = newsym(NS_IDEN, NULL); sym->u.init = v; sym->type = ip->type; sym->flags |= SINITLST; diff --git a/cc1/lex.c b/cc1/lex.c @@ -334,7 +334,7 @@ integer(char *s, char base) convert: tp = ctype(INT, sign, size); - sym = newsym(NS_IDEN); + sym = newsym(NS_IDEN, NULL); sym->type = tp; sym->flags |= SCONSTANT; yylval.sym = readint(s, base, sign, sym); @@ -449,7 +449,7 @@ character(void) else ++input->p; - sym = newsym(NS_IDEN); + sym = newsym(NS_IDEN, NULL); sym->u.i = c; sym->type = inttype; yylval.sym = sym; diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -57,18 +57,13 @@ hash(const char *s) static void unlinkhash(Symbol *sym) { - Symbol **tab, **h, *p, *prev; + Symbol **tab, **h; if ((sym->flags & SDECLARED) == 0) return; tab = (sym->ns == NS_CPP) ? htabcpp : htab; h = &tab[hash(sym->name)]; - for (prev = p = *h; p != sym; prev = p, p = p->hash) - /* nothing */; - if (prev == p) - *h = sym->hash; - else - prev->hash = sym->hash; + *h = sym->next; } void @@ -142,7 +137,24 @@ newid(void) } static Symbol * -allocsym(int ns, char *name) +linksym(Symbol *sym) +{ + Symbol *p, *prev; + + switch (sym->ns) { + case NS_CPP: + return sym; + case NS_LABEL: + sym->next = labels; + return labels = sym; + default: + sym->next = head; + return head = sym; + } +} + +Symbol * +newsym(int ns, char *name) { Symbol *sym; @@ -158,73 +170,29 @@ allocsym(int ns, char *name) sym->u.s = NULL; sym->type = NULL; sym->next = sym->hash = NULL; - return sym; -} - -static Symbol * -linksym(Symbol *sym) -{ - Symbol *p, *prev; - - switch (sym->ns) { - case NS_CPP: - return sym; - case NS_LABEL: - sym->next = labels; - return labels = sym; - default: - for (prev = p = head; p; prev = p, p = p->next) { - if (p->ctx <= sym->ctx) - break; - } - if (p == prev) { - sym->next = head; - head = sym; - } else { - p = prev->next; - prev->next = sym; - sym->next = p; - } - return sym; - } + return linksym(sym); } static Symbol * linkhash(Symbol *sym) { - Symbol **tab, **h, *p, *prev; + Symbol **tab, **h; tab = (sym->ns == NS_CPP) ? htabcpp : htab; h = &tab[hash(sym->name)]; - for (prev = p = *h; p; prev = p, p = p->hash) { - if (p->ctx <= sym->ctx) - break; - } - if (p == prev) { - sym->hash = *h; - *h = sym; - } else { - p = prev->hash; - prev->hash = sym; - sym->hash = p; - } + sym->hash = *h; + *h = sym; if (sym->ns != NS_CPP) sym->id = newid(); sym->flags |= SDECLARED; - return linksym(sym); -} - -Symbol * -newsym(int ns) -{ - return linksym(allocsym(ns, NULL)); + return sym; } Symbol * newstring(char *s, size_t len) { - Symbol *sym = newsym(NS_IDEN); + Symbol *sym = newsym(NS_IDEN, NULL); if (lexmode != CPPMODE) sym->type = mktype(chartype, ARY, len, NULL); @@ -240,7 +208,7 @@ newstring(char *s, size_t len) Symbol * newlabel(void) { - Symbol *sym = newsym(NS_LABEL); + Symbol *sym = newsym(NS_LABEL, NULL); sym->id = newid(); return sym; } @@ -273,7 +241,7 @@ lookup(int ns, char *name, int alloc) return sym; } } - return (alloc == ALLOC) ? allocsym(ns, name) : NULL; + return (alloc == ALLOC) ? newsym(ns, name) : NULL; } Symbol * @@ -282,7 +250,7 @@ install(int ns, Symbol *sym) if (sym->flags & SDECLARED) { if (sym->ctx == curctx && ns == sym->ns) return NULL; - sym = allocsym(ns, sym->name); + sym = newsym(ns, sym->name); } return linkhash(sym); } @@ -293,7 +261,7 @@ keywords(struct keyword *key, int ns) Symbol *sym; for ( ; key->str; ++key) { - sym = linkhash(allocsym(ns, key->str)); + sym = linkhash(newsym(ns, key->str)); sym->token = key->token; sym->u.token = key->value; }