Skip to content

Commit

Permalink
fix: Pass func parameters to regs with valid sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Nov 29, 2024
1 parent c8e5a5b commit acba071
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
79 changes: 79 additions & 0 deletions codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,83 @@ char *reg_b(general_type *tp, context *ctx)
else if (sz == 8)
return "rbx";
return NULL;
}

char *reg_typed(char *reg, general_type *tp, context *ctx)
{
int sz = tp->size(tp, ctx);
if (strcmp(reg, "rdi") == 0)
{
if (sz == 8)
return "rdi";
else if (sz == 4)
return "edi";
else if (sz == 2)
return "di";
else if (sz == 1)
return "dil";
}
else if (strcmp(reg, "rsi") == 0)
{
if (sz == 8)
return "rsi";
else if (sz == 4)
return "esi";
else if (sz == 2)
return "si";
else if (sz == 1)
return "sil";
}
else if (strcmp(reg, "rdx") == 0)
{
if (sz == 8)
return "rdx";
else if (sz == 4)
return "edx";
else if (sz == 2)
return "dx";
else if (sz == 1)
return "dl";
}
else if (strcmp(reg, "rcx") == 0)
{
if (sz == 8)
return "rcx";
else if (sz == 4)
return "ecx";
else if (sz == 2)
return "cx";
else if (sz == 1)
return "cl";
}
else if (strcmp(reg, "r8") == 0)
{
if (sz == 8)
return "r8";
else if (sz == 4)
return "r8d";
else if (sz == 2)
return "r8w";
else if (sz == 1)
return "r8b";
}
else if (strcmp(reg, "r9") == 0)
{
if (sz == 8)
return "r9";
else if (sz == 4)
return "r9d";
else if (sz == 2)
return "r9w";
else if (sz == 1)
return "r9b";
}
else
{
fprintf(stderr, "Unknown register %s!\n", reg);
exit(1);
}
fprintf(stderr, "Unknown size %u!\n", sz);
exit(1);
return "";
}
1 change: 1 addition & 0 deletions codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ void new_struct(context *ctx, context_struct *s);

char *reg_a(general_type *tp, context *ctx);
char *reg_b(general_type *tp, context *ctx);
char *reg_typed(char *reg, general_type *tp, context *ctx);
#endif
7 changes: 6 additions & 1 deletion parser/expr/func_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ apply_result *func_call_apply(parser_node *node, context *ctx)
node_func_call *call = (node_func_call *)node->data;

char **argvals = (char **)malloc(sizeof(char *) * 6);
general_type **argtypes = (general_type **)malloc(sizeof(general_type *) * 6);
for (int i = 0; i < call->num_args; i++)
{
apply_result *regval = call->args[i]->apply(call->args[i], ctx);
Expand All @@ -44,6 +45,7 @@ apply_result *func_call_apply(parser_node *node, context *ctx)
add_text(ctx, "mov %s, %s", tmp->repl, rega);

argvals[i] = tmp->repl;
argtypes[i] = regval->type;
}
for (int i = 0; i < call->num_args; i++)
{
Expand All @@ -65,6 +67,7 @@ apply_result *func_call_apply(parser_node *node, context *ctx)
fprintf(stderr, "Cannot provide more than 6 args!\n");
exit(1);
}
regname = reg_typed(regname, argtypes[i], ctx);
add_text(ctx, "mov %s, %s", regname, argvals[i]);
}

Expand Down Expand Up @@ -92,7 +95,9 @@ apply_result *func_call_apply(parser_node *node, context *ctx)
symbol *tmp = new_temp_symbol(ctx, ret_type);
add_text(ctx, "mov %s, %s", tmp->repl, rega);
return new_result(tmp->repl, tmp->type);
} else {
}
else
{
return NULL;
}
}
Expand Down
5 changes: 4 additions & 1 deletion parser/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ apply_result *func_def_apply(parser_node *node, context *ctx)
{
add_text(ctx, "extern %s", func->identity);
return NULL;
} else {
}
else
{
char *extern_txt = cc_asprintf("extern %s", func->identity);
char *cmt_extern_txt = cc_asprintf(";extern %s", func->identity);
replace_text(ctx, extern_txt, cmt_extern_txt);
Expand Down Expand Up @@ -53,6 +55,7 @@ apply_result *func_def_apply(parser_node *node, context *ctx)
fprintf(stderr, "Cannot define a function with more than 6 args!");
exit(1);
}
regname = reg_typed(regname, par->type, ctx);

symbol *sym = new_symbol(ctx, par->name, par->type);
add_text(ctx, "mov %s, %s", sym->repl, regname);
Expand Down
4 changes: 2 additions & 2 deletions tests/output/inp3.c_asm_output.asm
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ mov [rbp-49], rax
mov al, 99
mov [rbp-50], al
mov rdi, [rbp-40]
mov rsi, [rbp-41]
mov sil, [rbp-41]
mov rdx, [rbp-49]
mov rcx, [rbp-50]
mov cl, [rbp-50]
mov rax, rbp
sub rax, 8
mov [rbp-58], rax
Expand Down
2 changes: 1 addition & 1 deletion tests/output/inp6.c_asm_output.asm
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mov [rbp-276], rax
mov rdi, [rbp-162]
mov rsi, [rbp-194]
mov rdx, [rbp-226]
mov rcx, [rbp-244]
mov cl, [rbp-244]
mov r8, [rbp-276]
mov rax, rbp
sub rax, 8
Expand Down
2 changes: 1 addition & 1 deletion tests/output/inp_pointer.c_asm_output.asm
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ mov [rbp-481], al
mov al, [rbp-481]
mov [rbp-482], al
mov rdi, [rbp-456]
mov rsi, [rbp-482]
mov sil, [rbp-482]
mov rax, rbp
sub rax, 8
mov [rbp-490], rax
Expand Down

0 comments on commit acba071

Please sign in to comment.