commit 2c0c00b541f14afe82c47e08b6bb2ec76c84d013
parent 04b9d5bad1698e233f405703461f5da1be3a1e65
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 7 Jan 2025 16:32:45 +0100
cc2: Move ternary() to common code
Diffstat:
2 files changed, 35 insertions(+), 29 deletions(-)
diff --git a/src/cmd/scc-cc/cc2/qbe/cgen.c b/src/cmd/scc-cc/cc2/qbe/cgen.c
@@ -378,30 +378,6 @@ lhs(Node *np)
}
static Node *
-ternary(Node *np)
-{
- Node ifyes, ifno, phi, *colon, *tmp;
-
- tmp = tmpnode(&np->type, NULL);
- label2node(&ifyes, NULL);
- label2node(&ifno, NULL);
- label2node(&phi, NULL);
-
- colon = np->right;
- code(ASBRANCH, rhs(np->left), &ifyes, &ifno);
-
- setlabel(ifyes.u.sym);
- copy(&tmp->type, tmp, rhs(colon->left));
- code(ASJMP, NULL, &phi, NULL);
-
- setlabel(ifno.u.sym);
- copy(&tmp->type, tmp, rhs(colon->right));
- setlabel(phi.u.sym);
-
- return tmp;
-}
-
-static Node *
function(void)
{
Node aux;
@@ -605,8 +581,6 @@ rhs(Node *np)
return cast(tp, rhs(l));
case OASSIG:
return assign(np);
- case OASK:
- return ternary(np);
case OSNEG:
sign = (tp->flags & SIGNF) == 0;
size = tp->size == 8;
diff --git a/src/cmd/scc-cc/cc2/sethi.c b/src/cmd/scc-cc/cc2/sethi.c
@@ -106,6 +106,37 @@ comma(Node *np)
}
static Node *
+ternary(Node *np)
+{
+ Type *tp;
+ Node *tmpvar, *colon, *p;
+ Symbol *tmpsym, *true, *false, *phi;
+
+ true = newlabel();
+ false = newlabel();
+ phi = newlabel();
+ bool(np->left, true, false);
+
+ tp = &np->type;
+ colon = np->right;
+ tmpvar = tmpnode(tp, NULL);
+ tmpsym = tmpvar->u.sym;
+
+ prestmt(labelstmt(NULL, true));
+ p = assignnode(tp, tmpnode(tp, tmpsym), sethi(colon->left));
+ prestmt(p);
+ prestmt(branchnode(NULL, phi));
+
+ prestmt(labelstmt(NULL, false));
+ p = assignnode(tp, tmpnode(tp, tmpsym), sethi(colon->right));
+ prestmt(p);
+
+ prestmt(labelstmt(NULL, phi));
+
+ return sethi(tmpvar);
+}
+
+static Node *
replace(Node *what, Node *with)
{
Node *prev, *next;
@@ -122,7 +153,7 @@ Node *
sethi(Node *np)
{
int op;
- Node *next, *p, *l, *r;
+ Node *next, *l, *r;
if (!np)
return np;
@@ -152,11 +183,12 @@ sethi(Node *np)
r = np->right;
comma(np->left);
return replace(np, r);
+ case OASK:
+ return replace(np, ternary(np));
case ONEG:
case OAND:
case OOR:
- p = logicexpr(np);
- return replace(np, p);
+ return replace(np, logicexpr(np));
default:
np = tsethi(np);
break;