From 902a81528b9d2edcf93226a2ca13da6fcc1839e5 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 23 Dec 2019 08:57:48 +0100 Subject: wip: function pointers and other stuff --- executor/x86_64/executor.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'executor/x86_64/executor.c') diff --git a/executor/x86_64/executor.c b/executor/x86_64/executor.c index 65f8baa..378bc31 100644 --- a/executor/x86_64/executor.c +++ b/executor/x86_64/executor.c @@ -136,6 +136,17 @@ static void asm_cmp(Asm *self, AsmOperand *op1, AsmOperand *op2) { } } +static void asm_call(Asm *self, AsmOperand *op) { + switch(op->type) { + case OPERAND_TYPE_REG: + asm_callr(self, op->value.reg); + break; + case OPERAND_TYPE_MEM: + asm_callm(self, &op->value.mem); + break; + } +} + int amal_executor_init(amal_executor **self) { amal_executor_impl **impl; impl = (amal_executor_impl**)self; @@ -388,7 +399,7 @@ int amal_exec_call(amal_executor *self, u32 code_offset, AmalReg dst_reg) { /* 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); + isize asm_offset; /* TODO: Do not push */ int num_pushed_stack = impl->num_pushed_values;/* + impl->num_saved_params_for_call - (int)NUM_REG_PARAMS;*/ ASM_ENSURE_CAPACITY @@ -398,6 +409,7 @@ int amal_exec_call(amal_executor *self, u32 code_offset, AmalReg dst_reg) { ++num_pushed_stack; asm_sub_rm64_imm(&impl->asm, RSP, sizeof(isize)); } + asm_offset = asm_get_size(&impl->asm); assert(code_offset < asm_offset); asm_call_rel32(&impl->asm, (isize)code_offset - asm_offset); @@ -453,11 +465,36 @@ int amal_exec_calle(amal_executor *self, void *func, AmalReg dst_reg) { return 0; } -/* -int amal_exec_callr(AmalReg dst_reg, BufferView data) { +int amal_exec_callr(amal_executor *self, AmalReg reg, AmalReg dst_reg) { + amal_executor_impl *impl = (amal_executor_impl*)self; + /* TODO: Preserve necessary registers before call? */ + /* TODO: This assumes all arguments are isize */ + /* TODO: Do not push */ + int num_pushed_stack = impl->num_pushed_values;/* + impl->num_saved_params_for_call - (int)NUM_REG_PARAMS;*/ + AsmOperand func_ptr_op; + ASM_ENSURE_CAPACITY + + /*assert((num_pushed_stack <= 0 || num_pushed_stack % 2 == 0) && "TODO: Align stack to 16-bytes before calling functions");*/ + if(num_pushed_stack & 1) { + ++num_pushed_stack; + asm_sub_rm64_imm(&impl->asm, RSP, sizeof(isize)); + } + func_ptr_op = amal_reg_to_asm_operand(reg); + asm_call(&impl->asm, &func_ptr_op); + /* Function result */ + { + AsmOperand dst_op = amal_reg_to_asm_operand(dst_reg); + AsmOperand rax_op = asm_reg_to_operand(RAX); + /* TODO: Make this work when result is not stored in RAX (multiple return results) */ + asm_mov(&impl->asm, &dst_op, &rax_op); + } + /* Function cleanup */ + 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_eq(amal_executor *self, AmalReg dst_reg, AmalReg src_reg1, AmalReg src_reg2) { AsmOperand dst_op = amal_reg_to_asm_operand(dst_reg); -- cgit v1.2.3