commit 36bf2b2d93e248eb7c2789070a198972e756ab26
parent 1a31d8e1d051c04df16ab22f03f4a9354c15c05b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 4 Jan 2025 16:27:52 +0100
cc2: Add sym paramter to tmpnode()
There are multiple cases where we will have to use the same
temporary in different nodes, so it is very useful to pass
the parameter to tmpnode().
Diffstat:
4 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/src/cmd/scc-cc/cc2/cc2.h b/src/cmd/scc-cc/cc2/cc2.h
@@ -276,7 +276,7 @@ extern void defvar(Symbol *), defpar(Symbol *), defglobal(Symbol *);
extern void setlabel(Symbol *);
extern Node *label2node(Node *np, Symbol *sym);
extern Node *constnode(Node *np, TUINT n, Type *tp);
-extern Node *tmpnode(Type *);
+extern Node *tmpnode(Type *, Symbol *);
extern Node *idxnode(Node *, long);
extern void delcode(void);
extern Symbol *newlabel(void);
diff --git a/src/cmd/scc-cc/cc2/code.c b/src/cmd/scc-cc/cc2/code.c
@@ -152,21 +152,25 @@ label2node(Node *np, Symbol *sym)
}
Node *
-tmpnode(Type *tp)
+tmpnode(Type *tp, Symbol *sym)
{
unsigned short flags;
- Symbol *sym;
Node *np;
np = node(OTMP);
- sym = getsym(TMPSYM);
- sym->type = np->type = *tp;
+ if (!sym) {
+ sym = getsym(TMPSYM);
+ sym->type = np->type = *tp;
+ sym->kind = STMP;
+ }
+
flags = tp->flags & ~(PARF|INITF);
sym->type.flags = np->type.flags = flags;
- sym->kind = STMP;
+ np->type = *tp;
np->left = np->right = NULL;
np->u.sym = sym;
np->op = OTMP;
+
return np;
}
diff --git a/src/cmd/scc-cc/cc2/qbe/cgen.c b/src/cmd/scc-cc/cc2/qbe/cgen.c
@@ -235,7 +235,7 @@ load(Type *tp, Node *np)
if ((flags & (INTF|SIGNF)) == INTF && tp->size < 8)
++op;
- new = tmpnode(tp);
+ new = tmpnode(tp, NULL);
code(op, new, np, NULL);
return new;
@@ -289,7 +289,7 @@ cast(Type *td, Node *np)
}
assert(op != 0);
- tmp = tmpnode(td);
+ tmp = tmpnode(td, NULL);
code(op, tmp, np, NULL);
return tmp;
@@ -306,12 +306,12 @@ call(Node *np, Node *fun)
pars[n++] = rhs(p->left);
tp = &np->type;
- tmp = tmpnode(tp);
+ tmp = tmpnode(tp, NULL);
code(ASCALL, tmp, fun, NULL);
for (q = pars; q < &pars[n]; ++q) {
op = (q == &pars[n-1]) ? ASPARE : ASPAR;
- code(op, NULL, *q, tmpnode(&(*q)->type));
+ code(op, NULL, *q, tmpnode(&(*q)->type, NULL));
}
code((np->op == OCALL) ? ASCALLE : ASCALLEX, NULL, NULL, NULL);
@@ -416,7 +416,7 @@ ternary(Node *np)
{
Node ifyes, ifno, phi, *colon, *tmp;
- tmp = tmpnode(&np->type);
+ tmp = tmpnode(&np->type, NULL);
label2node(&ifyes, NULL);
label2node(&ifno, NULL);
label2node(&phi, NULL);
@@ -609,7 +609,7 @@ rhs(Node *np)
true = newlabel();
false = newlabel();
phi = label2node(&aux1, NULL);
- tmp = tmpnode(&int32type);
+ tmp = tmpnode(&int32type, NULL);
bool(np, true, false);
@@ -646,7 +646,7 @@ rhs(Node *np)
isfloat = (tp->flags & FLOATF) != 0;
op = opbin[isfloat][size][np->op][sign];
rhs_rhs(&l, &r);
- tmp = tmpnode(tp);
+ tmp = tmpnode(tp, NULL);
code(op, tmp, l, r);
return tmp;
case OCALL:
@@ -668,7 +668,7 @@ rhs(Node *np)
size = tp->size == 8;
isfloat = (tp->flags & FLOATF) != 0;
op = opbin[isfloat][size][np->op][sign];
- tmp = tmpnode(tp);
+ tmp = tmpnode(tp, NULL);
code(op, tmp, rhs(l), NULL);
return tmp;
case OPTR:
@@ -689,7 +689,7 @@ rhs(Node *np)
return NULL;
case BVA_ARG:
l = rhs(l);
- tmp = tmpnode(tp);
+ tmp = tmpnode(tp, NULL);
code(ASVARG, tmp, l, NULL);
return tmp;
default:
diff --git a/src/cmd/scc-cc/cc2/swtch.c b/src/cmd/scc-cc/cc2/swtch.c
@@ -17,7 +17,7 @@ swtch_if(Node *np)
swt = np->u.swtch;
tp = &np->left->type;
- tmpvar = tmpnode(tp);
+ tmpvar = tmpnode(tp, NULL);
np->type = *tp;
np->right = np->left;
np->left = tmpvar;
@@ -26,10 +26,9 @@ swtch_if(Node *np)
cases = swt->cases;
for (bp = cases; bp < &cases[swt->nr]; ++bp) {
- Node *eq, *tmp = node(OTMP);
+ Node *eq, *tmp = tmpnode(tp, tmpvar->u.sym);
p = *bp;
- *tmp = *tmpvar;
eq = node(OEQ);
eq->type = *tp;
eq->left = p->left;