commit 48a743e93ef92d5aba6f4291f36f14f0af50a42e
parent 38fe0156647811e12774202b493915534f36fdb6
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Wed, 2 Feb 2022 19:03:43 -0600
move emit* call to main
This is more consistent with the rest, since lexing, parsing, and
generation are also called from main.
Diffstat:
M | ir.c | | | 28 | +++++++++------------------- |
M | ir.h | | | 2 | +- |
M | main.c | | | 11 | +++++++++-- |
3 files changed, 19 insertions(+), 22 deletions(-)
diff --git a/ir.c b/ir.c
@@ -446,30 +446,23 @@ chooseregs(const struct iproc *const proc)
}
}
}
-
}
-size_t
-genproc(struct stack *blockstack, struct decl *const decl, const struct proc *const proc)
+void
+genproc(struct stack *blockstack, struct iproc *const out, const struct proc *const proc)
{
tmpi = labeli = curi = 1;
rblocki = reali = 0;
blocks = blockstack;
loops = (struct stack){ 0 };
struct type *type;
- struct iproc iproc = {
- .s = decl->s,
- .addr = decl->w.addr,
- };
-
- struct iproc *out = &iproc; // for macros to work, a bit hacky
// put a blank interval, since tmpi starts at 1
{
struct temp temp = { 0 };
- array_add((&iproc.temps), temp);
+ array_add((&out->temps), temp);
}
- array_add((&iproc.labels), labeli);
+ array_add((&out->labels), labeli);
size_t startlabel = bumplabel(out), endlabel = bumplabel(out);
{
@@ -487,8 +480,8 @@ genproc(struct stack *blockstack, struct decl *const decl, const struct proc *co
size_t what = NEWTMP;
decl->index = what;
STARTINS(IR_ASSIGN, what, VT_TEMP);
- iproc.temps.data[what].flags = TF_INT; // FIXME: move this to a separate function?
- iproc.temps.data[what].size = type->size; // FIXME: should we check that it's a power of 2?
+ out->temps.data[what].flags = TF_INT; // FIXME: move this to a separate function?
+ out->temps.data[what].size = type->size; // FIXME: should we check that it's a power of 2?
putins(out, IR_IN, i, VT_IMM);
}
@@ -498,8 +491,8 @@ genproc(struct stack *blockstack, struct decl *const decl, const struct proc *co
size_t what = NEWTMP;
decl->index = what;
STARTINS(IR_ASSIGN, what, VT_TEMP);
- iproc.temps.data[what].flags = TF_PTR; // FIXME: move this to a separate function?
- iproc.temps.data[what].size = type->size; // FIXME: should we check that it's a power of 2?
+ out->temps.data[what].flags = TF_PTR; // FIXME: move this to a separate function?
+ out->temps.data[what].size = type->size; // FIXME: should we check that it's a power of 2?
putins(out, IR_IN, i, VT_IMM);
}
@@ -511,8 +504,5 @@ genproc(struct stack *blockstack, struct decl *const decl, const struct proc *co
free(loops.data);
LABEL(endlabel);
- chooseregs(&iproc);
- array_add((&toplevel.code), iproc);
-
- return targ.emitproc(&toplevel.text, out);
+ chooseregs(out);
}
diff --git a/ir.h b/ir.h
@@ -96,4 +96,4 @@ struct toplevel {
uint64_t entry;
};
-size_t genproc(struct stack *blockstack, struct decl *const decl, const struct proc *const proc);
+void genproc(struct stack *blockstack, struct iproc *const out, const struct proc *const proc);
diff --git a/main.c b/main.c
@@ -117,14 +117,21 @@ gentoplevel(struct toplevel *toplevel, struct block *block)
decl_alloc(block, decl);
if (expr->class == C_PROC) {
+ iproc = (struct iproc){
+ .s = decl->s,
+ .addr = curaddr
+ };
+
if (slice_cmplit(&decl->s, "main") == 0) {
toplevel->entry = curaddr;
}
+
assert(expr->kind == EXPR_PROC);
stackpush(&blocks, &expr->d.proc.block);
typecheck(&blocks, &expr->d.proc.block);
- decl->w.addr = curaddr;
- curaddr += genproc(&blocks, decl, &expr->d.proc);
+ genproc(&blocks, &iproc, &expr->d.proc);
+ array_add((&toplevel->code), iproc);
+ curaddr += targ.emitproc(&toplevel->text, &iproc);
stackpop(&blocks);
} else {
if (slice_cmplit(&decl->s, "main") == 0) {