diff --git a/bytecode_interpreter.cpp b/bytecode_interpreter.cpp index 4583e36..12df954 100644 --- a/bytecode_interpreter.cpp +++ b/bytecode_interpreter.cpp @@ -8,14 +8,15 @@ #endif // -// Generated using code_generating_script.py +// *Begin* of enum generated using code_generating_script.py // - -enum Operation: S32{ +enum Operation: U16{ BC_END_OF_INSTRUCTIONS, BC_POP_STACK, BC_PUSH_STACK, BC_LOAD_CONSTANT, + BC_STORE_CONSTANT, + BC_CALL, BC_LOAD_FROM_MEMORY64, BC_LOAD_FROM_MEMORY32, BC_LOAD_FROM_MEMORY16, @@ -76,6 +77,8 @@ const char *op_name[] = { "BC_POP_STACK", "BC_PUSH_STACK", "BC_LOAD_CONSTANT", + "BC_STORE_CONSTANT", + "BC_CALL", "BC_LOAD_FROM_MEMORY64", "BC_LOAD_FROM_MEMORY32", "BC_LOAD_FROM_MEMORY16", @@ -133,7 +136,7 @@ const char *op_name[] = { }; // -// **End** of generated using code_generating_script.py +// *End* of enum generated using code_generating_script.py // typedef S32 Register_Index; @@ -164,12 +167,15 @@ struct Instruction{ Token *debug_pos; }; -// -// Bytecode interpreter context -// +struct Call_Frame{ + Call_Frame *previous_call; + Register_Index first_register; +}; + struct Bc{ U32 dis; // @debug_id + Call_Frame *top_call; U64 *stack_bottom; U64 *stack_pointer; U64 *stack_top; @@ -257,6 +263,14 @@ new_instruction(Bc *b, Token *pos){ return i; } +// function void +// emit_call(Bc *b, Token *pos, Register_Index register_with_call_address, Register_Index register_with_last_argument){ +// auto i = new_instruction(b, pos); +// i->operation = BC_CALL; +// i->index_a = register_with_call_address; +// i->index_b = register_with_last_argument; +// } + function void emit_load_constant_f64(Bc *b, Token *pos, Register_Index dst, F64 constant){ auto i = new_instruction(b, pos); @@ -398,7 +412,7 @@ run_bytecode_interp(Bc *b){ }break; // -// Generated using code_generating_script.py +// *Begin* of switch_cases generated using code_generating_script.py // case BC_ADD_S64:{ @@ -406,7 +420,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left + right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] + [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] + [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_SUB_S64:{ @@ -414,7 +428,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left - right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] - [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] - [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_DIV_S64:{ @@ -422,7 +436,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left / right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] / [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] / [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_MUL_S64:{ @@ -430,7 +444,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left * right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] * [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] * [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_MOD_S64:{ @@ -438,7 +452,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left % right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] % [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] % [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_SHR_S64:{ @@ -446,7 +460,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left >> right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] >> [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] >> [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_SHL_S64:{ @@ -454,7 +468,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left << right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] << [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] << [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITAND_S64:{ @@ -462,7 +476,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left & right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] & [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] & [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITOR_S64:{ @@ -470,7 +484,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left | right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] | [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] | [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITXOR_S64:{ @@ -478,7 +492,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left ^ right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] ^ [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] ^ [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITNOT_S64:{ @@ -494,7 +508,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left == right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] == [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] == [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_NEQ_S64:{ @@ -502,7 +516,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left != right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] != [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] != [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_GT_S64:{ @@ -510,7 +524,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left > right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] > [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] > [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_LT_S64:{ @@ -518,7 +532,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left < right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] < [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] < [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_OR_S64:{ @@ -526,7 +540,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left || right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] || [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] || [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_GTE_S64:{ @@ -534,7 +548,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left >= right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] >= [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] >= [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_LTE_S64:{ @@ -542,7 +556,7 @@ run_bytecode_interp(Bc *b){ S64 right = b->registers[instr->index_b].s64; S64 result = left <= right; b->registers[instr->index_c].s64 = result; - bc_log("r%u + r%u = r%u => [%lld] <= [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%lld] <= [%lld] = [%lld]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_ADD_U64:{ @@ -550,7 +564,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left + right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] + [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] + [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_SUB_U64:{ @@ -558,7 +572,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left - right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] - [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] - [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_DIV_U64:{ @@ -566,7 +580,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left / right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] / [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] / [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_MUL_U64:{ @@ -574,7 +588,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left * right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] * [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] * [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_MOD_U64:{ @@ -582,7 +596,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left % right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] % [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] % [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_SHR_U64:{ @@ -590,7 +604,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left >> right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] >> [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] >> [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_SHL_U64:{ @@ -598,7 +612,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left << right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] << [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] << [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITAND_U64:{ @@ -606,7 +620,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left & right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] & [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] & [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITOR_U64:{ @@ -614,7 +628,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left | right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] | [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] | [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITXOR_U64:{ @@ -622,7 +636,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left ^ right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] ^ [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] ^ [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_BITNOT_U64:{ @@ -638,7 +652,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left == right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] == [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] == [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_NEQ_U64:{ @@ -646,7 +660,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left != right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] != [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] != [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_GT_U64:{ @@ -654,7 +668,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left > right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] > [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] > [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_LT_U64:{ @@ -662,7 +676,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left < right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] < [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] < [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_OR_U64:{ @@ -670,7 +684,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left || right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] || [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] || [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_GTE_U64:{ @@ -678,7 +692,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left >= right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] >= [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] >= [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_LTE_U64:{ @@ -686,7 +700,7 @@ run_bytecode_interp(Bc *b){ U64 right = b->registers[instr->index_b].u64; U64 result = left <= right; b->registers[instr->index_c].u64 = result; - bc_log("r%u + r%u = r%u => [%llu] <= [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%llu] <= [%llu] = [%llu]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_ADD_F64:{ @@ -694,7 +708,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left + right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] + [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] + [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_SUB_F64:{ @@ -702,7 +716,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left - right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] - [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] - [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_DIV_F64:{ @@ -710,7 +724,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left / right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] / [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] / [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_MUL_F64:{ @@ -718,7 +732,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left * right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] * [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] * [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_EQ_F64:{ @@ -726,7 +740,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left == right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] == [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] == [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_NEQ_F64:{ @@ -734,7 +748,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left != right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] != [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] != [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_GT_F64:{ @@ -742,7 +756,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left > right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] > [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] > [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_LT_F64:{ @@ -750,7 +764,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left < right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] < [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] < [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_GTE_F64:{ @@ -758,7 +772,7 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left >= right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] >= [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] >= [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; case BC_LTE_F64:{ @@ -766,15 +780,15 @@ run_bytecode_interp(Bc *b){ F64 right = b->registers[instr->index_b].f64; F64 result = left <= right; b->registers[instr->index_c].f64 = result; - bc_log("r%u + r%u = r%u => [%f] <= [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); + bc_log("r%s + r%s = r%s => [%f] <= [%f] = [%f]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }break; // -// **End** of generated using code_generating_script.py +// *End* of switch_cases generated using code_generating_script.py // } bc_log("\n"); } end_of_program:; -} +} \ No newline at end of file diff --git a/code_generating_script.py b/code_generating_script.py index 47056f7..2be1b8c 100644 --- a/code_generating_script.py +++ b/code_generating_script.py @@ -1,8 +1,4 @@ -result = """ -// -// Generated using code_generating_script.py -// -""" +result = "" sizes = ["64", "32", "16", "8"] types = ["S64", "U64", "F64"] @@ -20,10 +16,45 @@ def should_skip(T, op): and op != "GT" and op != "LT" and op != "GTE" and op != "LTE": return True +def update_file(filename, comment_name, data_to_write): + begin = f"""// +// *Begin* of {comment_name} generated using code_generating_script.py +// +""" + end = f""" +// +// *End* of {comment_name} generated using code_generating_script.py +//""" + + with open(filename, 'r+') as file: + data = file.read() + file.seek(0) + + begin_index = data.find(begin) + end_index = data.find(end) + if begin_index == -1 or end_index == -1: + print(f"Error: Couldn't find comment: {comment_name}") + exit(0) + + end_index += len(end) + + with open('backup', 'a') as backup: + backup.write(f"\n*** FILE = {filename} NAME = {comment_name} ***") + backup.write(f"\n*** FILE = {filename} NAME = {comment_name} ***") + backup.write(f"\n*** FILE = {filename} NAME = {comment_name} ***") + backup.write(data) + + a_part = data[0:begin_index] + b_part = data[begin_index:end_index] + c_part = data[end_index:-1] + + data_to_write = begin + data_to_write + end + + file.write(a_part + data_to_write + c_part) + file.truncate() + + -# -# Generate enum -# if False: enum_members = [] enum_members.append("BC_END_OF_INSTRUCTIONS") @@ -31,6 +62,7 @@ if False: enum_members.append("BC_PUSH_STACK") enum_members.append("BC_LOAD_CONSTANT") enum_members.append("BC_STORE_CONSTANT") + enum_members.append("BC_CALL") load_store = ["LOAD_FROM_MEMORY", "STORE_TO_MEMORY"] for op in load_store: @@ -42,7 +74,6 @@ if False: if should_skip(T, op): continue enum_members.append(f"BC_{op}_{T}") - result += "\n" result += "enum Operation: U16{\n" for i in enum_members: @@ -53,6 +84,7 @@ if False: for i in enum_members: result += f" \"{i}\",\n" result += "};\n" + update_file("bytecode_interpreter.cpp", "enum", result) # # Generate switch cases @@ -71,9 +103,9 @@ if True: if symbol == "~": result += f""" case BC_{op_name}_{T}:{{ - {T} left = ({T})b->registers[instr->a].{t}; + {T} left = ({T})b->registers[instr->index_a].{t}; {T} result = {symbol}left; - {T} *dst = b->registers[instr->dst].pointer_{t}; + {T} *dst = b->registers[instr->index_c].pointer_{t}; *dst = result; bc_log("{symbol} [{sign}] = [{sign}]", left, result); }}break; @@ -85,25 +117,21 @@ if True: # Binary operation result += f""" case BC_{op_name}_{T}:{{ - {T} left = b->registers[instr->a].{t}; - {T} right = b->registers[instr->b].{t}; + {T} left = b->registers[instr->index_a].{t}; + {T} right = b->registers[instr->index_b].{t}; {T} result = left {symbol} right; - b->registers[instr->dst].{t} = result; - bc_log("r%s + r%s = r%s => [{sign}] {symbol} [{sign}] = [{sign}]", instr->a, instr->b, instr->dst, left, right, result); + b->registers[instr->index_c].{t} = result; + bc_log("r%s + r%s = r%s => [{sign}] {symbol} [{sign}] = [{sign}]", instr->index_a, instr->index_b, instr->index_c, left, right, result); }}break; """ ################################ - -result += """ -// -// **End** of generated using code_generating_script.py -// -""" + update_file("bytecode_interpreter.cpp", "switch_cases", result) -# -# Copy to **WINDOWS** clipboard -# -import subprocess -subprocess.run("clip", universal_newlines=True, input=result) + +# # +# # Copy backup to **WINDOWS** clipboard +# # +# import subprocess +# subprocess.run("clip", universal_newlines=True, input=data)