shithub: mc

Download patch

ref: 5d325b59af353ea90e4a6f974c50944e822148d5
parent: a9bfe8ce61dee5b1b8b273173dcaba6afee99bb9
author: Ori Bernstein <[email protected]>
date: Mon Jul 9 04:31:46 EDT 2012

Make generating global uninitialized blobs work.

--- a/8/isel.c
+++ b/8/isel.c
@@ -771,17 +771,22 @@
 
 void genblob(FILE *fd, Node *blob, Htab *globls)
 {
+    size_t i;
     char *lbl;
 
     /* lits and such also get wrapped in decls */
     assert(blob->type == Ndecl);
-    assert(blob->decl.init != NULL);
 
     lbl = htget(globls, blob);
     fprintf(fd, "%s:\n", lbl);
-    if (exprop(blob->decl.init) != Olit)
-        die("Nonliteral initializer for global");
-    writelit(fd, blob->decl.init->expr.args[0]);
+    if (blob->decl.init) {
+        if (exprop(blob->decl.init) != Olit)
+            die("Nonliteral initializer for global");
+        writelit(fd, blob->decl.init->expr.args[0]);
+    } else {
+        for (i = 0; i < size(blob); i++)
+            fprintf(fd, "\t.byte 0\n");
+    }
 }
 
 /* genasm requires all nodes in 'nl' to map cleanly to operations that are
--- a/8/simp.c
+++ b/8/simp.c
@@ -1090,6 +1090,9 @@
     } else {
         if (dcl->decl.init && exprop(dcl->decl.init) == Olit)
             lappend(&s.blobs, &s.nblobs, dcl);
+        /* uninitialized global vars get zero-initialized decls */
+        else if (!dcl->decl.isconst && !dcl->decl.init)
+            lappend(&s.blobs, &s.nblobs, dcl);
         else
             die("We don't lower globls with nonlit inits yet...");
     }
--- a/test/tests
+++ b/test/tests
@@ -24,7 +24,7 @@
 B structasn	E	42
 B structret	E	42
 B array		E	7
-B global-arrayvar	E	8
+B global-arrayvar	E	7
 B arraylen	E	12
 B slice		E	7
 B slicelen	E	5