Add operation works!

This commit is contained in:
Krzosa Karol
2022-06-23 12:48:40 +02:00
parent d33a18c8fe
commit 25fffae7e0
5 changed files with 171 additions and 146 deletions

View File

@@ -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<Incomplete_Instruction>(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);

View File

@@ -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;
@@ -498,7 +512,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -506,7 +520,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -514,7 +528,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -522,7 +536,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -530,7 +544,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -538,7 +552,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -546,7 +560,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -554,7 +568,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -562,7 +576,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -570,7 +584,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -586,7 +600,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -594,7 +608,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -602,7 +616,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -610,7 +624,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -618,7 +632,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -626,7 +640,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -634,7 +648,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -642,7 +656,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -650,7 +664,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -658,7 +672,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -666,7 +680,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -674,7 +688,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -682,7 +696,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -690,7 +704,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -698,7 +712,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -706,7 +720,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -714,7 +728,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -730,7 +744,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -738,7 +752,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -746,7 +760,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -754,7 +768,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -762,7 +776,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -770,7 +784,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -778,7 +792,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -786,7 +800,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -794,7 +808,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -802,7 +816,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -810,7 +824,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -818,7 +832,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -826,7 +840,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -834,7 +848,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -842,7 +856,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -850,7 +864,7 @@ run_bytecode_interp(Bc *b){
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:{
@@ -858,7 +872,7 @@ run_bytecode_interp(Bc *b){
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;
//

View File

@@ -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;
"""
################################

View File

@@ -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);

View File

@@ -3,3 +3,8 @@ global_variable := 10 + 29
main :: ()
a := 10 + 20 // a := 10 + 20
b := a + 20
return
thing :: ()
b := 20