shithub: scc

Download patch

ref: e879cdfcae193d1ee6d3615534f2d44843a0a229
parent: 24572d7623dc7497d48c4e36f3250761377aba2f
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed May 6 14:40:49 EDT 2015

Pass aditional parameter to elements of opcode[]

We need this element for emitsymid(), which will be a member of
opcode[] in some moment.

--- a/cc1/code.c
+++ b/cc1/code.c
@@ -7,9 +7,10 @@
 #include "../inc/cc.h"
 #include "cc1.h"
 
-static void emitbin(void *), emitunary(void *), emitternary(void *),
-     emitcast(void *), emitsym(void *), emitfield(void *),
-     emitsizeof(void *);
+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 *);
 
 char *optxt[] = {
 	[OADD] = "+",
@@ -57,7 +58,7 @@
 	[OBRANCH] = "\tj\tL%d",
 };
 
-void (*opcode[])(void *) = {
+void (*opcode[])(uint8_t, void *) = {
 	[OADD] = emitbin,
 	[OSUB] = emitbin,
 	[OMUL] = emitbin,
@@ -114,6 +115,14 @@
 }
 
 static void
+emitnode(Node *np)
+{
+	if (np)
+		(*opcode[np->op])(np->op, np);
+}
+
+void
+static void
 emitvar(Symbol *sym)
 {
 	char c;
@@ -157,7 +166,7 @@
 }
 
 void
-emitsym(void *arg)
+emitsym(uint8_t op, void *arg)
 {
 	Node *np = arg;
 	putchar('\t');
@@ -180,30 +189,26 @@
 }
 
 void
-emitcast(void *arg)
+emitcast(uint8_t op, void *arg)
 {
 	Node *np = arg, *lp = np->left;
 
-	(*opcode[lp->op])(lp);
+	emitnode(lp);
 	printf("\t%c%c", lp->type->letter, np->type->letter);
 }
 
 void
-emitbin(void *arg)
+emitbin(uint8_t op, void *arg)
 {
-	Node *lp, *rp, *np = arg;
+	Node *np = arg;
 
-	lp = np->left;
-	rp = np->rigth;
-	if (lp)
-		(*opcode[lp->op])(lp);
-	if (rp)
-		(*opcode[rp->op])(rp);
-	printf("\t%s%c", optxt[np->op], np->type->letter);
+	emitnode(np->left);
+	emitnode(np->rigth);
+	printf("\t%s%c", optxt[op], np->type->letter);
 }
 
 void
-emitternary(void *arg)
+emitternary(uint8_t op, void *arg)
 {
 	Node *cond, *ifyes, *ifno, *np = arg;
 
@@ -210,14 +215,14 @@
 	cond = np->left;
 	ifyes = np->rigth->left;
 	ifno = np->rigth->rigth;
-	(*opcode[cond->op])(cond);
-	(*opcode[ifyes->op])(ifyes);
-	(*opcode[ifno->op])(ifno);
+	emitnode(cond);
+	emitnode(ifyes);
+	emitnode(ifno);
 	printf("\t?%c", np->type->letter);
 }
 
 void
-emitsizeof(void *arg)
+emitsizeof(uint8_t op, void *arg)
 {
 	Node *np = arg;
 	printf("\t#%c", np->left->type->letter);
@@ -226,8 +231,7 @@
 void
 emitexp(Node *np)
 {
-	if (np)
-		(*opcode[np->op])(np);
+	emitnode(np);
 	putchar('\n');
 }
 
@@ -234,7 +238,7 @@
 void
 emitprint(Node *np)
 {
-	(*opcode[np->op])(np);
+	emitnode(np);
 	printf("\tk%c\n", np->type->letter);
 	fflush(stdout);
 }
@@ -284,11 +288,11 @@
 }
 
 void
-emitfield(void *arg)
+emitfield(uint8_t op, void *arg)
 {
-	Node *np = arg, *lp = np->left;
+	Node *np = arg;
 
-	(*opcode[lp->op])(lp);
+	emitnode(np->left);
 	putchar('\t');
 	emitvar(np->sym);
 }