ref: 6e422cdf9d119e0fb96626ef7f7e39611d6655b4
parent: acfa5cdb41d731e93faa295039dfaff5379b1c44
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Jun 5 19:28:50 EDT 2012
Remove useless code.
--- a/8/Makefile
+++ b/8/Makefile
@@ -1,7 +1,6 @@
BIN=8m
OBJ=isel.o \
main.o \
- simp.o \
reduce.o \
CFLAGS+=-I../parse
--- a/8/asm.h
+++ b/8/asm.h
@@ -13,7 +13,6 @@
Nreg
} Reg;
-
typedef enum {
Locnone,
Loclbl,
@@ -34,7 +33,15 @@
typedef struct Insn Insn;
typedef struct Loc Loc;
+typedef struct Func Func;
+typedef struct Blob Blob;
+struct Blob {
+ char *name; /* mangled asm name */
+ void *data;
+ size_t ndata;
+};
+
struct Loc {
LocType type;
Mode mode;
@@ -59,10 +66,22 @@
int narg;
};
-void genasm(Fn *fn, Htab *globls);
+struct Func {
+ char *name;
+ int isglobl;
+ size_t stksz;
+ Htab *locs;
+ Node *ret;
+ Node **nl;
+ size_t nn;
+};
+void genasm(Func *fn, Htab *globls);
+void gen(Node *file, char *out);
+
Loc *loclbl(Loc *l, Node *lbl);
Loc *locreg(Loc *l, Reg r);
Loc *locmem(Loc *l, long disp, Reg base, Reg idx, Mode mode);
Loc *locmeml(Loc *l, char *disp, Reg base, Reg idx, Mode mode);
Loc *loclit(Loc *l, long val);
+
--- a/8/gen.h
+++ /dev/null
@@ -1,71 +1,0 @@
-typedef struct Comp Comp;
-typedef struct Blob Blob;
-typedef struct Fn Fn;
-typedef struct Bb Bb;
-
-struct Comp {
- /* we generate blobs and functions */
- Blob **globl;
- size_t nglobl;
- Fn **func;
- size_t nfunc;
-};
-
-struct Blob {
- char *name; /* mangled asm name */
- void *data;
- size_t ndata;
-};
-
-struct Fn {
- char *name; /* assembly name; mangled */
- int isglobl;
-
- /* filled in by the lowering process */
- size_t stksz;
- size_t argsz;
- Htab *locs;
- Node *ret;
-
- Htab *bbnames; /* char* => Bb* map */
- Bb *start;
- Bb *end;
- Bb *cur;
- Bb **bb;
- size_t nbb;
- /* we can't know all the edges as we
- * construct the bb list, so we fix up later */
- Bb **fixup;
- size_t nfixup;
-
- /* FIXME: do we want the node list raw here? For now, it's here. */
- Node **nl;
- size_t nn;
-
-};
-
-struct Bb {
- int id;
-
- /* nodes in bb */
- Node **n;
- size_t nn;
-
- /* edges */
- Bb **in;
- size_t nin;
- Bb **out;
- size_t nout;
-};
-
-/* toplevel code gen */
-Comp *mkcomp(Node *f);
-void gen(Node *file, char *out);
-void assemble(char *asmfile, char *out);
-void compdump(Comp *c, FILE *fd);
-
-/* cfg */
-Fn *mkfn(char *name);
-Bb *mkbb(void);
-void edge(Bb *from, Bb *to);
-Node **reduce(Fn *fn, Node *n, int *ret_nn);
--- a/8/isel.c
+++ b/8/isel.c
@@ -11,7 +11,6 @@
#include <unistd.h>
#include "parse.h"
-#include "gen.h"
#include "asm.h"
/* instruction selection state */
@@ -662,7 +661,7 @@
g(s, Iret, NULL);
}
-static void writeasm(Fn *fn, Isel *is, FILE *fd)
+static void writeasm(Func *fn, Isel *is, FILE *fd)
{
int i;
@@ -676,7 +675,7 @@
/* 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(Fn *fn, Htab *globls)
+void genasm(Func *fn, Htab *globls)
{
struct Isel is = {0,};
int i;
--- a/8/main.c
+++ b/8/main.c
@@ -10,7 +10,7 @@
#include <unistd.h>
#include "parse.h"
-#include "gen.h"
+#include "asm.h"
Node *file;
static char *outfile;
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -10,9 +10,10 @@
#include <unistd.h>
#include "parse.h"
-#include "gen.h"
#include "asm.h"
+#include "platform.h" /* HACK. We need some platform specific code gen behavior. *sigh.* */
+
void breakhere()
{
volatile int x = 0;
@@ -27,11 +28,10 @@
*/
typedef struct Simp Simp;
struct Simp {
- Node **blk;
- size_t nblk;
+ int isglobl;
- /* the function that we're reducing the body for */
- Fn *fn;
+ Node **stmts;
+ size_t nstmts;
/* return handling */
Node *endlbl;
@@ -40,6 +40,12 @@
/* pre/postinc handling */
Node **incqueue;
size_t nqueue;
+
+ /* location handling */
+ size_t stksz;
+ size_t argsz;
+ Htab *locs;
+ Node *ret;
};
Node *simp(Simp *s, Node *n);
@@ -49,7 +55,7 @@
void append(Simp *s, Node *n)
{
- lappend(&s->blk, &s->nblk, n);
+ lappend(&s->stmts, &s->nstmts, n);
}
int isimpure(Node *n)
@@ -57,6 +63,33 @@
return 0;
}
+int isconstfn(Sym *s)
+{
+ return s->isconst && s->type->type == Tyfunc;
+}
+
+static char *asmname(Node *n)
+{
+ int i;
+ char *s;
+ char *sep;
+ int len;
+
+ len = strlen(Fprefix);
+ for (i = 0; i < n->name.nparts; i++)
+ len += strlen(n->name.parts[i]) + 1;
+
+ s = xalloc(len);
+ s[0] = '\0';
+ sep = Fprefix;
+ for (i = 0; i < n->name.nparts; i++) {
+ sprintf(s, "%s%s", sep, n->name.parts[i]);
+ sep = "$";
+ }
+ return s;
+}
+
+
size_t size(Node *n)
{
Type *t;
@@ -377,10 +410,10 @@
break;
case Oret:
if (n->expr.args[0]) {
- if (s->fn->ret)
- t = s->fn->ret;
+ if (s->ret)
+ t = s->ret;
else
- t = s->fn->ret = temp(s, args[0]);
+ t = s->ret = temp(s, args[0]);
t = store(t, rval(s, args[0]));
append(s, t);
}
@@ -408,26 +441,20 @@
void declarelocal(Simp *s, Node *n)
{
- Fn *f;
-
assert(n->type == Ndecl);
- f = s->fn;
if (debug)
- printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, f->stksz);
- htput(f->locs, (void*)n->decl.sym->id, (void*)f->stksz);
- f->stksz += size(n);
+ printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, s->stksz);
+ htput(s->locs, (void*)n->decl.sym->id, (void*)s->stksz);
+ s->stksz += size(n);
}
void declarearg(Simp *s, Node *n)
{
- Fn *f;
-
assert(n->type == Ndecl);
- f = s->fn;
if (debug)
- printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, -f->argsz);
- htput(f->locs, (void*)n->decl.sym->id, (void*)-f->argsz);
- f->argsz += size(n);
+ printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, -s->argsz);
+ htput(s->locs, (void*)n->decl.sym->id, (void*)-s->argsz);
+ s->argsz += size(n);
}
Node *simp(Simp *s, Node *n)
@@ -473,27 +500,104 @@
return r;
}
-Node **reduce(Fn *fn, Node *f, int *ret_nn)
+void reduce(Simp *s, Node *f)
{
- Simp s = {0,};
int i;
- s.nblk = 0;
- s.endlbl = genlbl();
- s.retval = NULL;
- s.fn = fn;
+ assert(f->type == Nfunc);
+ s->nstmts = 0;
+ s->stmts = NULL;
+ s->endlbl = genlbl();
+ s->retval = NULL;
if (f->type == Nfunc) {
for (i = 0; i < f->func.nargs; i++) {
- declarearg(&s, f->func.args[i]);
+ declarearg(s, f->func.args[i]);
}
- simp(&s, f->func.body);
+ simp(s, f->func.body);
} else {
- die("Got a non-block (%s) to reduce", nodestr(f->type));
+ die("Got a non-func (%s) to reduce", nodestr(f->type));
}
- append(&s, s.endlbl);
+ append(s, s->endlbl);
+}
- *ret_nn = s.nblk;
- return s.blk;
+static void lowerfn(char *name, Node *n, Htab *globls)
+{
+ int i;
+ Simp s = {0,};
+ Func fn;
+
+ /* set up the simp context */
+ s.locs = mkht(ptrhash, ptreq);
+
+ /* unwrap to the function body */
+ n = n->expr.args[0];
+ n = n->lit.fnval;
+ reduce(&s, n);
+
+ if (debug)
+ for (i = 0; i < s.nstmts; i++)
+ dump(s.stmts[i], stdout);
+
+ fn.name = name;
+ fn.isglobl = 1; /* FIXME: we should actually use the visibility of the sym... */
+ fn.stksz = s.stksz;
+ fn.locs = s.locs;
+ fn.ret = s.ret;
+ fn.nl = s.stmts;
+ fn.nn = s.nstmts;
+ genasm(&fn, globls);
+}
+
+void blobdump(Blob *b, FILE *fd)
+{
+ int i;
+ char *p;
+
+ p = b->data;
+ for (i = 0; i < b->ndata; i++)
+ if (isprint(p[i]))
+ fprintf(fd, "%c", p[i]);
+ else
+ fprintf(fd, "\\%x", p[i]);
+ fprintf(fd, "\n");
+}
+
+void gen(Node *file, char *out)
+{
+ Node **n;
+ int nn, i;
+ Sym *s;
+ char *name;
+ Htab *globls;
+
+ n = file->file.stmts;
+ nn = file->file.nstmts;
+
+ globls = mkht(ptrhash, ptreq);
+ /* We need to declare all variables before use */
+ for (i = 0; i < nn; i++)
+ if (n[i]->type == Ndecl)
+ htput(globls, (void*)n[i]->decl.sym->id, asmname(n[i]->decl.sym->name));
+
+ for (i = 0; i < nn; i++) {
+ switch (n[i]->type) {
+ case Nuse: /* nothing to do */
+ break;
+ case Ndecl:
+ s = n[i]->decl.sym;
+ name = asmname(s->name);
+ if (isconstfn(s)) {
+ lowerfn(name, n[i]->decl.init, globls);
+ free(name);
+ } else {
+ die("We don't lower globls yet...");
+ }
+ break;
+ default:
+ die("Bad node %s in toplevel", nodestr(n[i]->type));
+ break;
+ }
+ }
}
--- a/8/simp.c
+++ /dev/null
@@ -1,157 +1,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.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 "gen.h"
-#include "asm.h"
-
-#include "platform.h" /* HACK. We need some platform specific code gen behavior. *sigh.* */
-
-static void lowerglobl(Comp *c, char *name, Node *init);
-static void lowerfn(Comp *c, char *name, Node *n, Htab *globls);
-
-static ulong ptrhash(void *key)
-{
- return (ulong)key;
-}
-
-static int ptreq(void *a, void *b)
-{
- return a == b;
-}
-
-Fn *mkfn(char *name)
-{
- Fn *f;
-
- f = zalloc(sizeof(Fn));
- f->name = strdup(name);
- f->isglobl = 1;
- f->locs = mkht(ptrhash, ptreq);
-
- return f;
-}
-
-static char *asmname(Node *n)
-{
- int i;
- char *s;
- char *sep;
- int len;
-
- len = strlen(Fprefix);
- for (i = 0; i < n->name.nparts; i++)
- len += strlen(n->name.parts[i]) + 1;
-
- s = xalloc(len);
- s[0] = '\0';
- sep = Fprefix;
- for (i = 0; i < n->name.nparts; i++) {
- sprintf(s, "%s%s", sep, n->name.parts[i]);
- sep = "$";
- }
- return s;
-}
-
-static void lowerglobl(Comp *c, char *name, Node *init)
-{
- printf("gen globl %s\n", name);
-}
-
-static void lowerfn(Comp *c, char *name, Node *n, Htab *globls)
-{
- Fn *fn;
- Node **nl;
- int nn;
- int i;
-
- /* unwrap to the function body */
- n = n->expr.args[0];
- n = n->lit.fnval;
- assert(n->type == Nfunc);
-
- /* lower */
- fn = mkfn(name);
- fn->name = strdup(name);
-
- nl = reduce(fn, n, &nn);
-
- if (debug) {
- for (i = 0; i < nn; i++) {
- dump(nl[i], stdout);
- }
- }
-
- fn->nl = nl;
- fn->nn = nn;
- genasm(fn, globls);
-}
-
-int isconstfn(Sym *s)
-{
- return s->isconst && s->type->type == Tyfunc;
-}
-
-void blobdump(Blob *b, FILE *fd)
-{
- int i;
- char *p;
-
- p = b->data;
- fprintf(fd, "\t%s => ", b->name);
- for (i = 0; i < b->ndata; i++)
- if (isprint(p[i]))
- fprintf(fd, "%c", p[i]);
- else
- fprintf(fd, "\\%x", p[i]);
- fprintf(fd, "\n");
-}
-
-void gen(Node *file, char *out)
-{
- Node **n;
- int nn, i;
- Sym *s;
- char *name;
- Comp *c;
- Htab *globls;
-
- c = zalloc(sizeof(Comp));
-
- n = file->file.stmts;
- nn = file->file.nstmts;
-
- globls = mkht(ptrhash, ptreq);
- /* We need to declare all variables before use */
- for (i = 0; i < nn; i++)
- if (n[i]->type == Ndecl)
- htput(globls, (void*)n[i]->decl.sym->id, asmname(n[i]->decl.sym->name));
-
- for (i = 0; i < nn; i++) {
- switch (n[i]->type) {
- case Nuse: /* nothing to do */
- break;
- case Ndecl:
- s = n[i]->decl.sym;
- name = asmname(s->name);
- if (isconstfn(s)) {
- lowerfn(c, name, n[i]->decl.init, globls);
- free(name);
- } else {
- lowerglobl(c, name, n[i]);
- }
- break;
- default:
- die("Bad node %s in toplevel", nodestr(n[i]->type));
- break;
- }
- }
-}
--- a/parse/htab.c
+++ b/parse/htab.c
@@ -161,3 +161,14 @@
{
return !strcmp(a, b);
}
+
+ulong ptrhash(void *key)
+{
+ return (ulong)key;
+}
+
+int ptreq(void *a, void *b)
+{
+ return a == b;
+}
+
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -240,6 +240,8 @@
/* useful key types */
ulong strhash(void *str);
int streq(void *s1, void *s2);
+ulong ptrhash(void *ptr);
+int ptreq(void *s1, void *s2);
/* util functions */
void *zalloc(size_t size);