shithub: mc

Download patch

ref: a9407d8bbc6edce694be614f4bfed8bd037c6862
parent: cc8d65b2d48716971a2bd060c7e55351137f638f
author: Ori Bernstein <[email protected]>
date: Tue Jun 18 10:27:45 EDT 2013

Fix a bug in array size inference.

    It's not a literal any more. Don't use fields from literals.

--- a/6/simp.c
+++ b/6/simp.c
@@ -916,6 +916,26 @@
     return r;
 }
 
+static Node *assignat(Simp *s, Node *r, size_t off, Node *val)
+{
+    Node *pval, *pdst;
+    Node *sz;
+    Node *st;
+
+    val = rval(s, val, NULL);
+    pdst = add(r, disp(val->line, off));
+
+    if (stacknode(val)) {
+        sz = disp(val->line, size(val));
+        pval = addr(s, val, exprtype(val));
+        st = mkexpr(val->line, Oblit, pdst, pval, sz, NULL);
+    } else {
+        st = set(deref(pdst), val);
+    }
+    append(s, st);
+    return r;
+}
+
 /* Simplify tuple construction to a stack allocated
  * value by evaluating the rvalue of each node on the
  * rhs and assigning it to the correct offset from the
@@ -922,7 +942,7 @@
  * head of the tuple. */
 static Node *simptup(Simp *s, Node *n, Node *dst)
 {
-    Node *pdst, *pval, *val, *sz, *st, **args;
+    Node **args;
     Node *r;
     size_t i, off;
 
@@ -933,17 +953,7 @@
 
     off = 0;
     for (i = 0; i < n->expr.nargs; i++) {
-        val = rval(s, args[i], NULL);
-        pdst = add(r, disp(n->line, off));
-
-        if (stacknode(args[i])) {
-            sz = disp(n->line, size(val));
-            pval = addr(s, val, exprtype(val));
-            st = mkexpr(n->line, Oblit, pdst, pval, sz, NULL);
-        } else {
-            st = set(deref(pdst), val);
-        }
-        append(s, st);
+        assignat(s, r, off, args[i]);
         off += size(args[i]);
     }
     return dst;
@@ -1002,26 +1012,6 @@
     die("No uget simplification yet");
 }
 
-static Node *assignat(Simp *s, Node *r, size_t off, Node *val)
-{
-    Node *pval, *pdst;
-    Node *sz;
-    Node *st;
-
-    val = rval(s, val, NULL);
-    pdst = add(r, disp(val->line, off));
-
-    if (stacknode(val)) {
-        sz = disp(val->line, size(val));
-        pval = addr(s, val, exprtype(val));
-        st = mkexpr(val->line, Oblit, pdst, pval, sz, NULL);
-    } else {
-        st = set(deref(pdst), val);
-    }
-    append(s, st);
-    return r;
-}
-
 /* simplifies 
  *      a || b
  * to
@@ -1134,7 +1124,7 @@
                 r = dst;
             else
                 r = temp(s, n);
-            t = addr(s, dst, exprtype(dst));
+            t = addr(s, r, exprtype(r));
             for (i = 0; i < n->expr.nargs; i++)
                 assignat(s, t, size(n->expr.args[i])*i, n->expr.args[i]);
             break;
@@ -1143,7 +1133,7 @@
                 r = dst;
             else
                 r = temp(s, n);
-            t = addr(s, dst, exprtype(dst));
+            t = addr(s, r, exprtype(r));
             for (i = 0; i < n->expr.nargs; i++)
                 assignat(s, t, offset(n, n->expr.args[i]->expr.idx), n->expr.args[i]);
             break;
@@ -1255,7 +1245,7 @@
     s->stksz = align(s->stksz, min(size(n), Ptrsz));
     if (debugopt['i']) {
         dump(n, stdout);
-        printf("declared at %zd\n", s->stksz);
+        printf("declared at %zd, size = %zd\n", s->stksz, size(n));
     }
     htput(s->stkoff, n, (void*)s->stksz);
 }
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -112,6 +112,7 @@
 {
     size_t i;
     char *ty;
+    int tid;
 
     indent(fd, depth);
     if (!n) {
@@ -174,8 +175,12 @@
             break;
         case Nexpr:
             ty = tystr(n->expr.type);
+            if (n->expr.type)
+                tid = n->expr.type->tid;
+            else
+                tid = -1;
             fprintf(fd, " (type = %s [tid %d], op = %s, isconst = %d, did=%zd)\n",
-                    ty, n->expr.type->tid, opstr(n->expr.op), n->expr.isconst, n->expr.did);
+                    ty, tid, opstr(n->expr.op), n->expr.isconst, n->expr.did);
             free(ty);
             outnode(n->expr.idx, fd, depth + 1);
             for (i = 0; i < n->expr.nargs; i++)
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -616,11 +616,12 @@
             {$$ = mkexprl($1->line, Oarr, $2.nl, $2.nn);}
         | Tosqbrac structelts Tcsqbrac
             {$$ = mkexprl($1->line, Ostruct, $2.nl, $2.nn);}
+        | Tosqbrac Tcsqbrac /* [] is the empty array. */
+            {$$ = mkexprl($1->line, Oarr, NULL, 0);}
         ;
 
 arrayelts
-        : /* empty */
-        | arrayelt
+        : arrayelt
             {$$.nl = NULL; $$.nn = 0;
              lappend(&$$.nl, &$$.nn, mkidxinit($1->line, mkint($1->line, 0), $1));}
         | arrayelts Tcomma arrayelt
@@ -630,8 +631,7 @@
         ;
 
 structelts
-        : /* empty */ {$$.nl = NULL; $$.nn = 0;}
-        | structelt
+        : structelt
             {$$.nl = NULL; $$.nn = 0;
              lappend(&$$.nl, &$$.nn, $1);}
         | structelts Tcomma structelt
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -759,7 +759,7 @@
     Type *t;
     Node *len;
 
-    len = mkintlit(n->line, n->lit.nelt);
+    len = mkintlit(n->line, n->expr.nargs);
     t = mktyarray(n->line, mktyvar(n->line), len);
     for (i = 0; i < n->expr.nargs; i++) {
         infernode(st, n->expr.args[i], NULL, NULL);
--- a/parse/type.c
+++ b/parse/type.c
@@ -423,7 +423,7 @@
             break;
         case Tyarray:
             p += tybfmt(p, end - p, t->sub[0]);
-            p += snprintf(p, end - p, "[LEN]");
+            p += snprintf(p, end - p, "[%llu]", t->asize->expr.args[0]->lit.intval);
             break;
         case Tyfunc:
             p += snprintf(p, end - p, "(");