[hackers] [scc] [cc1] Make symbol table an ordered hash table again || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 22 Dec 2016 15:27:06 +0100 (CET)

commit 49121885af59183def0534697a2414733ac881cf
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Thu Dec 22 15:02:21 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Thu Dec 22 15:02:21 2016 +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.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index e1acb92..ac9f9d3 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -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
index 5d8de53..5a48318 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -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
index 1d9e132..b032197 100644
--- a/cc1/decl.c
+++ b/cc1/decl.c
_AT_@ -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);
         }
_AT_@ -481,7 +483,7 @@ newtag(void)
                 next();
                 break;
         default:
- sym = newsym(NS_TAG);
+ sym = newsym(NS_TAG, NULL);
                 break;
         }
         if (!sym->type) {
_AT_@ -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
index 898c8ce..d888095 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -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
index b21046c..d352920 100644
--- a/cc1/fold.c
+++ b/cc1/fold.c
_AT_@ -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);
_AT_@ -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
index bbffa54..94f4c6f 100644
--- a/cc1/init.c
+++ b/cc1/init.c
_AT_@ -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
index 604fab2..98a9713 100644
--- a/cc1/lex.c
+++ b/cc1/lex.c
_AT_@ -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);
_AT_@ -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
index 9568a59..15a1876 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -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
_AT_@ -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;
 
_AT_@ -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);
_AT_@ -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;
 }
_AT_@ -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 *
_AT_@ -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);
 }
_AT_@ -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;
         }
Received on Thu Dec 22 2016 - 15:27:06 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 22 2016 - 15:36:22 CET