ref: 829833bcfdb0f780537568a12041c83533401220
parent: 3fa39059929b951b229ccc4525c1981a7ca470fa
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Nov 4 05:24:54 EDT 2021
cc2/qbe: Change ternary() 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 ternary() 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 ternaryed where it can write the results.
--- a/src/cmd/cc/cc2/target/qbe/cgen.c
+++ b/src/cmd/cc/cc2/target/qbe/cgen.c
@@ -386,27 +386,27 @@
}
static Node *
-ternary(Node *np, Node *ret)
+ternary(Node *np)
{
- Node ifyes, ifno, phi, *colon, aux1, aux2, aux3;
+ Node ifyes, ifno, phi, *colon, *tmp;
- tmpnode(ret, &np->type);
+ tmp = tmpnode(NULL, &np->type);
label2node(&ifyes, NULL);
label2node(&ifno, NULL);
label2node(&phi, NULL);
colon = np->right;
- code(ASBRANCH, rhs(np->left, &aux1), &ifyes, &ifno);
+ code(ASBRANCH, rhs(np->left, node(OTMP)), &ifyes, &ifno);
setlabel(ifyes.u.sym);
- copy(&ret->type, ret, rhs(colon->left, &aux2));
+ copy(&tmp->type, tmp, rhs(colon->left, node(OTMP)));
code(ASJMP, NULL, &phi, NULL);
setlabel(ifno.u.sym);
- copy(&ret->type, ret, rhs(colon->right, &aux3));
+ copy(&tmp->type, tmp, rhs(colon->right, node(OTMP)));
setlabel(phi.u.sym);
- return ret;
+ return tmp;
}
static Node *
@@ -615,7 +615,8 @@
}
return ret;
case OASK:
- return ternary(np, ret);
+ *ret = *ternary(np);
+ return ret;
case OCOMMA:
rhs(l, &aux1);
return rhs(r, ret);