shithub: scc

Download patch

ref: 68746484af7bee6078b7516cc19a85ff861f98c3
parent: b3477280fbd5d5ca483d13ff771be76d98ca2b49
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Mar 10 03:39:43 EDT 2014

Remove keyword hash

If we use the symbol hash we save time, because we look at the same
time for keywords and for identifiers, and saves the memory from the
keyword hash.

--- a/decl.c
+++ b/decl.c
@@ -32,7 +32,7 @@
 		expect(')');
 	} else if (ns != NS_TYPE) {
 		if (yytoken == IDEN) {
-			sym = lookup(yytext, ns);
+			sym = (yyval->ns == ns) ? yyval : lookup(yytext, ns);
 			if (!sym->ctype.defined)
 				sym->ctx = curctx;
 			else if (sym->ctx == curctx)
@@ -193,21 +193,11 @@
 				next();
 				(tok == ENUM) ? enumdcl(tp) : structdcl(tp);
 				return true;
+			case TYPEDEF:
+				tp->base = &yyval->ctype;
+				break;
 			}
 			break;
-		case IDEN:
-			if (!tp->defined) {
-				register struct symbol *sym;
-
-				sym = lookup(yytext, NS_IDEN);
-				if (sym->ctype.defined &&
-				    sym->store.c_typedef) {
-					tp = ctype(tp, TYPEDEF);
-					tp->base = &sym->ctype;
-					break;
-				}
-			}
-			/* it is not a type name */
 		default:
 			goto check_type;
 		}
@@ -310,6 +300,10 @@
 		sym->store = *store;
 		sym->qlf = *qlf;
 		sym->ctype = *decl_type(base);
+		if (sym->store.c_typedef) {
+			sym->tok = TYPE;
+			sym->c = TYPEDEF;
+		}
 		tp = &sym->ctype;
 		aux = NULL;
 
--- a/lex.c
+++ b/lex.c
@@ -21,11 +21,9 @@
 	char *str;
 	unsigned char tok;
 	unsigned char value;
-	struct keyword *next;
 };
 
 static FILE *yyin;
-static struct keyword *ktab[NR_KEYW_HASH];
 
 struct symbol *yyval;
 
@@ -162,36 +160,19 @@
 		{NULL, 0, 0},
 	};
 	register struct keyword *bp;
+	register struct symbol *sym;
 
 	for (bp = buff;  bp->str; ++bp) {
-		register unsigned char h = hash(bp->str) & NR_KEYW_HASH-1;
-		bp->next = ktab[h];
-		ktab[h] = bp;
+		sym = lookup(bp->str, NS_IDEN);
+		sym->tok = bp->tok;
+		sym->c = bp->value;
 	}
 }
 
 static unsigned char
-keyword(register char *s)
-{
-	register struct keyword *bp;
-	static struct symbol sym;
-
-	for (bp = ktab[hash(s) & NR_KEYW_HASH-1]; bp; bp = bp->next) {
-		if (*s == *bp->str && !strcmp(bp->str, s)) {
-			sym.c = bp->value;
-			yyval = &sym;
-			return bp->tok;
-		}
-	}
-	return 0;
-}
-
-static unsigned char
 iden(void)
 {
 	register char ch, *bp;
-	register struct symbol *sym;
-	static unsigned char tok;
 
 	for (bp = yytext; bp < yytext + IDENTSIZ; *bp++ = ch) {
 		if (!isalnum(ch = getc(yyin)) && ch != '_')
@@ -202,7 +183,10 @@
 	*bp = '\0';
 	ungetc(ch, yyin);
 
-	return (tok = keyword(yytext)) ? tok : IDEN;
+	yyval = lookup(yytext, NS_IDEN);
+	if (!yyval->tok)
+		yyval->tok = IDEN;
+	return yyval->tok;
 }
 
 static unsigned char
--- a/symbol.c
+++ b/symbol.c
@@ -87,6 +87,7 @@
 	sym->next = head;
 	sym->ctx = curctx;
 	sym->ns = ns;
+	sym->tok = 0;
 	head = sym;
 	sym->hash = htab[key];
 	htab[key] = sym;
--- a/symbol.h
+++ b/symbol.h
@@ -59,6 +59,7 @@
 	unsigned char ctx;
 	unsigned char ns;
 	char *name;
+	unsigned char tok;
 	struct {
 		union {
 			char c;   /* numerical constants */