shithub: mc

Download patch

ref: 5beec1cadbf562073ea57e65337cca33b36a4627
parent: 3e622c8ee2d7aac48da5e8614b88aaa4ea550aae
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Jun 5 22:01:15 EDT 2012

Don't generate function calls too many times.

    We used to return the subtree of impure stores. Don't do that.

--- a/8/isel.c
+++ b/8/isel.c
@@ -152,7 +152,7 @@
         case Ovar:
             if (hthas(s->locs, (void*)n->expr.did)) {
                 stkoff = (size_t)htget(s->locs, (void*)n->expr.did);
-                locmem(&l, -stkoff, Resp, Rnone, ModeL);
+                locmem(&l, -stkoff, Rebp, Rnone, ModeL);
             } else if (hthas(s->globls, (void*)n->expr.did)) {
                 locstrlbl(&l, htget(s->globls, (void*)n->expr.did));
             } else {
@@ -439,6 +439,7 @@
 
     locreg(&esp, Resp);
     locreg(&eax, Reax);
+    claimreg(s, Reax);
     argsz = 0;
     /* Have to calculate the amount to bump the stack
      * pointer by in one pass first, otherwise if we push
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -58,9 +58,9 @@
     lappend(&s->stmts, &s->nstmts, n);
 }
 
-int isimpure(Node *n)
+int ispure(Node *n)
 {
-    return 0;
+    return ispureop[exprop(n)];
 }
 
 int isconstfn(Sym *s)
@@ -176,7 +176,6 @@
 
 void jmp(Simp *s, Node *lbl) { append(s, mkexpr(-1, Ojmp, lbl, NULL)); }
 Node *store(Node *t, Node *n) { return mkexpr(-1, Ostor, t, n, NULL); }
-Node *storetmp(Simp *s, Node *n) { return store(temp(s, n), n); }
 
 void cjmp(Simp *s, Node *cond, Node *iftrue, Node *iffalse)
 {
@@ -195,7 +194,7 @@
 
     l1 = genlbl();
     l2 = genlbl();
-    c = simp(s, n->ifstmt.cond);
+    c = rval(s, n->ifstmt.cond);
     cjmp(s, c, l1, l2);
     simp(s, l1);
     simp(s, n->ifstmt.iftrue);
@@ -425,15 +424,13 @@
             r = mkexpr(-1, Ostor, t, v, NULL);
             break;
         default:
-            if (isimpure(n)) {
-                v = rval(s, n);
-                t = storetmp(s, v);
-                append(s, t);
-                r = t;
-            } else {
-                for (i = 0; i < n->expr.nargs; i++)
-                    n->expr.args[i] = rval(s, n->expr.args[i]);
+            for (i = 0; i < n->expr.nargs; i++)
+                n->expr.args[i] = rval(s, n->expr.args[i]);
+            if (ispure(n)) {
                 r = n;
+            } else {
+                r = temp(s, n);
+                append(s, store(r, n));
             }
     }
     return r;
@@ -443,8 +440,8 @@
 {
     assert(n->type == Ndecl);
     if (debug)
-        printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, s->stksz);
-    htput(s->locs, (void*)n->decl.sym->id, (void*)s->stksz);
+        printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, s->stksz + 4);
+    htput(s->locs, (void*)n->decl.sym->id, (void*)(s->stksz + 4));
     s->stksz += size(n);
 }
 
@@ -453,7 +450,7 @@
     assert(n->type == Ndecl);
     if (debug)
         printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, -(s->argsz + 8));
-    htput(s->locs, (void*)n->decl.sym->id, (void*)-s->argsz);
+    htput(s->locs, (void*)n->decl.sym->id, (void*)-(s->argsz + 8));
     s->argsz += size(n);
 }
 
@@ -527,6 +524,9 @@
     int i;
     Simp s = {0,};
     Func fn;
+
+    if(debug)
+        printf("\n\nfunction %s\n", name);
 
     /* set up the simp context */
     s.locs = mkht(ptrhash, ptreq);
--- a/parse/names.c
+++ b/parse/names.c
@@ -12,7 +12,13 @@
 #include "parse.h"
 
 static char *optab[] =  {
-#define O(op) #op,
+#define O(op, pure) #op,
+#include "ops.def"
+#undef O
+};
+
+int ispureop[] = {
+#define O(op, pure) pure,
 #include "ops.def"
 #undef O
 };
--- a/parse/ops.def
+++ b/parse/ops.def
@@ -1,54 +1,54 @@
-O(Obad)
-O(Oadd)
-O(Osub)
-O(Omul)
-O(Odiv)
-O(Omod)
-O(Oneg)
-O(Obor)
-O(Oband)
-O(Obxor)
-O(Obsl)
-O(Obsr)
-O(Obnot)
-O(Opreinc)
-O(Opostinc)
-O(Opredec)
-O(Opostdec)
-O(Oaddr)
-O(Oderef)
-O(Olor)
-O(Oland)
-O(Olnot)
-O(Oeq)
-O(One)
-O(Ogt)
-O(Oge)
-O(Olt)
-O(Ole)
-O(Oasn)
-O(Oaddeq)
-O(Osubeq)
-O(Omuleq)
-O(Odiveq)
-O(Omodeq)
-O(Oboreq)
-O(Obandeq)
-O(Obxoreq)
-O(Obsleq)
-O(Obsreq)
-O(Oidx)
-O(Oslice)
-O(Omemb)
-O(Osize)
-O(Ocall)
-O(Ocast)
-O(Oret)
-O(Ojmp)
-O(Ovar)
-O(Olit)
-O(Olbl)
+O(Obad, 1)
+O(Oadd, 1)
+O(Osub, 1)
+O(Omul, 1)
+O(Odiv, 1)
+O(Omod, 1)
+O(Oneg, 1)
+O(Obor, 1)
+O(Oband, 1)
+O(Obxor, 1)
+O(Obsl, 1)
+O(Obsr, 1)
+O(Obnot, 1)
+O(Opreinc, 1)
+O(Opostinc, 1)
+O(Opredec, 1)
+O(Opostdec, 1)
+O(Oaddr, 1)
+O(Oderef, 1)
+O(Olor, 1)
+O(Oland, 1)
+O(Olnot, 1)
+O(Oeq, 1)
+O(One, 1)
+O(Ogt, 1)
+O(Oge, 1)
+O(Olt, 1)
+O(Ole, 1)
+O(Oasn, 1)
+O(Oaddeq, 1)
+O(Osubeq, 1)
+O(Omuleq, 1)
+O(Odiveq, 1)
+O(Omodeq, 1)
+O(Oboreq, 1)
+O(Obandeq, 1)
+O(Obxoreq, 1)
+O(Obsleq, 1)
+O(Obsreq, 1)
+O(Oidx, 1)
+O(Oslice, 1)
+O(Omemb, 1)
+O(Osize, 1)
+O(Ocall, 0)
+O(Ocast, 1)
+O(Oret, 1)
+O(Ojmp, 1)
+O(Ovar, 1)
+O(Olit, 1)
+O(Olbl, 1)
 /* backend-only */
-O(Ocjmp)        /* conditional jump */
-O(Oload)        /* load from memory */
-O(Ostor)        /* store to memory */
+O(Ocjmp, 1)        /* conditional jump */
+O(Oload, 1)        /* load from memory */
+O(Ostor, 1)        /* store to memory */
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -17,7 +17,7 @@
 
 
 typedef enum {
-#define O(op) op,
+#define O(op, pure) op,
 #include "ops.def"
     Numops,
 #undef O
@@ -219,6 +219,7 @@
 extern int ncstrs;
 
 extern Type *littypes[]; /* literal type -> type map */
+extern int ispureop[];
 
 /* data structures */
 Bitset *mkbs(void);
--