Begin to codegen bytecode instructions

This commit is contained in:
Krzosa Karol
2022-06-21 15:49:39 +02:00
parent 6ed17a3c1c
commit 2c3a8dc764
6 changed files with 59 additions and 6 deletions

View File

@@ -170,6 +170,7 @@ struct Bc{
U64 dis; // @debug_id
U8 *stack_bottom;
Register registers[256];
Arena memory;
Arena instructions;
Arena stack; // We reserve 4 gibs and allocate only 4 kibs to make sure we know when we
// accidently overshoot the stack by 2 gigabytes woo yeee
@@ -180,7 +181,6 @@ create_bytecode_interp(){
Bc b = {};
{
arena_init(&b.instructions, "Bytecode instructions"_s);
b.instructions.alignment = 1;
// Commit
arena_push_size(&b.instructions, 16);
@@ -190,16 +190,24 @@ create_bytecode_interp(){
{
arena_init(&b.stack, "Bytecode stack"_s);
b.stack.alignment = 8;
// Setup a 4 kilobyte stack
arena_push_size(&b.stack, kib(4));
b.registers[REG_STACK_POINTER].pointer = b.stack_bottom = b.stack.memory.data;
}
arena_init(&b.memory, "Bytecode memory"_s);
return b;
}
function void
destroy_bytecode_interp(Bc *b){
arena_release(&b->instructions);
arena_release(&b->stack);
}
function void
emit_load_constant_f64(Bc *b, U8 dst, F64 constant){
auto i = exp_alloc_type(&b->instructions, Instruction_Constant);
@@ -287,6 +295,7 @@ run_bytecode_interp(Bc *b){
b->registers[REG_INS_POINTER].pointer += sizeof(Instruction);
switch(instr->operation){
default:{}
case BC_LOAD_FROM_MEMORY64:{
U64 *load_address = b->registers[instr->src].pointer64;
b->registers[instr->dst].u64 = *load_address;
@@ -722,4 +731,5 @@ test_interpreter(){
emit_pop(&b, 5);
emit_end(&b);
run_bytecode_interp(&b);
destroy_bytecode_interp(&b);
}