shithub: mc

Download patch

ref: 2fecc0b35b964469479c81cab2ebf5359f2bab89
parent: a252f51ddd3d85944a26a2f84e5d885eed504b98
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Mar 19 18:59:17 EDT 2015

Generate type descriptions for various types.

    On our way to sane printf()!

--- a/6/gengas.c
+++ b/6/gengas.c
@@ -506,17 +506,17 @@
 
 static void genstructmemb(FILE *fd, Node *sdecl)
 {
-    fprintf(fd, ".ascii \"%s\" /* struct member */\n", namestr(sdecl->decl.name));
+    fprintf(fd, "\t.ascii \"%s\" /* struct member */\n", namestr(sdecl->decl.name));
     gentydesc(fd, sdecl->decl.type);
 }
 
 static void genunionmemb(FILE *fd, Ucon *ucon)
 {
-    fprintf(fd, ".ascii \"%s\" /* union constructor */\n", namestr(ucon->name));
+    fprintf(fd, "\t.ascii \"%s\" /* union constructor */\n", namestr(ucon->name));
     if (ucon->etype)
         gentydesc(fd, ucon->etype);
     else
-        fprintf(fd, ".byte %d /* no union type */\n", Tybad);
+        fprintf(fd, "\t.byte %d /* no union type */\n", Tybad);
 }
 
 /* on x86, unaligned pointers are cheap. we shouldn't be introspecting too
@@ -528,7 +528,7 @@
     Node *sz;
     size_t i;
 
-    fprintf(fd, ".byte %d /* type: %s */\n", ty->type, tidstr[ty->type]);
+    fprintf(fd, "\t.byte %d\n", ty->type);
     switch (ty->type) {
         case Ntypes: case Tyvar: case Tybad: case Typaram:
         case Tygeneric: case Tyunres:
@@ -554,15 +554,15 @@
                 assert(sz->type == Nexpr);
                 sz = sz->expr.args[0];
                 assert(sz->type == Nlit && sz->lit.littype == Lint);
-                fprintf(fd, ".quad %lld /* array size */\n", (vlong)sz->lit.intval);
+                fprintf(fd, "\t.quad %lld /* array size */\n", (vlong)sz->lit.intval);
             } else {
-                fprintf(fd, ".quad -1 /* array size */\n");
+                fprintf(fd, "\t.quad -1 /* array size */\n");
             }
 
             gentydesc(fd, ty->sub[0]);
             break;
         case Tyfunc:
-            fprintf(fd, ".byte %zd /* nargs + ret */\n", ty->nsub);
+            fprintf(fd, "\t.byte %zd /* nargs + ret */\n", ty->nsub);
             for (i = 0; i < ty->nsub; i++)
                 gentydesc(fd, ty->sub[i]);
             break;
@@ -579,7 +579,7 @@
                 genunionmemb(fd, ty->udecls[i]);
             break;
         case Tyname:
-            fprintf(fd, ".quad %s\n", tydescid(buf, sizeof buf, ty));
+            fprintf(fd, "\t.quad %s\n", tydescid(buf, sizeof buf, ty));
             break;
     }
 }
@@ -593,7 +593,7 @@
             return;
         tydescid(buf, sizeof buf, ty);
         if (ty->vis == Visexport)
-            fprintf(fd, ".globl %s\n", buf);
+            fprintf(fd, ".globl %s /* tid: %d */\n", buf, ty->tid);
         fprintf(fd, "%s:\n", buf);
         gentydesc(fd, ty->sub[0]);
     } else {
@@ -651,11 +651,18 @@
     fprintf(fd, ".data\n");
     for (i = 0; i < nblob; i++)
         genblob(fd, blob[i], globls, strtab);
+    fprintf(fd, "\n");
+
     fprintf(fd, ".text\n");
     for (i = 0; i < nfn; i++)
         genfunc(fd, fn[i], globls, strtab);
-    for (i = 0; i < file->file.ntydefs; i++)
-        gentype(fd, file->file.tydefs[i]);
+    fprintf(fd, "\n");
+
+    for (i = 0; i < ntypes; i++)
+        if (types[i]->isreflect && !types[i]->isimport)
+            gentype(fd, types[i]);
+    fprintf(fd, "\n");
+
     genstrings(fd, strtab);
     fclose(fd);
 }
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -406,7 +406,6 @@
                 $$ = $2;
                 if ($$.nparams == 0) {
                     $$.type = mktyname($2.loc, mkname($2.loc, $2.name), $4);
-                    lappend(&file->file.tydefs, &file->file.ntydefs, $$.type);
                 } else {
                     $$.type = mktygeneric($2.loc, mkname($2.loc, $2.name), $2.params, $2.nparams, $4);
                 }
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -2010,6 +2010,7 @@
                     taghidden(t->udecls[i]->etype);
             break;
         case Tyname:
+            t->isreflect = 1;
             for (i = 0; i < t->narg; i++)
                 taghidden(t->arg[i]);
         case Tygeneric:
@@ -2167,12 +2168,14 @@
         taghidden(t);
         for (j = 0; j < t->nsub; j++)
             taghidden(t->sub[j]);
-        if (t->type == Tyname)
+        if (t->type == Tyname) {
+            t->isreflect = 1;
             for (j = 0; j < t->narg; j++)
                 taghidden(t->arg[j]);
-        if (t->type == Tygeneric)
+        } else if (t->type == Tygeneric) {
             for (j = 0; j < t->ngparam; j++)
                 taghidden(t->gparam[j]);
+        }
     }
     free(k);
 
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -171,6 +171,7 @@
     char ishidden;      /* Tyname: whether this is hidden or not */
     char ispkglocal;    /* Tyname: whether this is package local or not */
     char isimport;      /* Tyname: whether tyis type was imported. */
+    char isreflect;     /* Tyname: whether this type has reflection info */
 };
 
 struct Ucon {
@@ -213,8 +214,6 @@
             Node **stmts;
             size_t nstmts;
             Stab  *globls;
-            size_t ntydefs;
-            Type **tydefs;
         } file;
 
         struct {
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -52,7 +52,6 @@
             htput(tsmap, t, ret);
             for (i = 0; i < t->ngparam; i++)
                 lappend(&ret->arg, &ret->narg, tyspecialize(t->gparam[i], tsmap, delayed));
-            lappend(&file->file.tydefs, &file->file.ntydefs, ret);
             break;
         case Tyname:
             arg = NULL;