ref: 8cd0cd7c7f5ae4003bcb9e95cd8e3f81a63d30a3
parent: 3d8f9560acd89ec538d4262919159988409cc802
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Dec 22 10:02:21 EST 2016
[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.
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -371,7 +371,7 @@
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);
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -501,7 +501,7 @@
{
Symbol *sym;
- sym = newsym(NS_IDEN);
+ sym = newsym(NS_IDEN, NULL);
sym->type = sizettype;
sym->u.i = tp->size;
return constnode(sym);
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -334,9 +334,11 @@
} 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 @@
next();
break;
default:
- sym = newsym(NS_TAG);
+ sym = newsym(NS_TAG, NULL);
break;
}
if (!sym->type) {
@@ -760,20 +762,10 @@
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");
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -637,7 +637,7 @@
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;
--- a/cc1/fold.c
+++ b/cc1/fold.c
@@ -301,7 +301,7 @@
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 @@
goto noconstant;
}
- sym = newsym(NS_IDEN);
+ sym = newsym(NS_IDEN, NULL);
np->type = sym->type = newtp;
np->sym = sym;
sym->u = aux.u;
--- a/cc1/init.c
+++ b/cc1/init.c
@@ -170,7 +170,7 @@
}
}
- sym = newsym(NS_IDEN);
+ sym = newsym(NS_IDEN, NULL);
sym->u.init = v;
sym->type = ip->type;
sym->flags |= SINITLST;
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -334,7 +334,7 @@
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 @@
else
++input->p;
- sym = newsym(NS_IDEN);
+ sym = newsym(NS_IDEN, NULL);
sym->u.i = c;
sym->type = inttype;
yylval.sym = sym;
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -57,18 +57,13 @@
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,8 +137,25 @@
}
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;
sym = xmalloc(sizeof(*sym));
@@ -158,73 +170,29 @@
sym->u.s = NULL;
sym->type = NULL;
sym->next = sym->hash = NULL;
- return sym;
+ return linksym(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;
- }
-}
-
-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);
+ return sym;
}
Symbol *
-newsym(int ns)
-{
- return linksym(allocsym(ns, NULL));
-}
-
-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 @@
Symbol *
newlabel(void)
{
- Symbol *sym = newsym(NS_LABEL);
+ Symbol *sym = newsym(NS_LABEL, NULL);
sym->id = newid();
return sym;
}
@@ -273,7 +241,7 @@
return sym;
}
}
- return (alloc == ALLOC) ? allocsym(ns, name) : NULL;
+ return (alloc == ALLOC) ? newsym(ns, name) : NULL;
}
Symbol *
@@ -282,7 +250,7 @@
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 @@
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;
}