shithub: mc

Download patch

ref: f4a4987fb85dc9ea146146068734988bb046a3ab
parent: e1bd55e7d0fd87b66041c1f90cc4cba370cb25ed
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Sep 26 09:21:11 EDT 2015

Fix up constant initialization a bit.

--- a/6/asm.h
+++ b/6/asm.h
@@ -279,6 +279,7 @@
 Loc *loclit(long val, Mode m);
 Loc *loclitl(char *lbl);
 char *asmname(Node *dcl);
+int isconstfn(Node *n);
 char *tydescid(char *buf, size_t bufsz, Type *ty);
 Loc *coreg(Reg r, Mode m);
 int isfloatmode(Mode m);
--- a/6/gen.c
+++ b/6/gen.c
@@ -67,6 +67,22 @@
     return buf;
 }
 
+int isconstfn(Node *n)
+{
+    Node *d;
+
+    if (n->type == Nexpr) {
+        if (exprop(n) != Ovar)
+            return 0;
+        d = decls[n->expr.did];
+    } else {
+        d = n;
+    }
+    if (d && d->decl.isconst && d->decl.isglobl)
+        return tybase(decltype(d))->type == Tyfunc;
+    return 0;
+}
+
 /* 
  * For x86, the assembly names are generated as follows:
  *      local symbols: .name
--- a/6/isel.c
+++ b/6/isel.c
@@ -478,26 +478,12 @@
     }
 }
 
-static int isconstfunc(Isel *s, Node *n)
-{
-    Node *d;
-
-    if (exprop(n) != Ovar)
-        return 0;
-    if (!hthas(s->globls, n))
-        return 0;
-    d = decls[n->expr.did];
-    if (d && d->decl.isconst)
-        return tybase(decltype(d))->type == Tyfunc;
-    return 0;
-}
-
 static void call(Isel *s, Node *n)
 {
     AsmOp op;
     Loc *f;
 
-    if (isconstfunc(s, n)) {
+    if (isconstfn(n)) {
         op = Icall;
         f = locmeml(htget(s->globls, n), NULL, NULL, mode(n));
     } else {
@@ -810,7 +796,7 @@
             r = loc(s, n);
             break;
         case Ovar:
-            if (isconstfunc(s, n)) {
+            if (isconstfn(n)) {
                 r = locreg(ModeQ);
                 a = loc(s, n);
                 g(s, Ilea, a, r, NULL);
--- a/6/simp.c
+++ b/6/simp.c
@@ -85,11 +85,6 @@
     return opispure[exprop(n)];
 }
 
-static int isconstfn(Node *s)
-{
-    return s->decl.isconst && decltype(s)->type == Tyfunc;
-}
-
 size_t alignto(size_t sz, Type *t)
 {
     size_t a;
@@ -709,6 +704,7 @@
     d->decl.init = blob;
     d->decl.type = blob->expr.type;
     d->decl.isconst = 1;
+    d->decl.isglobl = 1;
     htput(s->globls, d, asmname(d));
 
     r->expr.did = d->decl.did;
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -501,10 +501,15 @@
     }
     if (file->file.localinit)
         callinit(block, file->file.localinit, tyvoid, tyvoidfn);
+
     func = mkfunc(Zloc, NULL, 0, mktype(Zloc, Tyvoid), block);
     func->expr.type = tyvoidfn;
-    decl->decl.init = mkexpr(Zloc, Olit, func, NULL);
+    init = mkexpr(Zloc, Olit, func, NULL);
+    init->expr.type = tyvoidfn;
+
+    decl->decl.init = init;
     decl->decl.isconst = 1;
+    decl->decl.isglobl = 1;
     decl->decl.type = tyvoidfn;
     decl->decl.vis = Vishidden;