void initfile(File *f, char *name) { memset(f, 0, sizeof(*f)); f->builtins = mkstab(0); f->globls = mkstab(0); f->globls->super = file.builtins; f->ns = mkht(strhash, streq); lappend(&f->files, &f->nfiles, name); tyinit(f->builtins); }
int main(int argc, char **argv) { FILE *f; int opt; int i; while ((opt = getopt(argc, argv, "hud:I:")) != -1) { switch (opt) { case 'h': usage(argv[0]); exit(0); break; case 'u': fromuse = 1; break; case 'd': debug = 1; while (optarg && *optarg) debugopt[*optarg++ & 0x7f] = 1; break; case 'I': lappend(&incpaths, &nincpaths, optarg); break; default: usage(argv[0]); exit(0); break; } } for (i = optind; i < argc; i++) { lappend(&incpaths, &nincpaths, Instroot "/lib/myr"); file = mkfile(argv[i]); file->file.exports = mkstab(); file->file.globls = mkstab(); tyinit(file->file.globls); printf("%s:\n", argv[i]); if (fromuse) { f = fopen(argv[i], "r"); if (!f) die("Unable to open usefile %s\n", argv[i]); loaduse(f, file->file.globls); dumpsyms(file->file.globls, 1); } else { tokinit(argv[i]); yyparse(); infer(file); dumpsyms(file->file.globls, 1); } } return 0; }
Node * mkfunc(Srcloc loc, Node **args, size_t nargs, Type *ret, Node *body) { Node *n; Node *f; size_t i; Stab *st; f = mknode(loc, Nfunc); f->func.args = args; f->func.nargs = nargs; f->func.body = body; f->func.scope = mkstab(1); f->func.type = mktyfunc(loc, args, nargs, ret); f->func.env = mkenv(); bindtype(f->func.env, f->func.type); st = body->block.scope; for (i = 0; i < nargs; i++) putdcl(st, args[i]); n = mknode(loc, Nlit); n->lit.littype = Lfunc; n->lit.fnval = f; return n; }
Node * mkloopstmt(Srcloc loc, Node *init, Node *cond, Node *incr, Node *body) { Node *n; n = mknode(loc, Nloopstmt); n->loopstmt.init = init; n->loopstmt.cond = cond; n->loopstmt.step = incr; n->loopstmt.body = body; n->loopstmt.scope = mkstab(0); return n; }
static Stab *findstab(Stab *st, char *pkg) { Stab *s; if (!pkg) { if (!st->name) return st; else return NULL; } s = getns(file, pkg); if (!s) { s = mkstab(0); s->name = strdup(pkg); putns(file, s); } return s; }
/* Reads a symbol table from file. The converse * of wrstab. */ static Stab *rdstab(FILE *fd, int isfunc) { Stab *st; Type *ty; Node *nm; int n; int i; /* read dcls */ st = mkstab(isfunc); st->name = rdstr(fd); n = rdint(fd); for (i = 0; i < n; i++) putdcl(st, rdsym(fd, NULL)); /* read types */ n = rdint(fd); for (i = 0; i < n; i++) { nm = unpickle(fd); rdtype(fd, &ty); puttype(st, nm, ty); } return st; }
int main(int argc, char **argv) { char buf[1024]; Stab *globls; Optctx ctx; size_t i; outfile = NULL; optinit(&ctx, "cd:?hSo:I:9G:", argv, argc); asmsyntax = Defaultasm; while (!optdone(&ctx)) { switch (optnext(&ctx)) { case 'o': outfile = ctx.optarg; break; case 'S': writeasm = 1; break; case '?': case 'h': usage(argv[0]); exit(0); break; case 'c': extracheck = 1; break; case 'd': while (ctx.optarg && *ctx.optarg) debugopt[*ctx.optarg++ & 0x7f]++; break; case '9': asmsyntax = Plan9; break; case 'G': if (!strcmp(ctx.optarg, "e")) asmsyntax = Gnugaself; else if (!strcmp(ctx.optarg, "m")) asmsyntax = Gnugasmacho; else die("unknown gnu syntax flavor"); break; case 'I': lappend(&incpaths, &nincpaths, ctx.optarg); break; default: usage(argv[0]); exit(0); break; } } lappend(&incpaths, &nincpaths, Instroot "/lib/myr"); if (ctx.nargs == 0) { fprintf(stderr, "No input files given\n"); exit(1); } else if (ctx.nargs > 1) outfile = NULL; for (i = 0; i < ctx.nargs; i++) { globls = mkstab(0); tyinit(globls); tokinit(ctx.args[i]); file = mkfile(ctx.args[i]); file->file.globls = globls; yyparse(); /* before we do anything to the parse */ if (debugopt['T']) dump(file, stdout); infer(file); if (hasmain(file)) geninit(file); tagexports(file, 0); /* after all type inference */ if (debugopt['t']) dump(file, stdout); if (writeasm) { if (outfile != NULL) swapout(buf, sizeof buf, ".s"); else swapsuffix(buf, sizeof buf, ctx.args[i], ".myr", ".s"); } else { gentempfile(buf, sizeof buf, ctx.args[i], ".s"); } genuse(ctx.args[i]); gen(file, buf); assemble(buf, ctx.args[i]); } return 0; }