diff --git a/bytecode_codegen.cpp b/bytecode_codegen.cpp index 57156b7..3955386 100644 --- a/bytecode_codegen.cpp +++ b/bytecode_codegen.cpp @@ -1,17 +1,25 @@ function Register_Index bc_emit_expr(Bc *b, Ast *ast){ - if(!ast) return REG_NULL; + if(!ast) return -1; if(!is_flag_set(ast->flags, AST_EXPR)) compiler_error(ast->pos, "Internal compiler error: Trying to emit expression but it doesn't have appropriate flag"); switch(ast->kind){ + + // @todo pass type and figure out what to do with untyped ?? CASE(VALUE, Atom){ Register_Index dst = allocate_register(b); - U64 value = bigint_as_unsigned(&node->big_int_val); - emit_load_constant_s64(b, dst, value); + switch(node->type->kind){ + CASE_UINT: emit_load_constant_u64(b, dst, bigint_as_unsigned(&node->big_int_val)); break; + CASE_SINT: emit_load_constant_s64(b, dst, bigint_as_signed(&node->big_int_val)); break; + CASE_FLOAT: emit_load_constant_f64(b, dst, node->f64_val); break; + invalid_default_case; + } + return dst; BREAK(); } + CASE(BINARY, Binary){ Register_Index left = bc_emit_expr(b, node->left); Register_Index right = bc_emit_expr(b, node->right); @@ -20,9 +28,10 @@ bc_emit_expr(Bc *b, Ast *ast){ return left; BREAK(); } + default:{} } - return REG_NULL; + return -1; } function void @@ -43,6 +52,7 @@ compile_to_bc(){ emit_load_constant_address(b, address_index, node->bytecode_data_position); emit_memory(b, BC_STORE_TO_MEMORY64, address_index, expression_index); release_register(b, expression_index); + release_register(b, address_index); } BREAK(); } diff --git a/bytecode_interpreter.cpp b/bytecode_interpreter.cpp index 0f4ac07..a171ed3 100644 --- a/bytecode_interpreter.cpp +++ b/bytecode_interpreter.cpp @@ -143,7 +143,6 @@ union Register{ }; enum{ - REG_NULL, REG_STACK_POINTER, REG_INSTRUCTION_POINTER, REG_SPECIAL_COUNT, @@ -186,7 +185,7 @@ struct Bc{ // function Register_Index allocate_register(Bc *b){ - Register_Index result = 0; + Register_Index result = -1; if(b->free_registers.len == 0) result = b->registers.addi({}); else @@ -208,6 +207,7 @@ release_register(Bc *b, Register_Index reg){ } assert_msg(found, "Trying to release register that is not used"); + b->free_registers.add(reg); } function Bc diff --git a/main.cpp b/main.cpp index 5aa469e..8fc5dd2 100644 --- a/main.cpp +++ b/main.cpp @@ -178,7 +178,7 @@ int main(int argument_count, char **arguments){ test_intern_table(); emit_line_directives = true; - String program_name = "vm.kl"_s; + String program_name = "main.kl"_s; if(argument_count > 1){ program_name = string_from_cstring(arguments[1]); } @@ -209,8 +209,8 @@ int main(int argument_count, char **arguments){ arena_clear(&pctx->stage_arena); - compile_to_bc(); - __debugbreak(); + // compile_to_bc(); + // __debugbreak(); String result = get_compilation_result(); assert(os_write_file("program.c"_s, result)); {