From 790493e6732236db71db0dc4fd462bae809e1908 Mon Sep 17 00:00:00 2001 From: mike lowis Date: Mon, 26 Apr 2021 23:55:04 -0400 Subject: [PATCH] fleshed out more kinds of operations --- cerise/codegen.c | 61 ++++++++++++++++++++++++++++++++++--------- cerise/parser.c | 3 ++- cerise/tests/Module.m | 12 ++++----- cerise/tests/Module.s | 23 +++++----------- 4 files changed, 64 insertions(+), 35 deletions(-) diff --git a/cerise/codegen.c b/cerise/codegen.c index c3b1636..8e2f452 100644 --- a/cerise/codegen.c +++ b/cerise/codegen.c @@ -238,12 +238,11 @@ static void load_var(Parser* p, Item* item) switch (item->mode) { case ITEM_CONST: - puts("BAR"); /* TODO: range check the constant */ - // emit instruction - item->reg = p->curr_reg; +// // emit instruction +// item->reg = p->curr_reg; - printf("movq %s, $%lld\n", curr_regname(p), item->imm.i); + printf(" movq %s, $%lld\n", curr_regname(p), item->imm.i); item->reg = p->curr_reg; next_reg(p); break; @@ -350,19 +349,27 @@ void codegen_unop(Parser* p, int op, Item* a) } } +static char* ImmOps[] = { + ['+'] = "addq", +}; + static void imm_binop(Parser* p, int op, Item* a, Item* b) { if (a->type->form == FORM_INT || a->type->form == FORM_BOOL) { - switch (op) + char* opstring = ImmOps[op]; + if (a->mode == ITEM_CONST) { - case '+': - printf(" addq $%lld, %s\n", b->imm.i, regname(a->reg)); - break; - - default: - assert(!"not implemented"); - break; +// printf(" %s $%lld, %s\n", +// opstring, regname(b->reg), a->imm.i, regname(b->reg)); + printf(" %s $%lld, %s\n", + opstring, a->imm.i, regname(b->reg)); + a->reg = b->reg; + } + else + { + printf(" %s $%lld, %s\n", + opstring, b->imm.i, regname(a->reg)); } } else @@ -371,6 +378,15 @@ static void imm_binop(Parser* p, int op, Item* a, Item* b) } } +static void imm_regop(Parser* p, int op, Item* a, Item* b) +{ + if (a->type->form == FORM_INT || a->type->form == FORM_BOOL) + { + printf(" addq %s, %s, %s\n", + regname(a->reg), regname(b->reg), regname(b->reg)); + } +} + void codegen_binop(Parser* p, int op, Item* a, Item* b) { if (items_const(a, b)) @@ -381,17 +397,38 @@ void codegen_binop(Parser* p, int op, Item* a, Item* b) { load_var(p, a); imm_binop(p, op, a, b); + p->curr_reg--; + } + else if (a->mode == ITEM_CONST) + { + load_var(p, b); + imm_binop(p, op, a, b); + p->curr_reg--; } else { load_var(p, a); load_var(p, b); + item_dump(a); + item_dump(b); + + imm_regop(p, op, a, b); + // emit instruction // p->topreg--; // a->reg = p->topreg-1; } } +// PROCEDURE Switch*(VAR x: Item); +// BEGIN Put1(Mov, RH, 0, -60); Put2(Ldw, RH, RH, 0); +// x.mode := Reg; +// x.type := intType; +// x.r := RH; +// INC(RH) +// END Switch; + + void codegen_store(Parser* p, Item* a, Item* b) { if (a->mode == ITEM_MVAR && b->reg == NOREG) diff --git a/cerise/parser.c b/cerise/parser.c index a76adf4..fed4f03 100644 --- a/cerise/parser.c +++ b/cerise/parser.c @@ -450,7 +450,7 @@ RULE(expression) { int ops[] = { EQ, NEQ, '<', LTEQ, '>', GTEQ, IS, 0 }; simple_expr(p, item); - item_dump(item); + if (matches_oneof(p, ops)) { Item right = { 0 }; @@ -597,6 +597,7 @@ RULE(statement_seq) init_item(item, sym); item->imm.s = sym->name; + printf("\n// %s = ...\n", sym->name); expression(p, &right); check_types(p, item, &right); codegen_store(p, item, &right); diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 21b89db..de35273 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -14,11 +14,11 @@ var c : Int begin - a = true; - a = A; - b = 24; - b = B; +# a = true; +# a = A; +# b = 24; +# b = B; c = b + 1; -# c = 1 + b; -# c = a + b; + c = 1 + b; +# c = b + b; end diff --git a/cerise/tests/Module.s b/cerise/tests/Module.s index 1f9c331..8ef8b9e 100644 --- a/cerise/tests/Module.s +++ b/cerise/tests/Module.s @@ -1,5 +1,3 @@ -// Item M:0 R:0 -// Item M:0 R:0 42 .data Module_a: .zero 1 @@ -18,23 +16,16 @@ Module: pushq %rbp movq %rsp, %rbp -// Item M:0 R:0 - movq $1, Module_a(%rip) -// Item M:0 R:0 - movq $1, Module_a(%rip) -// Item M:0 R:0 24 - movq $24, Module_b(%rip) -// Item M:0 R:0 42 - movq $42, Module_b(%rip) + +// c = ... + movq Module_b(%rip), %rdi + addq $1, %rdi + movq %rdi, Module_c(%rip) + +// c = ... movq Module_b(%rip), %rdi addq $1, %rdi -// Item M:2 R:1 b movq %rdi, Module_c(%rip) -BAR -movq %rsi, $1 - movq Module_b(%rip), %rdx -// Item M:0 R:2 1 - movq %rsi, Module_c(%rip) pop %rbp ret -- 2.49.0