shithub: mc

Download patch

ref: b61653aec1b32d98adbde2d9ff1a34ff412f8e94
parent: 67a8b4e0b7d43a09fa0f33d49f94a7cf7c9f7cbd
author: Ori Bernstein <[email protected]>
date: Sun May 13 10:32:44 EDT 2012

Make looking up builtin vars work.

--- a/8/main.c
+++ b/8/main.c
@@ -28,6 +28,7 @@
     int opt;
     int i;
     Node *rdback;
+    Stab *globls;
     FILE *tmp;
 
     while ((opt = getopt(argc, argv, "dho:")) != -1) {
@@ -47,11 +48,12 @@
     }
 
     for (i = optind; i < argc; i++) {
-        tyinit();
+        globls = mkstab();
+        tyinit(globls);
         tokinit(argv[i]);
         file = mkfile(argv[i]);
         file->file.exports = mkstab();
-        file->file.globls = mkstab();
+        file->file.globls = globls;
         yyparse();
 
         /* before we do anything to the parse */
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -60,6 +60,8 @@
 size_t size(Node *n)
 {
     Type *t;
+    size_t sz;
+    int i;
 
     if (n->type == Nexpr)
         t = n->expr.type;
@@ -66,6 +68,7 @@
     else
         t = n->decl.sym->type;
 
+    sz = 0;
     switch (t->type) {
         case Tyvoid:
             return 1;
@@ -97,6 +100,9 @@
         case Tyarray:
         case Tytuple:
         case Tystruct:
+            for (i = 0; i < t->nmemb; i++)
+                sz += size(t->sdecls[i]);
+            break;
         case Tyunion:
             die("Sizes for composite types not implemented yet");
             break;
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -25,6 +25,22 @@
     st->super = super;
 }
 
+static void tyresolve(Type *t)
+{
+    int i, nn;
+    Node **n;
+
+    nn = t->nmemb;
+    switch (t->type) {
+        case Tystruct: n = t->sdecls; break;
+        case Tyunion: n = t->udecls; break;
+        case Tyenum: n = t->edecls; break;
+        default: return;
+    }
+    for (i = 0; i < nn; i++)
+        infernode(n[i], NULL, NULL);
+}
+
 /* find the most accurate type mapping */
 static Type *tf(Type *t)
 {
@@ -37,6 +53,7 @@
             if (!(lu = gettype(curstab(), t->name)))
                 fatal(t->name->line, "Could not find type %s", t->name->name.parts[t->name->name.nparts - 1]);
             tytab[t->tid] = lu;
+            tyresolve(lu);
         }
 
         printf("%s => ", tyfmt(buf, 1024, t));
@@ -365,6 +382,19 @@
     }
 }
 
+static void inferstab(Stab *s)
+{
+    void **k;
+    int n, i;
+    Type *t;
+
+    k = htkeys(s->ty, &n);
+    for (i = 0; i < n; i++) {
+        t = tf(gettype(s, k[i]));
+        updatetype(s, k[i], t);
+    }
+}
+
 static void infernode(Node *n, Type *ret, int *sawret)
 {
     int i;
@@ -374,6 +404,7 @@
     switch (n->type) {
         case Nfile:
             pushstab(n->file.globls);
+            inferstab(n->file.globls);
             for (i = 0; i < n->file.nstmts; i++)
                 infernode(n->file.stmts[i], NULL, sawret);
             popstab();
@@ -384,6 +415,7 @@
         case Nblock:
             setsuper(n->block.scope, curstab());
             pushstab(n->block.scope);
+            inferstab(n->block.scope);
             for (i = 0; i < n->block.nstmts; i++)
                 infernode(n->block.stmts[i], ret, sawret);
             popstab();
@@ -407,6 +439,7 @@
         case Nfunc:
             setsuper(n->func.scope, curstab());
             pushstab(n->func.scope);
+            inferstab(n->block.scope);
             inferfunc(n);
             popstab();
             break;
--- a/parse/names.c
+++ b/parse/names.c
@@ -30,7 +30,7 @@
 };
 
 static char *tidtab[] =  {
-#define Ty(t) #t,
+#define Ty(t, n) n,
 #include "types.def"
 #undef Ty
 };
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -36,7 +36,7 @@
 } Littype;
 
 typedef enum {
-#define Ty(t) t,
+#define Ty(t, n) t,
 #include "types.def"
 #undef Ty
 } Ty;
@@ -98,7 +98,8 @@
     int tid;
     int line;
     Bitset *cstrs;    /* the type constraints matched on this type */
