Merge pull request #2010 from Sonicadvance1/add_support_for_32_bit_thunk_op

Thunks: Implement the Thunk IR op for 32-bit mode
This commit is contained in:
Ryan Houdek authored and GitHub committed 2022-09-26 02:33:47 -07:00
commit 1ea00f68a2
2 files changed
+28 -11

No files matched your search

+14 -4
View File
@@ -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<SHA256Sum*>(sha256)
);
if (CTX->Config.Is64BitMode) {
// x86-64 ABI puts the function argument in RDI
_Thunk(
_LoadContext(GPRSize, GPRClass, GPROffset(X86State::REG_RDI)),
*reinterpret_cast<SHA256Sum*>(sha256)
);
}
else {
// x86 fastcall ABI puts the function argument in ECX
_Thunk(
_LoadContext(GPRSize, GPRClass, GPROffset(X86State::REG_RCX)),
*reinterpret_cast<SHA256Sum*>(sha256)
);
}
auto Constant = _Constant(GPRSize);
auto OldSP = _LoadContext(GPRSize, GPRClass, RSPOffset);
+14 -7
View File
@@ -4,18 +4,25 @@
#include "PackedArguments.h"
#if __SIZEOF_POINTER__ == 8
#define THUNK_ABI
#else
#define THUNK_ABI [[gnu::fastcall]]
#endif
template<typename signature>
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<signature>)(void*) = fexthunks_##name;
template<> THUNK_ABI inline constexpr int (*fexthunks_invoke_callback<signature>)(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<typename Result, typename...Args>
static auto GetCallerForHostFunction(Result (*host_func)(Args...))
-> Result(*)(Args...) {
return &CallHostFunction<fexthunks_invoke_callback<Result(Args...)>, Result, Args...>;
static auto GetCallerForHostFunction(THUNK_ABI Result (*host_func)(Args...))
-> THUNK_ABI Result(*)(Args...) {
return &CallHostFunction<fexthunks_invoke_callback<THUNK_ABI Result(Args...)>, Result, Args...>;
}
// Ensures the given host function can safely be called from guest code.
template<typename Result, typename...Args>
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);
}