shithub: scc

Download patch

ref: 35f16441611ebbc740dfa103743ad9402d0155c6
parent: 1ca89cac46831770fd9fcde016b7c8a1d5ff7e58
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed May 6 14:56:16 EDT 2015

Add emit() function to cc1

emit() will be the unique emit interface to the rest of the code.
This is a first version that only substitute to emitexp(), which
becomes a static function of code.c

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -154,6 +154,7 @@
 	OA_AND, OA_XOR, OA_OR, OADDR,ONEG, OCPL, OEXC,
 	OCOMMA, OCAST, OSYM, OASK, OFIELD, OTYP,
 	OLABEL, ODEFAULT, OCASE, OSTRUCT, OJUMP, OBRANCH,
+	OEXPR,
 	/* TODO: This order is important, but must be changed */
 	OAND, OOR,
 	/*
@@ -166,7 +167,7 @@
 /*TODO: clean these declarations */
 extern void
 	emitdcl(Symbol *), emitefun(void),
-	emitexp(Node *),
+	emit(uint8_t, void *),
 	emitprint(Node *),
 	emitsymid(uint8_t op, Symbol *sym),
 	emitbloop(void), emiteloop(void),
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -10,7 +10,7 @@
 static void emitbin(uint8_t, void *), emitunary(uint8_t, void *),
             emitternary(uint8_t, void *), emitcast(uint8_t, void *),
             emitsym(uint8_t, void *), emitfield(uint8_t, void *),
-            emitsizeof(uint8_t, void *);
+            emitsizeof(uint8_t, void *), emitexp(uint8_t op, void *arg);
 
 char *optxt[] = {
 	[OADD] = "+",
@@ -99,7 +99,8 @@
 	[OCAST] = emitcast,
 	[OSYM] = emitsym,
 	[OASK] = emitternary,
-	[OFIELD]= emitfield
+	[OFIELD]= emitfield,
+	[OEXPR] = emitexp
 };
 
 void
@@ -122,6 +123,15 @@
 }
 
 void
+emit(uint8_t op, void *arg)
+{
+	extern uint8_t failure;
+
+	if (failure)
+		return;
+	(*opcode[op])(op, arg);
+}
+
 static void
 emitvar(Symbol *sym)
 {
@@ -228,9 +238,11 @@
 	printf("\t#%c", np->left->type->letter);
 }
 
-void
-emitexp(Node *np)
+static void
+emitexp(uint8_t op, void *arg)
 {
+	Node *np = arg;
+
 	emitnode(np);
 	putchar('\n');
 }
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -51,7 +51,7 @@
 
 	if (yytoken != ';') {
 		np = expr();
-		emitexp(np);
+		emit(OEXPR, np);
 	}
 
 	expect(';');
@@ -93,7 +93,7 @@
 	stmt(end, begin, lswitch);
 	emitsymid(OLABEL, cond);
 	emitsymid(OBRANCH, begin);
-	emitexp(np);
+	emit(OEXPR, np);
 	emiteloop();
 	emitsymid(OLABEL, end);
 	freetree(np);
@@ -118,15 +118,15 @@
 	einc = (yytoken != ')') ? expr() : NULL;
 	expect(')');
 
-	emitexp(einit);
+	emit(OEXPR, einit);
 	emitsymid(OJUMP, cond);
 	emitbloop();
 	emitsymid(OLABEL, begin);
 	stmt(end, begin, lswitch);
-	emitexp(einc);
+	emit(OEXPR, einc);
 	emitsymid(OLABEL, cond);
 	emitsymid(OBRANCH, begin);
-	emitexp(econd);
+	emit(OEXPR, econd);
 	emiteloop();
 	emitsymid(OLABEL, end);
 	freetree(einit);
@@ -149,7 +149,7 @@
 	expect(WHILE);
 	np = condition();
 	emitsymid(OBRANCH, begin);
-	emitexp(np);
+	emit(OEXPR, np);
 	emiteloop();
 	emitsymid(OLABEL, end);
 	freetree(np);
@@ -175,7 +175,7 @@
 			error("incorrect type in return");
 	}
 	emitret(tp);
-	emitexp(np);
+	emit(OEXPR, np);
 	freetree(np);
 }
 
@@ -250,10 +250,10 @@
 	stmt(lbreak, lcont, &lcase);
 	emitsymid(OLABEL, lcond);
 	emitswitch(lcase.nr);
-	emitexp(cond);
+	emit(OEXPR, cond);
 	for (p = lcase.head; p; p = next) {
 		emitsymid(OCASE, p->label);
-		emitexp(p->expr);
+		emit(OEXPR, p->expr);
 		next = p->next;
 		freetree(p->expr);
 		free(p);
@@ -307,7 +307,7 @@
 	np = condition();
 	NEGATE(np, 1);
 	emitsymid(OBRANCH, lelse);
-	emitexp(np);
+	emit(OEXPR, np);
 	stmt(lbreak, lcont, lswitch);
 	if (accept(ELSE)) {
 		end = install("", NS_LABEL);