#include "../executor.h" #include "../../include/std/alloc.h" #include "../../include/std/buffer.h" #include "../../include/std/log.h" #include "asm.h" #include /* TODO: Currently almost all operations are performed on memory. This should be optimized to take advantage of registers. TODO: Operations with memory registers could access outside the stack. Should this be checked? */ /* TODO: Allow this to dynamically change up to 1<<16, to match jump instruction. This is a sane limit for now */ #define MAX_LABELS 128 typedef struct { u32 asm_index; u16 target_label; bool condition; } JumpDefer; typedef struct { Asm asm; u16 func_counter; Buffer/*JumpDefer*/ jump_defer; u32 label_asm_index[MAX_LABELS]; int label_counter; int num_args; int num_pushed_values; } amal_executor_impl; #define ASM_ENSURE_CAPACITY return_if_error(asm_ensure_capacity(&impl->asm, 256)); #define IMPL_START \ amal_executor_impl *impl = (amal_executor_impl*)self; \ ASM_ENSURE_CAPACITY /* @reg will be a positive value when accessing local variables, in which case the first local variable is located at -sizeof(usize) and the next one is at -(2 * sizeof(usize)). @reg will be a negative value starting at -1 when accessing parameters. The first parameter is located at 3*sizeof(usize) and the next one is at 4*sizeof(usize). Parameter starts at 3*sizeof(usize) because offset 0 is the return address, offset 1*sizeof(usize) is the saved RBP and 2*sizeof(usize) is saved RBX. TODO: Use different offset when saving more registers, for example on Microsoft Windows. */ #define get_register_stack_offset(reg) \ (reg >= 0 ? (i32)(-reg * (int)sizeof(usize) - sizeof(usize)) : (i32)(-reg * (int)sizeof(usize) + 2*sizeof(usize))) static i64 abs_i64(i64 value) { return value >= 0 ? value : -value; } const Reg64 SYS_V_REG_PARAMS[] = { RDI, RSI, RDX, RCX, R8, R9, R10, R11 }; const int NUM_REG_PARAMS = 8; int amal_executor_init(amal_executor **self) { amal_executor_impl **impl; impl = (amal_executor_impl**)self; *impl = NULL; return_if_error(am_malloc(sizeof(amal_executor_impl), (void**)impl)); (*impl)->func_counter = 0; (*impl)->label_counter = 0; (*impl)->num_args = 0; (*impl)->num_pushed_values = 0; ignore_result_int(buffer_init(&(*impl)->jump_defer, NULL)); return asm_init(&(*impl)->asm); } void amal_executor_deinit(amal_executor *self) { amal_executor_impl *impl = (amal_executor_impl*)self; buffer_deinit(&impl->jump_defer); asm_deinit(&impl->asm); am_free(impl); } int amal_executor_run(amal_executor *self, u32 offset) { amal_executor_impl *impl = (amal_executor_impl*)self; return asm_execute(&impl->asm, offset); } u32 amal_exec_get_code_offset(amal_executor *self) { amal_executor_impl *impl = (amal_executor_impl*)self; return asm_get_size(&impl->asm); } int amal_executor_instructions_start(amal_executor *self, u16 num_functions) { (void)self; (void)num_functions; return 0; } int amal_executor_instructions_end(amal_executor *self) { amal_executor_impl *impl = (amal_executor_impl*)self; impl->func_counter = 0; return 0; } int amal_exec_nop(amal_executor *self) { IMPL_START asm_nop(&impl->asm); return 0; } int amal_exec_setz(amal_executor *self, i8 dst_reg) { AsmPtr dst; IMPL_START asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_mov_mi(&impl->asm, &dst, 0); return 0; } int amal_exec_mov(amal_executor *self, i8 dst_reg, i8 src_reg) { AsmPtr ptr; IMPL_START asm_ptr_init_disp(&ptr, RBP, get_register_stack_offset(src_reg)); asm_mov_rm(&impl->asm, RAX, &ptr); asm_ptr_init_disp(&ptr, RBP, get_register_stack_offset(dst_reg)); asm_mov_mr(&impl->asm, &ptr, RAX); return 0; } int amal_exec_movi(amal_executor *self, i8 dst_reg, i64 imm) { IMPL_START /* TODO: if @number is a float then use float instructions */ if(abs_i64(imm) <= INT32_MAX) { AsmPtr dst; asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_mov_mi(&impl->asm, &dst, imm); } else { AsmPtr dst; asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_mov_ri(&impl->asm, RAX, imm); asm_mov_mr(&impl->asm, &dst, RAX); } return 0; } int amal_exec_movd(amal_executor *self, i8 dst_reg, BufferView data) { AsmPtr dst; IMPL_START asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_mov_ri(&impl->asm, RAX, (uintptr_t)data.data); asm_mov_mr(&impl->asm, &dst, RAX); return 0; } int amal_exec_add(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) { AsmPtr dst; AsmPtr reg1; AsmPtr reg2; IMPL_START asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_ptr_init_disp(®1, RBP, get_register_stack_offset(src_reg1)); asm_ptr_init_disp(®2, RBP, get_register_stack_offset(src_reg2)); asm_mov_rm(&impl->asm, RAX, ®1); asm_mov_rm(&impl->asm, RCX, ®2); asm_add_rr(&impl->asm, RAX, RCX); asm_mov_mr(&impl->asm, &dst, RAX); return 0; } int amal_exec_sub(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) { AsmPtr dst; AsmPtr reg1; AsmPtr reg2; IMPL_START asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_ptr_init_disp(®1, RBP, get_register_stack_offset(src_reg1)); asm_ptr_init_disp(®2, RBP, get_register_stack_offset(src_reg2)); asm_mov_rm(&impl->asm, RAX, ®1); asm_mov_rm(&impl->asm, RCX, ®2); asm_sub_rr(&impl->asm, RAX, RCX); asm_mov_mr(&impl->asm, &dst, RAX); return 0; } int amal_exec_imul(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) { AsmPtr dst; AsmPtr reg1; AsmPtr reg2; IMPL_START asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_ptr_init_disp(®1, RBP, get_register_stack_offset(src_reg1)); asm_ptr_init_disp(®2, RBP, get_register_stack_offset(src_reg2)); asm_mov_rm(&impl->asm, RAX, ®1); asm_mov_rm(&impl->asm, RCX, ®2); asm_imul_rr(&impl->asm, RAX, RCX); asm_mov_mr(&impl->asm, &dst, RAX); return 0; } int amal_exec_mul(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) { (void)self; (void)dst_reg; (void)src_reg1; (void)src_reg2; /* TODO: Implement! */ #if 0 AsmPtr dst; AsmPtr reg1; AsmPtr reg2; asm_ptr_init_disp(&dst, RBP, -(i32)get_register_at_offset(0)); asm_ptr_init_disp(®1, RBP, -(i32)get_register_at_offset(1)); asm_ptr_init_disp(®2, RBP, -(i32)get_register_at_offset(2)); return_if_error(asm_mov_rm(&self->asm, RAX, ®1)); return_if_error(asm_mov_rm(&self->asm, RCX, ®2)); return_if_error(asm_mul_rr(&self->asm, RAX, RCX)); return_if_error(asm_mov_mr(&self->asm, &dst, RAX)); #endif assert(bool_false && "TODO: Implement!"); return 0; } int amal_exec_idiv(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) { AsmPtr dst; AsmPtr reg1; AsmPtr reg2; IMPL_START asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_ptr_init_disp(®1, RBP, get_register_stack_offset(src_reg1)); asm_ptr_init_disp(®2, RBP, get_register_stack_offset(src_reg2)); asm_mov_rm(&impl->asm, RAX, ®1); asm_mov_rm(&impl->asm, RCX, ®2); asm_cqo(&impl->asm); asm_idiv_rr(&impl->asm, RCX); asm_mov_mr(&impl->asm, &dst, RAX); return 0; } int amal_exec_div(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) { (void)self; (void)dst_reg; (void)src_reg1; (void)src_reg2; /* TODO: Implement! */ assert(bool_false && "TODO: Implement!"); return 0; } int amal_exec_push(amal_executor *self, i8 reg) { AsmPtr reg_ptr; IMPL_START asm_ptr_init_disp(®_ptr, RBP, get_register_stack_offset(reg)); if(impl->num_pushed_values < NUM_REG_PARAMS) { asm_mov_rm(&impl->asm, SYS_V_REG_PARAMS[impl->num_pushed_values], ®_ptr); } else { asm_mov_rm(&impl->asm, RAX, ®_ptr); asm_pushr(&impl->asm, RAX); } ++impl->num_pushed_values; return 0; } int amal_exec_pushi(amal_executor *self, i64 imm) { (void)self; (void)imm; /* TODO: Implement! */ assert(bool_false && "TODO: Implement!"); return 0; } int amal_exec_pushd(amal_executor *self, BufferView data) { (void)self; (void)data; /* TODO: Implement! */ assert(bool_false && "TODO: Implement!"); return 0; } int amal_exec_call_start(amal_executor *self, u8 num_args) { amal_executor_impl *impl = (amal_executor_impl*)self; impl->num_args = num_args; return 0; } int amal_exec_call(amal_executor *self, u32 code_offset, i8 dst_reg) { amal_executor_impl *impl = (amal_executor_impl*)self; /* TODO: Preserve necessary registers before call? */ /* TODO: This assumes all arguments are isize */ /* Do the function call */ isize asm_offset = asm_get_size(&impl->asm); int num_pushed_stack = impl->num_pushed_values - (int)NUM_REG_PARAMS; ASM_ENSURE_CAPACITY assert((num_pushed_stack <= 0 || num_pushed_stack % 2 == 0) && "TODO: Align stack to 16-bytes before calling functions"); assert(code_offset < asm_offset); asm_call_rel32(&impl->asm, (isize)code_offset - asm_offset); /* Handle function result and cleanup */ { AsmPtr dst; asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); /* TODO: Make this work when result is not stored in RAX (multiple return results) */ asm_mov_mr(&impl->asm, &dst, RAX); } if(num_pushed_stack > 0) asm_add_rm64_imm(&impl->asm, RSP, num_pushed_stack * sizeof(isize)); impl->num_pushed_values = 0; return 0; } void amal_exec_call_overwrite(amal_executor *self, u32 call_code_offset, i32 new_target_rel32) { amal_executor_impl *impl = (amal_executor_impl*)self; asm_overwrite_call_rel32(&impl->asm, call_code_offset, new_target_rel32); } /* TODO: Make argument passing work for different calling conventions and different ABI. This currently assumes x86_64 system v abi. System-V ABI parameters: RDI, RSI, RDX, RCX, R8, R9, XMM0–7. The rest are passed in the stack. */ /* TODO: Make this work when function returns something else than a POD */ int amal_exec_calle(amal_executor *self, void *func, i8 dst_reg) { AsmPtr dst; amal_executor_impl *impl = (amal_executor_impl*)self; int num_pushed_stack = impl->num_pushed_values - (int)NUM_REG_PARAMS; ASM_ENSURE_CAPACITY assert((num_pushed_stack <= 0 || num_pushed_stack % 2 == 0) && "TODO: Align stack to 16-bytes before calling functions"); /* TODO: Preserve necessary registers before call? */ /* TODO: This assumes all arguments are isize */ asm_mov_ri(&impl->asm, RAX, (intptr_t)func); asm_callr(&impl->asm, RAX); asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_mov_mr(&impl->asm, &dst, RAX); if(num_pushed_stack > 0) asm_add_rm64_imm(&impl->asm, RSP, num_pushed_stack * sizeof(isize)); impl->num_pushed_values = 0; return 0; } /* int amal_exec_callr(i8 dst_reg, BufferView data) { } */ int amal_exec_cmp(amal_executor *self, i8 dst_reg, i8 src_reg1, i8 src_reg2) { AsmPtr dst, src1, src2; IMPL_START asm_ptr_init_disp(&dst, RBP, get_register_stack_offset(dst_reg)); asm_ptr_init_disp(&src1, RBP, get_register_stack_offset(src_reg1)); asm_ptr_init_disp(&src2, RBP, get_register_stack_offset(src_reg2)); asm_mov_rm(&impl->asm, RCX, &dst); asm_xor_rm64(&impl->asm, RCX, RCX); asm_mov_rm(&impl->asm, RAX, &src1); asm_cmp_rm(&impl->asm, RAX, &src2); asm_sete_r(&impl->asm, RCX); asm_mov_mr(&impl->asm, &dst, RCX); return 0; } int amal_exec_jz(amal_executor *self, i8 reg, u16 target_label) { AsmPtr ptr; u32 asm_offset; IMPL_START asm_ptr_init_disp(&ptr, RBP, get_register_stack_offset(reg)); asm_mov_rm(&impl->asm, RAX, &ptr); asm_cmp_rm64_imm(&impl->asm, RAX, 0); asm_offset = asm_get_size(&impl->asm); if(target_label < impl->label_counter) { asm_jz(&impl->asm, (i32)impl->label_asm_index[target_label] - (i32)asm_offset); return 0; } else { JumpDefer jump_defer; jump_defer.asm_index = asm_offset; jump_defer.target_label = target_label; jump_defer.condition = bool_true; /* Insert dummy target, but it has to be above INT16_MAX, so the target can be replaced no matter how large the jump will be */ asm_jz(&impl->asm, INT32_MAX); return buffer_append(&impl->jump_defer, &jump_defer, sizeof(jump_defer)); } } int amal_exec_jmp(amal_executor *self, u16 target_label) { amal_executor_impl *impl = (amal_executor_impl*)self; u32 asm_offset = asm_get_size(&impl->asm); ASM_ENSURE_CAPACITY if(target_label < impl->label_counter) { asm_jmp(&impl->asm, (i32)impl->label_asm_index[target_label] - (i32)asm_offset); return 0; } else { JumpDefer jump_defer; jump_defer.asm_index = asm_offset; jump_defer.target_label = target_label; jump_defer.condition = bool_false; /* Insert dummy target, but it has to be above INT16_MAX, so the target can be replaced no matter how large the jump will be */ asm_jmp(&impl->asm, INT32_MAX); return buffer_append(&impl->jump_defer, &jump_defer, sizeof(jump_defer)); } } int amal_exec_ret(amal_executor *self, i8 reg) { AsmPtr ret_reg; IMPL_START asm_ptr_init_disp(&ret_reg, RBP, get_register_stack_offset(reg)); /* Result is returned in RAX register. TODO: Make this work when returning more than one result */ asm_mov_rm(&impl->asm, RAX, &ret_reg); return amal_exec_func_end(self); } static u32 get_next_uneven_number(u32 value) { return value + !(value & 1); } int amal_exec_func_start(amal_executor *self, u16 num_regs) { /* TODO: Validate stack size, or maybe remove all validation? do we really need validation? If we need security, we could fork the process instead. */ /* Some registers need to be preserved before entering a function scope and these registers are different on different platforms. 32-bit: EBX, ESI, EDI, EBP 64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15 64-bit Linux,BSD,Mac: RBX, RBP, R12-R15 */ IMPL_START asm_pushr(&impl->asm, RBX); asm_pushr(&impl->asm, RBP); asm_mov_rr(&impl->asm, RBP, RSP); /* Functions are entered with a stack alignment of 8 (because of call return address is pushed to stack). Make sure to align to to next 16-byte even if the extra bytes are not used. */ asm_sub_rm64_imm(&impl->asm, RSP, get_next_uneven_number(num_regs) * sizeof(isize)); return 0; } int amal_exec_func_end(amal_executor *self) { amal_executor_impl *impl = (amal_executor_impl*)self; JumpDefer *jump_defer = buffer_begin(&impl->jump_defer); JumpDefer *jump_defer_end = buffer_end(&impl->jump_defer); ASM_ENSURE_CAPACITY for(; jump_defer != jump_defer_end; ++jump_defer) { i32 jump_offset; if(jump_defer->target_label >= impl->label_counter) { amal_log_error("Program attempted to jump to a label that doesn't exist (label %u, while there are only %u labels)", jump_defer->target_label, impl->label_counter); return -1; } jump_offset = (isize)impl->label_asm_index[jump_defer->target_label] - (isize)jump_defer->asm_index; if(jump_defer->condition) asm_overwrite_jcc_rel32(&impl->asm, jump_defer->asm_index, jump_offset); else asm_overwrite_jmp_rel32(&impl->asm, jump_defer->asm_index, jump_offset); } buffer_clear(&impl->jump_defer); impl->label_counter = 0; asm_mov_rr(&impl->asm, RSP, RBP); asm_popr(&impl->asm, RBP); asm_popr(&impl->asm, RBX); asm_ret(&impl->asm, 0); return 0; } int amal_exec_label(amal_executor *self) { amal_executor_impl *impl = (amal_executor_impl*)self; assert(impl->label_counter < MAX_LABELS); impl->label_asm_index[impl->label_counter++] = asm_get_size(&impl->asm); return 0; }