diff --git a/bytecode_codegen.cpp b/bytecode_codegen.cpp index 5da5010..b1d33df 100644 --- a/bytecode_codegen.cpp +++ b/bytecode_codegen.cpp @@ -1,47 +1,54 @@ -function Register_Index -bc_emit_expr(Bc *b, Ast *ast){ - 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); - emit_load_constant(b, node->pos, dst, node->value); - - return dst; - BREAK(); - } - - CASE(BINARY, Binary){ - Register_Index left = bc_emit_expr(b, node->left); - Register_Index right = bc_emit_expr(b, node->right); - emit_arithmetic(b, node->pos, BC_ADD_S64, left, right, left); - release_register(b, right); - return left; - BREAK(); - } - - default:{} - } - return -1; -} - struct Incomplete_Instruction{ Instruction *instruction; Ast_Decl *decl; }; +function void +bc_emit_expr(Bc *b, Ast *ast, Register_Index result_index){ + if(!ast) return; + 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){ + CASE(VALUE, Atom){ + emit_load_constant(b, node->pos, result_index, node->value); + BREAK(); + } + + CASE(IDENT, Atom){ + emit_load_constant(b, node->pos, result_index, node->resolved_decl->value); + BREAK(); + } + + CASE(BINARY, Binary){ + Register_Index left_index = allocate_register(b); + Register_Index right_index = allocate_register(b); + bc_emit_expr(b, node->left, left_index); + bc_emit_expr(b, node->right, right_index); + + emit_arithmetic(b, node->pos, BC_ADD_S64, left_index, right_index, result_index); + release_register(b, left_index); + release_register(b, right_index); + BREAK(); + } + + invalid_default_case; + } +} + function void emit_stmt(Bc *b, Ast *ast){ switch(ast->kind){ CASE(VAR, Decl){ node->register_index = allocate_register(b); - if(is_flag_set(node->flags, AST_VAR_IS_CONST)){ - // emit_load_ - } + bc_emit_expr(b, node->expr, node->register_index); + BREAK(); + } + + CASE(RETURN, Return){ + + emit_return(b, node->pos); BREAK(); } default:{} @@ -60,7 +67,7 @@ compile_to_bc(){ // auto incomplete = array_make(scratch, 512); Register_Index main_call_register = allocate_register(b); - Instruction *load_main_address = emit_load_constant(b, 0, main_call_register, {}); + Instruction *load_main_address = emit_load_constant(b, 0, 0, {}); emit_call(b, 0, 0); For_Named(pctx->ordered_decls, ast){ @@ -69,6 +76,7 @@ compile_to_bc(){ CASE(LAMBDA, Decl){ Ast_Lambda *lambda = node->lambda; if(!lambda->scope) break; + if(node->pos->file.s == "language.kl"_s) break; node->bytecode_data_position = b->instruction_pointer; For(node->lambda->args){ @@ -79,6 +87,10 @@ compile_to_bc(){ emit_stmt(b, it); } + For(node->lambda->args){ + release_register(b, it->register_index); + } + if(node->name == intern_main){ found_main = true; @@ -90,6 +102,7 @@ compile_to_bc(){ } CASE(VAR, Decl){ + if(node->pos->file.s == "language.kl"_s) break; node->bytecode_data_position = exp_alloc(&b->memory, node->type->size, AF_ZeroMemory); if(is_flag_set(node->flags, AST_VAR_IS_CONST)){ switch(node->type->kind){ @@ -100,15 +113,6 @@ compile_to_bc(){ } } else if(node->expr) compiler_error(node->pos, "Todo: Global variable with non constant expression"); - - // if(node->expr){ - // Register_Index expression_index = bc_emit_expr(b, node->expr); - // Register_Index address_index = allocate_register(b); - // emit_load_constant_address(b, node->pos, address_index, node->bytecode_data_position); - // emit_memory(b, node->pos, BC_STORE_TO_MEMORY64, address_index, expression_index); - // release_register(b, expression_index); - // release_register(b, address_index); - // } BREAK(); } default: {} @@ -119,6 +123,7 @@ compile_to_bc(){ compiler_error(0, "Having a [main] function is required, it's an entry point of the application"); } + release_register(b, main_call_register); emit_end(b); run_bytecode_interp(b); destroy_bytecode_interp(b); diff --git a/bytecode_interpreter.cpp b/bytecode_interpreter.cpp index 470fef7..0572cb3 100644 --- a/bytecode_interpreter.cpp +++ b/bytecode_interpreter.cpp @@ -184,13 +184,12 @@ struct Call_Frame{ Call_Frame *previous_call; Instruction *saved_instruction_pointer; Register_Index first_register_index_in_window; - Register_Index register_where_you_write_return_value; }; struct Bc{ U32 dis; // @debug_ids - U8 *stack_bottom; + U8 *stack_base; U8 *stack_pointer; U8 *stack_top; @@ -225,6 +224,8 @@ allocate_register(Bc *b){ function void release_register(Bc *b, Register_Index reg){ + if(reg == -1) return; + B32 found = false; For(b->used_registers){ if(it == reg){ @@ -306,6 +307,7 @@ emit_load_constant(Bc *b, Token *pos, Register_Index dst, Value value){ CASE_UINT: i->constant.u64 = bigint_as_unsigned(&value.big_int_val); break; CASE_SINT: i->constant.s64 = bigint_as_signed(&value.big_int_val); break; CASE_FLOAT: i->constant.f64 = value.f64_val; break; + case TYPE_TYPE: i->constant.u64 = value.type_val->type_id; break; invalid_default_case; } } @@ -313,6 +315,16 @@ emit_load_constant(Bc *b, Token *pos, Register_Index dst, Value value){ return i; } +function Instruction * +emit_load_constant_address(Bc *b, Token *pos, Register_Index dst, void *address){ + auto i = new_instruction(b, pos); + i->operation = BC_LOAD_CONSTANT; + i->index_c = dst; + i->constant.u64 = (U64)address; + i->debug_type_flag = TYPE_POINTER; + return i; +} + function void emit_push(Bc *b, Token *pos, Register_Index src){ auto i = new_instruction(b, pos); @@ -364,11 +376,11 @@ bc_stack_push_size(Bc *b, U64 size){ function Call_Frame * bc_push_call_frame(Bc *b, Register_Index function_address_register){ auto call = bc_stack_push(b, Call_Frame); - call->first_register_index_in_window = function_address_register + 1; - call->register_where_you_write_return_value = function_address_register; + call->first_register_index_in_window = function_address_register; call->previous_call = b->top_call; call->saved_instruction_pointer = 0; + b->stack_pointer = b->stack_base = (U8 *)(call+1); b->top_call = call; return call; } @@ -376,7 +388,7 @@ bc_push_call_frame(Bc *b, Register_Index function_address_register){ function void run_bytecode_interp(Bc *b){ b->instruction_pointer = (Instruction *)b->instructions.memory.data; - b->stack_pointer = b->stack_bottom = b->stack.memory.data; + b->stack_pointer = b->stack_base = b->stack.memory.data; b->stack_top = (b->stack.memory.data + b->stack.len); bc_push_call_frame(b, 0); @@ -386,7 +398,7 @@ run_bytecode_interp(Bc *b){ #if BC_ASSERTS print_token_line(instr->debug_pos); assert_msg(b->stack_pointer < b->stack_top, "Bytecode stack overflow"); - assert_msg(b->stack_pointer >= b->stack_bottom, "Bytecode stack underflow"); + assert_msg(b->stack_pointer >= b->stack_base, "Bytecode stack underflow"); assert_msg(b->top_call->first_register_index_in_window < b->registers.cap, "Bytecode interpreter bug, register pointer is pointing over last possible register"); #endif @@ -435,6 +447,8 @@ run_bytecode_interp(Bc *b){ Call_Frame *prev = call->previous_call; assert(prev); + b->stack_base = (U8 *)(prev + 1); + b->stack_pointer = (U8 *)call; b->top_call = prev; }break; @@ -456,7 +470,7 @@ run_bytecode_interp(Bc *b){ *store_address = R(instr->index_a).u64; bc_log("src[r%u] store_address[r%u, %llx] value_written[0x%llx|%lld|%f]", instr->index_a, instr->index_c, store_address, *store_address, *store_address, *store_address); }break; - + case BC_LOAD_FROM_MEMORY32:{ U32 *load_address = R(instr->index_a).pointer_u32; R(instr->index_c).u32 = *load_address; @@ -468,7 +482,7 @@ run_bytecode_interp(Bc *b){ *store_address = R(instr->index_a).u32; bc_log("src[r%u] store_address[r%u, %llx] value_written[0x%llx|%lld|%f]", instr->index_a, instr->index_c, store_address, *store_address, *store_address, *store_address); }break; - + case BC_LOAD_FROM_MEMORY16:{ U16 *load_address = R(instr->index_a).pointer_u16; R(instr->index_c).u16 = *load_address; @@ -480,7 +494,7 @@ run_bytecode_interp(Bc *b){ *store_address = R(instr->index_a).u16; bc_log("src[r%u] store_address[r%u, %llx] value_written[0x%llx|%lld|%f]", instr->index_a, instr->index_c, store_address, *store_address, *store_address, *store_address); }break; - + case BC_LOAD_FROM_MEMORY8:{ U8 *load_address = R(instr->index_a).pointer_u8; R(instr->index_c).u8 = *load_address; @@ -492,87 +506,87 @@ run_bytecode_interp(Bc *b){ *store_address = R(instr->index_a).u8; bc_log("src[r%u] store_address[r%u, %llx] value_written[0x%llx|%lld|%f]", instr->index_a, instr->index_c, store_address, *store_address, *store_address, *store_address); }break; - + case BC_ADD_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left + right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] + [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] + [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_SUB_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left - right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] - [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] - [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_DIV_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left / right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] / [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] / [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_MUL_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left * right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] * [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] * [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_MOD_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left % right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] % [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] % [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_SHR_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left >> right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] >> [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] >> [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_SHL_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left << right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] << [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] << [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITAND_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left & right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] & [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] & [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITOR_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left | right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] | [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] | [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITXOR_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left ^ right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] ^ [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] ^ [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITNOT_S64:{ S64 left = (S64)R(instr->index_a).s64; S64 result = ~left; @@ -580,143 +594,143 @@ run_bytecode_interp(Bc *b){ *dst = result; bc_log("~ [%lld] = [%lld]", left, result); }break; - + case BC_EQ_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left == right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] == [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] == [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_NEQ_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left != right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] != [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] != [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_GT_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left > right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] > [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] > [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_LT_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left < right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] < [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] < [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_OR_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left || right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] || [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] || [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_GTE_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left >= right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] >= [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] >= [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_LTE_S64:{ S64 left = R(instr->index_a).s64; S64 right = R(instr->index_b).s64; S64 result = left <= right; R(instr->index_c).s64 = result; - bc_log("[r%s, %lld] <= [r%s, %lld] = [r%s, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %lld] <= [r%d, %lld] = [r%d, %lld]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_ADD_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left + right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] + [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] + [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_SUB_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left - right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] - [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] - [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_DIV_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left / right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] / [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] / [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_MUL_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left * right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] * [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] * [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_MOD_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left % right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] % [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] % [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_SHR_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left >> right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] >> [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] >> [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_SHL_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left << right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] << [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] << [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITAND_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left & right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] & [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] & [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITOR_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left | right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] | [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] | [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITXOR_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left ^ right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] ^ [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] ^ [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_BITNOT_U64:{ U64 left = (U64)R(instr->index_a).u64; U64 result = ~left; @@ -724,143 +738,143 @@ run_bytecode_interp(Bc *b){ *dst = result; bc_log("~ [%llu] = [%llu]", left, result); }break; - + case BC_EQ_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left == right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] == [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] == [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_NEQ_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left != right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] != [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] != [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_GT_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left > right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] > [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] > [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_LT_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left < right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] < [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] < [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_OR_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left || right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] || [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] || [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_GTE_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left >= right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] >= [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] >= [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_LTE_U64:{ U64 left = R(instr->index_a).u64; U64 right = R(instr->index_b).u64; U64 result = left <= right; R(instr->index_c).u64 = result; - bc_log("[r%s, %llu] <= [r%s, %llu] = [r%s, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %llu] <= [r%d, %llu] = [r%d, %llu]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_ADD_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left + right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] + [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] + [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_SUB_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left - right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] - [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] - [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_DIV_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left / right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] / [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] / [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_MUL_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left * right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] * [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] * [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_EQ_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left == right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] == [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] == [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_NEQ_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left != right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] != [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] != [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_GT_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left > right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] > [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] > [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_LT_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left < right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] < [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] < [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_GTE_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left >= right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] >= [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] >= [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + case BC_LTE_F64:{ F64 left = R(instr->index_a).f64; F64 right = R(instr->index_b).f64; F64 result = left <= right; R(instr->index_c).f64 = result; - bc_log("[r%s, %f] <= [r%s, %f] = [r%s, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, %f] <= [r%d, %f] = [r%d, %f]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }break; - + // // *End* of switch_cases generated using code_generating_script.py // diff --git a/code_generating_script.py b/code_generating_script.py index 4b003ba..3c48319 100644 --- a/code_generating_script.py +++ b/code_generating_script.py @@ -145,7 +145,7 @@ if True: {T} right = R(instr->index_b).{t}; {T} result = left {symbol} right; R(instr->index_c).{t} = result; - bc_log("[r%s, {sign}] {symbol} [r%s, {sign}] = [r%s, {sign}]", instr->index_a, left, instr->index_b, right, instr->index_c, result); + bc_log("[r%d, {sign}] {symbol} [r%d, {sign}] = [r%d, {sign}]", instr->index_a, left, instr->index_b, right, instr->index_c, result); }}break; """ ################################ diff --git a/main.cpp b/main.cpp index d65c2cb..9e3596f 100644 --- a/main.cpp +++ b/main.cpp @@ -214,9 +214,9 @@ int main(int argument_count, char **arguments){ Ast_Module *module = add_module(0, pctx->intern("language.kl"_s)); insert_builtin_types_into_scope(module); pctx->language_base_module = module; + parse_all_modules(); resolve_everything_in_module(module); - // @note: language stuff needs to be declared before type_info data // so we mark where it ends pctx->base_language_ordered_decl_len = pctx->ordered_decls.len; @@ -225,6 +225,7 @@ int main(int argument_count, char **arguments){ type_any = any_decl->type_val; } + Ast_Module *module = add_module(0, pctx->intern(program_name)); parse_all_modules(); assert(module); diff --git a/programs/vm.kl b/programs/vm.kl index 2b5b066..9454fe1 100644 --- a/programs/vm.kl +++ b/programs/vm.kl @@ -2,4 +2,9 @@ global_variable := 10 + 29 main :: () - a := 10 + 20 // a := 10 + 20 \ No newline at end of file + a := 10 + 20 // a := 10 + 20 + b := a + 20 + return + +thing :: () + b := 20 \ No newline at end of file