ref: 948714f154e2cb986a34225ab2cef602c746d106
parent: 68f6db30794bf0d5390fc895063e62306fc72753
author: Ori Bernstein <[email protected]>
date: Mon Jul 23 21:59:43 EDT 2012
Make folding preserve types.
--- a/opt/fold.c
+++ b/opt/fold.c
@@ -35,12 +35,13 @@
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,7 +71,7 @@
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 */
@@ -77,7 +78,7 @@
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 @@
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,7 +102,7 @@
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 */
@@ -108,11 +109,11 @@
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