shithub: scc

Download patch

ref: 7953f6f46ef27e1ab5bdb5cd543bba523ac3c0b3
parent: 5cc0256ea7e075ec951904e3cfcfcd30e0df31d4
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Jul 9 10:42:26 EDT 2014

Fix auxiliriay label definition

After the change of lookup/install auxiliary labels didn't
work anymore. This modification fixes this problem using
insert() for them, instead of using label().

--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -429,7 +429,8 @@
 		expect(')');
 		break;
 	default:
-		error("unexpected '%s'", yytext);
+		np = NULL;
+		break;
 	}
 	return np;
 }
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -30,10 +30,11 @@
 	Symbol *sym;
 
 	if ((sym = lookup(s, NS_LABEL)) != NULL) {
-		if (define && sym->s.isdefined)
-			error("label '%s' already defined", s);
-		else
+		if (define) {
+			if (sym->s.isdefined)
+				error("label '%s' already defined", s);
 			sym->s.isdefined = 1;
+		}
 		return sym;
 	}
 
@@ -67,9 +68,9 @@
 	Symbol *begin, *cond, *end;
 	Node *np;
 
-	begin = label("", 1);
-	end = label("", 1);
-	cond = label("", 1);
+	begin = install("", NS_LABEL);
+	end = install("", NS_LABEL);
+	cond = install("", NS_LABEL);
 
 	expect(WHILE);
 	np = condition();
@@ -87,29 +88,27 @@
 For(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {
 	Symbol *begin, *cond, *end;
-	Node *econd = NULL, *einc = NULL;
+	Node *econd, *einc, *einit;
 
-	begin = label("", 1);
-	end = label("", 1);
-	cond = label("", 1);
+	begin = install("", NS_LABEL);
+	end = install("", NS_LABEL);
+	cond = install("", NS_LABEL);
 
 	expect(FOR);
 	expect('(');
-	stmtexp();
-
-	if (yytoken != ';')
-		econd = expr();
+	einit = expr();
 	expect(';');
-	if (yytoken != ')')
-		einc = expr();
+	econd = expr();
+	expect(';');
+	einc = expr();
 	expect(')');
 
+	emitexp(einit);
 	emitjump(cond, NULL);
 	emitbloop();
 	emitlabel(begin);
 	stmt(end, begin, lswitch);
-	if (einc)
-		emitexp(einc);
+	emitexp(einc);
 	emitlabel(cond);
 	emitjump(begin, econd);
 	emiteloop();
@@ -119,8 +118,10 @@
 static void
 Dowhile(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {
-	Symbol *begin = label("", 1), *end = label("", 1);
+	Symbol *begin, *end;
 
+	begin = install("", NS_LABEL);
+	end = install("", NS_LABEL);
 	expect(DO);
 	emitbloop();
 	emitlabel(begin);
@@ -204,9 +205,10 @@
 	Caselist lcase = {.nr = 0, .head = NULL, .deflabel = NULL};
 	struct scase *p;
 	Node *cond;
-	Symbol *lcond = label("", 1);
+	Symbol *lcond;
 
-	lbreak = label("", 1);
+	lbreak = install("", NS_LABEL);
+	lcond = install("", NS_LABEL);
 	expect(SWITCH);
 	expect ('(');
 	cond = eval(expr());
@@ -228,9 +230,10 @@
 Case(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {
 	Node *np;
-	Symbol *lcase = label("", 1);
+	Symbol *lcase;
 	struct scase *pcase;
 
+	lcase = install("", NS_LABEL);
 	if (!lswitch)
 		error("case label not within a switch statement");
 	expect(CASE);
@@ -251,7 +254,7 @@
 static void
 Default(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {
-	Symbol *ldefault = label("", 1);
+	Symbol *ldefault = install("", NS_LABEL);
 
 	expect(DEFAULT);
 	expect(':');
@@ -262,9 +265,10 @@
 static void
 If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {
-	Symbol *end, *lelse = label("", 1);
+	Symbol *end, *lelse;
 	Node *np;
 
+	lelse = install("", NS_LABEL);
 	expect(IF);
 	np = condition();
 	NEGATE(np, 1);
@@ -271,7 +275,7 @@
 	emitjump(lelse, np);
 	stmt(lbreak, lcont, lswitch);
 	if (accept(ELSE)) {
-		end = label("", 1);
+		end = install("", NS_LABEL);
 		emitjump(end, NULL);
 		emitlabel(lelse);
 		stmt(lbreak, lcont, lswitch);
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -92,6 +92,7 @@
 	sym->ctx = curctx;
 	sym->token = IDEN;
 	sym->id = symid++;
+	sym->s.isdefined = 1;
 	tbl = &symtab[ns];
 	sym->next = tbl->head;
 	tbl->head = sym;