-    size_t nsub;      /* For fnsub, tusub, sdecls, udecls, edecls. */
+    size_t nsub;      /* For compound types */
+    size_t nmemb;     /* for aggregate types (struct, union, enum) */
     Type **sub;       /* sub-types; shared by all composite types */
     union {
         Node *name;    /* Tyname: unresolved name */
@@ -260,6 +261,7 @@
 
 void putns(Stab *st, Stab *scope);
 void puttype(Stab *st, Node *n, Type *ty);
+void updatetype(Stab *st, Node *n, Type *t);
 void putdcl(Stab *st, Sym *s);
 
 Stab *getns(Stab *st, Node *n);
@@ -273,7 +275,7 @@
 Sym *mksym(int line, Node *name, Type *ty);
 
 /* type creation */
-void tyinit(void); /* sets up built in types */
+void tyinit(Stab *st); /* sets up built in types */
 
 Type *mkty(int line, Ty ty);
 Type *mktyvar(int line);
--- a/parse/pickle.c
+++ b/parse/pickle.c
@@ -244,15 +244,18 @@
             wrstr(fd, ty->pname);
             break;
         case Tystruct: 
-            for (i = 0; i < ty->nsub; i++)
+            wrint(fd, ty->nmemb);
+            for (i = 0; i < ty->nmemb; i++)
                 pickle(ty->sdecls[i], fd);
             break;
         case Tyunion: 
-            for (i = 0; i < ty->nsub; i++)
+            wrint(fd, ty->nmemb);
+            for (i = 0; i < ty->nmemb; i++)
                 pickle(ty->udecls[i], fd);
             break;
         case Tyenum: 
-            for (i = 0; i < ty->nsub; i++)
+            wrint(fd, ty->nmemb);
+            for (i = 0; i < ty->nmemb; i++)
                 pickle(ty->edecls[i], fd);
             break;
         case Tyarray:
@@ -282,6 +285,8 @@
     /* tid is generated; don't write */
     /* cstrs are left out for now: FIXME */
     ty->nsub = rdint(fd);
+    if (ty->nsub > 0)
+        ty->sub = xalloc(ty->nsub * sizeof(Type*));
     switch (ty->type) {
         case Tyname:
             ty->name = unpickle(fd);
@@ -290,18 +295,21 @@
             ty->pname = rdstr(fd);
             break;
         case Tystruct: 
-            ty->sdecls = xalloc(ty->nsub * sizeof(Node*));
-            for (i = 0; i < ty->nsub; i++)
+            ty->nmemb = rdint(fd);
+            ty->sdecls = xalloc(ty->nmemb * sizeof(Node*));
+            for (i = 0; i < ty->nmemb; i++)
                 ty->sdecls[i] = unpickle(fd);
             break;
         case Tyunion: 
-            ty->udecls = xalloc(ty->nsub * sizeof(Node*));
-            for (i = 0; i < ty->nsub; i++)
+            ty->nmemb = rdint(fd);
+            ty->udecls = xalloc(ty->nmemb * sizeof(Node*));
+            for (i = 0; i < ty->nmemb; i++)
                 ty->udecls[i] = unpickle(fd);
             break;
         case Tyenum: 
-            ty->edecls = xalloc(ty->nsub * sizeof(Node*));
-            for (i = 0; i < ty->nsub; i++)
+            ty->nmemb = rdint(fd);
+            ty->edecls = xalloc(ty->nmemb * sizeof(Node*));
+            for (i = 0; i < ty->nmemb; i++)
                 ty->edecls[i] = unpickle(fd);
             break;
         case Tyarray:
@@ -309,8 +317,6 @@
             ty->asize = unpickle(fd);
             break;
         default:
-            if (ty->nsub > 0)
-                ty->sub = xalloc(ty->nsub * sizeof(Type*));
             for (i = 0; i < ty->nsub; i++)
                 ty->sub[i] = rdtype(fd);
             break;
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -131,11 +131,22 @@
     htput(st->dcl, s->name, s);
 }
 
+void updatetype(Stab *st, Node *n, Type *t)
+{
+    Tydefn *td;
+
+    td = htget(st->ty, n);
+    if (!td)
+        die("No type %s to update", name(n));
+    td->type = t;
+}
+
 void puttype(Stab *st, Node *n, Type *t)
 {
     Type *ty;
     Tydefn *td;
 
+    assert(t != NULL);
     ty = gettype(st, n);
     if (ty)
         fatal(n->line, "Type %s already defined", name(n));
--- a/parse/type.c
+++ b/parse/type.c
@@ -172,7 +172,7 @@
     Type *t;
 
     t = mkty(line, Tystruct);
-    t->nsub = ndecls;
+    t->nmemb = ndecls;
     t->sdecls = memdup(decls, ndecls*sizeof(Node *));
     return t;
 }
@@ -182,6 +182,7 @@
     Type *t;
 
     t = mkty(line, Tyunion);
+    t->nmemb = ndecls;
     t->udecls = decls;
     return t;
 }
