ps5: function call

This commit is contained in:
2026-04-01 20:13:44 +02:00
parent 9590ea43f1
commit 854a43bd8a
2 changed files with 14 additions and 0 deletions

View File

@@ -45,6 +45,7 @@
#define CQO EMIT("cqo"); // Sign extend RAX -> RDX:RAX
#define IDIVQ(by) EMIT("idivq %s", (by)) // Divide RDX:RAX by "by", store result in RAX
#define CALL(func) EMIT("call .%s", (func))
#define RET EMIT("ret")
#define CMPQ(op1, op2) EMIT("cmpq %s, %s", (op1), (op2)) // Compare the two operands

View File

@@ -164,6 +164,18 @@ static void generate_function(symbol_t* function)
static void generate_function_call(node_t* call)
{
// TODO (Task 4.3)
node_t *args = call->children[1];
for (int i = args->n_children - 1; i >= 0; i--) {
node_t *expr = args->children[i];
generate_expression(expr);
PUSHQ(RAX);
}
int spill = args->n_children > NUM_REGISTER_PARAMS
? NUM_REGISTER_PARAMS : args->n_children;
for (int i = 0; i < spill; i++) {
POPQ(REGISTER_PARAMS[i]);
}
CALL(call->children[0]->symbol->name);
}
#define UNARY_OP(OP, INST) \
@@ -247,6 +259,7 @@ static void generate_expression(node_t* expression)
}
break;
case FUNCTION_CALL:
generate_function_call(expression);
break;
}
}