diff --git a/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp b/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp index c9c939269..99a7109e1 100644 --- a/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp +++ b/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp @@ -126,10 +126,20 @@ void OpDispatchBuilder::ThunkOp(OpcodeArgs) { const uint8_t GPRSize = CTX->GetGPRSize(); uint8_t *sha256 = (uint8_t *)(Op->PC + 2); - _Thunk( - _LoadContext(GPRSize, GPRClass, GPROffset(X86State::REG_RDI)), - *reinterpret_cast(sha256) - ); + if (CTX->Config.Is64BitMode) { + // x86-64 ABI puts the function argument in RDI + _Thunk( + _LoadContext(GPRSize, GPRClass, GPROffset(X86State::REG_RDI)), + *reinterpret_cast(sha256) + ); + } + else { + // x86 fastcall ABI puts the function argument in ECX + _Thunk( + _LoadContext(GPRSize, GPRClass, GPROffset(X86State::REG_RCX)), + *reinterpret_cast(sha256) + ); + } auto Constant = _Constant(GPRSize); auto OldSP = _LoadContext(GPRSize, GPRClass, RSPOffset); diff --git a/ThunkLibs/include/common/Guest.h b/ThunkLibs/include/common/Guest.h index e0414761a..ef11d8595 100644 --- a/ThunkLibs/include/common/Guest.h +++ b/ThunkLibs/include/common/Guest.h @@ -4,18 +4,25 @@ #include "PackedArguments.h" +#if __SIZEOF_POINTER__ == 8 +#define THUNK_ABI +#else +#define THUNK_ABI [[gnu::fastcall]] +#endif + template +THUNK_ABI const int (*fexthunks_invoke_callback)(void*); #ifndef _M_ARM_64 #define MAKE_THUNK(lib, name, hash) \ - extern "C" int fexthunks_##lib##_##name(void *args); \ + extern "C" THUNK_ABI int fexthunks_##lib##_##name(void *args); \ asm(".text\nfexthunks_" #lib "_" #name ":\n.byte 0xF, 0x3F\n.byte " hash ); #define MAKE_CALLBACK_THUNK(name, signature, hash) \ - extern "C" int fexthunks_##name(void *args); \ + extern "C" THUNK_ABI int fexthunks_##name(void *args); \ asm(".text\nfexthunks_" #name ":\n.byte 0xF, 0x3F\n.byte " hash ); \ - template<> inline constexpr int (*fexthunks_invoke_callback)(void*) = fexthunks_##name; + template<> THUNK_ABI inline constexpr int (*fexthunks_invoke_callback)(void*) = fexthunks_##name; #else // We're compiling for IDE integration, so provide a dummy-implementation that just calls an undefined function. @@ -112,14 +119,14 @@ inline Result CallHostFunction(Args... args) { // Convenience wrapper that returns the function pointer to a CallHostFunction // instantiation matching the function signature of `host_func` template -static auto GetCallerForHostFunction(Result (*host_func)(Args...)) - -> Result(*)(Args...) { - return &CallHostFunction, Result, Args...>; +static auto GetCallerForHostFunction(THUNK_ABI Result (*host_func)(Args...)) + -> THUNK_ABI Result(*)(Args...) { + return &CallHostFunction, Result, Args...>; } // Ensures the given host function can safely be called from guest code. template -inline void MakeHostFunctionGuestCallable(Result (*host_func)(Args...)) { +inline void MakeHostFunctionGuestCallable(THUNK_ABI Result (*host_func)(Args...)) { auto caller = (uintptr_t)GetCallerForHostFunction(host_func); LinkAddressToFunction((uintptr_t)host_func, (uintptr_t)caller); }