shithub: scc

Download patch

ref: ad92212ec3827d9e105b874cdebf4ed3fc09709c
parent: ef05ad6ff2560acd1364b1d90cf52ec95603bc9e
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Apr 15 07:41:53 EDT 2014

Add constcode()

A node is generated from a constant in different places, so
it is a good idea to translate it to a function which is easier
of reading and generates less code.

--- a/cc.h
+++ b/cc.h
@@ -238,7 +238,8 @@
 	*bincode(char op, Type *tp, Node *np1, Node *np2),
 	*castcode(Node *child, Type *tp),
 	*sizeofcode(Type *tp), 
-	*ternarycode(Node *cond, Node *ifyes, Node *ifno);
+	*ternarycode(Node *cond, Node *ifyes, Node *ifno),
+	*constcode(Symbol *sym);
 
 #define SYM(s) ((union unode) {.sym = s})
 #define OP(s) ((union unode) {.op = s})
--- a/code.c
+++ b/code.c
@@ -198,3 +198,9 @@
 	np->childs[2] = ifno;
 	return np;
 }
+
+Node *
+constcode(Symbol *sym)
+{
+	return node(emitconst, inttype, SYM(sym), 0);
+}
--- a/expr.c
+++ b/expr.c
@@ -262,9 +262,9 @@
 	Node *ifyes, *ifno, *cmp;
 	char op;
 
-	ifyes = node(emitconst, inttype, SYM(one), 0);
-	ifno = node(emitconst, inttype, SYM(zero), 0);
-	cmp = node(emitconst, inttype, SYM(zero), 0);
+	ifyes = constcode(one);
+	ifno = constcode(zero);
+	cmp = constcode(zero);
 	op = (neg) ?  OEQ : ONE;
 
 	return ternarycode(compare(op, np, cmp), ifyes, ifno);
@@ -323,8 +323,7 @@
 		next();
 		break;
 	case CONSTANT:
-		sym = yylval.sym;
-		np = node(emitconst, sym->type, SYM(sym), 0);
+		np = constcode(yylval.sym);
 		next();
 		break;
 	case '(':