shithub: mc

Download patch

ref: bb4ff654b3f0ce2e067e707f83310af5ebc50679
parent: 0dd5d1f71505ce3d8a51e8244cf3128c54eb8293
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Sep 26 15:47:54 EDT 2015

Create a new call node instead of mutating.

    This should make indirect calls easier.

--- a/6/simp.c
+++ b/6/simp.c
@@ -1256,16 +1256,8 @@
     return r;
 }
 
-static int isvariadic(Node *fn)
+static Node *vatypeinfo(Simp *s, Node *n)
 {
-    Type *ty;
-
-    ty = exprtype(fn);
-    return tybase(ty->sub[ty->nsub - 1])->type == Tyvalist;
-}
-
-static void addvatype(Simp *s, Node *n)
-{
     Node *ti, *tp, *td, *tn;
     Type *ft, *vt, **st;
     size_t nst, i;
@@ -1304,8 +1296,8 @@
     tp = mkexpr(Zloc, Oaddr, ti, NULL);
     tp->expr.type = mktyptr(n->loc, td->decl.type);
 
-    linsert(&n->expr.args, &n->expr.nargs, ft->nsub - 1, tp);
     htput(s->globls, td, asmname(td));
+    return tp;
 }
 
 static Node *capture(Simp *s, Node *n, Node *dst)
@@ -1351,9 +1343,12 @@
 
 static Node *simpcall(Simp *s, Node *n, Node *dst)
 {
-    Node *r;
-    size_t i;
+    size_t i, nargs;
+    Node **args;
+    Node *r, *call;
+    Type *ft;
 
+    ft = tybase(exprtype(n->expr.args[0]));
     if (exprtype(n)->type == Tyvoid)
         r = NULL;
     else if (stacktype(exprtype(n)) && dst)
@@ -1361,25 +1356,33 @@
     else
         r = temp(s, n);
 
-    if (isvariadic(n->expr.args[0]))
-        addvatype(s, n);
+    args = NULL;
+    nargs = 0;
+    lappend(&args, &nargs, rval(s, n->expr.args[0], NULL));
+
     if (exprtype(n)->type != Tyvoid && stacktype(exprtype(n)))
-        linsert(&n->expr.args, &n->expr.nargs, 1, addr(s, r, exprtype(n)));
+        lappend(&args, &nargs, addr(s, r, exprtype(n)));
 
-    for (i = 0; i < n->expr.nargs; i++) {
-        n->expr.args[i] = rval(s, n->expr.args[i], NULL);
+    for (i = 1; i < n->expr.nargs; i++) {
+        if (i < ft->nsub && tybase(ft->sub[i])->type == Tyvalist)
+            lappend(&args, &nargs, vatypeinfo(s, n));
+        lappend(&args, &nargs, rval(s, n->expr.args[i], NULL));
         if (exprop(n->expr.args[i]) == Oaddr)
             if (exprop(n->expr.args[i]->expr.args[0]) == Ovar)
                 def(s, n->expr.args[i]->expr.args[0]);
     }
+    if (i < ft->nsub && tybase(ft->sub[i])->type == Tyvalist)
+        lappend(&args, &nargs, vatypeinfo(s, n));
 
     if (r)
         def(s, r);
 
+    call = mkexprl(n->loc, Ocall, args, nargs);
+    call->expr.type = exprtype(n);
     if (r && !stacktype(exprtype(n))) {
-        append(s, set(r, n));
+        append(s, set(r, call));
     } else {
-        append(s, n);
+        append(s, call);
     }
     return r;
 }