Slowly trying to add function calls, cases for RETURN and CALL
This commit is contained in:
@@ -17,6 +17,7 @@ enum Operation: U16{
|
|||||||
BC_LOAD_CONSTANT,
|
BC_LOAD_CONSTANT,
|
||||||
BC_STORE_CONSTANT,
|
BC_STORE_CONSTANT,
|
||||||
BC_CALL,
|
BC_CALL,
|
||||||
|
BC_CALL_RETURN,
|
||||||
BC_LOAD_FROM_MEMORY64,
|
BC_LOAD_FROM_MEMORY64,
|
||||||
BC_LOAD_FROM_MEMORY32,
|
BC_LOAD_FROM_MEMORY32,
|
||||||
BC_LOAD_FROM_MEMORY16,
|
BC_LOAD_FROM_MEMORY16,
|
||||||
@@ -79,6 +80,7 @@ const char *op_name[] = {
|
|||||||
"BC_LOAD_CONSTANT",
|
"BC_LOAD_CONSTANT",
|
||||||
"BC_STORE_CONSTANT",
|
"BC_STORE_CONSTANT",
|
||||||
"BC_CALL",
|
"BC_CALL",
|
||||||
|
"BC_CALL_RETURN",
|
||||||
"BC_LOAD_FROM_MEMORY64",
|
"BC_LOAD_FROM_MEMORY64",
|
||||||
"BC_LOAD_FROM_MEMORY32",
|
"BC_LOAD_FROM_MEMORY32",
|
||||||
"BC_LOAD_FROM_MEMORY16",
|
"BC_LOAD_FROM_MEMORY16",
|
||||||
@@ -181,18 +183,20 @@ struct Instruction{
|
|||||||
|
|
||||||
struct Call_Frame{
|
struct Call_Frame{
|
||||||
Call_Frame *previous_call;
|
Call_Frame *previous_call;
|
||||||
Register_Index first_register;
|
Instruction *saved_instruction_pointer;
|
||||||
|
Register_Index first_register_index_in_window;
|
||||||
|
Register_Index register_where_you_write_return_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Bc{
|
struct Bc{
|
||||||
U32 dis; // @debug_ids
|
U32 dis; // @debug_ids
|
||||||
|
|
||||||
U64 *stack_bottom;
|
U8 *stack_bottom;
|
||||||
U64 *stack_pointer;
|
U8 *stack_pointer;
|
||||||
U64 *stack_top;
|
U8 *stack_top;
|
||||||
|
|
||||||
Call_Frame *top_call;
|
Call_Frame *top_call;
|
||||||
Register_Index first_register_index_in_window;
|
|
||||||
Array<Register> registers;
|
Array<Register> registers;
|
||||||
Array<Register_Index> used_registers;
|
Array<Register_Index> used_registers;
|
||||||
Array<Register_Index> free_registers;
|
Array<Register_Index> free_registers;
|
||||||
@@ -274,13 +278,13 @@ new_instruction(Bc *b, Token *pos){
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
// function void
|
function void
|
||||||
// emit_call(Bc *b, Token *pos, Register_Index register_with_call_address, Register_Index register_with_last_argument){
|
emit_call(Bc *b, Token *pos, Register_Index register_with_call_address, Register_Index register_with_last_argument){
|
||||||
// auto i = new_instruction(b, pos);
|
auto i = new_instruction(b, pos);
|
||||||
// i->operation = BC_CALL;
|
i->operation = BC_CALL;
|
||||||
// i->index_a = register_with_call_address;
|
i->index_a = register_with_call_address;
|
||||||
// i->index_b = register_with_last_argument;
|
i->index_b = register_with_last_argument;
|
||||||
// }
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
emit_load_constant_f64(Bc *b, Token *pos, Register_Index dst, F64 constant){
|
emit_load_constant_f64(Bc *b, Token *pos, Register_Index dst, F64 constant){
|
||||||
@@ -356,15 +360,36 @@ emit_end(Bc *b){
|
|||||||
i->operation = BC_END_OF_INSTRUCTIONS;
|
i->operation = BC_END_OF_INSTRUCTIONS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define R(x) (b->registers[b->top_call->first_register_index_in_window + (x)]) // Get register
|
||||||
|
#define bc_stack_push(b, T) (T *)bc__stack_push(b, sizeof(T))
|
||||||
|
|
||||||
|
function U8 *
|
||||||
|
bc__stack_push(Bc *b, U64 size){
|
||||||
|
U8 *result = (U8 *)b->stack_pointer;
|
||||||
|
b->stack_pointer += size;
|
||||||
|
assert(b->stack_pointer < b->stack_top);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
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->previous_call = b->top_call;
|
||||||
|
call->saved_instruction_pointer = 0;
|
||||||
|
|
||||||
|
b->top_call = call;
|
||||||
|
return call;
|
||||||
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
run_bytecode_interp(Bc *b){
|
run_bytecode_interp(Bc *b){
|
||||||
// Setup registers
|
|
||||||
b->instruction_pointer = (Instruction *)b->instructions.memory.data;
|
b->instruction_pointer = (Instruction *)b->instructions.memory.data;
|
||||||
b->stack_pointer = b->stack_bottom = (U64 *)b->stack.memory.data;
|
b->stack_pointer = b->stack_bottom = b->stack.memory.data;
|
||||||
b->stack_top = (U64 *)(b->stack.memory.data + b->stack.len);
|
b->stack_top = (b->stack.memory.data + b->stack.len);
|
||||||
b->first_register_index_in_window = 0;
|
bc_push_call_frame(b, 0);
|
||||||
|
|
||||||
#define R(x) (b->registers[b->first_register_index_in_window + (x)]) // Get register
|
|
||||||
for(;;){
|
for(;;){
|
||||||
Instruction *instr = b->instruction_pointer++;
|
Instruction *instr = b->instruction_pointer++;
|
||||||
|
|
||||||
@@ -372,7 +397,7 @@ run_bytecode_interp(Bc *b){
|
|||||||
print_token_line(instr->debug_pos);
|
print_token_line(instr->debug_pos);
|
||||||
assert_msg(b->stack_pointer < b->stack_top, "Bytecode stack overflow");
|
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_bottom, "Bytecode stack underflow");
|
||||||
assert_msg(b->first_register_index_in_window < b->registers.cap, "Bytecode interpreter bug, register pointer is pointing over last possible register");
|
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
|
#endif
|
||||||
|
|
||||||
bc_log("i%u[0x%llx] %s ", instr->di, instr, op_name[instr->operation]);
|
bc_log("i%u[0x%llx] %s ", instr->di, instr, op_name[instr->operation]);
|
||||||
@@ -380,7 +405,8 @@ run_bytecode_interp(Bc *b){
|
|||||||
invalid_default_case;
|
invalid_default_case;
|
||||||
|
|
||||||
case BC_PUSH_STACK:{
|
case BC_PUSH_STACK:{
|
||||||
U64 *stack = b->stack_pointer++;
|
U64 *stack = (U64 *)b->stack_pointer;
|
||||||
|
b->stack_pointer += sizeof(U64);
|
||||||
|
|
||||||
U64 src = R(instr->index_a).u64;
|
U64 src = R(instr->index_a).u64;
|
||||||
bc_log("r%u(src) [0x%llx|%lld|%f]", instr->index_a, src, src, src);
|
bc_log("r%u(src) [0x%llx|%lld|%f]", instr->index_a, src, src, src);
|
||||||
@@ -388,15 +414,12 @@ run_bytecode_interp(Bc *b){
|
|||||||
}break;
|
}break;
|
||||||
|
|
||||||
case BC_POP_STACK:{
|
case BC_POP_STACK:{
|
||||||
U64 *stack = --b->stack_pointer;
|
b->stack_pointer -= sizeof(U64);
|
||||||
|
U64 *stack = (U64 *)b->stack_pointer;
|
||||||
bc_log("r%u(dst) [0x%llx|%lld|%f]", instr->index_c, *stack, *stack, *stack);
|
bc_log("r%u(dst) [0x%llx|%lld|%f]", instr->index_c, *stack, *stack, *stack);
|
||||||
R(instr->index_c).u64 = *stack;
|
R(instr->index_c).u64 = *stack;
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case BC_END_OF_INSTRUCTIONS:{
|
|
||||||
goto end_of_program;
|
|
||||||
}break;
|
|
||||||
|
|
||||||
case BC_LOAD_CONSTANT: {
|
case BC_LOAD_CONSTANT: {
|
||||||
R(instr->index_c) = instr->constant;
|
R(instr->index_c) = instr->constant;
|
||||||
#if BC_LOG
|
#if BC_LOG
|
||||||
@@ -410,6 +433,25 @@ run_bytecode_interp(Bc *b){
|
|||||||
#endif
|
#endif
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
|
case BC_CALL:{
|
||||||
|
Register_Index register_with_call_address = instr->index_a;
|
||||||
|
// Register_Index register_with_last_argument = instr->index_b;
|
||||||
|
Call_Frame *call = bc_push_call_frame(b, register_with_call_address);
|
||||||
|
call->saved_instruction_pointer = b->instruction_pointer;
|
||||||
|
b->instruction_pointer = (Instruction *)R(register_with_call_address).pointer;
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case BC_CALL_RETURN:{
|
||||||
|
Call_Frame *call = b->top_call;
|
||||||
|
Call_Frame *prev = call->previous_call;
|
||||||
|
assert(prev);
|
||||||
|
|
||||||
|
b->top_call = prev;
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case BC_END_OF_INSTRUCTIONS:{
|
||||||
|
goto end_of_program;
|
||||||
|
}break;
|
||||||
//
|
//
|
||||||
// *Begin* of switch_cases generated using code_generating_script.py
|
// *Begin* of switch_cases generated using code_generating_script.py
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ def should_skip(T, op):
|
|||||||
and op != "GT" and op != "LT" and op != "GTE" and op != "LTE":
|
and op != "GT" and op != "LT" and op != "GTE" and op != "LTE":
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
# This function inserts generated code into the specified file
|
||||||
|
# it requires a specific comment to be triggered.
|
||||||
|
# Lossing data would be terrible so we also backup that file
|
||||||
|
# in case something bad happens
|
||||||
def update_file(filename, comment_name, data_to_write):
|
def update_file(filename, comment_name, data_to_write):
|
||||||
begin = f"""//
|
begin = f"""//
|
||||||
// *Begin* of {comment_name} generated using code_generating_script.py
|
// *Begin* of {comment_name} generated using code_generating_script.py
|
||||||
@@ -55,13 +59,15 @@ def update_file(filename, comment_name, data_to_write):
|
|||||||
|
|
||||||
if True:
|
if True:
|
||||||
result = ""
|
result = ""
|
||||||
enum_members = []
|
enum_members = [
|
||||||
enum_members.append("BC_END_OF_INSTRUCTIONS")
|
"BC_END_OF_INSTRUCTIONS",
|
||||||
enum_members.append("BC_POP_STACK")
|
"BC_POP_STACK",
|
||||||
enum_members.append("BC_PUSH_STACK")
|
"BC_PUSH_STACK",
|
||||||
enum_members.append("BC_LOAD_CONSTANT")
|
"BC_LOAD_CONSTANT",
|
||||||
enum_members.append("BC_STORE_CONSTANT")
|
"BC_STORE_CONSTANT",
|
||||||
enum_members.append("BC_CALL")
|
"BC_CALL",
|
||||||
|
"BC_CALL_RETURN",
|
||||||
|
]
|
||||||
|
|
||||||
load_store = ["LOAD_FROM_MEMORY", "STORE_TO_MEMORY"]
|
load_store = ["LOAD_FROM_MEMORY", "STORE_TO_MEMORY"]
|
||||||
for op in load_store:
|
for op in load_store:
|
||||||
|
|||||||
Reference in New Issue
Block a user