diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-08-17 02:57:08 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 81c5f8e750fcda6a2451fb54604130431434f88f (patch) | |
tree | 944fa06c781d57b1db90e9153080f411a1c34a55 /src/bytecode | |
parent | 20662a1d203ffb9e05d6694347fd258115b41d0a (diff) |
Implement more instructions, implement function parameters and arguments
Diffstat (limited to 'src/bytecode')
-rw-r--r-- | src/bytecode/bytecode.c | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/src/bytecode/bytecode.c b/src/bytecode/bytecode.c index 70a5cfa..8a41900 100644 --- a/src/bytecode/bytecode.c +++ b/src/bytecode/bytecode.c @@ -42,9 +42,10 @@ static CHECK_RESULT usize ssa_extract_func_start(u8 *instruction_data, SsaInsFun } static CHECK_RESULT usize ssa_extract_func_call(u8 *instruction_data, SsaInsFuncCall *result) { - am_memcpy(&result->result, instruction_data, sizeof(result->result)); - am_memcpy(&result->func_decl, instruction_data + sizeof(result->result), sizeof(result->func_decl)); - return sizeof(result->result) + sizeof(result->func_decl); + result->num_args = instruction_data[0]; + am_memcpy(&result->result, instruction_data + 1, sizeof(result->result)); + am_memcpy(&result->func_decl, instruction_data + 1 + sizeof(result->result), sizeof(result->func_decl)); + return sizeof(u8) + sizeof(result->result) + sizeof(result->func_decl); } static CHECK_RESULT usize ssa_extract_jump_zero(u8 *instruction_data, SsaInsJumpZero *result) { @@ -94,7 +95,7 @@ static void add_intermediates(BytecodeCompilerContext *self) { } } -void add_strings(BytecodeCompilerContext *self) { +static void add_strings(BytecodeCompilerContext *self) { /*doc(Bytecode strings) # Strings layout |Type |Field |Description | @@ -144,6 +145,18 @@ void add_strings(BytecodeCompilerContext *self) { } } +static void add_functions(BytecodeCompilerContext *self) { + /*doc(Bytecode functions) + # Internal functions layout + |Type|Field |Description | + |----|-------------------|---------------------------------| + |u16 |Number of functions|The number of internal functions.| + */ + + assert(sizeof(SsaFuncIndex) == sizeof(u16) && "Program decoder needs to be updated since size of func index has changed"); + throw_if_error(buffer_append(&self->bytecode.data, &self->parser->ssa->func_counter, sizeof(u16))); +} + static void add_ins1(BytecodeCompilerContext *self, AmalOpcode opcode, const char *fmt) { throw_if_error(buffer_append(&self->bytecode.data, &opcode, sizeof(AmalOpcodeType))); fprintf(stderr, fmt); @@ -437,7 +450,7 @@ static void add_instructions(BytecodeCompilerContext *self) { } case SSA_ASSIGN_REG: { instruction += ssa_extract_form1(instruction, &ssa_ins_form1); - add_ins3(self, AMAL_OP_MOV, ssa_ins_form1.lhs, ssa_ins_form1.rhs, "mov r%d, d%d"); + add_ins3(self, AMAL_OP_MOV, ssa_ins_form1.lhs, ssa_ins_form1.rhs, "mov r%d, r%d"); break; } case SSA_ADD: { @@ -505,8 +518,8 @@ static void add_instructions(BytecodeCompilerContext *self) { is defined as the size of all previous files' number of functions. */ instruction += ssa_extract_func_call(instruction, &ssa_ins_func_call); - /* TODO: Replace 0 with the number of arguments */ - add_ins7(self, AMAL_OP_CALL, ssa_ins_func_call.func_decl->ssa_func_index, 0, "call %d, %d"); + add_ins7(self, AMAL_OP_CALL, ssa_ins_func_call.func_decl->ssa_func_index, ssa_ins_func_call.num_args, "call %d, %d"); + assert(bool_false && "TODO: Assign function result (RAX for x86_64) to ssa_ins_func_call.result reg"); break; } case SSA_JUMP_ZERO: { @@ -519,6 +532,13 @@ static void add_instructions(BytecodeCompilerContext *self) { add_ins4(self, AMAL_OP_JMP, ssa_ins_jump.jump_offset, "jmp %d"); break; } + case SSA_RET: { + SsaRegister reg; + am_memcpy(®, instruction, sizeof(SsaRegister)); + instruction += sizeof(SsaRegister); + add_ins2(self, AMAL_OP_RET, reg, "ret r%d"); + break; + } } } @@ -537,6 +557,6 @@ static void add_instructions(BytecodeCompilerContext *self) { void generate_bytecode_from_ssa(BytecodeCompilerContext *self) { add_intermediates(self); add_strings(self); - /* TODO: Also add strings in ssa, so we can index them */ + add_functions(self); add_instructions(self); } |