scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 7b5c97a6ece75c5cf7ecbe272b4b7ff3b8f7caf9
parent 09bdb8f4945c2f70f95c92ad3b7c323ba7b7948b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  4 Nov 2021 09:24:54 +0100

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.

Diffstat:
Msrc/cmd/cc/cc2/target/qbe/cgen.c | 17+++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/cmd/cc/cc2/target/qbe/cgen.c b/src/cmd/cc/cc2/target/qbe/cgen.c @@ -386,27 +386,27 @@ bool(Node *np, Symbol *true, Symbol *false) } 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 @@ rhs(Node *np, Node *ret) } return ret; case OASK: - return ternary(np, ret); + *ret = *ternary(np); + return ret; case OCOMMA: rhs(l, &aux1); return rhs(r, ret);