ps5: generate_return_statement and fix epilogue

This commit is contained in:
2026-04-01 21:09:22 +02:00
parent 1759a99c5c
commit 47b4c646e1
2 changed files with 7 additions and 0 deletions

View File

@@ -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

View File

@@ -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.