Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions source/lib/DllCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct DYNAPARM

#ifdef _WIN64
// This function was borrowed from http://dyncall.org/
extern "C" UINT_PTR PerformDynaCall(size_t stackArgsSize, DWORD_PTR* stackArgs, DWORD_PTR* regArgs, void* aFunction);
extern "C" UINT_PTR PerformDynaCall(size_t stackArgsCount, DWORD_PTR* stackArgs, DWORD_PTR* regArgs, void* aFunction);

// Retrieve a float or double return value. These don't actually do anything, since the value we
// want is already in the xmm0 register which is used to return float or double values.
Expand Down Expand Up @@ -215,7 +215,7 @@ void DynaCall(void *aFunction, DYNAPARM aParam[], int aParamCount, DWORD &aExcep
int params_left = aParamCount, i = 0, r = 0;
DWORD_PTR regArgs[4];
DWORD_PTR* stackArgs = NULL;
size_t stackArgsSize = 0;
size_t stackArgsCount = 0;

if (!register_return)
{
Expand All @@ -230,8 +230,8 @@ void DynaCall(void *aFunction, DYNAPARM aParam[], int aParamCount, DWORD &aExcep
// Copy the remaining parameters
if(params_left)
{
stackArgsSize = params_left * 8;
stackArgs = (DWORD_PTR*) _alloca(stackArgsSize);
stackArgsCount = params_left;
stackArgs = (DWORD_PTR*) _alloca(stackArgsCount*sizeof(DWORD_PTR));

for(int i = 0; i < params_left; i ++)
stackArgs[i] = DynaParamToElement(aParam[i+4]);
Expand All @@ -240,7 +240,7 @@ void DynaCall(void *aFunction, DYNAPARM aParam[], int aParamCount, DWORD &aExcep
// Call the function.
__try
{
Res.UIntPtr = PerformDynaCall(stackArgsSize, stackArgs, regArgs, aFunction);
Res.UIntPtr = PerformDynaCall(stackArgsCount, stackArgs, regArgs, aFunction);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
Expand Down
23 changes: 13 additions & 10 deletions source/libx64call/x64call.asm
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,31 @@ PerformDynaCall proc frame
.endprolog

; Arguments:
; rcx: size of arguments to be passed via stack
; rcx: number of QWORDs to be passed as arguments via stack
; rdx: pointer to arguments to be passed via stack
; r8: pointer to arguments to be passed by registers
; r9: target function pointer

; Setup stack frame by subtracting the size of the arguments
sub rsp, rcx
test rcx, rcx
jz save_func_address

; Ensure the stack is 16-byte aligned
mov rax, rcx
and rax, 15
mov rsi, 16
sub rsi, rax
sub rsp, rsi
and rax, 1
add rax, rcx
shl rax, 3

; Save function address
mov rax, r9
; Setup stack frame by subtracting the size of the arguments
sub rsp, rax

; Copy the stack arguments
mov rsi, rdx ; let rsi point to the arguments.
mov rdi, rsp ; store pointer to beginning of stack arguments in rdi (for rep movsb).
rep movsb ; @@@ should be optimized (e.g. movq)
rep movsq

save_func_address:
; Save function address
mov rax, r9

; Copy the register arguments
mov rcx, qword ptr[r8 ] ; copy first four arguments to rcx, rdx, r8, r9 and xmm0-xmm3.
Expand Down
12 changes: 6 additions & 6 deletions source/libx64call/x64stub.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ RegisterCallbackAsmStub proc frame
.allocstack 8*5
.endprolog

; For the 'mov' further below
add rsp, 8
; For the 'push' further below
add rsp, 8*5

; Save the parameters in the spill area for consistency
mov qword ptr[rsp+8*0], rcx
mov qword ptr[rsp+8*1], rdx
mov qword ptr[rsp+8*2], r8
mov qword ptr[rsp+8*3], r9
push r9
push r8
push rdx
push rcx

; Set parameters for the upcoming function call
mov rcx, rsp ; UINT_PTR* aParams
Expand Down