ref: 63405aa8ca5abcca5cf1b1abb4c0b3648aa5bfc4
parent: 7096d0e99d9deb053a7763f49b2b1b7b1df20a6a
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Feb 20 17:14:26 EST 2016
Move more code to flatten.c
--- a/6/simp.c
+++ b/6/simp.c
@@ -734,53 +734,31 @@
return dst;
}
-/* Takes a tuple and binds the i'th element of it to the
- * i'th name on the rhs of the assignment. */
-static Node *destructure(Simp *s, Node *lhs, Node *rhs)
-{
- Node *lv, *rv, **args;
- size_t i;
-
- args = lhs->expr.args;
- rhs = rval(s, rhs, NULL);
- for (i = 0; i < lhs->expr.nargs; i++) {
- lv = lval(s, args[i]);
- rv = tupget(s, rhs, i, lv);
- assert(rv == lv);
- }
-
- return NULL;
-}
-
static Node *assign(Simp *s, Node *lhs, Node *rhs)
{
Node *t, *u, *v, *r;
- if (exprop(lhs) == Otup) {
- r = destructure(s, lhs, rhs);
- } else {
- t = lval(s, lhs);
- u = rval(s, rhs, t);
+ t = lval(s, lhs);
+ u = rval(s, rhs, t);
- if (tybase(exprtype(lhs))->type == Tyvoid)
- return u;
+ if (tybase(exprtype(lhs))->type == Tyvoid)
+ return u;
- /* hack: we're assigning to lhs, but blitting shit over doesn't
- * trigger that */
- if (stacknode(lhs))
- def(s, lhs);
- /* if we stored the result into t, rval() should return that,
- * so we know our work is done. */
- if (u == t) {
- r = t;
- } else if (stacknode(lhs)) {
- t = addr(s, t, exprtype(lhs));
- u = addr(s, u, exprtype(lhs));
- v = disp(lhs->loc, size(lhs));
- r = mkexpr(lhs->loc, Oblit, t, u, v, NULL);
- } else {
- r = set(t, u);
- }
+ /* hack: we're assigning to lhs, but blitting shit over doesn't
+ * trigger that */
+ if (stacknode(lhs))
+ def(s, lhs);
+ /* if we stored the result into t, rval() should return that,
+ * so we know our work is done. */
+ if (u == t) {
+ r = t;
+ } else if (stacknode(lhs)) {
+ t = addr(s, t, exprtype(lhs));
+ u = addr(s, u, exprtype(lhs));
+ v = disp(lhs->loc, size(lhs));
+ r = mkexpr(lhs->loc, Oblit, t, u, v, NULL);
+ } else {
+ r = set(t, u);
}
return r;
}
@@ -893,54 +871,7 @@
}
-static Node *comparecomplex(Simp *s, Node *n, Op op)
-{
- fatal(n, "Complex comparisons not yet supported\n");
- return NULL;
-}
-static Node *compare(Simp *s, Node *n, int fields)
-{
- const Op cmpmap[Numops][3] = {
- [Oeq] = {Oeq, Oueq, Ofeq},
- [One] = {One, Oune, Ofne},
- [Ogt] = {Ogt, Ougt, Ofgt},
- [Oge] = {Oge, Ouge, Ofge},
- [Olt] = {Olt, Oult, Oflt},
- [Ole] = {Ole, Oule, Ofle}
- };
- Node *r;
- Type *ty;
- Op newop;
-
- /* void is always void */
- if (tybase(exprtype(n->expr.args[0]))->type == Tyvoid)
- return mkboollit(n->loc, 1);
-
- newop = Obad;
- ty = tybase(exprtype(n->expr.args[0]));
- if (istysigned(ty))
- newop = cmpmap[n->expr.op][0];
- else if (istyunsigned(ty))
- newop = cmpmap[n->expr.op][1];
- else if (istyunsigned(ty))
- newop = cmpmap[n->expr.op][1];
- else if (ty->type == Typtr)
- newop = cmpmap[n->expr.op][1];
- else if (istyfloat(ty))
- newop = cmpmap[n->expr.op][2];
-
- if (newop != Obad) {
- n->expr.op = newop;
- r = visit(s, n);
- } else if (fields) {
- r = comparecomplex(s, n, exprop(n));
- } else {
- fatal(n, "unsupported comparison on values");
- }
- return r;
-}
-
static Node *vatypeinfo(Simp *s, Node *n)
{
Node *ti, *tp, *td, *tn;
@@ -1304,7 +1235,7 @@
break;
case Oneg:
if (istyfloat(exprtype(n))) {
- t =mkfloat(n->loc, -1.0);
+ t = mkfloat(n->loc, -1.0);
u = mkexpr(n->loc, Olit, t, NULL);
t->lit.type = n->expr.type;
u->expr.type = n->expr.type;
@@ -1325,12 +1256,6 @@
if (s->nloopstep == 0)
fatal(n, "trying to continue when not in loop");
jmp(s, s->loopstep[s->nloopstep - 1]);
- break;
- case Oeq: case One:
- r = compare(s, n, 1);
- break;
- case Ogt: case Oge: case Olt: case Ole:
- r = compare(s, n, 0);
break;
case Otupget:
assert(exprop(args[1]) == Olit);
--- a/mi/flatten.c
+++ b/mi/flatten.c
@@ -278,6 +278,54 @@
return rhs;
}
+static Node *comparecomplex(Flattenctx *s, Node *n, Op op)
+{
+ fatal(n, "Complex comparisons not yet supported\n");
+ return NULL;
+}
+
+static Node *compare(Flattenctx *s, Node *n, int fields)
+{
+ const Op cmpmap[Numops][3] = {
+ [Oeq] = {Oeq, Oueq, Ofeq},
+ [One] = {One, Oune, Ofne},
+ [Ogt] = {Ogt, Ougt, Ofgt},
+ [Oge] = {Oge, Ouge, Ofge},
+ [Olt] = {Olt, Oult, Oflt},
+ [Ole] = {Ole, Oule, Ofle}
+ };
+ Node *r;
+ Type *ty;
+ Op newop;
+
+ /* void is always void */
+ if (tybase(exprtype(n->expr.args[0]))->type == Tyvoid)
+ return mkboollit(n->loc, 1);
+
+ newop = Obad;
+ ty = tybase(exprtype(n->expr.args[0]));
+ if (istysigned(ty))
+ newop = cmpmap[n->expr.op][0];
+ else if (istyunsigned(ty))
+ newop = cmpmap[n->expr.op][1];
+ else if (istyunsigned(ty))
+ newop = cmpmap[n->expr.op][1];
+ else if (ty->type == Typtr)
+ newop = cmpmap[n->expr.op][1];
+ else if (istyfloat(ty))
+ newop = cmpmap[n->expr.op][2];
+
+ if (newop != Obad) {
+ n->expr.op = newop;
+ r = visit(s, n);
+ } else if (fields) {
+ r = comparecomplex(s, n, exprop(n));
+ } else {
+ fatal(n, "unsupported comparison on values");
+ }
+ r->expr.type = mktype(n->loc, Tybool);
+ return r;
+}
static Node *rval(Flattenctx *s, Node *n)
{
Node *t, *u; /* temporary nodes */
@@ -486,12 +534,12 @@
fatal(n, "trying to continue when not in loop");
jmp(s, s->loopstep[s->nloopstep - 1]);
break;
-// case Oeq: case One:
-// r = compare(s, n, 1);
-// break;
-// case Ogt: case Oge: case Olt: case Ole:
-// r = compare(s, n, 0);
-// break;
+ case Oeq: case One:
+ r = compare(s, n, 1);
+ break;
+ case Ogt: case Oge: case Olt: case Ole:
+ r = compare(s, n, 0);
+ break;
// case Otupget:
// assert(exprop(args[1]) == Olit);
// i = args[1]->expr.args[0]->lit.intval;
@@ -708,6 +756,7 @@
flatten(s, lcond);
len = seqlen(s, seq, idxtype);
done = mkexpr(n->loc, Olt, idx, len, NULL);
+ done->expr.type = mktype(n->loc, Tybool);
cjmp(s, done, lmatch, lend);
flatten(s, lmatch);
val = mkexpr(n->loc, Oidx, seq, idx);