aboutsummaryrefslogtreecommitdiff
path: root/src/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytecode')
-rw-r--r--src/bytecode/bytecode.c43
1 files changed, 32 insertions, 11 deletions
diff --git a/src/bytecode/bytecode.c b/src/bytecode/bytecode.c
index 9bf5f24..ffcd2e0 100644
--- a/src/bytecode/bytecode.c
+++ b/src/bytecode/bytecode.c
@@ -18,7 +18,7 @@
throw(return_if_result); \
} while(0)
-int bytecode_init(Bytecode *self, ScopedAllocator *allocator) {
+int bytecode_init(Bytecode *self, ArenaAllocator *allocator) {
return buffer_init(&self->data, allocator);
}
@@ -37,8 +37,8 @@ static CHECK_RESULT usize ssa_extract_form2(u8 *instruction_data, SsaInsForm2 *r
static CHECK_RESULT usize ssa_extract_func_start(u8 *instruction_data, SsaInsFuncStart *result) {
am_memcpy(&result->func_index, instruction_data, sizeof(result->func_index));
- am_memcpy(&result->num_args, instruction_data + sizeof(result->func_index), sizeof(result->num_args));
- return sizeof(result->func_index) + sizeof(result->num_args);
+ am_memcpy(&result->num_registers, instruction_data + sizeof(result->func_index), sizeof(result->num_registers));
+ return sizeof(result->func_index) + sizeof(result->num_registers);
}
static CHECK_RESULT usize ssa_extract_func_call(u8 *instruction_data, SsaInsFuncCall *result) {
@@ -109,7 +109,7 @@ void add_strings(BytecodeCompilerContext *self) {
throw_if_error(buffer_append(instructions, &strings_size, sizeof(u32)));
for(; string != strings_end; ++string) {
throw_if_error(buffer_append(instructions, &string->size, sizeof(u16)));
- throw_if_error(buffer_append(instructions, &string->data, string->size));
+ throw_if_error(buffer_append(instructions, string->data, string->size));
}
}
@@ -189,6 +189,20 @@ static void add_ins6(BytecodeCompilerContext *self, AmalOpcode opcode, u8 dst_re
fputc('\n', stderr);
}
+static void add_ins7(BytecodeCompilerContext *self, AmalOpcode opcode, u16 idx, u8 arg, const char *fmt) {
+ Buffer *instructions;
+ size_t index;
+ instructions = &self->bytecode.data;
+ index = instructions->size;
+
+ throw_if_error(buffer_append_empty(instructions, sizeof(AmalOpcodeType) + sizeof(idx) + sizeof(arg)));
+ instructions->data[index] = opcode;
+ memcpy(instructions->data + index + sizeof(AmalOpcodeType), &idx, sizeof(idx));
+ instructions->data[index + sizeof(AmalOpcodeType) + sizeof(idx)] = arg;
+ fprintf(stderr, fmt, idx, arg);
+ fputc('\n', stderr);
+}
+
#if 0
#define NUM_MAX_REGS 256
#define NUM_MAX_FUNC_ARGS 32
@@ -397,11 +411,21 @@ static void add_instructions(BytecodeCompilerContext *self) {
add_ins5(self, AMAL_OP_SUB, ssa_ins_form2.result, ssa_ins_form2.lhs, ssa_ins_form2.rhs, "sub r%d, r%d, r%d");
break;
}
+ case SSA_IMUL: {
+ instruction += ssa_extract_form2(instruction, &ssa_ins_form2);
+ add_ins5(self, AMAL_OP_IMUL, ssa_ins_form2.result, ssa_ins_form2.lhs, ssa_ins_form2.rhs, "imul r%d, r%d, r%d");
+ break;
+ }
case SSA_MUL: {
instruction += ssa_extract_form2(instruction, &ssa_ins_form2);
add_ins5(self, AMAL_OP_MUL, ssa_ins_form2.result, ssa_ins_form2.lhs, ssa_ins_form2.rhs, "mul r%d, r%d, r%d");
break;
}
+ case SSA_IDIV: {
+ instruction += ssa_extract_form2(instruction, &ssa_ins_form2);
+ add_ins5(self, AMAL_OP_IDIV, ssa_ins_form2.result, ssa_ins_form2.lhs, ssa_ins_form2.rhs, "idiv r%d, r%d, r%d");
+ break;
+ }
case SSA_DIV: {
instruction += ssa_extract_form2(instruction, &ssa_ins_form2);
add_ins5(self, AMAL_OP_DIV, ssa_ins_form2.result, ssa_ins_form2.lhs, ssa_ins_form2.rhs, "div r%d, r%d, r%d");
@@ -414,11 +438,11 @@ static void add_instructions(BytecodeCompilerContext *self) {
}
case SSA_FUNC_START: {
instruction += ssa_extract_func_start(instruction, &ssa_ins_func_start);
- add_ins1(self, AMAL_OP_FUNC_START, "func_start");
+ add_ins4(self, AMAL_OP_FUNC_START, ssa_ins_func_start.num_registers, "func_start %u");
break;
}
case SSA_FUNC_END: {
- add_ins1(self, AMAL_OP_FUNC_START, "func_end");
+ add_ins1(self, AMAL_OP_FUNC_END, "func_end");
break;
}
case SSA_PUSH: {
@@ -429,7 +453,6 @@ static void add_instructions(BytecodeCompilerContext *self) {
break;
}
case SSA_CALL: {
- /* TODO: Add args, using number of bytes to pop after function call. */
/*
TODO: Pass return register to function. The register should be a pointer that
has the size of the function return values so the return values can fit in it.
@@ -443,7 +466,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);
- add_ins4(self, AMAL_OP_CALL, ssa_ins_func_call.func_decl->ssa_func_index, "call %d");
+ /* 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");
break;
}
case SSA_JUMP_ZERO: {
@@ -456,9 +480,6 @@ static void add_instructions(BytecodeCompilerContext *self) {
add_ins4(self, AMAL_OP_JMP, ssa_ins_jump.jump_offset, "jmp %d");
break;
}
- default:
- amal_log_error("Instruction not yet implemented: %d", ins);
- assert(bool_false && "Instruction not yet implemented");
}
}