scc

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

commit d04a92e4e8dc5714985476ba7f99683ee6818a58
parent bae05fff3765490d73e5e69945d2ff9a5ffdb2ee
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 12 Mar 2014 08:00:18 +0100

Add memory to lookup

lookup can detect that yyval is pointing to the same symbol that
the user already had and return it. This change avoid continuos
check in the code.

Diffstat:
decl.c | 2+-
lex.c | 2+-
symbol.c | 5++++-
symbol.h | 2+-
4 files changed, 7 insertions(+), 4 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 = (yyval->ns == ns) ? yyval : lookup(yytext, ns); + sym = lookup(yytext, ns); if (!sym->ctype.defined) sym->ctx = curctx; else if (sym->ctx == curctx) diff --git a/lex.c b/lex.c @@ -24,7 +24,7 @@ struct keyword { static FILE *yyin; static struct symbol yysym = {.name = ""}, *yynval = &yysym; -struct symbol *yyval; +struct symbol *yyval = &yysym; struct symbol * integer(char *s, char base) diff --git a/symbol.c b/symbol.c @@ -64,9 +64,10 @@ freesyms(void) } struct symbol * -lookup(register const char *s, signed char ns) +lookup(register const char *s, unsigned char ns) { register struct symbol *sym; + extern struct symbol *yyval; static unsigned char key; register char *t; @@ -75,6 +76,8 @@ lookup(register const char *s, signed char ns) sym->next = head; return sym; } + if (yyval->ns == ns && !strcmp(yyval->name, s)) + return yyval; key = hash(s) & NR_SYM_HASH - 1; for (sym = htab[key]; sym; sym = sym->hash) { diff --git a/symbol.h b/symbol.h @@ -66,7 +66,7 @@ extern struct ctype *ctype(struct ctype *tp, unsigned char tok); extern void new_ctx(void); extern void del_ctx(void); extern void freesyms(void); -extern struct symbol *lookup(const char *s, signed char ns); +extern struct symbol *lookup(const char *s, unsigned char ns); extern void insert(struct symbol *sym, unsigned char ctx); extern void delctype(struct ctype *tp); extern unsigned char hash(register const char *s);