shithub: scc

Download patch

ref: e951f65cd7c66260d1b3c0c4b7ea89cd01dd9b5e
parent: ca3407661071828c8ebac7b66e1eb975a7b5743c
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Dec 22 10:50:06 EST 2016

[cc1] Centralize hash selection

We have several hash tables now, and it is not a good idea to
repeat the selection code everywhere. Hash() is going to be the
function which returns a pointer to the bucket position in the
hash table.

--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -44,25 +44,29 @@
 }
 #endif
 
-static unsigned
-hash(const char *s)
+static Symbol **
+hash(const char *s, int ns)
 {
 	unsigned c, h;
+	Symbol **tab;
 
+
 	for (h = 0; c = *s; ++s)
 		h ^= 33 * c;
-	return h & NR_SYM_HASH-1;
+	h &= NR_SYM_HASH-1;
+
+	tab = (ns == NS_CPP) ? htabcpp : htab;
+	return &tab[h];
 }
 
 static void
 unlinkhash(Symbol *sym)
 {
-	Symbol **tab, **h;
+	Symbol **h;
 
 	if ((sym->flags & SDECLARED) == 0)
 		return;
-	tab = (sym->ns == NS_CPP) ? htabcpp : htab;
-	h = &tab[hash(sym->name)];
+	h = hash(sym->name, sym->ns);
 	*h = sym->next;
 }
 
@@ -167,10 +171,9 @@
 static Symbol *
 linkhash(Symbol *sym)
 {
-	Symbol **tab, **h;
+	Symbol **h;
 
-	tab = (sym->ns == NS_CPP) ? htabcpp : htab;
-	h = &tab[hash(sym->name)];
+	h = hash(sym->name, sym->ns);
 	sym->hash = *h;
 	*h = sym;
 
@@ -207,13 +210,12 @@
 Symbol *
 lookup(int ns, char *name, int alloc)
 {
-	Symbol *sym, **tab;
+	Symbol *sym;
 	int sns;
 	char *t, c;
 
 	c = *name;
-	tab = (ns == NS_CPP) ? htabcpp : htab;
-	for (sym = tab[hash(name)]; sym; sym = sym->hash) {
+	for (sym = *hash(name, ns); sym; sym = sym->hash) {
 		t = sym->name;
 		if (*t != c || strcmp(t, name))
 			continue;