Skip to content

Commit

Permalink
IR generation for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
LesleyLai committed Feb 1, 2025
1 parent a8bf8de commit ba7dbef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
5 changes: 4 additions & 1 deletion include/mcc/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ typedef struct IRProgram {

typedef struct IRFunctionDef {
StringView name;
size_t instruction_count;
uint32_t instruction_count;
uint32_t param_count;

struct IRInstruction* instructions;
StringView* params;
} IRFunctionDef;

typedef enum IRValueType {
Expand Down
33 changes: 32 additions & 1 deletion src/ir/ir_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,29 @@ static IRValue emit_ir_instructions_from_expr(const Expr* expr,
push_instruction(context, ir_label(end_label));
return result;
}
case EXPR_CALL: MCC_UNIMPLEMENTED();
case EXPR_CALL: {
Expr* function = expr->call.function;
MCC_ASSERT(function->tag == EXPR_VARIABLE);
const StringView function_name = function->variable->rewrote_name;

uint32_t arg_count = expr->call.arg_count;
IRValue* args = ARENA_ALLOC_ARRAY(context->tu_context->permanent_arena,
IRValue, arg_count);
for (uint32_t i = 0; i < arg_count; ++i) {
args[i] = emit_ir_instructions_from_expr(expr->call.args[i], context);
}

const IRValue result = ir_variable(create_fresh_variable_name(context));
push_instruction(context, (IRInstruction){.typ = IR_CALL,
.call = {
.func_name = function_name,
.dest = result,
.arg_count = arg_count,
.args = args,
}});

return result;
}
}

MCC_UNREACHABLE();
Expand Down Expand Up @@ -677,7 +699,16 @@ static IRFunctionDef generate_ir_function_def(const FunctionDecl* decl,
context.instructions.length * sizeof(IRInstruction));
}

uint32_t param_count = decl->params.length;
StringView* parameters =
ARENA_ALLOC_ARRAY(tu_context->permanent_arena, StringView, param_count);
for (uint32_t i = 0; i < param_count; ++i) {
parameters[i] = decl->params.data[i]->rewrote_name;
}

return (IRFunctionDef){.name = decl->name,
.param_count = param_count,
.params = parameters,
.instruction_count = context.instructions.length,
.instructions = instructions};
}
Expand Down
19 changes: 17 additions & 2 deletions src/ir/ir_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ void print_ir(const IRProgram* ir)
for (size_t i = 0; i < ir->function_count; i++) {
const IRFunctionDef ir_function = ir->functions[i];
const StringView name = ir_function.name;
printf("func %.*s():\n", (int)name.size, name.start);
printf("func %.*s(", (int)name.size, name.start);
for (uint32_t j = 0; j < ir_function.param_count; ++j) {
if (j != 0) { printf(", "); }
printf("%.*s", (int)ir_function.params[j].size,
ir_function.params[j].start);
}
printf("):\n");

for (size_t j = 0; j < ir_function.instruction_count; j++) {
const IRInstruction instruction = ir_function.instructions[j];
switch (instruction.typ) {
Expand Down Expand Up @@ -84,7 +91,15 @@ void print_ir(const IRProgram* ir)
instruction.label.start);
} break;
case IR_CALL: {
MCC_UNIMPLEMENTED();
printf(" ");
print_ir_value(instruction.call.dest);
printf(" = call %.*s(", (int)instruction.call.func_name.size,
instruction.call.func_name.start);
for (uint32_t k = 0; k < instruction.call.arg_count; ++k) {
if (k != 0) { printf(", "); }
print_ir_value(instruction.call.args[k]);
}
printf(")\n");
} break;
}
}
Expand Down

0 comments on commit ba7dbef

Please sign in to comment.