shithub: mc

Download patch

ref: 0781d54100ac448eda8cc9e39bd931d47b21988a
parent: f65cbaf876d37084d8fc421b4642847bf5ac9162
author: Ori Bernstein <[email protected]>
date: Tue Oct 8 19:00:29 EDT 2013

Allow folding initializers for constants.

    Todo, prevent cyclic initializations. Eg:

        const x = y
        const y = x

--- a/6/simp.c
+++ b/6/simp.c
@@ -1411,7 +1411,7 @@
             printf("FOLD FROM ----------\n");
             dump(s->stmts[i], stdout);
         }
-        s->stmts[i] = fold(s->stmts[i]);
+        s->stmts[i] = fold(s->stmts[i], 0);
         if (debugopt['f']) {
             printf("TO ------------\n");
             dump(s->stmts[i], stdout);
@@ -1459,7 +1459,7 @@
 {
     Node *e;
 
-    dcl->decl.init = fold(dcl->decl.init);;
+    dcl->decl.init = fold(dcl->decl.init, 1);;
     e = dcl->decl.init;
     if (e && exprop(e) == Olit) {
         if (e->expr.args[0]->lit.littype == Lfunc)
--- a/mi/fold.c
+++ b/mi/fold.c
@@ -45,7 +45,7 @@
     return n;
 }
 
-Node *fold(Node *n)
+Node *fold(Node *n, int foldvar)
 {
     Node **args, *r;
     vlong a, b;
@@ -59,10 +59,11 @@
     r = NULL;
     args = n->expr.args;
     for (i = 0; i < n->expr.nargs; i++)
-        args[i] = fold(args[i]);
+        args[i] = fold(args[i], foldvar);
     switch (exprop(n)) {
         case Ovar:
-            /* FIXME: chase small consts */
+            if (foldvar && decls[n->expr.did]->decl.isconst)
+                r = fold(decls[n->expr.did]->decl.init, foldvar);
             break;
         case Oadd:
             /* x + 0 = 0 */
--- a/mi/opt.h
+++ b/mi/opt.h
@@ -25,7 +25,7 @@
 };
 
 /* expression folding */
-Node *fold(Node *n);
+Node *fold(Node *n, int foldvar);
 /* Takes a reduced block, and returns a flow graph. */
 Cfg *mkcfg(Node **nl, size_t nn);
 void dumpcfg(Cfg *c, FILE *fd);