2018-01-08 19:33:42 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2018 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-02-07 13:24:42 +01:00
|
|
|
#include <cstring>
|
2018-01-08 19:33:42 +01:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "backend_x64/callback.h"
|
2018-02-07 13:24:42 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/cast_util.h"
|
2018-01-08 19:33:42 +01:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/mp.h"
|
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
namespace BackendX64 {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
2018-01-12 18:31:21 +01:00
|
|
|
template <typename FunctionType, FunctionType mfp>
|
2018-01-08 19:33:42 +01:00
|
|
|
struct ThunkBuilder;
|
|
|
|
|
2018-01-12 18:31:21 +01:00
|
|
|
template <typename C, typename R, typename... Args, R(C::*mfp)(Args...)>
|
|
|
|
struct ThunkBuilder<R(C::*)(Args...), mfp> {
|
2018-01-08 19:33:42 +01:00
|
|
|
static R Thunk(C* this_, Args... args) {
|
2018-01-12 18:31:21 +01:00
|
|
|
return (this_->*mfp)(std::forward<Args>(args)...);
|
2018-01-08 19:33:42 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace impl
|
|
|
|
|
2018-01-12 18:31:21 +01:00
|
|
|
template <typename FunctionType, FunctionType mfp>
|
2018-02-07 13:24:42 +01:00
|
|
|
ArgCallback DevirtualizeGeneric(mp::class_type_t<FunctionType>* this_) {
|
2018-01-12 18:31:21 +01:00
|
|
|
return ArgCallback{&impl::ThunkBuilder<FunctionType, mfp>::Thunk, reinterpret_cast<u64>(this_)};
|
2018-01-08 19:33:42 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 13:24:42 +01:00
|
|
|
template <typename FunctionType, FunctionType mfp>
|
|
|
|
ArgCallback DevirtualizeItanium(mp::class_type_t<FunctionType>* this_) {
|
|
|
|
struct MemberFunctionPointer {
|
|
|
|
/// For a non-virtual function, this is a simple function pointer.
|
|
|
|
/// For a virtual function, it is (1 + virtual table offset in bytes).
|
|
|
|
u64 ptr;
|
|
|
|
/// The required adjustment to `this`, prior to the call.
|
|
|
|
u64 adj;
|
|
|
|
} mfp_struct = Common::BitCast<MemberFunctionPointer>(mfp);
|
|
|
|
|
|
|
|
static_assert(sizeof(MemberFunctionPointer) == 16);
|
|
|
|
static_assert(sizeof(MemberFunctionPointer) == sizeof(mfp));
|
|
|
|
|
|
|
|
u64 fn_ptr = mfp_struct.ptr;
|
|
|
|
u64 this_ptr = reinterpret_cast<u64>(this_) + mfp_struct.adj;
|
|
|
|
if (mfp_struct.ptr & 1) {
|
|
|
|
u64 vtable = Common::BitCastPointee<u64>(this_ptr);
|
|
|
|
fn_ptr = Common::BitCastPointee<u64>(vtable + fn_ptr - 1);
|
|
|
|
}
|
|
|
|
return ArgCallback{fn_ptr, this_ptr};
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__APPLE__) || defined(linux) || defined(__linux) || defined(__linux__)
|
|
|
|
#define DEVIRT(this_, mfp) Dynarmic::BackendX64::DevirtualizeItanium<decltype(mfp), mfp>(this_)
|
|
|
|
#else
|
|
|
|
#define DEVIRT(this_, mfp) Dynarmic::BackendX64::DevirtualizeGeneric<decltype(mfp), mfp>(this_)
|
|
|
|
#endif
|
2018-01-12 18:31:21 +01:00
|
|
|
|
2018-01-08 19:33:42 +01:00
|
|
|
} // namespace BackendX64
|
|
|
|
} // namespace Dynarmic
|