summaryrefslogtreecommitdiff
path: root/opt
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2012-07-24 01:59:43 -0400
committerOri Bernstein <ori@eigenstate.org>2012-07-24 01:59:43 -0400
commit948714f154e2cb986a34225ab2cef602c746d106 (patch)
tree8bbe81f5c15bff368a11c58b09e442629e450a9c /opt
parent68f6db30794bf0d5390fc895063e62306fc72753 (diff)
downloadmc-948714f154e2cb986a34225ab2cef602c746d106.tar.gz
Make folding preserve types.
Diffstat (limited to 'opt')
-rw-r--r--opt/fold.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/opt/fold.c b/opt/fold.c
index feabef4..04befa3 100644
--- a/opt/fold.c
+++ b/opt/fold.c
@@ -35,12 +35,13 @@ static int isval(Node *n, vlong val)
return v == val;
}
-static Node *val(int line, vlong val)
+static Node *val(int line, vlong val, Type *t)
{
Node *n;
n = mkint(line, val);
n = mkexpr(line, Olit, n, NULL);
+ n->expr.type = t;
return n;
}
@@ -70,14 +71,14 @@ Node *fold(Node *n)
if (isval(args[1], 0))
r = args[0];
if (islit(args[0], &a) && islit(args[1], &b))
- r = val(n->line, a + b);
+ r = val(n->line, a + b, exprtype(n));
break;
case Osub:
/* x - 0 = 0 */
if (isval(args[1], 0))
r = args[0];
if (islit(args[0], &a) && islit(args[1], &b))
- r = val(n->line, a - b);
+ r = val(n->line, a - b, exprtype(n));
break;
case Omul:
/* 1 * x = x */
@@ -91,7 +92,7 @@ Node *fold(Node *n)
if (isval(args[1], 0))
r = args[1];
if (islit(args[0], &a) && islit(args[1], &b))
- r = val(n->line, a * b);
+ r = val(n->line, a * b, exprtype(n));
break;
case Odiv:
/* x/1 = x */
@@ -101,18 +102,18 @@ Node *fold(Node *n)
if (isval(args[1], 0))
r = args[1];
if (islit(args[0], &a) && islit(args[1], &b))
- r = val(n->line, a / b);
+ r = val(n->line, a / b, exprtype(n));
break;
case Omod:
/* x%1 = x */
if (isval(args[1], 0))
r = args[0];
if (islit(args[0], &a) && islit(args[1], &b))
- r = val(n->line, a % b);
+ r = val(n->line, a % b, exprtype(n));
break;
case Oneg:
if (islit(args[0], &a))
- r = val(n->line, -a);
+ r = val(n->line, -a, exprtype(n));
break;
case Ocast:
/* FIXME: we currentl assume that the bits of the