shithub: scc

Download patch

ref: bce2cfac7f576b3a9afe811fd9a6d71ae18d3cb0
parent: 507e3c823892f2f2bac6acd42bab1b079798fa05
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed May 6 14:04:32 EDT 2015

Unify emitstruct, emitlabel, emitjump, emitbranch, emitcase, emitdefault

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -153,6 +153,7 @@
 	OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR,
 	OA_AND, OA_XOR, OA_OR, OADDR,ONEG, OCPL, OEXC,
 	OCOMMA, OCAST, OSYM, OASK, OFIELD, OTYP,
+	OLABEL, ODEFAULT, OCASE, OSTRUCT, OJUMP, OBRANCH,
 	/* TODO: This order is important, but must be changed */
 	OAND, OOR,
 	/*
@@ -166,14 +167,13 @@
 extern void
 	emitdcl(Symbol *), emitefun(void),
 	emitexp(Node *),
-	emitprint(Node *), emitlabel(Symbol *), emitjump(Symbol *),
-	emitbranch(Symbol *sym),
+	emitprint(Node *),
+	emitsymid(uint8_t op, Symbol *sym),
 	emitbloop(void), emiteloop(void),
-	emitswitch(short), emitcase(Symbol *),
+	emitswitch(short),
 	emitret(Type *tp),
 	emitfun(Symbol *sym),
-	emitdefault(Symbol *),
-	emitstruct(Symbol *sym), emitestruct(void);
+	emitestruct(void);
 
 extern Node *node(uint8_t op, Type *tp, Node *left, Node *rigth);
 extern Node *symbol(Symbol *sym);
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -48,7 +48,13 @@
 	[OCPL] = "~",
 	[OAND] = "y",
 	[OOR] = "o",
-	[OCOMMA] = ","
+	[OCOMMA] = ",",
+	[OLABEL] = "L%d\n",
+	[ODEFAULT] = "\tf\tL%d\n",
+	[OCASE] = "\tw\tL%d",
+	[OSTRUCT] = "S%d\t(\n",
+	[OJUMP] = "\tj\tL%d\n",
+	[OBRANCH] = "\tj\tL%d",
 };
 
 void (*opcode[])(void *) = {
@@ -145,12 +151,6 @@
 }
 
 void
-emitstruct(Symbol *sym)
-{
-	printf("S%d\t(\n", sym->id);
-}
-
-void
 emitestruct(void)
 {
 	puts(")");
@@ -270,12 +270,6 @@
 }
 
 void
-emitlabel(Symbol *sym)
-{
-	printf("L%d\n", sym->id);
-}
-
-void
 emitbloop(void)
 {
 	puts("\td");
@@ -288,35 +282,15 @@
 }
 
 void
-emitjump(Symbol *sym)
+emitsymid(uint8_t op, Symbol *sym)
 {
-	printf("\tj\tL%d\n", sym->id);
+	printf(optxt[op], sym->id);
 }
 
 void
-emitbranch(Symbol *sym)
-{
-	printf("\tj\tL%d", sym->id);
-}
-
-void
 emitswitch(short nr)
 {
 	printf("\teI\t#%0x", nr);
-}
-
-void
-emitcase(Symbol *sym)
-{
-	fputs("\tw\t", stdout);
-	printf("L%d", sym->id);
-}
-
-void
-emitdefault(Symbol *sym)
-{
-	fputs("\tf\t", stdout);
-	emitlabel(sym);
 }
 
 void
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -315,7 +315,7 @@
 	if (tagtype->defined)
 		error("redefinition of struct/union '%s'", yytext);
 	tagtype->defined = 1;
-	emitstruct(tagsym);
+	emitsymid(OSTRUCT, tagsym);
 
 	while (!accept('}')) {
 		Type *base, *tp;
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -87,15 +87,15 @@
 
 	expect(WHILE);
 	np = condition();
-	emitjump(cond);
+	emitsymid(OJUMP, cond);
 	emitbloop();
-	emitlabel(begin);
+	emitsymid(OLABEL, begin);
 	stmt(end, begin, lswitch);
-	emitlabel(cond);
-	emitbranch(begin);
+	emitsymid(OLABEL, cond);
+	emitsymid(OBRANCH, begin);
 	emitexp(np);
 	emiteloop();
-	emitlabel(end);
+	emitsymid(OLABEL, end);
 	freetree(np);
 }
 
@@ -119,16 +119,16 @@
 	expect(')');
 
 	emitexp(einit);
-	emitjump(cond);
+	emitsymid(OJUMP, cond);
 	emitbloop();
-	emitlabel(begin);
+	emitsymid(OLABEL, begin);
 	stmt(end, begin, lswitch);
 	emitexp(einc);
-	emitlabel(cond);
-	emitbranch(begin);
+	emitsymid(OLABEL, cond);
+	emitsymid(OBRANCH, begin);
 	emitexp(econd);
 	emiteloop();
-	emitlabel(end);
+	emitsymid(OLABEL, end);
 	freetree(einit);
 	freetree(econd);
 	freetree(einc);
@@ -144,14 +144,14 @@
 	end = install("", NS_LABEL);
 	expect(DO);
 	emitbloop();
-	emitlabel(begin);
+	emitsymid(OLABEL, begin);
 	stmt(end, begin, lswitch);
 	expect(WHILE);
 	np = condition();
-	emitbranch(begin);
+	emitsymid(OBRANCH, begin);
 	emitexp(np);
 	emiteloop();
-	emitlabel(end);
+	emitsymid(OLABEL, end);
 	freetree(np);
 }
 
@@ -185,7 +185,7 @@
 	expect(BREAK);
 	if (!lbreak)
 		error("break statement not within loop or switch");
-	emitjump(lbreak);
+	emitsymid(OJUMP, lbreak);
 	expect(';');
 }
 
@@ -196,7 +196,7 @@
 {
 	switch (yytoken) {
 	case IDEN: case TYPEIDEN:
-		emitlabel(label(yytext, 1));
+		emitsymid(OLABEL, label(yytext, 1));
 		next();
 		expect(':');
 		stmt(lbreak, lcont, lswitch);
@@ -212,7 +212,7 @@
 	expect(CONTINUE);
 	if (!lcont)
 		error("continue statement not within loop");
-	emitjump(lcont);
+	emitsymid(OJUMP, lcont);
 	expect(';');
 }
 
@@ -223,7 +223,7 @@
 
 	if (yytoken != IDEN)
 		unexpected();
-	emitjump(label(yytext, 0));
+	emitsymid(OJUMP, label(yytext, 0));
 	next();
 	expect(';');
 }
@@ -246,13 +246,13 @@
 
 	lbreak = install("", NS_LABEL);
 	lcond = install("", NS_LABEL);
-	emitjump(lcond);
+	emitsymid(OJUMP, lcond);
 	stmt(lbreak, lcont, &lcase);
-	emitlabel(lcond);
+	emitsymid(OLABEL, lcond);
 	emitswitch(lcase.nr);
 	emitexp(cond);
 	for (p = lcase.head; p; p = next) {
-		emitcase(p->label);
+		emitsymid(OCASE, p->label);
 		emitexp(p->expr);
 		next = p->next;
 		freetree(p->expr);
@@ -259,8 +259,8 @@
 		free(p);
 	}
 	if (lcase.deflabel)
-		emitdefault(lcase.deflabel);
-	emitlabel(lbreak);
+		emitsymid(ODEFAULT, lcase.deflabel);
+	emitsymid(OLABEL, lbreak);
 	freetree(cond);
 }
 
@@ -280,7 +280,7 @@
 	pcase = xmalloc(sizeof(*pcase));
 	pcase->expr = np;
 	pcase->next = lswitch->head;
-	emitlabel(pcase->label = install("", NS_LABEL));
+	emitsymid(OLABEL, pcase->label = install("", NS_LABEL));
 	lswitch->head = pcase;
 	++lswitch->nr;
 }
@@ -292,7 +292,7 @@
 
 	expect(DEFAULT);
 	expect(':');
-	emitlabel(ldefault);
+	emitsymid(OLABEL, ldefault);
 	lswitch->deflabel = ldefault;
 }
 
@@ -306,17 +306,17 @@
 	expect(IF);
 	np = condition();
 	NEGATE(np, 1);
-	emitbranch(lelse);
+	emitsymid(OBRANCH, lelse);
 	emitexp(np);
 	stmt(lbreak, lcont, lswitch);
 	if (accept(ELSE)) {
 		end = install("", NS_LABEL);
-		emitjump(end);
-		emitlabel(lelse);
+		emitsymid(OJUMP, end);
+		emitsymid(OLABEL, lelse);
 		stmt(lbreak, lcont, lswitch);
-		emitlabel(end);
+		emitsymid(OLABEL, end);
 	} else {
-		emitlabel(lelse);
+		emitsymid(OLABEL, lelse);
 	}
 	freetree(np);
 }