shithub: mc

Download patch

ref: 0766e6b1624a4074a1564d274c2fd029b799901c
parent: c056146c07a6521ddbf351f47539058af2436c36
author: Ori Bernstein <[email protected]>
date: Mon Jun 18 21:51:37 EDT 2012

Work towards making a "real" hello world

    Proper syscalls, etc.

--- a/8/reduce.c
+++ b/8/reduce.c
@@ -777,6 +777,8 @@
     if(debug)
         printf("\n\nfunction %s\n", name);
 
+    if (!n->decl.init)
+        return NULL;
     /* set up the simp context */
     /* unwrap to the function body */
     n = n->expr.args[0];
@@ -849,8 +851,10 @@
     s.nblobs = *nblob;
 
     if (isconstfn(dcl)) {
-        f = lowerfn(&s, name, dcl->decl.init);
-        lappend(fn, nfn, f);
+        if (!dcl->decl.isextern) {
+            f = lowerfn(&s, name, dcl->decl.init);
+            lappend(fn, nfn, f);
+        }
     } else {
         if (dcl->decl.init && exprop(dcl->decl.init) == Olit)
             lappend(blob, nblob, dcl);
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -18,7 +18,7 @@
 int yylex(void);
 static Op binop(int toktype);
 Stab *curscope;
-//int n = 0;
+
 %}
 
 %token<tok> Terror
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -455,6 +455,9 @@
     if (n->decl.init) {
         inferexpr(n->decl.init, NULL, NULL);
         unify(n, type(n), type(n->decl.init));
+    } else {
+        if (n->decl.isconst && !n->decl.isextern)
+            fatal(n->line, "non-extern \"%s\" has no initializer", ctxstr(n));
     }
 }
 
@@ -474,9 +477,7 @@
 
 static void infernode(Node *n, Type *ret, int *sawret)
 {
-    void **k;
-    size_t i, nk;
-    Type *ty;
+    size_t i;
     Node *d;
     Node *s;
 
@@ -486,15 +487,7 @@
         case Nfile:
             pushstab(n->file.globls);
             /* exports allow us to specify types later in the body, so we
-             * need to patch the types in. */
-            k = htkeys(file->file.exports->ty, &nk);
-            for (i = 0; i < nk; i++) {
-                ty = gettype(file->file.globls, k[i]);
-                if (!ty) 
-                    fatal(((Node*)k[i])->line, "Exported type %s not declared", namestr(k[i]));
-                updatetype(file->file.exports, k[i], tf(ty));
-            }
-            free(k);
+             * need to patch the types in if they don't have a definition */
             inferstab(n->file.globls);
             inferstab(n->file.exports);
 	    for (i = 0; i < n->file.nstmts; i++) {
@@ -719,11 +712,55 @@
     }
 }
 
+void mergeexports(Node *file)
+{
+    Stab *exports, *globls;
+    size_t i, nk;
+    void **k;
+    Node *n;
+    Type *ty, *t;
+
+    exports = file->file.exports;
+    globls = file->file.globls;
+
+    pushstab(globls);
+    k = htkeys(exports->ty, &nk);
+    for (i = 0; i < nk; i++) {
+        ty = gettype(exports, k[i]);
+        n = k[i];
+        if (ty) {
+            if (!gettype(globls, n))
+                puttype(globls, n, ty);
+            else
+                unify(file, gettype(globls, n), ty);
+        } else {
+            t = gettype(globls, n);
+            if (t) 
+                updatetype(exports, n, tf(t));
+            else
+                fatal(n->line, "Exported type %s not declared", namestr(n));
+        }
+    }
+    free(k);
+
+    k = htkeys(exports->dcl, &nk);
+    for (i = 0; i < nk; i++) {
+        n = getdcl(exports, k[i]);
+        if (!getdcl(globls, k[i]))
+            putdcl(globls, n);
+        else
+            unify(n, type(getdcl(globls, k[i])), type(n));
+    }
+    free(k);
+    popstab();
+}
+
 void infer(Node *file)
 {
     assert(file->type == Nfile);
 
     loaduses(file);
+    mergeexports(file);
     infernode(file, NULL, NULL);
     infercompn(file);
     checkcast(file);
--- a/parse/type.c
+++ b/parse/type.c
@@ -162,6 +162,7 @@
     Type *t;
 
     t = mkty(line, Tystruct);
+    t->nsub = 0;
     t->nmemb = ndecls;
     t->sdecls = memdup(decls, ndecls*sizeof(Node *));
     return t;
--- a/parse/use.c
+++ b/parse/use.c
@@ -118,7 +118,7 @@
 
     k = htkeys(st->ty, &n);
     for (i = 0; i < n; i++) {
-	t = htget(st->ty, k[i]);
+	t = gettype(st, k[i]);
 	wrbyte(f, 'T');
         wrstr(f, namestr(k[i]));
 	typickle(t, f);
--- a/util/muse.c
+++ b/util/muse.c
@@ -32,7 +32,6 @@
 {
     int opt;
     int i;
-    Stab *s;
     Stab *globls;
     Node *rdback;
     FILE *tmp;
@@ -86,9 +85,6 @@
 	f = fopen(outfile, "w");
 	writeuse(file, f);
 	fclose(f);
-        s = mkstab();
-	readuse(mkuse(-1, outfile, 1), s);
-        dumpstab(s, stdout);
     }
 
     return 0;