shithub: scc

Download patch

ref: efb9d623ed24a601a5e82ba5ccf117251a94d974
parent: c6e3c000059d37084d67ec3bde819cbe82299920
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Oct 6 07:12:11 EDT 2013

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.

--- a/decl.c
+++ b/decl.c
@@ -27,8 +27,8 @@
 			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;
--- a/symbol.c
+++ b/symbol.c
@@ -63,30 +63,16 @@
 }
 
 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;
+	static unsigned char key, l, ins;
 
 	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;
-
-	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;
@@ -93,6 +79,9 @@
 		if (!memcmp(sym->name, s, l))
 			return sym;
 	}
+
+	if (!ins)
+		return NULL;
 	sym = xmalloc(sizeof(*sym));
 	sym->name = xstrdup(s);
 	sym->next = head;
--- a/symbol.h
+++ b/symbol.h
@@ -66,8 +66,7 @@
 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);