diff --git a/ps5/src/emit.h b/ps5/src/emit.h index a9080ff..abcb07c 100644 --- a/ps5/src/emit.h +++ b/ps5/src/emit.h @@ -51,6 +51,8 @@ #define CMPQ(op1, op2) EMIT("cmpq %s, %s", (op1), (op2)) // Compare the two operands +#define CLEAR(reg) EMIT("xor %s, %s", (reg), (reg)) + // The SETcc-family of instructions assigns either 0 or 1 to a byte register, based on a comparison. // The instruction immediately before the SETcc should be a // cmpq op1, op2 diff --git a/ps5/src/generator.c b/ps5/src/generator.c index 0e0050b..d3732fe 100644 --- a/ps5/src/generator.c +++ b/ps5/src/generator.c @@ -154,6 +154,7 @@ static void generate_function(symbol_t* function) generate_statement(function->node); // TODO (Task 3.2): Emit the epilogue, including a label and a default return value (0) + CLEAR(RAX); LABEL(".%s.epilogue", function->name); MOVQ(RBP, RSP); POPQ(RBP); @@ -337,6 +338,10 @@ static void generate_print_statement(node_t* statement) static void generate_return_statement(node_t* statement) { // TODO (Task 4.5): Evaluate the return value, store it in %rax and jump to the function epilogue + generate_expression(statement->children[0]); + char epilogue[64]; + snprintf(epilogue, sizeof(epilogue), ".%s.epilogue", current_function->name); + JMP(epilogue); } // Recursively generate the given statement node, and all sub-statements.