shithub: scc

Download patch

ref: 508b00b42102538e576dad13db861e699dec9092
parent: 0521af76024d66c24768f7e8e3f1fbdc946fa142
author: Quentin Rameau <[email protected]>
date: Mon May 9 10:15:47 EDT 2016

cc2: pass op type to newnode()

Using newnode(int op) saves us a few temporary variables as we almost
always assign an op after creating a new node.

--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -107,11 +107,10 @@
 static Node *
 load(Node *np)
 {
-	Node *new;
 	int op;
+	Node *new = tmpnode(newnode(ONOP));
 	Type *tp = &np->type;
 
-	new = tmpnode(newnode());
 	new->left = np;
 	new->type = *tp;
 
@@ -191,7 +190,7 @@
 		switch (ts->size) {
 		case 1:
 		case 2:
-			tmp = tmpnode(newnode());
+			tmp = tmpnode(newnode(ONOP));
 			tmp->type = (ts->flags&SIGNF) ? int32type : uint32type;
 			tmp->left = ns;
 			nd->left = ns = cast(tmp, ns);
@@ -228,9 +227,7 @@
 	if (np->label) {
 		setlabel(np->label);
 		if (np->next == NULL) {
-			Node *tmp = newnode();
-			tmp->op = ORET;
-			addstmt(tmp);
+			addstmt(newnode(ORET));
 			prevstmt();
 		}
 	}
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -208,7 +208,7 @@
 extern void cleannodes(void);
 extern void delnode(Node *np);
 extern void deltree(Node *np);
-extern Node *newnode(void);
+extern Node *newnode(int op);
 extern Node *addstmt(Node *np);
 extern Node *prevstmt(void), *nextstmt(void);
 
--- a/cc2/node.c
+++ b/cc2/node.c
@@ -23,7 +23,7 @@
 static int inhome;
 
 Node *
-newnode(void)
+newnode(int op)
 {
 	struct arena *ap;
 	Node *np;
@@ -42,7 +42,10 @@
 	np = freep;
 	freep = np->left;
 
-	return memset(np, 0, sizeof(*np));
+	memset(np, 0, sizeof(*np));
+	np->op = op;
+
+	return np;
 }
 
 Node *
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -179,15 +179,12 @@
 static void
 symbol(char *token, union tokenop u)
 {
-	Node *np;
-	Symbol *sym;
+	Node *np = newnode(u.op & 0xFF);
+	Symbol *sym = getsym(atoi(token+1));
 
 	sclass = u.op >> 8;
-	np = newnode();
-	sym = getsym(atoi(token+1));
 	np->u.sym = sym;
 	np->type = sym->type;
-	np->op = u.op & 0xFF;
 	push(np);
 }
 
@@ -207,7 +204,7 @@
 constant(char *token, union tokenop u)
 {
 	static char letters[] = "0123456789ABCDEF";
-	Node *np = newnode();
+	Node *np;
 	TUINT v;
 	unsigned c;
 
@@ -214,13 +211,13 @@
 	++token;
 	if (*token == OSTRING) {
 		++token;
-		np->op = OSTRING;
+		np = newnode(OSTRING);
 		np->type.flags = STRF;
 		np->type.size = strlen(token);
 		np->type.align = int8type.align;
 		np->u.s = xstrdup(token);
 	} else {
-		np->op = OCONST;
+		np = newnode(OCONST);
 		np->type = *gettype(token++);
 		for (v = 0; c = *token++; v += c) {
 			v <<= 4;
@@ -234,8 +231,8 @@
 static void
 assign(char *token, union tokenop u)
 {
-	int subop, op = u.op;
-	Node *np = newnode();
+	int subop;
+	Node *np = newnode(u.op);
 
 	switch (subop = *++token) {
 	case '/':
@@ -258,7 +255,6 @@
 	}
 
 	np->u.subop = subop;
-	np->op = op;
 	np->type = *gettype(token);
 	np->right = pop();
 	np->left = pop();
@@ -268,18 +264,12 @@
 static void
 ternary(char *token, union tokenop u)
 {
-	Node *ask, *colon;
-	Type *tp;
+	Node *ask = newnode(OCOLON), *colon = newnode(OASK);
+	Type *tp = gettype(token+1);
 
-	tp = gettype(token+1);
-
-	colon = newnode();
-	colon->op = OCOLON;
 	colon->right = pop();
 	colon->left = pop();
 
-	ask = newnode();
-	ask->op = OASK;
 	ask->type = *tp;
 	ask->left = pop();
 	push(ask);
@@ -328,10 +318,8 @@
 static void
 oreturn(char *token, union tokenop u)
 {
-	Node *np;
+	Node *np = newnode(u.op);
 
-	np = newnode();
-	np->op = u.op;
 	eval(strtok(NULL, "\t\n"));
 	if (!empty())
 		np->left = pop();
@@ -341,10 +329,8 @@
 static void
 jump(char *token, union tokenop u)
 {
-	Node *np, *aux;
+	Node *aux, *np = newnode(u.op);
 
-	np = newnode();
-	np->op = u.op;
 	eval(strtok(NULL, "\t\n"));
 
 	if (u.op != OJMP)
@@ -358,10 +344,8 @@
 static void
 casetbl(char *token, union tokenop u)
 {
-	Node *np, *aux;
+	Node *np = newnode(u.op);
 
-	np = newnode();
-	np->op = u.op;
 	eval(strtok(NULL, "\t\n"));
 	np->left = pop();
 	push(np);
@@ -370,19 +354,14 @@
 static void
 loop(char *token, union tokenop u)
 {
-	Node *np;
-
-	np = newnode();
-	np->op = u.op;
-	push(np);
+	push(newnode(u.op));
 }
 
 static void
 unary(char *token, union tokenop u)
 {
-	Node *np = newnode();
+	Node *np = newnode(u.op);
 
-	np->op = u.op;
 	np->type = *gettype(token+1);
 	np->left = pop();
 	np->right = NULL;
@@ -392,7 +371,7 @@
 static void
 call(char *token, union tokenop u)
 {
-	Node *np, *par, *fun;
+	Node *np, *par, *fun = newnode(u.op);
 
 	for (par = NULL;; par = np) {
 		np = pop();
@@ -400,8 +379,7 @@
 			break;
 		np->right = par;
 	}
-	fun = newnode();
-	fun->op = u.op;
+
 	fun->type = *gettype(token+1);
 	fun->left = np;
 	fun->right = par;
@@ -411,9 +389,8 @@
 static void
 binary(char *token, union tokenop u)
 {
-	Node *np = newnode();
+	Node *np = newnode(u.op);
 
-	np->op = u.op;
 	np->type = *gettype(token+1);
 	np->right = pop();
 	np->left = pop();