scc

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

commit efb9d623ed24a601a5e82ba5ccf117251a94d974
parent c6e3c000059d37084d67ec3bde819cbe82299920
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun,  6 Oct 2013 11:12:11 +0200

Remove insert

insert does the same than lookup, but it never installs a new symbol,
so we have duplicated the code in both functions. insert is used only
once in all the code, so add a new parameter doesn't seems me the
correct way, because it implies modify a lot of code, and it is possible
also get some performance penalty. The solution used is insert in the
namespace parameter of lookup a new parameter which indicatens if it is
needed install the symbol, using a negative number when it is not needed.

Diffstat:
decl.c | 4++--
symbol.c | 27++++++++-------------------
symbol.h | 3+--
3 files changed, 11 insertions(+), 23 deletions(-)

diff --git a/decl.c b/decl.c @@ -27,8 +27,8 @@ namespace(register unsigned char ns, signed char alloc) return NULL; else if (yyns == ns) return sym; - else - return find(yytext, ns); + else /* don't create new symbol */ + return lookup(yytext, -ns); } else { if (yyns == NS_ANY) { sym->ns = ns; diff --git a/symbol.c b/symbol.c @@ -63,36 +63,25 @@ freesyms(void) } struct symbol * -find(const char *s, register char ns) +lookup(register const char *s, signed char ns) { register struct symbol *sym; - static unsigned char l; - - l = strlen(s); - for (sym = htab[hash(s)]; sym; sym = sym->hash) { - if (ns != NS_ANY && ns != sym->ns) - continue; - if (!memcmp(sym->name, s, l)) - return sym; - } - return NULL; -} - -struct symbol * -lookup(register const char *s, char ns) -{ - register struct symbol *sym; - register unsigned char l; - static unsigned char key; + static unsigned char key, l, ins; l = strlen(s); key = hash(s); + if (!(ins = ns >= 0)) + ns = -ns; + for (sym = htab[key]; sym; sym = sym->hash) { if (ns != NS_ANY && ns != sym->ns) continue; if (!memcmp(sym->name, s, l)) return sym; } + + if (!ins) + return NULL; sym = xmalloc(sizeof(*sym)); sym->name = xstrdup(s); sym->next = head; diff --git a/symbol.h b/symbol.h @@ -66,8 +66,7 @@ extern struct ctype *btype(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, char ns); -extern struct symbol *find(const char *s, char ns); +extern struct symbol *lookup(const char *s, signed char ns); extern void insert(struct symbol *sym, unsigned char ctx); extern struct ctype *storage(struct ctype *tp, unsigned char mod); extern struct ctype *newctype(void);