scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 68746484af7bee6078b7516cc19a85ff861f98c3
parent b3477280fbd5d5ca483d13ff771be76d98ca2b49
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 10 Mar 2014 07:39:43 +0100

Remove keyword hash

If we use the symbol hash we save time, because we look at the same
time for keywords and for identifiers, and saves the memory from the
keyword hash.

Diffstat:
decl.c | 22++++++++--------------
lex.c | 32++++++++------------------------
symbol.c | 1+
symbol.h | 1+
4 files changed, 18 insertions(+), 38 deletions(-)

diff --git a/decl.c b/decl.c @@ -32,7 +32,7 @@ directdcl(register struct ctype *tp, unsigned char ns, unsigned char isfun) expect(')'); } else if (ns != NS_TYPE) { if (yytoken == IDEN) { - sym = lookup(yytext, ns); + sym = (yyval->ns == ns) ? yyval : lookup(yytext, ns); if (!sym->ctype.defined) sym->ctx = curctx; else if (sym->ctx == curctx) @@ -193,21 +193,11 @@ specifier(register struct ctype *tp, next(); (tok == ENUM) ? enumdcl(tp) : structdcl(tp); return true; + case TYPEDEF: + tp->base = &yyval->ctype; + break; } break; - case IDEN: - if (!tp->defined) { - register struct symbol *sym; - - sym = lookup(yytext, NS_IDEN); - if (sym->ctype.defined && - sym->store.c_typedef) { - tp = ctype(tp, TYPEDEF); - tp->base = &sym->ctype; - break; - } - } - /* it is not a type name */ default: goto check_type; } @@ -310,6 +300,10 @@ listdcl(struct ctype *base, sym->store = *store; sym->qlf = *qlf; sym->ctype = *decl_type(base); + if (sym->store.c_typedef) { + sym->tok = TYPE; + sym->c = TYPEDEF; + } tp = &sym->ctype; aux = NULL; diff --git a/lex.c b/lex.c @@ -21,11 +21,9 @@ struct keyword { char *str; unsigned char tok; unsigned char value; - struct keyword *next; }; static FILE *yyin; -static struct keyword *ktab[NR_KEYW_HASH]; struct symbol *yyval; @@ -162,36 +160,19 @@ init_keywords(void) {NULL, 0, 0}, }; register struct keyword *bp; + register struct symbol *sym; for (bp = buff; bp->str; ++bp) { - register unsigned char h = hash(bp->str) & NR_KEYW_HASH-1; - bp->next = ktab[h]; - ktab[h] = bp; + sym = lookup(bp->str, NS_IDEN); + sym->tok = bp->tok; + sym->c = bp->value; } } static unsigned char -keyword(register char *s) -{ - register struct keyword *bp; - static struct symbol sym; - - for (bp = ktab[hash(s) & NR_KEYW_HASH-1]; bp; bp = bp->next) { - if (*s == *bp->str && !strcmp(bp->str, s)) { - sym.c = bp->value; - yyval = &sym; - return bp->tok; - } - } - return 0; -} - -static unsigned char iden(void) { register char ch, *bp; - register struct symbol *sym; - static unsigned char tok; for (bp = yytext; bp < yytext + IDENTSIZ; *bp++ = ch) { if (!isalnum(ch = getc(yyin)) && ch != '_') @@ -202,7 +183,10 @@ iden(void) *bp = '\0'; ungetc(ch, yyin); - return (tok = keyword(yytext)) ? tok : IDEN; + yyval = lookup(yytext, NS_IDEN); + if (!yyval->tok) + yyval->tok = IDEN; + return yyval->tok; } static unsigned char diff --git a/symbol.c b/symbol.c @@ -87,6 +87,7 @@ lookup(register const char *s, signed char ns) sym->next = head; sym->ctx = curctx; sym->ns = ns; + sym->tok = 0; head = sym; sym->hash = htab[key]; htab[key] = sym; diff --git a/symbol.h b/symbol.h @@ -59,6 +59,7 @@ struct symbol { unsigned char ctx; unsigned char ns; char *name; + unsigned char tok; struct { union { char c; /* numerical constants */