shithub: mc

Download patch

ref: 513bacd4d7929deaef69c08b79e4d724c9506136
parent: ce384085a46a728d096389e3df59d1ca25d51aa2
author: Ori Bernstein <[email protected]>
date: Tue Jun 5 17:41:36 EDT 2012

Don't die when trying to look up global vars

    We now do a pre-scan of all globals so we can get their decls
    set up properly.

--- a/8/asm.h
+++ b/8/asm.h
@@ -59,7 +59,7 @@
     int narg;
 };
 
-void genasm(Fn *fn);
+void genasm(Fn *fn, Htab *globls);
 
 Loc *loclbl(Loc *l, Node *lbl);
 Loc *locreg(Loc *l, Reg r);
--- a/8/isel.c
+++ b/8/isel.c
@@ -20,7 +20,8 @@
     Insn **il;
     size_t ni;
     Node *ret;
-    Htab *locs; /* Node => int stkoff */
+    Htab *locs; /* decl id => int stkoff */
+    Htab *globls; /* decl id => char *globlname */
 
     /* 6 general purpose regs */
     int rtaken[Nreg];
@@ -146,10 +147,14 @@
 
     switch (exprop(n)) {
         case Ovar:
-            if (!hthas(s->locs, (void*)n->expr.did))
-                die("%s(%ld) not found", declname(n->expr.args[0]), n->expr.did);
-            stkoff = (size_t)htget(s->locs, (void*)n->expr.did);
-            locmem(&l, stkoff, Resp, Rnone, ModeL);
+            if (hthas(s->locs, (void*)n->expr.did)) {
+                stkoff = (size_t)htget(s->locs, (void*)n->expr.did);
+                locmem(&l, stkoff, Resp, Rnone, ModeL);
+            } else if (hthas(s->globls, (void*)n->expr.did)) {
+                loclbl(&l, htget(s->globls, (void*)n->expr.did));
+            } else {
+                die("%s (id=%ld) not found", namestr(n->expr.args[0]), n->expr.did);
+            }
             break;
         case Olit:
             v = n->expr.args[0];
@@ -671,7 +676,7 @@
 /* genasm requires all nodes in 'nl' to map cleanly to operations that are
  * natively supported, as promised in the output of reduce().  No 64-bit
  * operations on x32, no structures, and so on. */
-void genasm(Fn *fn)
+void genasm(Fn *fn, Htab *globls)
 {
     struct Isel is = {0,};
     int i;
@@ -678,6 +683,7 @@
     FILE *fd;
 
     is.locs = fn->locs;
+    is.globls = globls;
     is.ret = fn->ret;
 
     prologue(&is, fn->stksz);
--- a/8/simp.c
+++ b/8/simp.c
@@ -16,7 +16,7 @@
 #include "platform.h" /* HACK. We need some platform specific code gen behavior. *sigh.* */
 
 static void lowerglobl(Comp *c, char *name, Node *init);
-static void lowerfn(Comp *c, char *name, Node *n);
+static void lowerfn(Comp *c, char *name, Node *n, Htab *globls);
 
 static ulong ptrhash(void *key)
 {
@@ -66,7 +66,7 @@
     printf("gen globl %s\n", name);
 }
 
-static void lowerfn(Comp *c, char *name, Node *n)
+static void lowerfn(Comp *c, char *name, Node *n, Htab *globls)
 {
     Fn *fn;
     Node **nl;
@@ -92,7 +92,7 @@
 
     fn->nl = nl;
     fn->nn = nn;
-    genasm(fn);
+    genasm(fn, globls);
 }
 
 int isconstfn(Sym *s)
@@ -122,6 +122,7 @@
     Sym *s;
     char *name;
     Comp *c;
+    Htab *globls;
 
     c = zalloc(sizeof(Comp));
 
@@ -128,7 +129,17 @@
     n = file->file.stmts;
     nn = file->file.nstmts;
 
+    globls = mkht(ptrhash, ptreq);
+    /* We need to declare all variables before use */
+    printf("nn = %d\n", nn);
     for (i = 0; i < nn; i++) {
+        if (n[i]->type == Ndecl) {
+            printf("declaring %ld => %s\n", n[i]->decl.sym->id, asmname(n[i]->decl.sym->name));
+            htput(globls, (void*)n[i]->decl.sym->id, asmname(n[i]->decl.sym->name));
+        }
+    }
+
+    for (i = 0; i < nn; i++) {
         switch (n[i]->type) {
             case Nuse: /* nothing to do */ 
                 break;
@@ -136,7 +147,7 @@
                 s = n[i]->decl.sym;
                 name = asmname(s->name);
                 if (isconstfn(s)) {
-                    lowerfn(c, name, n[i]->decl.init);
+                    lowerfn(c, name, n[i]->decl.init, globls);
                     free(name);
                 } else {
                     lowerglobl(c, name, n[i]);
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -189,6 +189,8 @@
     /* a ==> b */
     a = tf(a);
     b = tf(b);
+    if (a == b)
+        return a;
     if (b->type == Tyvar) {
         t = a;
         a = b;