summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/ppmf.h
blob: 6ce65d4e960aabf3b5f950120532a6f71c05e74a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * ppmf.h
 *
 */

#ifndef PPMF_H_
#define PPMF_H_

#include "pconfig.h"

#include <cstdint> // uintptr_t
#include <utility>

/*
 *
 * NL_PMF_TYPE_GNUC_PMF
 *      Use standard pointer to member function syntax C++11
 *
 *  NL_PMF_TYPE_GNUC_PMF_CONV
 *      Use gnu extension and convert the pmf to a function pointer.
 *      This is not standard compliant and needs
 *      -Wno-pmf-conversions to compile.
 *
 *  NL_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
 *
 *  NL_PMF_TYPE_INTERNAL:       215%    215%
 *  NL_PMF_TYPE_GNUC_PMF:       163%    196%
 *  NL_PMF_TYPE_GNUC_PMF_CONV:  215%    215%
 *  NL_PMF_TYPE_VIRTUAL:        213%    209%
 *
 *  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.
 *
 */

#if (PPMF_TYPE == PPMF_TYPE_GNUC_PMF_CONV)
#pragma GCC diagnostic ignored "-Wpmf-conversions"
#endif

#if defined(__GNUC__) && (__GNUC__ > 6)
#pragma GCC diagnostic ignored "-Wnoexcept-type"
#endif

namespace plib {
/*
 * The following class was derived from the MAME delegate.h code.
 * It derives a pointer to a member function.
 */

#if (PHAS_PMF_INTERNAL > 0)
	class mfp
	{
	public:
		// construct from any member function pointer
#ifdef _MSC_VER
		class __single_inheritance si_generic_class;
		class generic_class { };
#else
		class generic_class;
#endif
		using generic_function = void (*)();

		template<typename MemberFunctionType>
		mfp(MemberFunctionType mftp)
		: m_function(0), m_this_delta(0), m_dummy1(0), m_dummy2(0), m_size(sizeof(mfp))
		{
			*reinterpret_cast<MemberFunctionType *>(this) = mftp; // NOLINT
			// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.UninitializedObject)
		}

		template<typename MemberFunctionType, typename FunctionType, typename ObjectType>
		static void get_mfp(MemberFunctionType mftp, FunctionType &func, ObjectType *&object)
		{
			mfp mfpo(mftp);
			//return mfpo.update_after_bind<FunctionType>(object);
			generic_function rfunc(nullptr);
			auto robject = reinterpret_cast<generic_class *>(object);
			mfpo.convert_to_generic(rfunc, robject);
			func = reinterpret_cast<FunctionType>(rfunc);
			object = reinterpret_cast<ObjectType *>(robject);
		}

	private:
		// extract the generic function and adjust the object pointer
		void convert_to_generic(generic_function &func, generic_class *&object) const
		{
			if (PHAS_PMF_INTERNAL == 1)
			{
				// apply the "this" delta to the object first
				// NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult)
				auto 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))
					func = reinterpret_cast<generic_function>(m_function);
				else
				{
					// otherwise, it is the byte index into the vtable where the actual function lives
					std::uint8_t *vtable_base = *reinterpret_cast<std::uint8_t **>(o_p_delta);
					func = *reinterpret_cast<generic_function *>(vtable_base + m_function - 1);
				}
				object = o_p_delta;
			}
			else if (PHAS_PMF_INTERNAL == 2)
			{
				if ((m_this_delta & 1) == 0) {
					object = reinterpret_cast<generic_class *>(reinterpret_cast<std::uint8_t *>(object) + m_this_delta);
					func = reinterpret_cast<generic_function>(m_function);
				}
				else
				{
					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
					std::uint8_t *vtable_base = *reinterpret_cast<std::uint8_t **>(object);
					func = *reinterpret_cast<generic_function *>(vtable_base + m_function + m_this_delta - 1);
				}
			}
			else if (PHAS_PMF_INTERNAL == 3)
			{
				const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (generic_class::*)());

				func = reinterpret_cast<generic_function>(m_function);
				if (m_size == SINGLE_MEMFUNCPTR_SIZE + sizeof(int))
					object = reinterpret_cast<generic_class *>(reinterpret_cast<std::uint8_t *>(object) + m_this_delta);
			}

		}

		// 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;
	};
#endif

