Skip to content

Commit 86779cd

Browse files
committed
Support function call
1 parent 11a06ae commit 86779cd

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

example.ssa

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
function w $pp(w %a, w %b) {
2+
%t =w add %a, %b
3+
ret %t
4+
}
5+
16
function w $main() {
27
%a =w copy 1
38
%t =w alloc 4
49
jnz %a, @L1, @L2
510
@L1
6-
%t =w add %t, %t
11+
%t =w call $pp(w 0, w 0)
712
jmp @L3
813
@L2
914
%t =w copy 1

src/ir_translator.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,26 @@ void qs_gen_inst(qs_ir_inst_t *inst, basic_block_t *bb, block_t *blk)
193193

194194
add_insn(blk, bb, OP_assign, dest, rs1, NULL, 0, NULL);
195195
break;
196+
case QS_OP_CALL: {
197+
var_t *args[MAX_PARAMS];
198+
199+
for (int i = 1; i < inst->narg.len; i++)
200+
args[i - 1] = qs_gen_value(&inst->args[i], bb, blk);
201+
202+
for (int i = 0; i < inst->narg.len - 1; i++)
203+
add_insn(blk, bb, OP_push, NULL, args[i], NULL,
204+
inst->narg.len - i - 1, NULL);
205+
206+
add_insn(blk, bb, OP_call, NULL, NULL, NULL, 0,
207+
trim_sigil(rs1_val->global->name));
208+
209+
if (inst->dest) {
210+
dest = qs_gen_dest(inst->dest, bb, blk);
211+
212+
add_insn(blk, bb, OP_func_ret, dest, NULL, NULL, 0, NULL);
213+
}
214+
break;
215+
}
196216
case QS_OP_JMP:
197217
break;
198218
case QS_OP_JNZ:
@@ -289,13 +309,14 @@ void qs_gen_module(qs_ir_module_t *mod)
289309
func->return_def.type = qs_convert_type(ir_func->rty);
290310
init_var(&func->return_def);
291311

312+
func->stack_size = 4;
292313
func->num_params = ir_func->nparams;
293-
for (int j = 0; j < ir_func->nparams; j++) {
314+
for (int j = 0; j < func->num_params; j++) {
294315
qs_ir_temp_t *temp = &ir_func->temps[j];
295316

296317
init_var(&func->param_defs[j]);
297-
strcpy(func->param_defs->var_name, temp->name);
298-
func->param_defs->type = qs_convert_type(temp->type);
318+
strcpy(func->param_defs[j].var_name, trim_sigil(temp->name));
319+
func->param_defs[j].type = qs_convert_type(temp->type);
299320
}
300321

301322
func->va_args = ir_func->variadic;

0 commit comments

Comments
 (0)