/* 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 #include #include #include "backend_x64/callback.h" #include "common/assert.h" #include "common/cast_util.h" #include "common/common_types.h" #include "common/mp/function_info.h" namespace Dynarmic { namespace BackendX64 { namespace impl { template struct ThunkBuilder; template struct ThunkBuilder { static R Thunk(C* this_, Args... args) { return (this_->*mfp)(std::forward(args)...); } }; } // namespace impl template ArgCallback DevirtualizeGeneric(Common::mp::class_type_t* this_) { return ArgCallback{&impl::ThunkBuilder::Thunk, reinterpret_cast(this_)}; } template ArgCallback DevirtualizeWindows(Common::mp::class_type_t* this_) { static_assert(sizeof(mfp) == 8); return ArgCallback{Common::BitCast(mfp), reinterpret_cast(this_)}; } template ArgCallback DevirtualizeItanium(Common::mp::class_type_t* 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(mfp); static_assert(sizeof(MemberFunctionPointer) == 16); static_assert(sizeof(MemberFunctionPointer) == sizeof(mfp)); u64 fn_ptr = mfp_struct.ptr; u64 this_ptr = reinterpret_cast(this_) + mfp_struct.adj; if (mfp_struct.ptr & 1) { u64 vtable = Common::BitCastPointee(this_ptr); fn_ptr = Common::BitCastPointee(vtable + fn_ptr - 1); } return ArgCallback{fn_ptr, this_ptr}; } template ArgCallback Devirtualize(Common::mp::class_type_t* this_) { #if defined(__APPLE__) || defined(linux) || defined(__linux) || defined(__linux__) return DevirtualizeItanium(this_); #elif defined(__MINGW64__) return DevirtualizeItanium(this_); #elif defined(_WIN32) return DevirtualizeWindows(this_); #else return DevirtualizeGeneric(this_); #endif } } // namespace BackendX64 } // namespace Dynarmic