shithub: scc

Download patch

ref: 1145b68f8535abf344d36cf9cd4d4f1283f42aa6
parent: 99642057a0e6a038dcd4a92500d1a016b137244e
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Feb 9 16:29:35 EST 2012

Moving hashfun to lex.c

This function will be used only in the lexical analisys so, the best
place for it is in the lex.c file. This avoid disturb the space name.

--- a/lex.c
+++ b/lex.c
@@ -10,7 +10,6 @@
 
 #define TOKSIZ_MAX 21
 #define NR_KWD_HASH 32
-/* TODO: move hashfun here */
 
 static struct keyword {
 	char *str;
@@ -70,6 +69,17 @@
 
 
 
+static unsigned char hashfun(register const char *s)
+{
+	register unsigned char h, ch;
+
+	for (h = 0; ch = *s++; h += ch)
+		/* nothing */;
+	return h;
+}
+
+
+
 void init_lex(void)
 {
 	register struct keyword *bp;
@@ -77,7 +87,7 @@
 
 	for (bp = keywords; bp->str; bp++) {
 		register struct keyword *aux, *ant;
-		h = hashfun(bp->str);
+		h = hashfun(bp->str) % (NR_KWD_HASH - 1);
 		if (!(aux = khash[h]) || strcmp(bp->str, aux->str) < 0) {
 			khash[h] = bp;
 			bp->next = aux;
--- a/symbol.c
+++ b/symbol.c
@@ -27,18 +27,8 @@
 
 
 
-unsigned char hashfun(register const char *s)
-{
-	register unsigned char h, ch;
 
-	for (h = 0; ch = *s++; h += ch)
-		/* nothing */;
-	return h & NR_SYM_HASH - 1;
-}
 
-
-
-
 void new_ctx(struct symctx *ctx)
 {
 	ctx->siden = siden.top;
@@ -78,11 +68,11 @@
 
 
 
-struct symbol *pushsym(struct symhash *h, struct symbol *sym)
+struct symbol *pushsym(struct symhash *h, struct symbol *sym, unsigned char hash)
 {
 	static unsigned char key;
-	key = hashfun(sym->str);
 
+	key = hash % NR_SYM_HASH;
 	h->top = sym;
 	sym->next = h->buf[key];
 	return h->buf[key] = sym;
@@ -91,13 +81,11 @@
 
 
 
-struct symbol *findsym(struct symhash *h, char *s)
+struct symbol *findsym(struct symhash *h, char *s, unsigned char hash)
 {
 	register struct symbol *bp;
-	static unsigned char key;
 
-	key = hashfun(s);
-	for (bp = h->buf[key]; bp; bp = bp->next) {
+	for (bp = h->buf[hash % NR_SYM_HASH]; bp; bp = bp->next) {
 		if (!strcmp(bp->str, s))
 			return bp;
 	}
--- a/symbol.h
+++ b/symbol.h
@@ -16,7 +16,5 @@
 struct symhash;
 extern struct symhash siden, sgoto, sstruct;
 
-extern unsigned char hashfun(register const char *s);
-
 
 #endif