diff options
Diffstat (limited to 'src/lib/netlist/plib/ppmf.h')
-rw-r--r-- | src/lib/netlist/plib/ppmf.h | 734 |
1 files changed, 411 insertions, 323 deletions
diff --git a/src/lib/netlist/plib/ppmf.h b/src/lib/netlist/plib/ppmf.h index 4c175fe8b10..56f40bde922 100644 --- a/src/lib/netlist/plib/ppmf.h +++ b/src/lib/netlist/plib/ppmf.h @@ -8,7 +8,7 @@ /// \file ppmf.h /// /// -/// PMF_TYPE_GNUC_PMF +/// PMF_TYPE_PMF /// Use standard pointer to member function syntax C++11 /// /// PMF_TYPE_GNUC_PMF_CONV @@ -16,365 +16,378 @@ /// This is not standard compliant and needs /// -Wno-pmf-conversions to compile. /// -/// PMF_TYPE_INTERNAL +/// PMF_TYPE_INTERNAL_* /// Use the same approach as MAME for deriving the function pointer. /// This is compiler-dependent as well /// -/// Benchmarks for ./nltool -c run -f src/mame/machine/nl_pong.cpp -t 10 -n pong_fast +/// Benchmarks for `./nltool -c run -t 10 -n pong src/mame/machine/nl_pong.cpp` /// -/// PMF_TYPE_INTERNAL: 215% 215% -/// PMF_TYPE_GNUC_PMF: 163% 196% -/// PMF_TYPE_GNUC_PMF_CONV: 215% 215% +/// PMF_TYPE_INTERNAL: 215% 215% 564% 580% +/// PMF_TYPE_GNUC_PMF: 163% 196% 516% 490% +/// PMF_TYPE_GNUC_PMF_CONV: 215% 215% 560% 575% /// -/// The whole exercise was done to avoid virtual calls. In prior versions of -/// netlist, the INTERNAL and GNUC_PMF_CONV approach provided significant improvement. -/// Since than, "hot" was removed from functions declared as virtual. -/// This may explain that the recent benchmarks show no difference at all. /// +/// \brief Enable experimental code on Visual Studio builds and VS clang llvm builds +/// +/// This enables experimental code which uses optimized builds the +/// ppmf_type::INTERNAL_MSC path also for complex (struct/union) return types. +/// This currently depends on whether the code can adequately determine on +/// x64 builds if the return type is returned through registers or passed as a +/// second argument as a pointer to the member function. +/// +/// The experimental code uses a temporary storage for the return value. This a +/// copy overhead for causes for large sized return types a copy overhead. +/// It would be easier if we would be able to obtain the RDX register on entry +/// to the call. On MSVC this seems not to be possible since on x64 inline +/// assembly is not supported. Even with clang-cl inline assembly this was not +/// successful when optimized code was compiled. Therefore we have to live with +/// the limitations. +/// +/// This code path is disabled by default currently. +/// +#if !defined(PPMF_EXPERIMENTAL) +#define PPMF_EXPERIMENTAL 0 +#endif + +/// brief Enable using MAME delegates as a replacement for ppmf. +/// +/// This define enables the use of MAME delegates (src/lib/util/delegate.h +/// as a replacement to ppmf. Enable this setting if you want to use the nltool +/// test suite (nltool -c tests) to produce comparisons to ppmf. +/// +#if !defined(PPMF_USE_MAME_DELEGATES) +#define PPMF_USE_MAME_DELEGATES 0 +#endif + +#if PPMF_USE_MAME_DELEGATES + +#include "../../util/delegate.h" + +namespace plib { + template<typename Signature> + class pmfp : public delegate<Signature> + { + public: + using basetype = delegate<Signature>; + using basetype::basetype; + using basetype::object; + explicit operator bool() const { return !this->isnull(); } + }; +} +#else + #include "pconfig.h" #include "ptypes.h" #include <algorithm> +#include <cstddef> // ptrdiff_t #include <cstdint> // uintptr_t +#include <type_traits> #include <utility> //============================================================ // Macro magic //============================================================ -//#define PPMF_TYPE 2 - -#define PPMF_TYPE_PMF 0 -#define PPMF_TYPE_GNUC_PMF_CONV 1 -#define PPMF_TYPE_INTERNAL 2 - -// FIXME: Remove this macro madmess latest after September, 2020 -// FIXME: Do we still need to support MINGW <= 4.6? - -#if defined(__clang__) && defined(__i386__) && defined(_WIN32) - #define PHAS_PMF_INTERNAL 0 -#elif defined(__GNUC__) || defined(__clang__) - // does not work in versions over 4.7.x of 32bit MINGW - #if defined(__MINGW32__) && !defined(__x86_64) && defined(__i386__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7))) - #define PHAS_PMF_INTERNAL 0 - #elif defined(__MINGW32__) && !defined(__x86_64) && defined(__i386__) - #define PHAS_PMF_INTERNAL 1 - #define MEMBER_ABI _thiscall - #elif defined(__arm__) || defined(__ARMEL__) || defined(__aarch64__) || defined(__MIPSEL__) || defined(__mips_isa_rev) || defined(__mips64) || defined(__EMSCRIPTEN__) - #define PHAS_PMF_INTERNAL 2 - #else - #define PHAS_PMF_INTERNAL 1 - #endif -#elif defined(_MSC_VER) && defined (_M_X64) - #define PHAS_PMF_INTERNAL 3 -#else - #define PHAS_PMF_INTERNAL 0 -#endif +//#define PPMF_FORCE_TYPE 1 -#ifndef MEMBER_ABI - #define MEMBER_ABI +#ifndef PPMF_FORCE_TYPE +#define PPMF_FORCE_TYPE -1 #endif -#ifndef PPMF_TYPE - #if (PHAS_PMF_INTERNAL > 0) - #define PPMF_TYPE PPMF_TYPE_INTERNAL - #else - #define PPMF_TYPE PPMF_TYPE_PMF - #endif -#else - #if (PPMF_TYPE == PPMF_TYPE_INTERNAL) - #if (PHAS_PMF_INTERNAL == 0) - #error "Internal type not supported" - #endif - #else - #undef PHAS_PMF_INTERNAL - #define PHAS_PMF_INTERNAL 0 - #undef MEMBER_ABI - #define MEMBER_ABI - #endif -#endif +namespace plib { -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpmf-conversions" -#endif + enum class ppmf_type + { + PMF, + GNUC_PMF_CONV, + INTERNAL_ITANIUM, + INTERNAL_ARM, + INTERNAL_MSC + }; -namespace plib { - struct ppmf_internal + struct ppmf_internal_selector { using ci = compile_info; - enum { value = + constexpr static ppmf_type value = + (PPMF_FORCE_TYPE >= 0) ? static_cast<ppmf_type>(PPMF_FORCE_TYPE) : (ci::type() == ci_compiler::CLANG && !ci::m64() - && ci::os() == ci_os::WINDOWS) ? 0 : - (ci::mingw() && !ci::m64() && ci::version() >= 407) ? 0 : - (ci::mingw() && !ci::m64()) ? 1 : + && ci::os() == ci_os::WINDOWS) ? ppmf_type::PMF : + (ci::mingw() && !ci::m64() && ci::version::full() >= typed_version<4,7>::full()) ? ppmf_type::PMF : + (ci::mingw() && !ci::m64()) ? ppmf_type::PMF : // Dropped support for mingw32 < 407 ppmf_type::INTERNAL_ITANIUM : + (ci::env() == ci_env::MSVC && ci::m64()) ? ppmf_type::INTERNAL_MSC : ((ci::type() == ci_compiler::CLANG || ci::type() == ci_compiler::GCC) && (ci::arch() == ci_arch::MIPS || ci::arch() == ci_arch::ARM - || ci::os() == ci_os::EMSCRIPTEN)) ? 2 : - (ci::type() == ci_compiler::CLANG || ci::type() == ci_compiler::GCC) ? 1 : - (ci::type() == ci_compiler::MSC && ci::m64()) ? 3 : - 0 - }; + || ci::os() == ci_os::EMSCRIPTEN)) ? ppmf_type::INTERNAL_ARM : + (ci::type() == ci_compiler::CLANG || ci::type() == ci_compiler::GCC) ? ppmf_type::INTERNAL_ITANIUM : + ppmf_type::PMF + ; }; - // FIXME: on supported platforms we should consider using GNU PMF extensions - // if no internal solution exists + static_assert(!(compile_info::type() == ci_compiler::CLANG && ppmf_internal_selector::value == (ppmf_type::GNUC_PMF_CONV)), "clang does not support ppmf_type::GNUC_PMF_CONV"); + static_assert(!(compile_info::env() == ci_env::NVCC && ppmf_internal_selector::value == (ppmf_type::GNUC_PMF_CONV)), "nvcc does not support ppmf_type::GNUC_PMF_CONV"); + + template<typename R, typename... Targs> + struct mfp_traits + { + template<typename C> using specific_member_function = R (C::*)(Targs...); + template<typename C> using const_specific_member_function = R (C::*)(Targs...) const; + template<typename C> using member_static_ref = R (*)(C &, Targs...); + template<typename C> using member_static_ptr = R (*)(C *, Targs...); + }; - using ppmf_type = std::integral_constant<int, (ppmf_internal::value > 0 ? PPMF_TYPE_INTERNAL : PPMF_TYPE_PMF)>; + class mfp_generic_class; - // check against previous implementation - static_assert(ppmf_internal::value == PHAS_PMF_INTERNAL, "internal mismatch"); - static_assert(ppmf_type::value == PPMF_TYPE, "type mismatch"); /// /// \brief Used to derive a pointer to a member function. /// /// The following class was derived from the MAME delegate.h code. /// - template <int PMFINTERNAL> - class mfp_raw + template <ppmf_type PMFINTERNAL> + class mfp_raw; + + template <> + class mfp_raw<ppmf_type::INTERNAL_ITANIUM> { public: // construct from any member function pointer - class generic_class; using generic_function = void (*)(); template<typename MemberFunctionType> - mfp_raw(MemberFunctionType mftp) - : m_function(0), m_this_delta(0), m_dummy1(0), m_dummy2(0), m_size(sizeof(mfp_raw)) - { - static_assert(sizeof(*this) >= sizeof(MemberFunctionType), "size mismatch"); - *reinterpret_cast<MemberFunctionType *>(this) = mftp; // NOLINT - // NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.UninitializedObject) - } + mfp_raw(MemberFunctionType mftp); - //private: // extract the generic function and adjust the object pointer - void convert_to_generic(generic_function &func, generic_class *&object) const - { - if (PMFINTERNAL == 1) - { - // apply the "this" delta to the object first - // NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult,cppcoreguidelines-pro-type-reinterpret-cast) - generic_class *o_p_delta = reinterpret_cast<generic_class *>(reinterpret_cast<std::uint8_t *>(object) + m_this_delta); - - // if the low bit of the vtable index is clear, then it is just a raw function pointer - if ((m_function & 1) == 0) - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - func = reinterpret_cast<generic_function>(m_function); - } - else - { - // otherwise, it is the byte index into the vtable where the actual function lives - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - std::uint8_t *vtable_base = *reinterpret_cast<std::uint8_t **>(o_p_delta); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - func = *reinterpret_cast<generic_function *>(vtable_base + m_function - 1); - } - object = o_p_delta; - } - else if (PMFINTERNAL == 2) - { - if ((m_this_delta & 1) == 0) - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - object = reinterpret_cast<generic_class *>(reinterpret_cast<std::uint8_t *>(object) + m_this_delta); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - func = reinterpret_cast<generic_function>(m_function); - } - else - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - object = reinterpret_cast<generic_class *>(reinterpret_cast<std::uint8_t *>(object)); - - // otherwise, it is the byte index into the vtable where the actual function lives - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - std::uint8_t *vtable_base = *reinterpret_cast<std::uint8_t **>(object); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - func = *reinterpret_cast<generic_function *>(vtable_base + m_function + m_this_delta - 1); - } - } - else if (PMFINTERNAL == 3) - { - const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (generic_class::*)()); + void convert_to_generic(generic_function &func, mfp_generic_class *&object) const; + + /// \brief Byte offset into the vtable + /// + /// On x86-64, the vtable contains pointers to code, and function pointers + /// are pointers to code. To obtain a function pointer for a virtual + /// member function, you fetch a pointer to code from the vtable. + /// + /// On traditional PPC64, the vtable contains pointers to function + /// descriptors, and function pointers are pointers to function descriptors. + /// To obtain a function pointer for a virtual member function, you + /// fetch a pointer to a function descriptor from the vtable. + /// + /// On IA64, the vtable contains function descriptors, and function + /// pointers are pointers to function descriptors. To obtain a + /// function pointer for a virtual member function, you calculate + /// the address of the function descriptor in the vtable. + /// + /// Simply adding the byte offset to the vtable pointer creates a + /// function pointer on IA64 because the vtable contains function + /// descriptors; on most other targets, the vtable contains function + /// pointers, so you need to fetch the function pointer after + /// calculating its address in the vtable. + /// + uintptr_t m_function; // first item can be one of two things: + // if even, it's a function pointer + // if odd, it's the byte offset into the vtable + ptrdiff_t m_this_delta; // delta to apply to the 'this' pointer + }; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - func = reinterpret_cast<generic_function>(m_function); - if (m_size == SINGLE_MEMFUNCPTR_SIZE + sizeof(int)) - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - object = reinterpret_cast<generic_class *>(reinterpret_cast<std::uint8_t *>(object) + m_this_delta); - } - } + template <> + class mfp_raw<ppmf_type::INTERNAL_ARM> + { + public: + // construct from any member function pointer + using generic_function = void (*)(); + + template<typename MemberFunctionType> + mfp_raw(MemberFunctionType mftp); + + // extract the generic function and adjust the object pointer + void convert_to_generic(generic_function &func, mfp_generic_class *&object) const; // actual state - uintptr_t m_function; // first item can be one of two things: - // if even, it's a pointer to the function - // if odd, it's the byte offset into the vtable - int m_this_delta; // delta to apply to the 'this' pointer - - int m_dummy1; // only used for visual studio x64 - int m_dummy2; - int m_size; + uintptr_t m_function; // first item can be a function pointer or a byte offset into the vtable + ptrdiff_t m_this_delta; // delta to apply to the 'this' pointer after right shifting by one bit + // if even, m_function is a fuction pointer + // if odd, m_function is the byte offset into the vtable }; - template<int PMFTYPE, int PMFINTERNAL, typename R, typename... Targs> + template <> + class mfp_raw<ppmf_type::INTERNAL_MSC> + { + public: + // construct from any member function pointer + using generic_function = void (*)(); + struct unknown_base_equiv { generic_function fptr; int thisdisp, vptrdisp, vtdisp; }; + struct single_base_equiv { generic_function fptr; }; + + template<typename MemberFunctionType> + mfp_raw(MemberFunctionType mftp); + + // extract the generic function and adjust the object pointer + void convert_to_generic(generic_function &func, mfp_generic_class *&object) const; + + // actual state + uintptr_t m_function; // pointer to the function + int m_this_delta; // delta to apply to the 'this' pointer + + int m_vptr_index; // index into the vptr table. + int m_vt_index; // offset to be applied after vptr table lookup. + unsigned m_size; + }; + + template <typename R> + using pmf_is_register_return_type = std::integral_constant<bool, + std::is_void_v<R> || + std::is_scalar_v<R> || + std::is_reference_v<R> || + std::is_same_v<std::remove_cv_t<R>, compile_info::int128_type> || + std::is_same_v<std::remove_cv_t<R>, compile_info::uint128_type> >; + + template<ppmf_type PMFINTERNAL, typename R, typename... Targs> struct mfp_helper { - static_assert(PMFTYPE == 2 && PMFINTERNAL >= 1 && PMFINTERNAL <=3, "Invalid PMF type"); + protected: + static_assert(PMFINTERNAL >= ppmf_type::INTERNAL_ITANIUM && PMFINTERNAL <= ppmf_type::INTERNAL_MSC, "Invalid PMF type"); + + using traits = mfp_traits<R, Targs...>; + using generic_member_function = typename traits::template specific_member_function<mfp_generic_class>; + using generic_member_abi_function = typename traits::template member_static_ptr<mfp_generic_class>; using raw_type = mfp_raw<PMFINTERNAL>; using generic_function_storage = typename raw_type::generic_function; - using generic_class = typename raw_type::generic_class; - template <class C> - using specific_member_function = R (C::*)(Targs...); + mfp_helper(); - template <class C> - using const_specific_member_function = R (C::*)(Targs...) const; + template<typename O, typename F> + void bind(O *object, F *mftp); - using generic_member_function = specific_member_function<generic_class>; - - template <class C> - using member_abi_function = MEMBER_ABI R (*)(C *obj, Targs... args); - - template<typename FunctionType, typename MemberFunctionType, typename ObjectType> - static std::pair<FunctionType, ObjectType *> get(MemberFunctionType mftp, ObjectType *object) + R call(Targs&&... args) const noexcept(true) { - raw_type mfpo(mftp); - generic_function_storage rfunc(nullptr); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto *robject = reinterpret_cast<generic_class *>(object); - mfpo.convert_to_generic(rfunc, robject); +#if defined(_MSC_VER) && (PPMF_EXPERIMENTAL) + if constexpr (pmf_is_register_return_type<R>::value) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + const auto* func = reinterpret_cast<const generic_member_abi_function*>(&m_resolved); + return (*func)(m_obj, std::forward<Targs>(args)...); + } + else + { + using generic_member_abi_function_alt = void (*)(mfp_generic_class *,void *, Targs...); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + const auto* func = reinterpret_cast<const generic_member_abi_function_alt*>(&m_resolved); + std::uint8_t temp[sizeof(typename std::conditional<std::is_void_v<R>, void *, R>::type)]; + (*func)(m_obj, &temp[0], std::forward<Targs>(args)...); + return *reinterpret_cast<R *>(&temp); + } +#else // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::make_pair(reinterpret_cast<FunctionType>(rfunc), reinterpret_cast<ObjectType *>(robject)); - } - template<typename O> - static R call(const member_abi_function<O> *func, O *obj, Targs&&... args) noexcept(true) - { - return (*func)(obj, std::forward<Targs>(args)...); + const auto* func = reinterpret_cast<const generic_member_abi_function*>(&m_resolved); + return (*func)(m_obj, std::forward<Targs>(args)...); +#endif } + + generic_function_storage m_resolved; + mfp_generic_class *m_obj; }; template<typename R, typename... Targs> - struct mfp_helper<PPMF_TYPE_PMF, 0, R, Targs...> + struct mfp_helper<ppmf_type::PMF, R, Targs...> { - template <class C> - using specific_member_function = R (C::*)(Targs...); + protected: + using traits = mfp_traits<R, Targs...>; + using generic_member_function = typename traits::template specific_member_function<mfp_generic_class>; template <class C> - using const_specific_member_function = R (C::*)(Targs...) const; + using member_abi_function = typename traits::template specific_member_function<C>; - class generic_class; - using generic_member_function = specific_member_function<generic_class>; + mfp_helper(); - template <class C> - using member_abi_function = specific_member_function<C>; + template<typename O, typename F> + void bind(O *object, F *mftp); - using generic_function_storage = generic_member_function; - - template<typename FunctionType, typename MemberFunctionType, typename ObjectType> - static std::pair<FunctionType, ObjectType *> get(MemberFunctionType mftp, ObjectType *object) + R call(Targs&&... args) const noexcept(true) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::make_pair(reinterpret_cast<FunctionType>(mftp), reinterpret_cast<ObjectType *>(object)); + auto* func = reinterpret_cast<const generic_member_function *>(&m_resolved); + return (*m_stub)(func, m_obj, std::forward<Targs>(args)...); } + generic_member_function m_resolved; + mfp_generic_class *m_obj; + R (*m_stub)(const generic_member_function *funci, mfp_generic_class *obji, Targs&&... args); + + private: template<typename O> - static R call(const member_abi_function<O> *func, O *obj, Targs&&... args) noexcept(true) - { - return (obj->*(*func))(std::forward<Targs>(args)...); - } + static R stub(const generic_member_function* funci, mfp_generic_class* obji, Targs&&... args) noexcept(true); }; template<typename R, typename... Targs> - struct mfp_helper<PPMF_TYPE_GNUC_PMF_CONV, 0, R, Targs...> + struct mfp_helper<ppmf_type::GNUC_PMF_CONV, R, Targs...> { - template <class C> - using specific_member_function = R (C::*)(Targs...); + protected: + using traits = mfp_traits<R, Targs...>; template <class C> - using const_specific_member_function = R (C::*)(Targs...) const; - - class generic_class; - using generic_member_function = specific_member_function<generic_class>; + using member_abi_function = typename traits::template member_static_ptr<C>; - template <class C> - using member_abi_function = MEMBER_ABI R (*)(C *obj, Targs... args); + mfp_helper(); - using generic_function_storage = void (*)(); + template<typename O, typename F> + void bind(O *object, F *mftp); - template<typename FunctionType, typename MemberFunctionType, typename ObjectType> - static std::pair<FunctionType, ObjectType *> get(MemberFunctionType mftp, ObjectType *object) + R call(Targs&&... args) const noexcept(true) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::make_pair(reinterpret_cast<FunctionType>(object->*mftp), reinterpret_cast<ObjectType *>(object)); - } - template<typename O> - static R call(const member_abi_function<O> *func, O *obj, Targs&&... args) noexcept(true) - { - return (*func)(obj, std::forward<Targs>(args)...); + auto* func = reinterpret_cast<const member_abi_function<mfp_generic_class> *>(&m_resolved); + return (*func)(m_obj, std::forward<Targs>(args)...); } - }; - template<int PMFTYPE, int PMFINTERNAL, typename R, typename... Targs> - class pmfp_base - { - public: - using helper = mfp_helper<PMFTYPE, PMFINTERNAL, R, Targs...>; - - template <class C> - using specific_member_function = typename helper::template specific_member_function<C>; + member_abi_function<mfp_generic_class> m_resolved; + mfp_generic_class *m_obj; + }; - template <class C> - using const_specific_member_function = typename helper::template const_specific_member_function<C>; + template <ppmf_type PMFINTERNAL, typename R, typename... Targs> + using pmfp_helper_select = std::conditional< + pmf_is_register_return_type<R>::value + || PMFINTERNAL != ppmf_type::INTERNAL_MSC || (PPMF_EXPERIMENTAL), + mfp_helper<PMFINTERNAL, R, Targs...>, mfp_helper<ppmf_type::PMF, R, Targs...>>; - using generic_class = typename helper::generic_class; - using generic_member_function = typename helper::generic_member_function; + template<ppmf_type PMFINTERNAL, typename SIGNATURE> class pmfp_base; - template <class C> - using member_abi_function = typename helper::template member_abi_function<C>; + template<ppmf_type PMFINTERNAL, typename R, typename... Targs> + class pmfp_base<PMFINTERNAL, R (Targs...)> : public pmfp_helper_select<PMFINTERNAL, R, Targs...>::type + { + static_assert((compile_info::env::value != ci_env::NVCC) || (PMFINTERNAL != ppmf_type::GNUC_PMF_CONV), "GNUC_PMF_CONV not supported by nvcc"); + public: + using helper = typename pmfp_helper_select<PMFINTERNAL, R, Targs...>::type; - using generic_function_storage = typename helper::generic_function_storage; + using traits = mfp_traits<R, Targs...>; pmfp_base() - : m_obj(nullptr) + : helper() { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto *s = reinterpret_cast<std::uint8_t *>(&m_resolved); - std::fill(s, s + sizeof(m_resolved), 0); } - template<typename O> - pmfp_base(specific_member_function<O> mftp, O *object) - : m_obj(nullptr) + template<typename O, typename P> + pmfp_base(typename traits::template specific_member_function<O> mftp, P *object) + : helper() { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto *s = reinterpret_cast<std::uint8_t *>(&m_resolved); - std::fill(s, s + sizeof(m_resolved), 0); - bind<specific_member_function<O>>(object, &mftp); + this->bind(static_cast<O*>(object), &mftp); } - template<typename O> - pmfp_base(const_specific_member_function<O> mftp, O *object) - : m_obj(nullptr) + template<typename O, typename P> + pmfp_base(typename traits::template const_specific_member_function<O> mftp, P *object) + : helper() { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto *s = reinterpret_cast<std::uint8_t *>(&m_resolved); - std::fill(s, s + sizeof(m_resolved), 0); - bind<const_specific_member_function<O>>(object, &mftp); + this->bind(static_cast<O*>(object), &mftp); } - generic_class *object() const noexcept { return m_obj; } - bool has_object() const noexcept { return m_obj != nullptr; } + mfp_generic_class *object() const noexcept { return this->m_obj; } template<typename O> - void set(specific_member_function<O> mftp, O *object) + void set(typename traits::template specific_member_function<O> mftp, O *object) { - bind<specific_member_function<O>>(object, &mftp); + this->bind(object, &mftp); } R operator()(Targs... args) const noexcept(true) @@ -382,48 +395,17 @@ namespace plib { return this->call(std::forward<Targs>(args)...); } - operator bool() const noexcept { return m_resolved != nullptr; } - private: - template<typename SPC, typename O, typename MF> - void bind(O * object, MF *fraw) - { - SPC pFunc; - static_assert(sizeof(pFunc) >= sizeof(MF), "size error"); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - *reinterpret_cast<MF *>(&pFunc) = *fraw; - - auto r = helper::template get<member_abi_function<O>>(pFunc, object); - - static_assert(sizeof(m_resolved) >= sizeof(r.first), "size mismatch 1"); - static_assert(sizeof(m_resolved) >= sizeof(member_abi_function<O>), "size mismatch 2"); - - //*reinterpret_cast<member_abi_function<O> *>(&m_resolved) = r.first; - reinterpret_copy(r.first, m_resolved); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_obj = reinterpret_cast<generic_class *>(r.second); - } + bool isnull() const noexcept { return this->m_resolved == nullptr; } + explicit operator bool() const noexcept { return !isnull(); } + bool has_object() const noexcept { return this->m_obj != nullptr; } + bool operator==(const pmfp_base &rhs) const { return this->m_resolved == rhs.m_resolved; } + bool operator!=(const pmfp_base &rhs) const { return !(*this == rhs); } - template<typename O> - R call(O *obj, Targs&&... args) const noexcept(true) - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return helper::call(reinterpret_cast<member_abi_function<O> *>(&m_resolved), - obj, std::forward<Targs>(args)...); - } - - R call(Targs&&... args) const noexcept(true) - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return helper::call(reinterpret_cast<const member_abi_function<generic_class> *>(&m_resolved), - m_obj, std::forward<Targs>(args)...); - } - - generic_function_storage m_resolved; - generic_class *m_obj; + private: }; - template<typename R, typename... Targs> - using pmfp = pmfp_base<ppmf_type::value, ppmf_internal::value, R, Targs...>; + template<typename Signature> + using pmfp = pmfp_base<ppmf_internal_selector::value, Signature>; /// /// \brief Class to support delegate late binding @@ -432,68 +414,174 @@ namespace plib { /// is virtual, the vtable may not yet be fully constructed. In these cases /// the following class allows to construct the delegate later. /// + /// ``` /// plib::late_pmfp<plib::pmfp<void, pstring>> a(&nld_7493::printer); /// // Store the a object somewhere /// /// // After full construction ... /// - /// auto dele = a(this); - /// dele(pstring("Hello World!")); - /// + /// auto delegate_obj = a(this); + /// delegate_obj(pstring("Hello World!")); + /// ``` template<typename T> class late_pmfp { public: using return_type = T; - - template <class C> - using specific_member_function = typename return_type::template specific_member_function<C>; // noexcept(true) --> c++-17 - - using generic_member_function = typename return_type::generic_member_function; - - class generic_class; - - using static_creator = return_type (*)(const generic_member_function *, generic_class *); - - late_pmfp() = default; + using traits = typename return_type::traits; + using generic_member_function = typename traits::template specific_member_function<mfp_generic_class>; + using static_creator = return_type (*)(const generic_member_function *, mfp_generic_class *); template<typename O> - late_pmfp(specific_member_function<O> mftp) - { - static_assert(sizeof(m_raw) >= sizeof(specific_member_function<O>), "size issue"); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - *reinterpret_cast<specific_member_function<O> *>(&m_raw) = mftp; - m_creator = creator<O>; - } + late_pmfp(typename traits::template specific_member_function<O> mftp); template<typename O> return_type operator()(O *object) const { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return m_creator(&m_raw, reinterpret_cast<generic_class *>(object)); + return m_creator(&m_raw, reinterpret_cast<mfp_generic_class *>(object)); } private: template <typename O> - static return_type creator(const generic_member_function *raw, generic_class *obj) - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto p = reinterpret_cast<const specific_member_function<O> *>(raw); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto o = reinterpret_cast<O *>(obj); - return return_type(*p, o); - } + static return_type creator(const generic_member_function *raw, mfp_generic_class *obj); generic_member_function m_raw; static_creator m_creator; }; -} // namespace plib + template<typename MemberFunctionType> + mfp_raw<ppmf_type::INTERNAL_ITANIUM>::mfp_raw(MemberFunctionType mftp) + : m_function(0), m_this_delta(0) + { + static_assert(sizeof(*this) >= sizeof(MemberFunctionType), "size mismatch"); + *reinterpret_cast<MemberFunctionType *>(this) = mftp; // NOLINT + // NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.UninitializedObject) + } + + template<typename MemberFunctionType> + mfp_raw<ppmf_type::INTERNAL_ARM>::mfp_raw(MemberFunctionType mftp) + : m_function(0), m_this_delta(0) + { + static_assert(sizeof(*this) >= sizeof(MemberFunctionType), "size mismatch"); + *reinterpret_cast<MemberFunctionType *>(this) = mftp; // NOLINT + } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop + template<typename MemberFunctionType> + mfp_raw<ppmf_type::INTERNAL_MSC>::mfp_raw(MemberFunctionType mftp) + : m_function(0), m_this_delta(0), m_vptr_index(0), m_vt_index(0), m_size(0) + { + static_assert(sizeof(*this) >= sizeof(MemberFunctionType), "size mismatch"); + *reinterpret_cast<MemberFunctionType *>(this) = mftp; // NOLINT + m_size = sizeof(mftp); //NOLINT + } + + template<ppmf_type PMFINTERNAL, typename R, typename... Targs> + mfp_helper<PMFINTERNAL, R, Targs...>::mfp_helper() + : m_obj(nullptr) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto *s = reinterpret_cast<std::uint8_t *>(&m_resolved); + std::fill(s, s + sizeof(m_resolved), 0); + } + + template<ppmf_type PMFINTERNAL, typename R, typename... Targs> + template<typename O, typename F> + void mfp_helper<PMFINTERNAL, R, Targs...>::bind(O *object, F *mftp) + { + typename traits::template specific_member_function<O> pFunc; + static_assert(sizeof(pFunc) >= sizeof(F), "size error"); + //# NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + //# *reinterpret_cast<F *>(&pFunc) = *mftp; + reinterpret_copy(*mftp, pFunc); + raw_type mfpo(pFunc); + generic_function_storage rfunc(nullptr); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto *robject = reinterpret_cast<mfp_generic_class *>(object); + mfpo.convert_to_generic(rfunc, robject); + reinterpret_copy(rfunc, this->m_resolved); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + m_obj = reinterpret_cast<mfp_generic_class *>(robject); + } + + template<typename R, typename... Targs> + mfp_helper<ppmf_type::PMF, R, Targs...>::mfp_helper() + : m_obj(nullptr) + , m_stub(nullptr) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto *s = reinterpret_cast<std::uint8_t *>(&m_resolved); + std::fill(s, s + sizeof(m_resolved), 0); + } + + template<typename R, typename... Targs> + template<typename O, typename F> + void mfp_helper<ppmf_type::PMF, R, Targs...>::bind(O *object, F *mftp) + { + reinterpret_copy(*mftp, this->m_resolved); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + m_obj = reinterpret_cast<mfp_generic_class *>(object); + m_stub = &stub<O>; + } + + template<typename R, typename... Targs> + template<typename O> + R mfp_helper<ppmf_type::PMF, R, Targs...>::stub(const generic_member_function* funci, mfp_generic_class* obji, Targs&&... args) noexcept(true) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto* obj = reinterpret_cast<O*>(obji); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto* func = reinterpret_cast<const member_abi_function<O> *>(funci); + return (obj->*(*func))(std::forward<Targs>(args)...); + } + + template<typename R, typename... Targs> + mfp_helper<ppmf_type::GNUC_PMF_CONV, R, Targs...>::mfp_helper() + : m_obj(nullptr) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto *s = reinterpret_cast<std::uint8_t *>(&m_resolved); + std::fill(s, s + sizeof(m_resolved), 0); + } + + template<typename R, typename... Targs> + template<typename O, typename F> + void mfp_helper<ppmf_type::GNUC_PMF_CONV, R, Targs...>::bind(O *object, F *mftp) + { + // nvcc still tries to compile the code below - even when shielded with a `if constexpr` +#if !defined(__NVCC__) + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + member_abi_function<O> t = reinterpret_cast<member_abi_function<O>>(object->*(*mftp)); + reinterpret_copy(t, this->m_resolved); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + m_obj = reinterpret_cast<mfp_generic_class *>(object); #endif + } + + template<typename T> + template<typename O> + late_pmfp<T>::late_pmfp(typename traits::template specific_member_function<O> mftp) + : m_creator(creator<O>) + { + static_assert(sizeof(m_raw) >= sizeof(typename traits::template specific_member_function<O>), "size issue"); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + *reinterpret_cast<typename traits::template specific_member_function<O> *>(&m_raw) = mftp; + } + template<typename T> + template<typename O> + typename late_pmfp<T>::return_type late_pmfp<T>::creator(const typename late_pmfp<T>::generic_member_function *raw, mfp_generic_class *obj) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto p = reinterpret_cast<const typename late_pmfp<T>::traits::template specific_member_function<O> *>(raw); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto *o = reinterpret_cast<O *>(obj); + return return_type(*p, o); + } + +} // namespace plib + +#endif #endif // PPMF_H_ |