shithub: scc

Download patch

ref: 688c48119a25ceef7429ee6cc280cfbe1b2c017a
parent: 104f6821fce36276746996377f2b78b631f2d368
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Apr 14 15:16:48 EDT 2014

Simplify install function

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

--- a/cc.h
+++ b/cc.h
@@ -36,7 +36,6 @@
 	NS_LABEL,
 	NS_TAG,
 	NR_NAMESPACES,
-	NS_KEYWORD,
 	NS_FREE
 };
 
--- a/decl.c
+++ b/decl.c
@@ -75,7 +75,7 @@
 		if (yytoken != IDEN) {
 			if (flags & ID_EXPECTED)
 				goto expected;
-			sym = install(NULL, ns);
+			sym = install("", ns);
 		} else {
 			sym = newiden(ns);
 		}
@@ -348,7 +348,7 @@
 		}
 		next();
 	} else {
-		sym = install(NULL, NS_TAG);
+		sym = install("", NS_TAG);
 	}
 	tp = sym->type = mktype(NULL, tag, NULL, 0);
 	sym->u.ns = ++namespace;
--- a/lex.c
+++ b/lex.c
@@ -127,12 +127,14 @@
 		{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
--- 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 @@
 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;
 }