[hackers] [scc] cc2: pass op type to newnode()

From: Quentin Rameau <quinq_AT_fifth.space>
Date: Mon, 9 May 2016 15:00:51 +0200

Using newnode(int op) saves us a few temporary variables as we almost
always assign an op after creating a new node.
---
 cc2/arch/qbe/cgen.c |  8 +++-----
 cc2/cc2.h           |  2 +-
 cc2/node.c          |  7 +++++--
 cc2/parser.c        | 57 ++++++++++++++++-------------------------------------
 4 files changed, 26 insertions(+), 48 deletions(-)
diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c
index 70ba9d6..19a8d72 100644
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
_AT_@ -111,7 +111,7 @@ load(Node *np)
 	int op;
 	Type *tp = &np->type;
 
-	new = tmpnode(newnode());
+	new = tmpnode(newnode(ONOP));
 	new->left = np;
 	new->type = *tp;
 
_AT_@ -191,7 +191,7 @@ cast(Node *nd, Node *ns)
 		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);
_AT_@ -228,9 +228,7 @@ cgen(Node *np)
 	if (np->label) {
 		setlabel(np->label);
 		if (np->next == NULL) {
-			Node *tmp = newnode();
-			tmp->op = ORET;
-			addstmt(tmp);
+			addstmt(newnode(ORET));
 			prevstmt();
 		}
 	}
diff --git a/cc2/cc2.h b/cc2/cc2.h
index 5ca106e..cd90a20 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
_AT_@ -208,7 +208,7 @@ extern void apply(Node *(*fun)(Node *));
 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);
 
diff --git a/cc2/node.c b/cc2/node.c
index 56b64fd..adccf1c 100644
--- a/cc2/node.c
+++ b/cc2/node.c
_AT_@ -23,7 +23,7 @@ static Node *freep;
 static int inhome;
 
 Node *
-newnode(void)
+newnode(int op)
 {
 	struct arena *ap;
 	Node *np;
_AT_@ -42,7 +42,10 @@ newnode(void)
 	np = freep;
 	freep = np->left;
 
-	return memset(np, 0, sizeof(*np));
+	memset(np, 0, sizeof(*np));
+	np->op = op;
+
+	return np;
 }
 
 Node *
diff --git a/cc2/parser.c b/cc2/parser.c
index 09b2353..9fe2722 100644
--- a/cc2/parser.c
+++ b/cc2/parser.c
_AT_@ -179,15 +179,12 @@ getname(char *t, union tokenop u)
 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);
 }
 
_AT_@ -207,20 +204,20 @@ static void
 constant(char *token, union tokenop u)
 {
 	static char letters[] = "0123456789ABCDEF";
-	Node *np = newnode();
+	Node *np;
 	TUINT v;
 	unsigned c;
 
 	++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;
_AT_@ -234,8 +231,8 @@ constant(char *token, union tokenop u)
 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 '/':
_AT_@ -258,7 +255,6 @@ assign(char *token, union tokenop u)
 	}
 
 	np->u.subop = subop;
-	np->op = op;
 	np->type = *gettype(token);
 	np->right = pop();
 	np->left = pop();
_AT_@ -268,18 +264,12 @@ assign(char *token, union tokenop u)
 static void
 ternary(char *token, union tokenop u)
 {
-	Node *ask, *colon;
-	Type *tp;
-
-	tp = gettype(token+1);
+	Node *ask = newnode(OCOLON), *colon = newnode(OASK);
+	Type *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);
_AT_@ -328,10 +318,8 @@ repeat:
 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();
_AT_@ -341,10 +329,8 @@ oreturn(char *token, union tokenop u)
 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)
_AT_@ -358,10 +344,8 @@ jump(char *token, union tokenop u)
 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);
_AT_@ -370,19 +354,14 @@ casetbl(char *token, union tokenop u)
 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;
_AT_@ -392,7 +371,7 @@ unary(char *token, union tokenop u)
 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();
_AT_@ -400,8 +379,7 @@ call(char *token, union tokenop u)
 			break;
 		np->right = par;
 	}
-	fun = newnode();
-	fun->op = u.op;
+
 	fun->type = *gettype(token+1);
 	fun->left = np;
 	fun->right = par;
_AT_@ -411,9 +389,8 @@ call(char *token, union tokenop u)
 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();
-- 
2.8.2
Received on Mon May 09 2016 - 15:00:51 CEST

This archive was generated by hypermail 2.3.0 : Mon May 09 2016 - 15:12:17 CEST