Calling main

This commit is contained in:
Krzosa Karol
2022-06-23 12:09:23 +02:00
parent bcfd586552
commit d33a18c8fe
6 changed files with 107 additions and 50 deletions

View File

@@ -141,7 +141,6 @@ const char *op_name[] = {
// *End* of enum generated using code_generating_script.py
//
typedef S32 Register_Index;
union Register{
F64 f64;
S64 s64;
@@ -272,54 +271,46 @@ destroy_bytecode_interp(Bc *b){
function Instruction *
new_instruction(Bc *b, Token *pos){
Instruction *i = exp_alloc_type(&b->instructions, Instruction);
Instruction *i = exp_alloc_type(&b->instructions, Instruction);
b->instruction_pointer = i + 1;
i->di = b->dis++;
i->debug_pos = pos;
return i;
}
function void
emit_call(Bc *b, Token *pos, Register_Index register_with_call_address, Register_Index register_with_last_argument){
function Instruction *
emit_call(Bc *b, Token *pos, Register_Index register_with_call_address){
auto i = new_instruction(b, pos);
i->operation = BC_CALL;
i->index_a = register_with_call_address;
i->index_b = register_with_last_argument;
return i;
}
function void
emit_load_constant_f64(Bc *b, Token *pos, Register_Index dst, F64 constant){
emit_return(Bc *b, Token *pos){
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;
i->operation = BC_CALL_RETURN;
}
function void
emit_load_constant_s64(Bc *b, Token *pos, Register_Index dst, S64 constant){
function Instruction *
emit_load_constant(Bc *b, Token *pos, Register_Index dst, Value value){
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, 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;
}
if(value.type){
i->debug_type_flag = value.type->kind;
switch(value.type->kind){
case TYPE_POINTER:
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;
invalid_default_case;
}
}
function void
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;
return i;
}
function void
@@ -356,15 +347,14 @@ emit_arithmetic(Bc *b, Token *pos, Operation ins, Register_Index left, Register_
function void
emit_end(Bc *b){
Instruction *i = new_instruction(b, 0);
i->di = b->dis++;
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))
#define bc_stack_push(b, T) (T *)bc_stack_push_size(b, sizeof(T))
function U8 *
bc__stack_push(Bc *b, U64 size){
bc_stack_push_size(Bc *b, U64 size){
U8 *result = (U8 *)b->stack_pointer;
b->stack_pointer += size;
assert(b->stack_pointer < b->stack_top);
@@ -435,7 +425,6 @@ run_bytecode_interp(Bc *b){
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;