shithub: mc

Download patch

ref: 8e1b91f8c81e73d60294cbb2f90449c38343d878
parent: 7194d93071002efaa90260999cc6530949e2b046
author: Ori Bernstein <[email protected]>
date: Tue Jun 19 16:50:06 EDT 2012

Start attempt to rewrite call expressions.

--- a/8/reduce.c
+++ b/8/reduce.c
@@ -56,6 +56,7 @@
 static Node *zero;
 static Node *ptrsz;
 static Type *tyword;
+static Type *tyvoid;
 
 static Type *base(Type *t)
 {
@@ -529,11 +530,27 @@
     return r;
 }
 
+static Node *visit(Simp *s, Node *n)
+{
+    size_t i;
+    Node *r;
+
+    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;
+}
+
 static Node *rval(Simp *s, Node *n)
 {
     Node *r; /* expression result */
     Node *t, *u, *v; /* temporary nodes */
-    size_t i;
+    Type *ty;
     Node **args;
     const Op fusedmap[] = {
         [Oaddeq]        = Oadd,
@@ -665,15 +682,19 @@
               r = store(t, u);
             }
             break;
-        default:
-            for (i = 0; i < n->expr.nargs; i++)
-                n->expr.args[i] = rval(s, n->expr.args[i]);
-            if (ispure(n)) {
-                r = n;
-            } else {
+        case Ocall:
+            if (size(n) > 4) {
                 r = temp(s, n);
-                append(s, store(r, n));
+                ty = mktyptr(n->line, exprtype(r));
+                linsert(&args[0]->expr.args, &n->expr.nargs, 1, addr(r, ty));
+                linsert(&args[0]->expr.type->sub, &n->expr.type->nsub, 1, ty);
+                args[0]->expr.type->sub[0] = tyvoid;
+                n->expr.type = tyvoid;
             }
+            r = visit(s, n);
+            break;
+        default:
+            r = visit(s, n);
     }
     return r;
 }
@@ -875,6 +896,7 @@
 
     /* declare useful constants */
     tyword = mkty(-1, Tyint);
+    tyword = mkty(-1, Tyvoid);
     one = word(-1, 1);
     zero = word(-1, 0);
     ptrsz = word(-1, 4);
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -363,6 +363,7 @@
 
 /* convenience funcs */
 void lappend(void *l, size_t *len, void *n); /* ugly hack; nl is void* because void*** is incompatible with T*** */
+void linsert(void *l, size_t *len, size_t idx, void *n);
 void *lpop(void *l, size_t *len);
 void ldel(void *l, size_t *len, size_t idx);
 void lfree(void *l, size_t *len);
--- a/parse/util.c
+++ b/parse/util.c
@@ -129,6 +129,20 @@
     return v;
 }
 
+void linsert(void *p, size_t *len, size_t idx, void *v)
+{
+    void ***pl, **l;
+    size_t i;
+
+    pl = p;
+    *pl = xrealloc(*pl, (*len + 1)*sizeof(void*));
+    l = *pl;
+    for (i = idx; i < *len; i++)
+        l[i + 1] = l[i];
+    l[idx] = v;
+    (*len)++;
+}
+
 void ldel(void *l, size_t *len, size_t idx)
 {
     void ***pl;