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;
}
}
+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
}
}
+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))
{
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)
{
int ops[] = { EQ, NEQ, '<', LTEQ, '>', GTEQ, IS, 0 };
simple_expr(p, item);
- item_dump(item);
+
if (matches_oneof(p, ops))
{
Item right = { 0 };
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);
-// Item M:0 R:0
-// Item M:0 R:0 42
.data
Module_a:
.zero 1
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