#if (PPMF_TYPE == PPMF_TYPE_PMF)
	template<typename R, typename... Targs>
	class pmfp_base
	{
	public:
		class generic_class;
#if defined (__INTEL_COMPILER) && defined (_M_X64) // needed for "Intel(R) C++ Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.2.176 Build 20140130" at least
		using generic_function = int [((sizeof(void *) + 4 * sizeof(int)) + (sizeof(int) - 1)) / sizeof(int)];
#elif defined(_MSC_VER) // all other cases - for MSVC maximum size is one pointer, plus 3 ints; all other implementations seem to be smaller
		using generic_function = int[((sizeof(void *) + 3 * sizeof(int)) + (sizeof(int) - 1)) / sizeof(int)];
#else
		using generic_function = R (generic_class::*)(Targs...);
#endif
		pmfp_base()
		{
			int *p = reinterpret_cast<int *>(&m_func);
			int *e = p + sizeof(generic_function) / sizeof(int);
			for (; p < e; p++)
				*p = 0;
		}

		template<typename MemberFunctionType, typename O>
		void set_base(MemberFunctionType mftp, O *object)
		{
			using function_ptr = R (O::*)(Targs...);
			function_ptr t = mftp;
			*reinterpret_cast<function_ptr *>(&m_func) = t;
		}
		template<typename O>
		inline R call(O *obj, Targs... args)
		{
			using function_ptr = R (O::*)(Targs...);
			function_ptr t = *reinterpret_cast<function_ptr *>(&m_func);
			return (obj->*t)(std::forward<Targs>(args)...);
		}
		bool is_set() {
#if defined(_MSC_VER) || (defined (__INTEL_COMPILER) && defined (_M_X64))
			int *p = reinterpret_cast<int *>(&m_func);
			int *e = p + sizeof(generic_function) / sizeof(int);
			for (; p < e; p++)
				if (*p != 0)
					return true;

			return false;
#else
			return m_func != nullptr;
#endif
		}
	private:
		generic_function m_func;
#if 0 && defined(_MSC_VER)
		int dummy[4];
#endif
	};

#elif ((PPMF_TYPE == PPMF_TYPE_GNUC_PMF_CONV) || (PPMF_TYPE == PPMF_TYPE_INTERNAL))
	template<typename R, typename... Targs>
	class pmfp_base
	{
	public:
		using generic_function = void (*)();

		pmfp_base() : m_func(nullptr) {}

		template<typename MemberFunctionType, typename O>
		void set_base(MemberFunctionType mftp, O *object)
		{
	#if (PPMF_TYPE == PPMF_TYPE_INTERNAL)
			using function_ptr = MEMBER_ABI R (*)(O *obj, Targs... args);
			function_ptr func(nullptr);
			plib::mfp::get_mfp(mftp, func, object);
			m_func = reinterpret_cast<generic_function>(func);
	#elif (PPMF_TYPE == PPMF_TYPE_GNUC_PMF_CONV)
			R (O::* pFunc)(Targs...) = mftp;
			m_func = reinterpret_cast<generic_function>((object->*pFunc));
	#endif
		}
		template<typename O>
		R call(O *obj, Targs... args) const
		{
			using function_ptr = MEMBER_ABI R (*)(O *obj, Targs... args);
			return (reinterpret_cast<function_ptr>(m_func))(obj, std::forward<Targs>(args)...);
		}
		bool is_set() noexcept { return m_func != nullptr; }
		generic_function get_function() const noexcept { return m_func; }
	private:
		generic_function m_func;
	};
#endif

	template<typename R, typename... Targs>
	class pmfp : public pmfp_base<R, Targs...>
	{
	public:
		class generic_class;

		template <class C>
		using MemberFunctionType =  R (C::*)(Targs...);

		pmfp() : pmfp_base<R, Targs...>(), m_obj(nullptr) {}

		template<typename O>
		pmfp(MemberFunctionType<O> mftp, O *object)
		: pmfp_base<R, Targs...>()
		{
			this->set_base(mftp, object);
			m_obj = reinterpret_cast<generic_class *>(object);
		}

		template<typename O>
		void set(MemberFunctionType<O> mftp, O *object)
		{
			this->set_base(mftp, object);
			m_obj = reinterpret_cast<generic_class *>(object);
		}

		inline R operator()(Targs ... args)
		{
			return this->call(m_obj, std::forward<Targs>(args)...);
		}

		generic_class *object() const noexcept { return m_obj; }
		bool has_object() const noexcept { return m_obj != nullptr; }
	private:
		generic_class *m_obj;
	};


} // namespace plib

#endif /* PPMF_H_ */