shithub: scc

Download patch

ref: 42aa3beda454c3476f337a2ffee6a91545c4ab59
parent: 4f18f449b7ddb35be623af059e5e8a570c60ed15
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 12 07:01:00 EDT 2016

[cc2] Add constnode()

This function it is going to be extensively used
because it is common to do some operation with some
constant value.

--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -388,19 +388,13 @@
 		break;
 	case OCPL:
 		np->op = OAND;
-		rp = newnode(OCONST);
-		rp->type = np->type;
-		rp->u.i = 0;
-		rp->u.i = ~np->u.i;
+		rp = constnode(~(TUINT) 0, &np->type);
 		goto binary;
 	case ONEG:
 		np->op = OSUB;
 		rp = lp;
-		lp = newnode(OCONST);
-		lp->type = np->type;
-		if (np->type.flags & INTF)
-			lp->u.i = 0;
-		else
+		lp = constnode(0, &np->type);
+		if ((np->type.flags & INTF) == 0)
 			lp->u.f = 0.0;
 	default:
 	binary:
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -216,7 +216,7 @@
 extern void code(int op, Node *to, Node *from1, Node *from2);
 extern void defvar(Symbol *), defpar(Symbol *), defglobal(Symbol *);
 extern void setlabel(Symbol *sym), getbblocks(void);
-extern Node *label2node(Symbol *sym);
+extern Node *label2node(Symbol *sym), *constnode(TUINT n, Type *tp);
 extern Symbol *newlabel(void);
 
 /* node.c */
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -77,6 +77,17 @@
 	return np;
 }
 
+Node *
+constnode(TUINT n, Type *tp)
+{
+	Node *np;
+
+	np = newnode(OCONST);
+	np->type = *tp;
+	np->u.i = n;
+	return np;
+}
+
 void
 setlabel(Symbol *sym)
 {