shithub: scc

Download patch

ref: 1f654ce8692fb5e961c140b4006466508b370a2d
parent: 2889a3cbd0cdafa116ded659d1b189a58cf30e80
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Dec 12 05:32:06 EST 2016

Revert "[cc2] Minimal fix for type symbols"

This reverts commit 5c3445da52cc656a9c415d1dcb8142ab9beb7400.

--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -102,7 +102,7 @@
 
 	if (!np)
 		np = newnode(OTMP);
-	sym = getsym(TMPSYM, 1);
+	sym = getsym(TMPSYM);
 	sym->type = np->type = *tp;
 	sym->kind = STMP;
 	np->u.sym = sym;
--- a/cc2/arch/qbe/optm.c
+++ b/cc2/arch/qbe/optm.c
@@ -41,7 +41,7 @@
 		break;
 	case OBRANCH:
 		if (!next->label) {
-			sym = getsym(TMPSYM, 1);
+			sym = getsym(TMPSYM);
 			sym->kind = SLABEL;
 			next->label = sym;
 		}
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -236,7 +236,7 @@
 
 /* symbol.c */
 #define TMPSYM  0
-extern Symbol *getsym(unsigned id, int islocal);
+extern Symbol *getsym(unsigned id);
 extern void popctx(void);
 extern void pushctx(void);
 extern void freesym(Symbol *sym);
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -61,7 +61,7 @@
 Symbol *
 newlabel(void)
 {
-	Symbol *sym = getsym(TMPSYM, 1);
+	Symbol *sym = getsym(TMPSYM);
 
 	sym->kind = SLABEL;
 	return sym;
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -178,7 +178,7 @@
 {
 	Symbol *sym;
 
-	sym = getsym(atoi(token+1), 0);
+	sym = getsym(atoi(token+1));
 	push(&sym->type);
 }
 
@@ -192,7 +192,7 @@
 symbol(char *token, union tokenop u)
 {
 	Node *np = newnode(u.op & 0xFF);
-	Symbol *sym = getsym(atoi(token+1), 1);
+	Symbol *sym = getsym(atoi(token+1));
 
 	sclass = u.op >> 8;
 	np->u.sym = sym;
--- a/cc2/symbol.c
+++ b/cc2/symbol.c
@@ -33,17 +33,24 @@
 void
 popctx(void)
 {
-	Symbol *sym, *next, **psym;
+	Symbol *sym, *next;
 
 	infunction = 0;
 	for (sym = locals; sym; sym = next) {
 		next = sym->next;
-		if (sym->id != TMPSYM) {
-			psym = &symtab[sym->id & NR_SYMHASH-1];
-			while (*psym != sym)
-				psym = &(*psym)->next;
-			*psym = sym->h_next;
-		}
+		/*
+		 * Symbols are inserted in the hash in the inverted
+		 * order they are found in locals and it is impossible
+		 * to have a global over a local, because a local is
+		 * any symbol defined in the body of a function,
+		 * even if it has extern linkage.
+		 * For this reason when we raich a symbol in the
+		 * locals list we know that it is the head of it
+		 * collision list and we can remove it assigning
+		 * it h_next to the hash table position
+		 */
+		if (sym->id != TMPSYM)
+			symtab[sym->id & NR_SYMHASH-1] = sym->h_next;
 		freesym(sym);
 	}
 	curlocal = locals = NULL;
@@ -50,7 +57,7 @@
 }
 
 Symbol *
-getsym(unsigned id, int islocal)
+getsym(unsigned id)
 {
 	Symbol **htab, *sym;
 	static unsigned short num;
@@ -68,7 +75,7 @@
 		sym->id = id;
 		if ((sym->numid = ++num) == 0)
 			error(EIDOVER);
-		if (infunction && islocal) {
+		if (infunction) {
 			if (!locals)
 				locals = sym;
 			if (curlocal)