From 5a728ae106b978c6de0fa6316f37eb3b4a86a405 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Mon, 15 Apr 2024 01:35:19 +0300 Subject: format --- src/asm.c | 25 +++++++++++++++++++------ src/correct.c | 11 +++++++++++ src/main.c | 8 ++++++++ src/nodes.c | 4 ++++ src/parser.y | 19 ++++++++++++------- 5 files changed, 54 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/asm.c b/src/asm.c index 65d0eb8..60d8139 100644 --- a/src/asm.c +++ b/src/asm.c @@ -248,11 +248,9 @@ static void output_restore(struct insn n, FILE *o, struct fn *f) static void output_store(struct insn n, FILE *o) { - struct val offset = n.out; struct val b = n.in[0]; struct val t = n.in[1]; - assert(offset.class == IMM); assert(b.class == REG); assert(t.class == REG); @@ -260,7 +258,7 @@ static void output_store(struct insn n, FILE *o) if (n.vtype == I9) width = 't'; - int64_t off = offset.v; + int64_t off = n.v; fprintf(o, "st %c %s, %lli(%s)\n", width, rname(t), (long long int)off, rname(b)); } @@ -269,9 +267,7 @@ static void output_load(struct insn n, FILE *o) { struct val t = n.out; struct val b = n.in[0]; - struct val offset = n.in[1]; - assert(offset.class == IMM); assert(b.class == REG); assert(t.class == REG); @@ -279,7 +275,7 @@ static void output_load(struct insn n, FILE *o) if (n.vtype == I9) width = 't'; - int64_t off = offset.v; + int64_t off = n.v; fprintf(o, "ld %c %s, %lli(%s)\n", width, rname(t), (long long int)off, rname(b)); } @@ -295,6 +291,21 @@ static void output_lt(struct insn i, FILE *o) rname(i.out), rname(i.in[0]), rname(i.in[1])); } +static void output_alloc(struct insn i, FILE *o) +{ + assert(i.type == ALLOC); + /** @todo alignment is ignored, is that our responsibility? */ + fprintf(o, "addi sp, sp, -%lli\n", i.v); + fprintf(o, "mov %s, sp\n", rname(i.out)); +} + +static void output_dealloc(struct insn i, FILE *o) +{ + assert(i.type == DEALLOC); + /** @todo alignment is ignored */ + fprintf(o, "addi sp, sp, %lli\n", i.v); +} + static void output_insn(struct insn n, FILE *o, struct fn *f) { /* one insn directly matches one or more assembly instructions, @@ -309,6 +320,8 @@ static void output_insn(struct insn n, FILE *o, struct fn *f) case CALL: output_call(n, o); break; case STORE: output_store(n, o); break; case LOAD: output_load(n, o); break; + case ALLOC: output_alloc(n, o); break; + case DEALLOC: output_dealloc(n, o); break; case SAVE: output_save(n, o, f); break; case RESTORE: output_restore(n, o, f); break; default: fprintf(stderr, "unimplemented insn: %s\n", op_str(n.type)); diff --git a/src/correct.c b/src/correct.c index 5db40dd..8c0b249 100644 --- a/src/correct.c +++ b/src/correct.c @@ -68,6 +68,14 @@ static size_t correct_relations(struct blk *b, size_t ii, struct insn i, return ri; } +static size_t correct_store(struct blk *b, size_t ii, struct insn i, size_t ri) +{ + if (i.in[1].class == IMM) + return spill_imm(b, ii, i, 1, ri); + + return ri; +} + static size_t correct_insn(struct blk *b, size_t ii, struct insn i, size_t ri) { /* replace references with instructions */ @@ -109,6 +117,9 @@ static size_t correct_insn(struct blk *b, size_t ii, struct insn i, size_t ri) case NE: return correct_relations(b, ii, i, ri); + case STORE: + return correct_store(b, ii, i, ri); + default: } diff --git a/src/main.c b/src/main.c index 8a59127..7ef7497 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,12 @@ int main(int argc, char *argv[]) struct parser *p = create_parser(); parse(p, fname, buf); + int ret = 0; + if (p->failed) { + ret = -1; + goto out; + } + foreach_fn(i, p->fns) { struct fn_map m = fn_at(p->fns, i); // also handles things like register mapping etc. @@ -63,6 +69,8 @@ int main(int argc, char *argv[]) output(m.fn, stdout); } +out: destroy_parser(p); free(buf); + return ret; } diff --git a/src/nodes.c b/src/nodes.c index 07a1706..88486a4 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -188,6 +188,10 @@ void dump_insn(struct insn i) dump_val(i.in[1]); } + /* crude, but works for now */ + if (i.v) + printf(" %lli ", i.v); + if (i.flags) printf("*"); diff --git a/src/parser.y b/src/parser.y index 24a2ebe..609c777 100644 --- a/src/parser.y +++ b/src/parser.y @@ -362,15 +362,13 @@ mem_off mem : type id "<<" mem_base mem_off { - struct val t = IDTOVAL($[id]); + struct val t = IDALLOC($[id]); struct val b = IDTOVAL($[mem_base]); INSADD(LOAD, $[type], t, b, noclass(), $[mem_off]); } - | id ">>" type mem_base mem_off { - struct val t = IDTOVAL($[id]); + | arg ">>" type mem_base mem_off { struct val b = IDTOVAL($[mem_base]); - /* really not a huge fan or 'reusing' the output slot... */ - INSADD(STORE, $[type], noclass(), b, t, $[mem_off]); + INSADD(STORE, $[type], noclass(), b, $[arg], $[mem_off]); } | id "<<*" int id { struct val t = IDTOVAL($1); @@ -384,6 +382,10 @@ stack INSADD(ALLOC, $[type], t, noclass(), noclass(), $[int]); } + | "^" "^" int { + INSADD(DEALLOC, NOTYPE, noclass(), noclass(), noclass(), $[int]); + } + cond : type id "=" arg "==" arg { struct val t = IDALLOC($[id]); @@ -480,8 +482,8 @@ opt_call_rets | {} call_arg - : type arg { - INSADD(ARG, $[type], noclass(), $[arg], imm_val(parser->idx++, I27), 0); + : arg { + INSADD(ARG, NOTYPE, noclass(), $[arg], imm_val(parser->idx++, I27), 0); } call_args @@ -568,6 +570,9 @@ top unit : top unit | top + | error { + parser->failed = true; + } input : unit -- cgit v1.3