commit e3cc2767a144c69b9c41cc3ccf1511554022e910
parent 2f3f71dcc8b38f2bb6541f85c6d064c2f4c0b234
Author: Brian Swetland <swetland@frotz.net>
Date: Thu, 2 Dec 2021 14:29:20 -0800
compiler2: more progress
- tinker with stack frame layout
- fix code interacting with stack frames
- write binary file to disk
Diffstat:
2 files changed, 44 insertions(+), 17 deletions(-)
diff --git a/src/codegen-risc5-simple.c b/src/codegen-risc5-simple.c
@@ -203,10 +203,10 @@ u32 gen_expr(Ast node);
void sym_get_loc(Symbol sym, u32* base, i32* offset) {
if (sym->kind == SYM_LOCAL) {
*base = FP;
- *offset = -(8 + sym->value);
+ *offset = -(4 + sym->value);
} else if (sym->kind == SYM_PARAM) {
*base = FP;
- *offset = sym->value;
+ *offset = 8 + sym->value;
} else if (sym->kind == SYM_GLOBAL) {
*base = SB;
*offset = sym->value;
@@ -382,18 +382,18 @@ void gen_block(Ast node) {
// --------------- --------------
// arg2 oldarg2
// arg1 oldarg1
-// FP -> arg0 oldarg0 <-+
-// fpsave oldfp |
-// lrsave oldlp |
+// FP -> arg0 oldarg0
+// lrsave oldlr
+// fpsave oldfp <-+
// loc0 oldloc0 |
// loc1 oldloc1 |
// ... ... |
// locn oldlocn |
-// newarg2 arg2 |
-// newarg1 arg1 |
-// SP -> newarg0 FP -> arg0 |
-// fpsave ---+
-// lrsave
+// callarg2 arg2 |
+// callarg1 arg1 |
+// SP -> callarg0 arg0 |
+// lrsave |
+// FP -> fpsave ---+
// loc0
// loc1
// ...
@@ -401,6 +401,8 @@ void gen_block(Ast node) {
void gen_func(Ast node) {
fprintf(stderr,"gen_func()\n");
+
+ // local space plus saved lr and fp
u32 x = node->sym->type->size + 8;
node->sym->value = ctx.pc;
@@ -412,9 +414,9 @@ void gen_func(Ast node) {
// generate prologue
emit_opi(SUB, SP, SP, x);
- emit_mem(STW, FP, SP, x - 4);
- emit_mem(STW, LR, SP, x - 8);
- emit_opi(ADD, FP, SP, x);
+ emit_mem(STW, LR, SP, x - 4);
+ emit_mem(STW, FP, SP, x - 8);
+ emit_opi(ADD, FP, SP, x - 8);
// setup list of branches-to-epilogue
FixupRec list;
@@ -428,15 +430,16 @@ void gen_func(Ast node) {
fixup_branches_fwd(list.next);
// generate epilogue
- emit_mem(LDW, LR, FP, x - 8);
- emit_mem(LDW, FP, FP, x - 4);
+ emit_mem(LDW, LR, FP, 4);
+ emit_mem(LDW, FP, FP, 0);
+ emit_opi(ADD, SP, SP, x);
emit_br(AL, LR);
}
void gen_risc5_simple(Ast node) {
fprintf(stderr, "gen_risc5_simple()\n");
- emit_mov(SB, 0); // placeholder SB load
+ emit_movi(SB, 0); // placeholder SB load
emit_bi(AL, 0); // placeholder branch to init
node = node->child;
diff --git a/src/compiler2.c b/src/compiler2.c
@@ -1908,6 +1908,28 @@ void type_dump_all() {
// ================================================================
+void binary_write(const char* outname) {
+ int fd = open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
+ if (fd < 0) {
+ error("cannot open '%s' to write", outname);
+ }
+ u32 n = 0;
+ while (n < ctx.pc) {
+ if (write(fd, ctx.code + (n/4), sizeof(u32)) != sizeof(u32)) {
+ error("error writing '%s'", outname);
+ }
+ n += 4;
+ }
+ n = 0;
+ while (n < ctx.gp) {
+ if (write(fd, ctx.data + (n/4), sizeof(u32)) != sizeof(u32)) {
+ error("error writing '%s'", outname);
+ }
+ n += 4;
+ }
+ close(fd);
+}
+
void listing_write(const char* listfn, const char* srcfn) {
FILE* fin = fopen(srcfn, "r");
if (fin == NULL) {
@@ -2040,10 +2062,12 @@ i32 main(int argc, args argv) {
Ast a = parse_program();
- ast_dump(a, 0);
+ //ast_dump(a, 0);
gen_risc5_simple(a);
+ binary_write(outname);
+
if (lstname != nil) {
listing_write(lstname, ctx.filename);
}