@@ -191,6 +192,7 @@
     Type *t;
 
     t = mkty(line, Tyenum);
+    t->nmemb = ndecls;
     t->edecls = decls;
     return t;
 }
@@ -267,7 +269,7 @@
     p = buf;
     end = p + len;
     p += snprintf(p, end - p, "struct ");
-    for (i = 0; i < t->nsub; i++) {
+    for (i = 0; i < t->nmemb; i++) {
         name = declname(t->edecls[i]);
         ty = tystr(decltype(t->edecls[i]));
         p += snprintf(p, end - p, "%s:%s; ", name, ty);
@@ -281,7 +283,7 @@
 {
     int i;
     *buf = 0;
-    for (i = 0; i < t->nsub; i++)
+    for (i = 0; i < t->nmemb; i++)
         dump(t->sdecls[i], stdout);
     return 0;
 }
@@ -290,7 +292,7 @@
 {
     int i;
     *buf = 0;
-    for (i = 0; i < t->nsub; i++)
+    for (i = 0; i < t->nmemb; i++)
         dump(t->sdecls[i], stdout);
     return 0;
 }
@@ -401,9 +403,10 @@
     return strdup(buf);
 }
 
-void tyinit(void)
+void tyinit(Stab *st)
 {
     int i;
+    Type *ty;
 
 #define Tc(c, n) \
     mkcstr(-1, n, NULL, 0, NULL, 0);
@@ -410,8 +413,12 @@
 #include "cstr.def"
 #undef Tc
 
-#define Ty(t) \
-    mkty(-1, t);
+/* define and register the type */
+#define Ty(t, n) {\
+    ty = mkty(-1, t); \
+    if (n) { \
+        puttype(st, mkname(-1, n), ty); \
+    }}
 #include "types.def"
 #undef Ty
 
--- a/parse/types.def
+++ b/parse/types.def
@@ -1,42 +1,42 @@
-Ty(Tybad)
-Ty(Tyvoid)
+Ty(Tybad, NULL)
+Ty(Tyvoid, "void")
 
 /* start integer types.
  * Keep them ordered between start
  * and end for faster
  * comparisons.*/
-Ty(Tybool)
-Ty(Tychar)
+Ty(Tybool, "bool")
+Ty(Tychar, "char")
 
-Ty(Tyint8)
-Ty(Tyint16)
-Ty(Tyint)
-Ty(Tyint32)
-Ty(Tyint64)
-Ty(Tylong)
+Ty(Tyint8, "int8")
+Ty(Tyint16, "int16")
+Ty(Tyint, "int")
+Ty(Tyint32, "int32")
+Ty(Tyint64, "int64")
+Ty(Tylong, "long")
 
-Ty(Tybyte)
-Ty(Tyuint8)
-Ty(Tyuint16)
-Ty(Tyuint)
-Ty(Tyuint32)
-Ty(Tyuint64)
-Ty(Tyulong)
+Ty(Tybyte, "byte")
+Ty(Tyuint8, "uint8")
+Ty(Tyuint16, "uint16")
+Ty(Tyuint, "uint")
+Ty(Tyuint32, "uint32")
+Ty(Tyuint64, "uint64")
+Ty(Tyulong, "ulong")
 /*end integer types*/
-Ty(Tyfloat32)
-Ty(Tyfloat64)
-Ty(Tyvalist)
+Ty(Tyfloat32, "float32")
+Ty(Tyfloat64, "float64")
+Ty(Tyvalist, NULL)
 
 /*end atomic types*/
-Ty(Typtr)
-Ty(Tyslice)
-Ty(Tyarray)
-Ty(Tyfunc)
-Ty(Tytuple)
-Ty(Tyvar)
-Ty(Typaram)
-Ty(Tyname)
-Ty(Tystruct)
-Ty(Tyunion)
-Ty(Tyenum)
-Ty(Ntypes)
+Ty(Typtr, NULL)
+Ty(Tyslice, NULL)
+Ty(Tyarray, NULL)
+Ty(Tyfunc, NULL)
+Ty(Tytuple, NULL)
+Ty(Tyvar, NULL)
+Ty(Typaram, NULL)
+Ty(Tyname, NULL)
+Ty(Tystruct, NULL)
+Ty(Tyunion, NULL)
+Ty(Tyenum, NULL)
+Ty(Ntypes, NULL)