ref: 4a25c643a09bd41509f7b86c68e6164c91f5f3ee
parent: e8c2fe57e7ec1d7465eb6f00bdb7aad8f4264c29
author: Ori Bernstein <[email protected]>
date: Wed Oct 8 07:42:07 EDT 2014
Pull out actual code writing into it's own file.
--- a/6/Makefile
+++ b/6/Makefile
@@ -1,9 +1,11 @@
INSTBIN=6m
-OBJ=isel.o \
- locs.o \
- main.o \
- ra.o \
- simp.o \
+OBJ= \
+ gen.o \
+ isel.o \
+ locs.o \
+ main.o \
+ ra.o \
+ simp.o \
DEPS=../parse/libparse.a ../opt/libmi.a
--- a/6/asm.h
+++ b/6/asm.h
@@ -185,15 +185,18 @@
Bitset *initial; /* initial set of locations used by this fn */
};
+/* globals */
extern char *modenames[];
+extern Type *tyintptr;
+extern Type *tyword;
+extern Type *tyvoid;
+extern Node *abortoob;
/* options */
extern int extracheck;
-/* entry points */
-void genblob(FILE *fd, Node *blob, Htab *globls, Htab *strtab);
-void genasm(FILE *fd, Func *fn, Htab *globls, Htab *strtab);
-void genstrings(FILE *fd, Htab *strtab);
+void simpglobl(Node *dcl, Htab *globls, Func ***fn, size_t *nfn, Node ***blob, size_t *nblob);
+void selfunc(Isel *is, Func *fn, Htab *globls, Htab *strtab);
void gen(Node *file, char *out);
/* location generation */
@@ -212,6 +215,7 @@
Loc *locmemls(char *disp, Loc *base, Loc *idx, int scale, Mode mode);
Loc *loclit(long val, Mode m);
Loc *loclitl(char *lbl);
+char *asmname(Node *n);
Loc *coreg(Reg r, Mode m);
int isfloatmode(Mode m);
int isintmode(Mode m);
--- /dev/null
+++ b/6/gen.c
@@ -1,0 +1,358 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <ctype.h>
+#include <string.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "parse.h"
+#include "opt.h"
+#include "asm.h"
+#include "../config.h"
+
+static size_t writeblob(FILE *fd, Htab *globls, Htab *strtab, Node *blob);
+
+static void fillglobls(Stab *st, Htab *globls)
+{
+ void **k;
+ size_t i, nk;
+ Stab *stab;
+ Node *s;
+
+ k = htkeys(st->dcl, &nk);
+ for (i = 0; i < nk; i++) {
+ s = htget(st->dcl, k[i]);
+ htput(globls, s, asmname(s->decl.name));
+ }
+ free(k);
+
+ k = htkeys(st->ns, &nk);
+ for (i = 0; i < nk; i++) {
+ stab = htget(st->ns, k[i]);
+ fillglobls(stab, globls);
+ }
+ free(k);
+}
+
+static void initconsts(Htab *globls)
+{
+ Type *ty;
+ Node *name;
+ Node *dcl;
+
+ tyintptr = mktype(Zloc, Tyuint64);
+ tyword = mktype(Zloc, Tyuint);
+ tyvoid = mktype(Zloc, Tyvoid);
+
+ ty = mktyfunc(Zloc, NULL, 0, mktype(Zloc, Tyvoid));
+ name = mknsname(Zloc, "_rt", "abort_oob");
+ dcl = mkdecl(Zloc, name, ty);
+ dcl->decl.isconst = 1;
+ dcl->decl.isextern = 1;
+ htput(globls, dcl, asmname(dcl->decl.name));
+
+ abortoob = mkexpr(Zloc, Ovar, name, NULL);
+ abortoob->expr.type = ty;
+ abortoob->expr.did = dcl->decl.did;
+ abortoob->expr.isconst = 1;
+}
+
+static void writebytes(FILE *fd, char *p, size_t sz)
+{
+ size_t i;
+
+ for (i = 0; i < sz; i++) {
+ if (i % 60 == 0)
+ fprintf(fd, "\t.ascii \"");
+ if (p[i] == '"' || p[i] == '\\')
+ fprintf(fd, "\\");
+ if (isprint(p[i]))
+ fprintf(fd, "%c", p[i]);
+ else
+ fprintf(fd, "\\%03o", (uint8_t)p[i] & 0xff);
+ /* line wrapping for readability */
+ if (i % 60 == 59 || i == sz - 1)
+ fprintf(fd, "\"\n");
+ }
+}
+
+static size_t writelit(FILE *fd, Htab *strtab, Node *v, Type *ty)
+{
+ char buf[128];
+ char *lbl;
+ size_t sz;
+ char *intsz[] = {
+ [1] = ".byte",
+ [2] = ".short",
+ [4] = ".long",
+ [8] = ".quad"
+ };
+ union {
+ float fv;
+ double dv;
+ uint64_t qv;
+ uint32_t lv;
+ } u;
+
+ assert(v->type == Nlit);
+ sz = tysize(ty);
+ switch (v->lit.littype) {
+ case Lint: fprintf(fd, "\t%s %lld\n", intsz[sz], v->lit.intval); break;
+ case Lbool: fprintf(fd, "\t.byte %d\n", v->lit.boolval); break;
+ case Lchr: fprintf(fd, "\t.long %d\n", v->lit.chrval); break;
+ case Lflt:
+ if (tybase(v->lit.type)->type == Tyflt32) {
+ u.fv = v->lit.fltval;
+ fprintf(fd, "\t.long 0x%" PRIx32 "\n", u.lv);
+ } else if (tybase(v->lit.type)->type == Tyflt64) {
+ u.dv = v->lit.fltval;
+ fprintf(fd, "\t.quad 0x%" PRIx64 "\n", u.qv);
+ }
+ break;
+ case Lstr:
+ if (hthas(strtab, v->lit.strval)) {
+ lbl = htget(strtab, v->lit.strval);
+ } else {
+ lbl = genlblstr(buf, sizeof buf);
+ htput(strtab, v->lit.strval, strdup(lbl));
+ }
+ fprintf(fd, "\t.quad %s\n", lbl);
+ fprintf(fd, "\t.quad %zd\n", strlen(v->lit.strval));
+ break;
+ case Lfunc:
+ die("Generating this shit ain't ready yet ");
+ break;
+ case Llbl:
+ die("Can't generate literal labels, ffs. They're not data.");
+ break;
+ }
+ return sz;
+}
+
+static size_t writepad(FILE *fd, size_t sz)
+{
+ assert((ssize_t)sz >= 0);
+ if (sz > 0)
+ fprintf(fd, "\t.fill %zd,1,0\n", sz);
+ return sz;
+}
+
+static size_t getintlit(Node *n, char *failmsg)
+{
+ if (exprop(n) != Olit)
+ fatal(n, "%s", failmsg);
+ n = n->expr.args[0];
+ if (n->lit.littype != Lint)
+ fatal(n, "%s", failmsg);
+ return n->lit.intval;
+}
+
+static size_t writeslice(FILE *fd, Htab *globls, Htab *strtab, Node *n)
+{
+ Node *base, *lo, *hi;
+ ssize_t loval, hival, sz;
+ char *lbl;
+
+ base = n->expr.args[0];
+ lo = n->expr.args[1];
+ hi = n->expr.args[2];
+
+ /* by this point, all slicing operations should have had their bases
+ * pulled out, and we should have vars with their pseudo-decls in their
+ * place */
+ if (exprop(base) != Ovar || !base->expr.isconst)
+ fatal(base, "slice base is not a constant value");
+ loval = getintlit(lo, "lower bound in slice is not constant literal");
+ hival = getintlit(hi, "upper bound in slice is not constant literal");
+ sz = tysize(tybase(exprtype(base))->sub[0]);
+
+ lbl = htget(globls, base);
+ fprintf(fd, "\t.quad %s + (%zd*%zd)\n", lbl, loval, sz);
+ fprintf(fd, "\t.quad %zd\n", (hival - loval));
+ return size(n);
+}
+
+static size_t writestruct(FILE *fd, Htab *globls, Htab *strtab, Node *n)
+{
+ Type *t;
+ Node **dcl;
+ int found;
+ size_t i, j;
+ size_t sz, pad, end;
+ size_t ndcl;
+
+ sz = 0;
+ t = tybase(exprtype(n));
+ assert(t->type == Tystruct);
+ dcl = t->sdecls;
+ ndcl = t->nmemb;
+ for (i = 0; i < ndcl; i++) {
+ pad = alignto(sz, decltype(dcl[i]));
+ sz += writepad(fd, pad - sz);
+ found = 0;
+ for (j = 0; j < n->expr.nargs; j++)
+ if (!strcmp(namestr(n->expr.args[j]->expr.idx), declname(dcl[i]))) {
+ found = 1;
+ sz += writeblob(fd, globls, strtab, n->expr.args[j]);
+ }
+ if (!found)
+ sz += writepad(fd, size(dcl[i]));
+ }
+ end = alignto(sz, t);
+ sz += writepad(fd, end - sz);
+ return sz;
+}
+static size_t writeblob(FILE *fd, Htab *globls, Htab *strtab, Node *n)
+{
+ size_t i, sz;
+
+ switch(exprop(n)) {
+ case Otup:
+ case Oarr:
+ sz = 0;
+ for (i = 0; i < n->expr.nargs; i++)
+ sz += writeblob(fd, globls, strtab, n->expr.args[i]);
+ break;
+ case Ostruct:
+ sz = writestruct(fd, globls, strtab, n);
+ break;
+ case Olit:
+ sz = writelit(fd, strtab, n->expr.args[0], exprtype(n));
+ break;
+ case Oslice:
+ sz = writeslice(fd, globls, strtab, n);
+ break;
+ default:
+ dump(n, stdout);
+ die("Nonliteral initializer for global");
+ break;
+ }
+ return sz;
+}
+
+void genstrings(FILE *fd, Htab *strtab)
+{
+ void **k;
+ size_t i, nk;
+
+ k = htkeys(strtab, &nk);
+ for (i = 0; i < nk; i++) {
+ fprintf(fd, "%s:\n", (char*)htget(strtab, k[i]));
+ writebytes(fd, k[i], strlen(k[i]));
+ }
+}
+
+
+static void writeasm(FILE *fd, Isel *s, Func *fn)
+{
+ size_t i, j;
+
+ if (fn->isexport || !strcmp(fn->name, Symprefix "main"))
+ fprintf(fd, ".globl %s\n", fn->name);
+ fprintf(fd, "%s:\n", fn->name);
+ for (j = 0; j < s->cfg->nbb; j++) {
+ if (!s->bb[j])
+ continue;
+ for (i = 0; i < s->bb[j]->nlbls; i++)
+ fprintf(fd, "%s:\n", s->bb[j]->lbls[i]);
+ for (i = 0; i < s->bb[j]->ni; i++)
+ iprintf(fd, s->bb[j]->il[i]);
+ }
+}
+
+void genblob(FILE *fd, Node *blob, Htab *globls, Htab *strtab)
+{
+ char *lbl;
+
+ /* lits and such also get wrapped in decls */
+ assert(blob->type == Ndecl);
+
+ lbl = htget(globls, blob);
+ if (blob->decl.vis != Visintern)
+ fprintf(fd, ".globl %s\n", lbl);
+ fprintf(fd, "%s:\n", lbl);
+ if (blob->decl.init)
+ writeblob(fd, globls, strtab, blob->decl.init);
+ else
+ writepad(fd, size(blob));
+}
+
+/* genfunc requires all nodes in 'nl' to map cleanly to operations that are
+ * natively supported, as promised in the output of reduce(). No 64-bit
+ * operations on x32, no structures, and so on. */
+void genfunc(FILE *fd, Func *fn, Htab *globls, Htab *strtab)
+{
+ Isel is = {0,};
+
+ is.reglocs = mkht(varhash, vareq);
+ is.stkoff = fn->stkoff;
+ is.globls = globls;
+ is.ret = fn->ret;
+ is.cfg = fn->cfg;
+
+ selfunc(&is, fn, globls, strtab);
+ if (debugopt['i'])
+ writeasm(stdout, &is, fn);
+ writeasm(fd, &is, fn);
+}
+
+void gen(Node *file, char *out)
+{
+ Htab *globls, *strtab;
+ Node *n, **blob;
+ Func **fn;
+ size_t nfn, nblob;
+ size_t i;
+ FILE *fd;
+
+ /* ensure that all physical registers have a loc created before any
+ * other locs, so that locmap[Physreg] maps to the Loc for the physreg
+ * in question */
+ for (i = 0; i < Nreg; i++)
+ locphysreg(i);
+
+ fn = NULL;
+ nfn = 0;
+ blob = NULL;
+ nblob = 0;
+ globls = mkht(varhash, vareq);
+ initconsts(globls);
+
+ /* We need to define all global variables before use */
+ fillglobls(file->file.globls, globls);
+
+ pushstab(file->file.globls);
+ for (i = 0; i < file->file.nstmts; i++) {
+ n = file->file.stmts[i];
+ switch (n->type) {
+ case Nuse: /* nothing to do */
+ case Nimpl:
+ break;
+ case Ndecl:
+ simpglobl(n, globls, &fn, &nfn, &blob, &nblob);
+ break;
+ default:
+ die("Bad node %s in toplevel", nodestr(n->type));
+ break;
+ }
+ }
+ popstab();
+
+ fd = fopen(out, "w");
+ if (!fd)
+ die("Couldn't open fd %s", out);
+
+ strtab = mkht(strhash, streq);
+ fprintf(fd, ".data\n");
+ for (i = 0; i < nblob; i++)
+ genblob(fd, blob[i], globls, strtab);
+ fprintf(fd, ".text\n");
+ for (i = 0; i < nfn; i++)
+ genfunc(fd, fn[i], globls, strtab);
+ genstrings(fd, strtab);
+ fclose(fd);
+}
--- a/6/isel.c
+++ b/6/isel.c
@@ -34,7 +34,6 @@
/* forward decls */
Loc *selexpr(Isel *s, Node *n);
-static size_t writeblob(FILE *fd, Htab *globls, Htab *strtab, Node *blob);
/* used to decide which operator is appropriate
* for implementing various conditional operators */
@@ -996,23 +995,6 @@
g(s, Iret, NULL);
}
-static void writeasm(FILE *fd, Isel *s, Func *fn)
-{
- size_t i, j;
-
- if (fn->isexport || !strcmp(fn->name, Symprefix "main"))
- fprintf(fd, ".globl %s\n", fn->name);
- fprintf(fd, "%s:\n", fn->name);
- for (j = 0; j < s->cfg->nbb; j++) {
- if (!s->bb[j])
- continue;
- for (i = 0; i < s->bb[j]->nlbls; i++)
- fprintf(fd, "%s:\n", s->bb[j]->lbls[i]);
- for (i = 0; i < s->bb[j]->ni; i++)
- iprintf(fd, s->bb[j]->il[i]);
- }
-}
-
static Asmbb *mkasmbb(Bb *bb)
{
Asmbb *as;
@@ -1028,226 +1010,22 @@
return as;
}
-static void writebytes(FILE *fd, char *p, size_t sz)
+void selfunc(Isel *is, Func *fn, Htab *globls, Htab *strtab)
{
- size_t i;
-
- for (i = 0; i < sz; i++) {
- if (i % 60 == 0)
- fprintf(fd, "\t.ascii \"");
- if (p[i] == '"' || p[i] == '\\')
- fprintf(fd, "\\");
- if (isprint(p[i]))
- fprintf(fd, "%c", p[i]);
- else
- fprintf(fd, "\\%03o", (uint8_t)p[i] & 0xff);
- /* line wrapping for readability */
- if (i % 60 == 59 || i == sz - 1)
- fprintf(fd, "\"\n");
- }
-}
-
-static size_t writelit(FILE *fd, Htab *strtab, Node *v, Type *ty)
-{
- char buf[128];
- char *lbl;
- size_t sz;
- char *intsz[] = {
- [1] = ".byte",
- [2] = ".short",
- [4] = ".long",
- [8] = ".quad"
- };
- union {
- float fv;
- double dv;
- uint64_t qv;
- uint32_t lv;
- } u;
-
- assert(v->type == Nlit);
- sz = tysize(ty);
- switch (v->lit.littype) {
- case Lint: fprintf(fd, "\t%s %lld\n", intsz[sz], v->lit.intval); break;
- case Lbool: fprintf(fd, "\t.byte %d\n", v->lit.boolval); break;
- case Lchr: fprintf(fd, "\t.long %d\n", v->lit.chrval); break;
- case Lflt:
- if (tybase(v->lit.type)->type == Tyflt32) {
- u.fv = v->lit.fltval;
- fprintf(fd, "\t.long 0x%" PRIx32 "\n", u.lv);
- } else if (tybase(v->lit.type)->type == Tyflt64) {
- u.dv = v->lit.fltval;
- fprintf(fd, "\t.quad 0x%" PRIx64 "\n", u.qv);
- }
- break;
- case Lstr:
- if (hthas(strtab, v->lit.strval)) {
- lbl = htget(strtab, v->lit.strval);
- } else {
- lbl = genlblstr(buf, sizeof buf);
- htput(strtab, v->lit.strval, strdup(lbl));
- }
- fprintf(fd, "\t.quad %s\n", lbl);
- fprintf(fd, "\t.quad %zd\n", strlen(v->lit.strval));
- break;
- case Lfunc:
- die("Generating this shit ain't ready yet ");
- break;
- case Llbl:
- die("Can't generate literal labels, ffs. They're not data.");
- break;
- }
- return sz;
-}
-
-static size_t writepad(FILE *fd, size_t sz)
-{
- assert((ssize_t)sz >= 0);
- if (sz > 0)
- fprintf(fd, "\t.fill %zd,1,0\n", sz);
- return sz;
-}
-
-static size_t getintlit(Node *n, char *failmsg)
-{
- if (exprop(n) != Olit)
- fatal(n, "%s", failmsg);
- n = n->expr.args[0];
- if (n->lit.littype != Lint)
- fatal(n, "%s", failmsg);
- return n->lit.intval;
-}
-
-static size_t writeslice(FILE *fd, Htab *globls, Htab *strtab, Node *n)
-{
- Node *base, *lo, *hi;
- ssize_t loval, hival, sz;
- char *lbl;
-
- base = n->expr.args[0];
- lo = n->expr.args[1];
- hi = n->expr.args[2];
-
- /* by this point, all slicing operations should have had their bases
- * pulled out, and we should have vars with their pseudo-decls in their
- * place */
- if (exprop(base) != Ovar || !base->expr.isconst)
- fatal(base, "slice base is not a constant value");
- loval = getintlit(lo, "lower bound in slice is not constant literal");
- hival = getintlit(hi, "upper bound in slice is not constant literal");
- sz = tysize(tybase(exprtype(base))->sub[0]);
-
- lbl = htget(globls, base);
- fprintf(fd, "\t.quad %s + (%zd*%zd)\n", lbl, loval, sz);
- fprintf(fd, "\t.quad %zd\n", (hival - loval));
- return size(n);
-}
-
-static size_t writestruct(FILE *fd, Htab *globls, Htab *strtab, Node *n)
-{
- Type *t;
- Node **dcl;
- int found;
- size_t i, j;
- size_t sz, pad, end;
- size_t ndcl;
-
- sz = 0;
- t = tybase(exprtype(n));
- assert(t->type == Tystruct);
- dcl = t->sdecls;
- ndcl = t->nmemb;
- for (i = 0; i < ndcl; i++) {
- pad = alignto(sz, decltype(dcl[i]));
- sz += writepad(fd, pad - sz);
- found = 0;
- for (j = 0; j < n->expr.nargs; j++)
- if (!strcmp(namestr(n->expr.args[j]->expr.idx), declname(dcl[i]))) {
- found = 1;
- sz += writeblob(fd, globls, strtab, n->expr.args[j]);
- }
- if (!found)
- sz += writepad(fd, size(dcl[i]));
- }
- end = alignto(sz, t);
- sz += writepad(fd, end - sz);
- return sz;
-}
-
-static size_t writeblob(FILE *fd, Htab *globls, Htab *strtab, Node *n)
-{
- size_t i, sz;
-
- switch(exprop(n)) {
- case Otup:
- case Oarr:
- sz = 0;
- for (i = 0; i < n->expr.nargs; i++)
- sz += writeblob(fd, globls, strtab, n->expr.args[i]);
- break;
- case Ostruct:
- sz = writestruct(fd, globls, strtab, n);
- break;
- case Olit:
- sz = writelit(fd, strtab, n->expr.args[0], exprtype(n));
- break;
- case Oslice:
- sz = writeslice(fd, globls, strtab, n);
- break;
- default:
- dump(n, stdout);
- die("Nonliteral initializer for global");
- break;
- }
- return sz;
-}
-
-void genblob(FILE *fd, Node *blob, Htab *globls, Htab *strtab)
-{
- char *lbl;
-
- /* lits and such also get wrapped in decls */
- assert(blob->type == Ndecl);
-
- lbl = htget(globls, blob);
- if (blob->decl.vis != Visintern)
- fprintf(fd, ".globl %s\n", lbl);
- fprintf(fd, "%s:\n", lbl);
- if (blob->decl.init)
- writeblob(fd, globls, strtab, blob->decl.init);
- else
- writepad(fd, size(blob));
-}
-
-/* genasm requires all nodes in 'nl' to map cleanly to operations that are
- * natively supported, as promised in the output of reduce(). No 64-bit
- * operations on x32, no structures, and so on. */
-void genasm(FILE *fd, Func *fn, Htab *globls, Htab *strtab)
-{
- Isel is = {0,};
Node *n;
Bb *bb;
size_t i, j;
char buf[128];
- is.reglocs = mkht(varhash, vareq);
- is.stkoff = fn->stkoff;
- is.globls = globls;
- is.ret = fn->ret;
- is.cfg = fn->cfg;
- /* ensure that all physical registers have a loc created, so we
- * don't get any surprises referring to them in the allocator */
- for (i = 0; i < Nreg; i++)
- locphysreg(i);
for (i = 0; i < fn->cfg->nbb; i++)
- lappend(&is.bb, &is.nbb, mkasmbb(fn->cfg->bb[i]));
+ lappend(&is->bb, &is->nbb, mkasmbb(fn->cfg->bb[i]));
- is.curbb = is.bb[0];
- prologue(&is, fn->stksz);
+ is->curbb = is->bb[0];
+ prologue(is, fn->stksz);
for (j = 0; j < fn->cfg->nbb - 1; j++) {
- is.curbb = is.bb[j];
- if (!is.bb[j])
+ is->curbb = is->bb[j];
+ if (!is->bb[j])
continue;
bb = fn->cfg->bb[j];
for (i = 0; i < bb->nnl; i++) {
@@ -1255,27 +1033,11 @@
n = bb->nl[i];
snprintf(buf, sizeof buf, "\n\t# bb = %ld, bbidx = %ld, %s:%d",
j, i, file->file.files[n->loc.file], n->loc.line);
- g(&is, Ilbl, locstrlbl(buf), NULL);
- isel(&is, fn->cfg->bb[j]->nl[i]);
+ g(is, Ilbl, locstrlbl(buf), NULL);
+ isel(is, fn->cfg->bb[j]->nl[i]);
}
}
- is.curbb = is.bb[is.nbb - 1];
- epilogue(&is);
- regalloc(&is);
-
- if (debugopt['i'])
- writeasm(stdout, &is, fn);
- writeasm(fd, &is, fn);
-}
-
-void genstrings(FILE *fd, Htab *strtab)
-{
- void **k;
- size_t i, nk;
-
- k = htkeys(strtab, &nk);
- for (i = 0; i < nk; i++) {
- fprintf(fd, "%s:\n", (char*)htget(strtab, k[i]));
- writebytes(fd, k[i], strlen(k[i]));
- }
+ is->curbb = is->bb[is->nbb - 1];
+ epilogue(is);
+ regalloc(is);
}
--- a/6/locs.c
+++ b/6/locs.c
@@ -13,6 +13,7 @@
#include "parse.h"
#include "opt.h"
#include "asm.h"
+#include "../config.h"
Mode regmodes[] = {
#define Reg(r, name, mode) mode,
@@ -72,6 +73,33 @@
{
return m == ModeF || m == ModeD;
}
+
+/* For x86, the assembly names are generated as follows:
+ * local symbols: .name
+ * un-namespaced symbols: <symprefix>name
+ * namespaced symbols: <symprefix>namespace$name
+ */
+char *asmname(Node *n)
+{
+ char *s;
+ int len;
+
+ len = strlen(Symprefix);
+ if (n->name.ns)
+ len += strlen(n->name.ns) + 1; /* +1 for separator */
+ len += strlen(n->name.name) + 1;
+
+ s = xalloc(len + 1);
+ s[0] = '\0';
+ if (n->name.ns)
+ snprintf(s, len, "%s%s$%s", Symprefix, n->name.ns, n->name.name);
+ else if (n->name.name[0] == '.')
+ snprintf(s, len, "%s", n->name.name);
+ else
+ snprintf(s, len, "%s%s", Symprefix, n->name.name);
+ return s;
+}
+
char *genlblstr(char *buf, size_t sz)
{
static int nextlbl;
--- a/6/simp.c
+++ b/6/simp.c
@@ -52,7 +52,6 @@
Htab *stkoff;
};
-static char *asmname(Node *n);
static Node *simp(Simp *s, Node *n);
static Node *rval(Simp *s, Node *n, Node *dst);
static Node *lval(Simp *s, Node *n);
@@ -65,10 +64,10 @@
static void matchpattern(Simp *s, Node *pat, Node *val, Type *t, Node *iftrue, Node *iffalse);
/* useful constants */
-static Type *tyintptr;
-static Type *tyword;
-static Type *tyvoid;
-static Node *abortfunc;
+Type *tyintptr;
+Type *tyword;
+Type *tyvoid;
+Node *abortoob;
size_t alignto(size_t sz, Type *t)
{
@@ -286,32 +285,6 @@
return s->decl.isconst && decltype(s)->type == Tyfunc;
}
-/* For x86, the assembly names are generated as follows:
- * local symbols: .name
- * un-namespaced symbols: <symprefix>name
- * namespaced symbols: <symprefix>namespace$name
- */
-static char *asmname(Node *n)
-{
- char *s;
- int len;
-
- len = strlen(Symprefix);
- if (n->name.ns)
- len += strlen(n->name.ns) + 1; /* +1 for separator */
- len += strlen(n->name.name) + 1;
-
- s = xalloc(len + 1);
- s[0] = '\0';
- if (n->name.ns)
- snprintf(s, len, "%s%s$%s", Symprefix, n->name.ns, n->name.name);
- else if (n->name.name[0] == '.')
- snprintf(s, len, "%s", n->name.name);
- else
- snprintf(s, len, "%s%s", Symprefix, n->name.name);
- return s;
-}
-
size_t tysize(Type *t)
{
size_t sz;
@@ -879,7 +852,7 @@
cmp->expr.type = mktype(len->loc, Tybool);
ok = genlbl(len->loc);
fail = genlbl(len->loc);
- die = mkexpr(idx->loc, Ocall, abortfunc, NULL);
+ die = mkexpr(idx->loc, Ocall, abortoob, NULL);
die->expr.type = mktype(len->loc, Tyvoid);
/* insert them */
@@ -1764,28 +1737,6 @@
return fn;
}
-static void fillglobls(Stab *st, Htab *globls)
-{
- void **k;
- size_t i, nk;
- Stab *stab;
- Node *s;
-
- k = htkeys(st->dcl, &nk);
- for (i = 0; i < nk; i++) {
- s = htget(st->dcl, k[i]);
- htput(globls, s, asmname(s->decl.name));
- }
- free(k);
-
- k = htkeys(st->ns, &nk);
- for (i = 0; i < nk; i++) {
- stab = htget(st->ns, k[i]);
- fillglobls(stab, globls);
- }
- free(k);
-}
-
static void extractsub(Simp *s, Node ***blobs, size_t *nblobs, Node *e)
{
size_t i;
@@ -1836,7 +1787,7 @@
}
}
-static void simpglobl(Node *dcl, Htab *globls, Func ***fn, size_t *nfn, Node ***blob, size_t *nblob)
+void simpglobl(Node *dcl, Htab *globls, Func ***fn, size_t *nfn, Node ***blob, size_t *nblob)
{
Simp s = {0,};
char *name;
@@ -1859,78 +1810,4 @@
*blob = s.blobs;
*nblob = s.nblobs;
free(name);
-}
-
-static void initconsts(Htab *globls)
-{
- Type *ty;
- Node *name;
- Node *dcl;
-
- tyintptr = mktype(Zloc, Tyuint64);
- tyword = mktype(Zloc, Tyuint);
- tyvoid = mktype(Zloc, Tyvoid);
-
- ty = mktyfunc(Zloc, NULL, 0, mktype(Zloc, Tyvoid));
- name = mknsname(Zloc, "_rt", "abort_oob");
- dcl = mkdecl(Zloc, name, ty);
- dcl->decl.isconst = 1;
- dcl->decl.isextern = 1;
- htput(globls, dcl, asmname(dcl->decl.name));
-
- abortfunc = mkexpr(Zloc, Ovar, name, NULL);
- abortfunc->expr.type = ty;
- abortfunc->expr.did = dcl->decl.did;
- abortfunc->expr.isconst = 1;
-}
-
-void gen(Node *file, char *out)
-{
- Htab *globls, *strtab;
- Node *n, **blob;
- Func **fn;
- size_t nfn, nblob;
- size_t i;
- FILE *fd;
-
- fn = NULL;
- nfn = 0;
- blob = NULL;
- nblob = 0;
- globls = mkht(varhash, vareq);
- initconsts(globls);
-
- /* We need to define all global variables before use */
- fillglobls(file->file.globls, globls);
-
- pushstab(file->file.globls);
- for (i = 0; i < file->file.nstmts; i++) {
- n = file->file.stmts[i];
- switch (n->type) {
- case Nuse: /* nothing to do */
- case Nimpl:
- break;
- case Ndecl:
- simpglobl(n, globls, &fn, &nfn, &blob, &nblob);
- break;
- default:
- die("Bad node %s in toplevel", nodestr(n->type));
- break;
- }
- }
- popstab();
-
- fd = fopen(out, "w");
- if (!fd)
- die("Couldn't open fd %s", out);
-
- strtab = mkht(strhash, streq);
- fprintf(fd, ".data\n");
- for (i = 0; i < nblob; i++)
- genblob(fd, blob[i], globls, strtab);
- fprintf(fd, ".text\n");
- for (i = 0; i < nfn; i++)
- genasm(fd, fn[i], globls, strtab);
- genstrings(fd, strtab);
- fclose(fd);
}