shithub: mc

Download patch

ref: f88d2664b6c80eb13ae0041ba1713a57a384ca20
parent: 0226a1a631e50ac5ad042efc85cf5d5461f5b0f8
author: Ori Bernstein <[email protected]>
date: Tue Jan 21 09:48:11 EST 2014

Implement more restrictive folding for casts.

    Now, at least, it will be correct.

--- a/6/simp.c
+++ b/6/simp.c
@@ -709,6 +709,7 @@
 
     pushstab(n->block.scope);
     for (i = 0; i < n->block.nstmts; i++) {
+        n->block.stmts[i] = fold(n->block.stmts[i], 0);
         simp(s, n->block.stmts[i]);
     }
     popstab();
--- a/mi/fold.c
+++ b/mi/fold.c
@@ -59,6 +59,46 @@
     return 0;
 }
 
+static Node *foldcast(Node *n)
+{
+    Type *to, *from;
+    Node *sub;
+
+    sub = n->expr.args[0];
+    to = exprtype(n);
+    from = exprtype(sub);
+
+    switch (tybase(to)->type) {
+        case Tybool:
+        case Tyint8: case Tyint16: case Tyint32: case Tyint64:
+        case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint64:
+        case Tyint: case Tyuint: case Tylong: case Tyulong:
+        case Tychar: case Tybyte:
+        case Typtr:
+            switch (tybase(from)->type) {
+                case Tybool:
+                case Tyint8: case Tyint16: case Tyint32: case Tyint64:
+                case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint64:
+                case Tyint: case Tyuint: case Tylong: case Tyulong:
+                case Tychar: case Tybyte:
+                case Typtr:
+                    if (exprop(sub) == Olit || tybase(from)->type == tybase(to)->type) {
+                        sub->expr.type = to;
+                        return sub;
+                    } else {
+                        return n;
+                    }
+                default:
+                    return n;
+            }
+        default:
+            return n;
+    }
+    return n;
+}
+
+
+
 Node *fold(Node *n, int foldvar)
 {
     Node **args, *r;
@@ -158,10 +198,7 @@
                 r = t->asize;
             break;
         case Ocast:
-            /* FIXME: we currentl assume that the bits of the
-             * val are close enough. */
-            r = args[0];
-            r->expr.type = exprtype(n);
+            r = foldcast(n);
             break;
         default:
             break;
@@ -172,3 +209,4 @@
     else
         return n;
 }
+