summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/plib/pconfig.h
blob: 1918ec61fb6d8044ad44fe92efbc21844e5388b9 (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
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * pconfig.h
 *
 */

#ifndef PCONFIG_H_
#define PCONFIG_H_

#ifndef PSTANDALONE
	#define PSTANDALONE (0)
#endif

#if !(PSTANDALONE)
#include "osdcore.h"
#include "eminline.h"

#ifndef assert
#define assert(x) do {} while (0)
#endif

#include "delegate.h"

#else
#include <stdint.h>
#include <cstddef>
#endif

//============================================================
//  Compiling standalone
//============================================================

#if !(PSTANDALONE)

#undef ATTR_COLD
#define ATTR_COLD

/* use MAME */
#if (USE_DELEGATE_TYPE == DELEGATE_TYPE_INTERNAL)
#define PHAS_PMF_INTERNAL 1
#else
#define PHAS_PMF_INTERNAL 0
#endif

#else

/* determine PMF approach */

#if defined(__GNUC__)
	/* 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 1
		#define MEMBER_ABI __thiscall
	#elif defined(EMSCRIPTEN)
		#define PHAS_PMF_INTERNAL 0
	#elif defined(__arm__) || defined(__ARMEL__)
		#define PHAS_PMF_INTERNAL 0
	#else
		#define PHAS_PMF_INTERNAL 1
	#endif
#else
#define USE_DELEGATE_TYPE PHAS_PMF_INTERNAL 0
#endif

#ifndef MEMBER_ABI
	#define MEMBER_ABI
#endif

/* not supported in GCC prior to 4.4.x */
/* ATTR_HOT and ATTR_COLD cause performance degration in 5.1 */
//#define ATTR_HOT
//#define ATTR_COLD
#define ATTR_HOT                __attribute__((hot))
#define ATTR_COLD              __attribute__((cold))

#define RESTRICT
#define EXPECTED(x)     (x)
#define UNEXPECTED(x)   (x)
#define ATTR_PRINTF(x,y)        __attribute__((format(printf, x, y)))
#define ATTR_UNUSED             __attribute__((__unused__))

/* 8-bit values */
typedef unsigned char                       UINT8;
typedef signed char                         INT8;

/* 16-bit values */
typedef unsigned short                      UINT16;
typedef signed short                        INT16;

/* 32-bit values */
#ifndef _WINDOWS_H
typedef unsigned int                        UINT32;
typedef signed int                          INT32;
#endif

/* 64-bit values */
#ifndef _WINDOWS_H
#ifdef _MSC_VER
typedef signed __int64                      INT64;
typedef unsigned __int64                    UINT64;
#else
typedef uint64_t    UINT64;
typedef int64_t      INT64;
#endif
#endif

/* U64 and S64 are used to wrap long integer constants. */
#if defined(__GNUC__) || defined(_MSC_VER)
#define U64(val) val##ULL
#define S64(val) val##LL
#else
#define U64(val) val
#define S64(val) val
#endif

/* MINGW has adopted the MSVC formatting for 64-bit ints as of gcc 4.4 */
#if (defined(__MINGW32__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))) || defined(_MSC_VER)
#define I64FMT   "I64"
#else
#define I64FMT   "ll"
#endif

#if defined(_MSC_VER) || defined(__MINGW32__)
#ifdef PTR64
#define SIZETFMT   "I64u"
#else
#define SIZETFMT   "u"
#endif
#else
#define SIZETFMT   "zu"
#endif

#endif

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

#if (PHAS_PMF_INTERNAL)
class pmfp
{
public:
	// construct from any member function pointer
	class generic_class;
	typedef void (*generic_function)();

	#if (PSTANDALONE)
	typedef std::size_t FPTR;
	#endif

	template<typename _MemberFunctionType>
	pmfp(_MemberFunctionType mfp)
	: m_function(0), m_this_delta(0)
	{
		*reinterpret_cast<_MemberFunctionType *>(this) = mfp;
	}

	// binding helper
	template<typename _FunctionType, typename _ObjectType>
	_FunctionType update_after_bind(_ObjectType *object)
	{
		return reinterpret_cast<_FunctionType>(
				convert_to_generic(reinterpret_cast<generic_class *>(object)));
	}
	template<typename _FunctionType, typename _MemberFunctionType, typename _ObjectType>
	static _FunctionType get_mfp(_MemberFunctionType mfp, _ObjectType *object)
	{
		pmfp mfpo(mfp);
		return mfpo.update_after_bind<_FunctionType>(object);
	}

private:
	// extract the generic function and adjust the object pointer
	generic_function convert_to_generic(generic_class * object) const
	{
		// apply the "this" delta to the object first
		generic_class * o_p_delta = reinterpret_cast<generic_class *>(reinterpret_cast<UINT8 *>(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))
			return reinterpret_cast<generic_function>(m_function);

		// otherwise, it is the byte index into the vtable where the actual function lives
		UINT8 *vtable_base = *reinterpret_cast<UINT8 **>(o_p_delta);
		return *reinterpret_cast<generic_function *>(vtable_base + m_function - 1);
	}

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

#endif /* PCONFIG_H_ */