shithub: scc

Download patch

ref: ce2658123d520c647f2c1a3d1a40b3f3d47f73bc
parent: 847edd52f60fa2a1d239659f2814255bd7ea0f26
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Sep 1 13:05:41 EDT 2016

[cc2] Accept a destiny pointer in constnode()

There are cases where we will need only temporary nodes,
so, like we already did in functions like label2node,
the best option is to pass a pointer and let to constnode()
to take the decision of allocating memory or not.

--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -420,11 +420,11 @@
 		bool(np, true, false);
 
 		setlabel(true);
-		assign(&int32type, ret, constnode(1, &int32type));
+		assign(&int32type, ret, constnode(&aux2, 1, &int32type));
 		code(ASJMP, NULL, phi, NULL);
 
 		setlabel(false);
-		assign(&int32type, ret, constnode(0, &int32type));
+		assign(&int32type, ret, constnode(&aux2, 0, &int32type));
 
 		setlabel(phi->u.sym);
 		return ret;
@@ -575,12 +575,12 @@
 		break;
 	case OCPL:
 		np->op = OAND;
-		rp = constnode(~(TUINT) 0, &np->type);
+		rp = constnode(NULL, ~(TUINT) 0, &np->type);
 		goto binary;
 	case OSNEG:
 		np->op = OSUB;
 		rp = lp;
-		lp = constnode(0, &np->type);
+		lp = constnode(NULL, 0, &np->type);
 		if ((np->type.flags & INTF) == 0)
 			lp->u.f = 0.0;
 	default:
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -216,7 +216,8 @@
 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(Node *np, Symbol *sym), *constnode(TUINT n, Type *tp);
+extern Node *label2node(Node *np, Symbol *sym);
+extern Node *constnode(Node *np, TUINT n, Type *tp);
 extern Symbol *newlabel(void);
 
 /* node.c */
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -83,11 +83,10 @@
 }
 
 Node *
-constnode(TUINT n, Type *tp)
+constnode(Node *np, TUINT n, Type *tp)
 {
-	Node *np;
-
-	np = newnode(OCONST);
+	if (!np)
+		np = newnode(OCONST);
 	np->type = *tp;
 	np->u.i = n;
 	return np;