aboutsummaryrefslogtreecommitdiff
path: root/src/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytecode')
-rw-r--r--src/bytecode/bytecode.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/bytecode/bytecode.c b/src/bytecode/bytecode.c
index fe3dc0f..47d492c 100644
--- a/src/bytecode/bytecode.c
+++ b/src/bytecode/bytecode.c
@@ -77,6 +77,7 @@ static void add_intermediates(BytecodeCompilerContext *self) {
Buffer *instructions = &self->bytecode.data;
SsaNumber *intermediate = buffer_begin(&ssa->intermediates);
SsaNumber *intermediates_end = buffer_end(&ssa->intermediates);
+ int i = 0;
u32 intemediates_size = (sizeof(u8) + sizeof(u64)) * buffer_get_size(&ssa->intermediates, SsaNumber);
throw_if_error(buffer_expand(instructions, sizeof(u32) + intemediates_size));
@@ -85,6 +86,7 @@ static void add_intermediates(BytecodeCompilerContext *self) {
throw_if_error(buffer_append(instructions, &intermediate->type, sizeof(u8)));
/* TODO: Store value using an encoding that will save space when using low numbers */
throw_if_error(buffer_append(instructions, &intermediate->value.integer, sizeof(u64)));
+ fprintf(stderr, "i%d = %ld\n", i++, intermediate->value.integer);
}
}
@@ -228,11 +230,12 @@ static void add_export_functions(BytecodeCompilerContext *self) {
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);
- fputc('\n', stderr);
+ if(fmt) {
+ fprintf(stderr, fmt);
+ fputc('\n', stderr);
+ }
}
-
static void add_ins2(BytecodeCompilerContext *self, AmalOpcode opcode, i8 reg, const char *fmt) {
Buffer *instructions = &self->bytecode.data;
size_t index = instructions->size;
@@ -325,6 +328,7 @@ static void add_instructions(BytecodeCompilerContext *self) {
Ssa *ssa = self->parser->ssa;
u8 *instruction = buffer_begin(&ssa->instructions);
u8 *instructions_end = buffer_end(&ssa->instructions);
+ u16 label_counter = 0;
u32 num_instructions_index = self->bytecode.data.size;
throw_if_error(buffer_append_empty(&self->bytecode.data, sizeof(num_instructions_index)));
@@ -386,6 +390,7 @@ static void add_instructions(BytecodeCompilerContext *self) {
case SSA_FUNC_START: {
instruction += ssa_extract_data(instruction, &ssa_ins_func_start, sizeof(ssa_ins_func_start));
add_ins6(self, AMAL_OP_FUNC_START, ssa_ins_func_start.flags, ssa_ins_func_start.num_local_vars_regs, "func_start 0x%02x, %u");
+ label_counter = 0;
break;
}
case SSA_FUNC_END: {
@@ -419,12 +424,12 @@ static void add_instructions(BytecodeCompilerContext *self) {
}
case SSA_JUMP_ZERO: {
instruction += ssa_extract_data(instruction, &ssa_ins_jump_zero, sizeof(ssa_ins_jump_zero));
- add_ins6(self, AMAL_OP_JZ, ssa_ins_jump_zero.condition_reg, ssa_ins_jump_zero.jump_offset, "jz r%d, %d");
+ add_ins6(self, AMAL_OP_JZ, ssa_ins_jump_zero.condition_reg, ssa_ins_jump_zero.target_label, "jz r%d, l%d");
break;
}
case SSA_JUMP: {
instruction += ssa_extract_data(instruction, &ssa_ins_jump, sizeof(ssa_ins_jump));
- add_ins4(self, AMAL_OP_JMP, ssa_ins_jump.jump_offset, "jmp %d");
+ add_ins4(self, AMAL_OP_JMP, ssa_ins_jump.target_label, "jmp l%d");
break;
}
case SSA_RET: {
@@ -434,6 +439,11 @@ static void add_instructions(BytecodeCompilerContext *self) {
add_ins2(self, AMAL_OP_RET, reg, "ret r%d");
break;
}
+ case SSA_LABEL: {
+ add_ins1(self, AMAL_OP_LABEL, NULL);
+ fprintf(stderr, "label l%d\n", label_counter++);
+ break;
+ }
}
}