shithub: mc

Download patch

ref: a6555761c1462da6c9f4134489a4d9b70bd1a0ea
parent: 513bacd4d7929deaef69c08b79e4d724c9506136
author: Ori Bernstein <[email protected]>
date: Tue Jun 5 17:57:04 EDT 2012

Make function arguments available.

    We didn't have a way of getting them declared. Fix that.

--- a/8/gen.h
+++ b/8/gen.h
@@ -23,6 +23,7 @@
 
     /* filled in by the lowering process */
     size_t stksz;
+    size_t argsz;
     Htab *locs;
     Node *ret;
 
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -45,7 +45,7 @@
 Node *simp(Simp *s, Node *n);
 Node *rval(Simp *s, Node *n);
 Node *lval(Simp *s, Node *n);
-void declare(Simp *s, Node *n);
+void declarelocal(Simp *s, Node *n);
 
 void append(Simp *s, Node *n)
 {
@@ -135,7 +135,7 @@
     n = mkname(-1, buf);
     s = mksym(-1, n, e->expr.type);
     t = mkdecl(-1, s);
-    declare(simp, t);
+    declarelocal(simp, t);
     r = mkexpr(-1, Ovar, t, NULL);
     r->expr.did = s->id;
     return r;
@@ -406,7 +406,7 @@
     return r;
 }
 
-void declare(Simp *s, Node *n)
+void declarelocal(Simp *s, Node *n)
 {
     Fn *f;
 
@@ -418,6 +418,18 @@
     f->stksz += size(n);
 }
 
+void declarearg(Simp *s, Node *n)
+{
+    Fn *f;
+
+    assert(n->type == Ndecl);
+    f = s->fn;
+    if (debug)
+        printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, -f->argsz);
+    htput(f->locs, (void*)n->decl.sym->id, (void*)-f->argsz);
+    f->argsz += size(n);
+}
+
 Node *simp(Simp *s, Node *n)
 {
     Node *r;
@@ -449,7 +461,7 @@
             r = n;
             break;
         case Ndecl:
-            declare(s, n);
+            declarelocal(s, n);
             break;
         case Nlbl:
             append(s, n);
@@ -461,9 +473,10 @@
     return r;
 }
 
-Node **reduce(Fn *fn, Node *n, int *ret_nn)
+Node **reduce(Fn *fn, Node *f, int *ret_nn)
 {
     Simp s = {0,};
+    int i;
 
     s.nblk = 0;
     s.endlbl = genlbl();
@@ -470,10 +483,14 @@
     s.retval = NULL;
     s.fn = fn;
 
-    if (n->type == Nblock)
-        simp(&s, n);
-    else
-        die("Got a non-block (%s) to reduce", nodestr(n->type));
+    if (f->type == Nfunc) {
+        for (i = 0; i < f->func.nargs; i++) {
+            declarearg(&s, f->func.args[i]);
+        }
+        simp(&s, f->func.body);
+    } else {
+        die("Got a non-block (%s) to reduce", nodestr(f->type));
+    }
 
     append(&s, s.endlbl);
 
--- a/8/simp.c
+++ b/8/simp.c
@@ -82,7 +82,7 @@
     fn = mkfn(name);
     fn->name = strdup(name);
 
-    nl = reduce(fn, n->func.body, &nn);
+    nl = reduce(fn, n, &nn);
 
     if (debug) {
       for (i = 0; i < nn; i++) {