shithub: scc

Download patch

ref: d5b51ff627be535ef485d228428c2a9b9f5824cc
parent: 7d77cd4c9f3bd9c9a4ad49890b08a4181552ff3d
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Oct 3 18:03:37 EDT 2013

Remove node1

node1 is only a space optimization of node, but it requieres
that a walk procedure can difference between binary nodes and
unitary nodes. If we reduce the number of unitary nodes to
symbol nodes, run over the tree is trivial.

--- a/expr.c
+++ b/expr.c
@@ -71,7 +71,7 @@
 		np1 = node(op, np1, nodesym(yyval.sym));
 		continue;
 	next:
-		np1 = node1(op, np1);
+		np1 = node(op, np1, NULL);
 		next();
 		continue;
 	}
@@ -107,11 +107,11 @@
 
 call_cast:
 	next();
-	return node1(op, cast());
+	return node(op, cast(), NULL);
 
 call_unary:
 	next();
-	return node1(op, unary());
+	return node(op, unary(), NULL);
 }
 
 static struct node *
--- a/flow.c
+++ b/flow.c
@@ -58,7 +58,7 @@
 	sym = yyval.sym;
 	if (sym->ns != NS_LABEL)
 		sym = newlabel(sym, yytext);
-	np = node1(OGOTO, nodesym(sym));
+	np = node(OGOTO, nodesym(sym), NULL);
 	expect(';');
 
 	return np;
@@ -169,7 +169,7 @@
 	expect(';');
 	if (blockp == blocks)
 		error("break statement not within loop or switch");
-	return node1(OBREAK, NULL);
+	return node(OBREAK, NULL, NULL);
 }
 
 static struct node *
@@ -185,7 +185,7 @@
 	if (bp == blockp)
 		error("continue statement not within loop");
 
-	return node1(OCONT, NULL);
+	return node(OCONT, NULL, NULL);
 }
 
 static struct node *
@@ -197,7 +197,7 @@
 	np = expr();
 	expect(';');
 
-	return node1(ORETURN, np);
+	return node(ORETURN, np, NULL);
 }
 
 static struct node *
@@ -214,7 +214,7 @@
 		; /* nothing */
 	if (bp == blockp)
 		error("case statement not within switch");
-	np = node1(OCASE, exp);
+	np = node(OCASE, exp, NULL);
 	expect(':');
 	return np;
 }
@@ -230,7 +230,7 @@
 	if (bp == blockp)
 		error("default statement not within switch");
 	expect(':');
-	return node1(ODEFAULT, NULL);
+	return node(ODEFAULT, NULL, NULL);
 }
 
 static struct node *
@@ -278,7 +278,7 @@
 function(register struct symbol *sym)
 {
 	curfun = sym;
-	return node1(OFTN, compound());
+	return node(OFTN, compound(), NULL);
 }
 
 void
--- a/syntax.h
+++ b/syntax.h
@@ -24,8 +24,6 @@
 extern struct node *function(struct symbol *sym);
 
 extern struct node *node(unsigned char op, struct node *l, struct node *r);
-extern struct node *node1(unsigned char op, struct node *i);
-
 extern struct node *nodesym(struct symbol *sym);
 extern struct node *nodecomp(void);
 extern struct node *addstmt(struct node *np, struct node *stmt);
--- a/tree.c
+++ b/tree.c
@@ -12,11 +12,6 @@
 	unsigned char op;
 };
 
-struct node_op1 {
-	struct node base;
-	struct node *infix;
-};
-
 struct node_op2 {
 	struct node base;
 	struct node *left;
@@ -61,17 +56,6 @@
 }
 
 struct node *
-node1(unsigned char op, struct node *i)
-{
-	register struct node_op1 *np = xmalloc(sizeof(*np));
-
-	np->base.op = op;
-	np->infix = i;
-
-	return (struct node *) np;
-}
-
-struct node *
 nodecomp(void)
 {
 	register struct node_comp *np = xmalloc(sizeof(*np));
@@ -111,20 +95,20 @@
 		unsigned char nchild;
 		const char *txt;
 	} *bp, optab [] = {
-		[OCALL] = {1, "()"},
+		[OCALL] = {2, "()"},
 		[OARY] = {2, "[]"},
 		[OFIELD] = {2, "."},
 		[OPTR] = {2, "->"},
-		[OPOSTINC] = {1, ".++"},
-		[OPOSTDEC] = {1, ".--"},
-		[OPREINC] = {1, "++."},
-		[OPREDEC] = {1, "--."},
-		[OADDR] = {1, "&."},
-		[OINDIR] = {1, "[*]"},
-		[OMINUS] = {1, "-."},
-		[OPLUS] = {1, "+."},
-		[OCPL] = {1, "~"},
-		[ONEG] = {1, "!"},
+		[OPOSTINC] = {2, ".++"},
+		[OPOSTDEC] = {2, ".--"},
+		[OPREINC] = {2, "++."},
+		[OPREDEC] = {2, "--."},
+		[OADDR] = {2, "&."},
+		[OINDIR] = {2, "[*]"},
+		[OMINUS] = {2, "-."},
+		[OPLUS] = {2, "+."},
+		[OCPL] = {2, "~"},
+		[ONEG] = {2, "!"},
 		[OMUL] = {2, "*"},
 		[ODIV] = {2, "/"},
 		[OMOD] = {2, "%"},
@@ -164,13 +148,13 @@
 		[ODO] = {2, "do"},
 		[OWHILE] = {2, "while"},
 		[OLABEL] = {2, "label"},
-		[OGOTO] = {1, "goto"},
-		[OBREAK] = {1, "break"},
-		[OCONT] = {1, "cont"},
-		[ORETURN] = {1, "return"},
-		[OCASE] = {1, "case"},
-		[ODEFAULT] = {1, "default"},
-		[OFTN] = {1, "function"},
+		[OGOTO] = {2, "goto"},
+		[OBREAK] = {2, "break"},
+		[OCONT] = {2, "cont"},
+		[ORETURN] = {2, "return"},
+		[OCASE] = {2, "case"},
+		[ODEFAULT] = {2, "default"},
+		[OFTN] = {2, "function"},
 		[ODEF] = {2, "def"},
 		[O2EXP] = { 2, ":"}
 	};
@@ -195,9 +179,6 @@
 		fputs((sym->name) ? sym->name : ".", stdout);
 		return;
 	}
-	case 1:
-		prtree_helper(((struct node_op1 *) np)->infix);
-		break;
 	case 2:
 		prtree_helper(((struct node_op2 *) np)->left);
 		prtree_helper(((struct node_op2 *) np)->rigth);