scc

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

commit 688c48119a25ceef7429ee6cc280cfbe1b2c017a
parent 104f6821fce36276746996377f2b78b631f2d368
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 14 Apr 2014 19:16:48 +0200

Simplify install function

The two preopmizations don't save too much time,
so it is better remove them and let is simpler.

Diffstat:
Mcc.h | 1-
Mdecl.c | 4++--
Mlex.c | 4+++-
Msymbol.c | 28+++++++++-------------------
4 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/cc.h b/cc.h @@ -36,7 +36,6 @@ enum { NS_LABEL, NS_TAG, NR_NAMESPACES, - NS_KEYWORD, NS_FREE }; diff --git a/decl.c b/decl.c @@ -75,7 +75,7 @@ directdcl(struct dcldata *dp, uint8_t ns, int8_t flags) if (yytoken != IDEN) { if (flags & ID_EXPECTED) goto expected; - sym = install(NULL, ns); + sym = install("", ns); } else { sym = newiden(ns); } @@ -348,7 +348,7 @@ newtag(uint8_t tag) } next(); } else { - sym = install(NULL, NS_TAG); + sym = install("", NS_TAG); } tp = sym->type = mktype(NULL, tag, NULL, 0); sym->u.ns = ++namespace; diff --git a/lex.c b/lex.c @@ -127,12 +127,14 @@ init_keywords(void) {NULL, 0, 0}, }; register Symbol *sym; + extern short symid; for (bp = buff; bp->str; ++bp) { - sym = install(bp->str, NS_KEYWORD); + sym = install(bp->str, NS_IDEN); sym->token = bp->token; sym->u.token = bp->value; } + symid = 0; } static uint8_t diff --git a/symbol.c b/symbol.c @@ -9,7 +9,8 @@ #define NR_SYM_HASH 32 uint8_t curctx; -uint8_t namespace = NS_KEYWORD + 1 ; +uint8_t namespace = NS_FREE + 1; +short symid; static struct symtab { Symbol *head; @@ -77,34 +78,23 @@ lookup(register char *s, uint8_t ns) Symbol * install(char *s, uint8_t ns) { - register Symbol *sym; - register Symbol **t; + register Symbol *sym, **t; struct symtab *tbl; - static short id; - - if (ns == NS_KEYWORD) { - ns = NS_IDEN; - } else { - ++id; - if (s != NULL) - s = xstrdup(s); - } sym = xcalloc(1, sizeof(*sym)); - sym->name = s; + sym->name = xstrdup(s); sym->ctx = curctx; sym->token = IDEN; sym->ns = ns; - sym->id = id; + sym->id = symid++; tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns]; sym->next = tbl->head; tbl->head = sym; - if (s != NULL) { - t = &tbl->htab[hash(s)]; - sym->hash = *t; - *t = sym; - } + + t = &tbl->htab[hash(s)]; + sym->hash = *t; + *t = sym; return sym; }