Prepare before typing the untyped

This commit is contained in:
Krzosa Karol
2022-06-22 10:07:23 +02:00
parent a229891485
commit de159fc7d9
3 changed files with 19 additions and 9 deletions

View File

@@ -1,17 +1,25 @@
function Register_Index function Register_Index
bc_emit_expr(Bc *b, Ast *ast){ bc_emit_expr(Bc *b, Ast *ast){
if(!ast) return REG_NULL; if(!ast) return -1;
if(!is_flag_set(ast->flags, AST_EXPR)) 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"); compiler_error(ast->pos, "Internal compiler error: Trying to emit expression but it doesn't have appropriate flag");
switch(ast->kind){ switch(ast->kind){
// @todo pass type and figure out what to do with untyped ??
CASE(VALUE, Atom){ CASE(VALUE, Atom){
Register_Index dst = allocate_register(b); Register_Index dst = allocate_register(b);
U64 value = bigint_as_unsigned(&node->big_int_val); switch(node->type->kind){
emit_load_constant_s64(b, dst, value); 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; return dst;
BREAK(); BREAK();
} }
CASE(BINARY, Binary){ CASE(BINARY, Binary){
Register_Index left = bc_emit_expr(b, node->left); Register_Index left = bc_emit_expr(b, node->left);
Register_Index right = bc_emit_expr(b, node->right); Register_Index right = bc_emit_expr(b, node->right);
@@ -20,9 +28,10 @@ bc_emit_expr(Bc *b, Ast *ast){
return left; return left;
BREAK(); BREAK();
} }
default:{} default:{}
} }
return REG_NULL; return -1;
} }
function void function void
@@ -43,6 +52,7 @@ compile_to_bc(){
emit_load_constant_address(b, address_index, node->bytecode_data_position); emit_load_constant_address(b, address_index, node->bytecode_data_position);
emit_memory(b, BC_STORE_TO_MEMORY64, address_index, expression_index); emit_memory(b, BC_STORE_TO_MEMORY64, address_index, expression_index);
release_register(b, expression_index); release_register(b, expression_index);
release_register(b, address_index);
} }
BREAK(); BREAK();
} }

View File

@@ -143,7 +143,6 @@ union Register{
}; };
enum{ enum{
REG_NULL,
REG_STACK_POINTER, REG_STACK_POINTER,
REG_INSTRUCTION_POINTER, REG_INSTRUCTION_POINTER,
REG_SPECIAL_COUNT, REG_SPECIAL_COUNT,
@@ -186,7 +185,7 @@ struct Bc{
// //
function Register_Index function Register_Index
allocate_register(Bc *b){ allocate_register(Bc *b){
Register_Index result = 0; Register_Index result = -1;
if(b->free_registers.len == 0) if(b->free_registers.len == 0)
result = b->registers.addi({}); result = b->registers.addi({});
else else
@@ -208,6 +207,7 @@ release_register(Bc *b, Register_Index reg){
} }
assert_msg(found, "Trying to release register that is not used"); assert_msg(found, "Trying to release register that is not used");
b->free_registers.add(reg);
} }
function Bc function Bc

View File

@@ -178,7 +178,7 @@ int main(int argument_count, char **arguments){
test_intern_table(); test_intern_table();
emit_line_directives = true; emit_line_directives = true;
String program_name = "vm.kl"_s; String program_name = "main.kl"_s;
if(argument_count > 1){ if(argument_count > 1){
program_name = string_from_cstring(arguments[1]); program_name = string_from_cstring(arguments[1]);
} }
@@ -209,8 +209,8 @@ int main(int argument_count, char **arguments){
arena_clear(&pctx->stage_arena); arena_clear(&pctx->stage_arena);
compile_to_bc(); // compile_to_bc();
__debugbreak(); // __debugbreak();
String result = get_compilation_result(); String result = get_compilation_result();
assert(os_write_file("program.c"_s, result)); assert(os_write_file("program.c"_s, result));
{ {