shithub: scc

Download patch

ref: 48669bd9d1a6bbec7837e05e81f6f283e5d0858a
parent: 3c2610fd4650e8e7bb396d86141ada54e52e368d
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Nov 4 05:24:54 EDT 2021

cc2/qbe: Change cast() to return Node

Receiving a pointer to the struct were we want to store the
result needs a lot of temporary variables in the code. If
we make that cast() returns a struct instead of a pointer to
struct we can remove a lot of these temporaries and
simplify the code. The code generated is pretty similar
because returning structs usually mean passing an additional
pointer to the function called where it can write the results.

--- a/src/cmd/cc/cc2/target/qbe/cgen.c
+++ b/src/cmd/cc/cc2/target/qbe/cgen.c
@@ -162,21 +162,20 @@
 static Node *rhs(Node *np, Node *new);
 
 static Node *
-cast(Type *td, Node *ns, Node *nd)
+cast(Type *td, Node *np)
 {
 	Type *ts;
-	Node aux1, aux2;
+	Node *tmp;
 	int op, d_isint, s_isint;
 
-	ts = &ns->type;
+	ts = &np->type;
 	d_isint = (td->flags & INTF) != 0;
 	s_isint = (ts->flags & INTF) != 0;
 
 	if (d_isint && s_isint) {
-		if (td->size <= ts->size) {
-			*nd = *ns;
-			return nd;
-		}
+		if (td->size <= ts->size)
+			return np;
+
 		assert(td->size == 4 || td->size == 8);
 		switch (ts->size) {
 		case 1:
@@ -215,7 +214,7 @@
 		case 1:
 		case 2:
 			ts = (ts->flags&SIGNF) ? &int32type : &uint32type;
-			ns = cast(ts, ns, tmpnode(&aux2, ts));
+			np = cast(ts, np);
 		case 4:
 			op = (td->size == 8) ? ASSWTOD : ASSWTOS;
 			break;
@@ -231,8 +230,10 @@
 		op = (td->size == 4) ? ASEXTS : ASTRUNCD;
 	}
 
-	code(op, tmpnode(nd, td), ns, NULL);
-	return nd;
+	tmp = tmpnode(NULL, td);
+	code(op, tmp, np, NULL);
+
+	return tmp;
 }
 
 static Node *
@@ -579,7 +580,8 @@
 			l = rhs(l, &aux1);
 		return call(np, l, ret);
 	case OCAST:
-		return cast(tp, rhs(l, &aux1), ret);
+		*ret = *cast(tp, rhs(l, &aux1));
+		return ret;
 	case OASSIG:
 		switch (np->u.subop) {
 		case OINC: