How to Generate Code?
c = a + 3 - b -- IR --> ASSIGN --- Assembly --> MOVEQ a, R0
/ \ MOVEQ $3, R1
ISUB c ADDQ R0, R1
/ \ MOVQ b, R0
IADD b SUBQ R0, R1
/ \ MOVQ R1, c
a 3
void expr_codegen( struct expr *e ) {
switch(e->kind) {
case EXPR_SUB:
expr_codegen(e->left);
expr_codegen(e->right);
printf("SUBQ %s, %s\n",
scratch_name(e->left->reg),
scratch_name(e->right->reg),
e->reg = e->right->reg;
scratch_free(e->left->reg);
break;
}
}