ref: 9c52b9aaf791386a95a6eb11e27d2a982b72d872
parent: 63405aa8ca5abcca5cf1b1abb4c0b3648aa5bfc4
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Feb 20 17:40:22 EST 2016
Move more operators to flatten. preinc, predec, comparison conversions.
--- a/6/simp.c
+++ b/6/simp.c
@@ -125,15 +125,6 @@
return n;
}
-static Node *subk(Node *n, uvlong v)
-{
- Node *k;
-
- k = mkintlit(n->loc, v);
- k->expr.type = exprtype(n);
- return sub(n, k);
-}
-
static Node *mul(Node *a, Node *b)
{
Node *n;
@@ -276,11 +267,6 @@
return t;
}
-static void jmp(Simp *s, Node *lbl)
-{
- append(s, mkexpr(lbl->loc, Ojmp, lbl, NULL));
-}
-
static void cjmp(Simp *s, Node *cond, Node *iftrue, Node *iffalse)
{
Node *jmp;
@@ -1064,19 +1050,6 @@
size_t i;
Type *ty;
- const Op fusedmap[Numops] = {
- [Oaddeq] = Oadd,
- [Osubeq] = Osub,
- [Omuleq] = Omul,
- [Odiveq] = Odiv,
- [Omodeq] = Omod,
- [Oboreq] = Obor,
- [Obandeq] = Oband,
- [Obxoreq] = Obxor,
- [Obsleq] = Obsl,
- [Obsreq] = Obsr,
- };
-
r = NULL;
args = n->expr.args;
switch (exprop(n)) {
@@ -1140,38 +1113,9 @@
r = simpcast(s, args[0], exprtype(n));
break;
- /* fused ops:
- * foo ?= blah
- * =>
- * foo = foo ? blah*/
- case Oaddeq: case Osubeq: case Omuleq: case Odiveq: case Omodeq:
- case Oboreq: case Obandeq: case Obxoreq: case Obsleq: case Obsreq:
- assert(fusedmap[exprop(n)] != Obad);
- u = lval(s, args[0]);
- v = rval(s, args[1], NULL);
- v = mkexpr(n->loc, fusedmap[exprop(n)], u, v, NULL);
- v->expr.type = u->expr.type;
- r = set(u, v);
- break;
-
/* ++expr(x)
* => args[0] = args[0] + 1
* expr(x) */
- case Opreinc:
- v = assign(s, args[0], addk(args[0], 1));
- append(s, v);
- r = rval(s, args[0], NULL);
- break;
- case Opredec:
- v = assign(s, args[0], subk(args[0], 1));
- append(s, v);
- r = rval(s, args[0], NULL);
- break;
-
- /* expr(x++)
- * => expr
- * x = x + 1
- */
case Olit:
switch (args[0]->lit.littype) {
case Lvoid:
@@ -1246,17 +1190,6 @@
r = visit(s, n);
}
break;
- /* FIXME: remove this after custom iters are done. */
- case Obreak:
- if (s->nloopexit == 0)
- fatal(n, "trying to break when not in loop");
- jmp(s, s->loopexit[s->nloopexit - 1]);
- break;
- case Ocontinue:
- if (s->nloopstep == 0)
- fatal(n, "trying to continue when not in loop");
- jmp(s, s->loopstep[s->nloopstep - 1]);
- break;
case Otupget:
assert(exprop(args[1]) == Olit);
i = args[1]->expr.args[0]->lit.intval;
@@ -1264,6 +1197,9 @@
r = tupget(s, t, i, dst);
break;
case Olor: case Oland:
+ case Oaddeq: case Osubeq: case Omuleq: case Odiveq: case Omodeq:
+ case Oboreq: case Obandeq: case Obxoreq: case Obsleq: case Obsreq:
+ case Opreinc: case Opredec:
case Opostinc: case Opostdec:
die("invalid operator: should have been removed");
case Obad:
--- a/mi/flatten.c
+++ b/mi/flatten.c
@@ -328,24 +328,24 @@
}
static Node *rval(Flattenctx *s, Node *n)
{
- Node *t, *u; /* temporary nodes */
+ Node *t, *u, *v; /* temporary nodes */
Node *r; /* expression result */
Node **args;
size_t i;
Type *ty;
-// const Op fusedmap[Numops] = {
-// [Oaddeq] = Oadd,
-// [Osubeq] = Osub,
-// [Omuleq] = Omul,
-// [Odiveq] = Odiv,
-// [Omodeq] = Omod,
-// [Oboreq] = Obor,
-// [Obandeq] = Oband,
-// [Obxoreq] = Obxor,
-// [Obsleq] = Obsl,
-// [Obsreq] = Obsr,
-// };
+ const Op fusedmap[Numops] = {
+ [Oaddeq] = Oadd,
+ [Osubeq] = Osub,
+ [Omuleq] = Omul,
+ [Odiveq] = Odiv,
+ [Omodeq] = Omod,
+ [Oboreq] = Obor,
+ [Obandeq] = Oband,
+ [Obxoreq] = Obxor,
+ [Obsleq] = Obsl,
+ [Obsreq] = Obsr,
+ };
r = NULL;
args = n->expr.args;
@@ -418,38 +418,40 @@
// r = flattencast(s, args[0], exprtype(n));
// break;
//
-// /* fused ops:
-// * foo ?= blah
-// * =>
-// * foo = foo ? blah*/
-// case Oaddeq: case Osubeq: case Omuleq: case Odiveq: case Omodeq:
-// case Oboreq: case Obandeq: case Obxoreq: case Obsleq: case Obsreq:
-// assert(fusedmap[exprop(n)] != Obad);
-// u = lval(s, args[0]);
-// v = rval(s, args[1], NULL);
-// v = mkexpr(n->loc, fusedmap[exprop(n)], u, v, NULL);
-// v->expr.type = u->expr.type;
-// r = set(u, v);
-// break;
-//
-// /* ++expr(x)
-// * => args[0] = args[0] + 1
-// * expr(x) */
-// case Opreinc:
-// v = assign(s, args[0], addk(args[0], 1));
-// append(s, v);
-// r = rval(s, args[0], NULL);
-// break;
-// case Opredec:
-// v = assign(s, args[0], subk(args[0], 1));
-// append(s, v);
-// r = rval(s, args[0], NULL);
-// break;
-//
-// /* expr(x++)
-// * => expr
-// * x = x + 1
-// */
+ /* fused ops:
+ * foo ?= blah
+ * =>
+ * foo = foo ? blah*/
+ case Oaddeq: case Osubeq: case Omuleq: case Odiveq: case Omodeq:
+ case Oboreq: case Obandeq: case Obxoreq: case Obsleq: case Obsreq:
+ assert(fusedmap[exprop(n)] != Obad);
+ u = lval(s, args[0]);
+ v = rval(s, args[1]);
+ v = mkexpr(n->loc, fusedmap[exprop(n)], u, v, NULL);
+ v->expr.type = u->expr.type;
+ r = asn(u, v);
+ break;
+
+ /* ++expr(x)
+ * => args[0] = args[0] + 1
+ * expr(x) */
+ case Opreinc:
+ t = lval(s, args[0]);
+ v = asn(t, addk(t, 1));
+ append(s, v);
+ r = rval(s, t);
+ break;
+ case Opredec:
+ t = lval(s, args[0]);
+ v = asn(t, subk(t, 1));
+ append(s, v);
+ r = rval(s, t);
+ break;
+
+ /* expr(x++)
+ * => expr
+ * x = x + 1
+ */
case Opostinc:
r = lval(s, args[0]);
t = asn(r, addk(r, 1));