Change error printing to use colors, enable colors on windows cmd, print bytecode instruction line

This commit is contained in:
Krzosa Karol
2022-06-22 18:14:43 +02:00
parent cd48253e3e
commit a36747bc9c
4 changed files with 128 additions and 84 deletions

View File

@@ -1,4 +1,6 @@
#define BC_LOG 1
#define BC_ASSERTS 1
#if BC_LOG
#define bc_log(...) log_info(__VA_ARGS__)
#else
@@ -159,6 +161,7 @@ struct Instruction{
U32 di; // @debug_id
U32 debug_type_flag;
Token *debug_pos;
};
//
@@ -169,8 +172,13 @@ struct Bc{
U64 *stack_bottom;
U64 *stack_pointer;
Instruction *instruction_pointer;
Array<Register> registers;
U64 *stack_top;
Instruction *instruction_pointer; // @todo Array can change location !!!!!!
Register *registers;
Register *register_end;
Array<Register> all_registers;
Array<Register_Index> used_registers;
Array<Register_Index> free_registers;
@@ -187,7 +195,7 @@ function Register_Index
allocate_register(Bc *b){
Register_Index result = -1;
if(b->free_registers.len == 0)
result = b->registers.addi({});
result = b->all_registers.addi({});
else
result = b->free_registers.pop();
@@ -214,7 +222,7 @@ function Bc
create_bytecode_interp(){
Bc b = {};
b.registers = array_make<Register>(pctx->heap, 1024);
b.all_registers = array_make<Register>(pctx->heap, 1024);
b.free_registers = array_make<Register_Index>(pctx->heap, 1024);
b.used_registers = array_make<Register_Index>(pctx->heap, 1024);
@@ -242,84 +250,85 @@ destroy_bytecode_interp(Bc *b){
}
function Instruction *
new_instruction(Bc *b){
Instruction *i = exp_alloc_type(&b->instructions, Instruction);
i->di = b->dis++;
new_instruction(Bc *b, Token *pos){
Instruction *i = exp_alloc_type(&b->instructions, Instruction);
i->di = b->dis++;
i->debug_pos = pos;
return i;
}
function void
emit_load_constant_f64(Bc *b, Register_Index dst, F64 constant){
auto i = new_instruction(b);
i->operation = BC_LOAD_CONSTANT;
i->constant.f64 = constant;
i->index_c = dst;
i->debug_type_flag = TYPE_F64;
emit_load_constant_f64(Bc *b, Token *pos, Register_Index dst, F64 constant){
auto i = new_instruction(b, pos);
i->operation = BC_LOAD_CONSTANT;
i->constant.f64 = constant;
i->index_c = dst;
i->debug_type_flag = TYPE_F64;
}
function void
emit_load_constant_s64(Bc *b, Register_Index dst, S64 constant){
auto i = new_instruction(b);
i->operation = BC_LOAD_CONSTANT;
i->constant.s64 = constant;
i->index_c = dst;
i->debug_type_flag = TYPE_S64;
emit_load_constant_s64(Bc *b, Token *pos, Register_Index dst, S64 constant){
auto i = new_instruction(b, pos);
i->operation = BC_LOAD_CONSTANT;
i->constant.s64 = constant;
i->index_c = dst;
i->debug_type_flag = TYPE_S64;
}
function void
emit_load_constant_u64(Bc *b, Register_Index dst, U64 constant){
auto i = new_instruction(b);
i->operation = BC_LOAD_CONSTANT;
i->constant.u64 = constant;
i->index_c = dst;
i->debug_type_flag = TYPE_U64;
emit_load_constant_u64(Bc *b, Token *pos, Register_Index dst, U64 constant){
auto i = new_instruction(b, pos);
i->operation = BC_LOAD_CONSTANT;
i->constant.u64 = constant;
i->index_c = dst;
i->debug_type_flag = TYPE_U64;
}
function void
emit_load_constant_address(Bc *b, Register_Index dst, void *address){
auto i = new_instruction(b);
i->operation = BC_LOAD_CONSTANT;
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->constant.pointer = (U8 *)address;
i->index_c = dst;
i->debug_type_flag = TYPE_POINTER;
i->index_c = dst;
i->debug_type_flag = TYPE_POINTER;
}
function void
emit_push(Bc *b, Register_Index src){
auto i = new_instruction(b);
i->operation = BC_PUSH_STACK;
i->index_a = src;
emit_push(Bc *b, Token *pos, Register_Index src){
auto i = new_instruction(b, pos);
i->operation = BC_PUSH_STACK;
i->index_a = src;
}
function void
emit_pop(Bc *b, Register_Index dst){
auto i = new_instruction(b);
i->operation = BC_POP_STACK;
i->index_c = dst;
emit_pop(Bc *b, Token *pos, Register_Index dst){
auto i = new_instruction(b, pos);
i->operation = BC_POP_STACK;
i->index_c = dst;
}
function void
emit_memory(Bc *b, Operation ins, Register_Index dst, Register_Index src){
auto i = new_instruction(b);
i->operation = ins;
i->index_c = dst;
i->index_a = src;
emit_memory(Bc *b, Token *pos, Operation ins, Register_Index dst, Register_Index src){
auto i = new_instruction(b, pos);
i->operation = ins;
i->index_c = dst;
i->index_a = src;
}
function void
emit_arithmetic(Bc *b, Operation ins, Register_Index left, Register_Index right, Register_Index dst){
Instruction *i = new_instruction(b);
i->operation = ins;
i->index_a = left;
i->index_b = right;
i->index_c = dst;
emit_arithmetic(Bc *b, Token *pos, Operation ins, Register_Index left, Register_Index right, Register_Index dst){
Instruction *i = new_instruction(b, pos);
i->operation = ins;
i->index_a = left;
i->index_b = right;
i->index_c = dst;
}
function void
emit_end(Bc *b){
Instruction *i = exp_alloc_type(&b->instructions, Instruction);
i->di = b->dis++;
i->operation = BC_END_OF_INSTRUCTIONS;
Instruction *i = new_instruction(b, 0);
i->di = b->dis++;
i->operation = BC_END_OF_INSTRUCTIONS;
}
function void
@@ -327,13 +336,24 @@ run_bytecode_interp(Bc *b){
// Setup registers
b->instruction_pointer = (Instruction *)b->instructions.memory.data;
b->stack_pointer = b->stack_bottom = (U64 *)b->stack.memory.data;
b->stack_top = (U64 *)(b->stack.memory.data + b->stack.len);
b->registers = &b->all_registers[0];
b->register_end = b->all_registers.end();
for(;;){
Instruction *instr = b->instruction_pointer++;
bc_log("i%u[0x%llx] %s ", instr->di, instr, op_name[instr->operation]);
#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->registers < b->register_end, "Bytecode interpreter bug, register pointer is pointing over last possible register");
#endif
bc_log("i%u[0x%llx] %s ", instr->di, instr, op_name[instr->operation]);
switch(instr->operation){
default:{}
invalid_default_case;
case BC_LOAD_FROM_MEMORY64:{
U64 *load_address = b->registers[instr->index_a].pointer64;
b->registers[instr->index_c].u64 = *load_address;
@@ -758,16 +778,3 @@ run_bytecode_interp(Bc *b){
}
end_of_program:;
}
function void
test_interpreter(){
Bc b = create_bytecode_interp();
emit_load_constant_f64(&b, 3, 100.32);
emit_load_constant_f64(&b, 4, 200.68);
emit_arithmetic(&b, BC_ADD_F64, 3, 4, 3);
emit_push(&b, 3);
emit_pop(&b, 3);
emit_end(&b);
run_bytecode_interp(&b);
destroy_bytecode_interp(&b);
}