1 // [AsmJit]
2 // Complete JIT Assembler for C++ Language.
3 //
4 // [License]
5 // Zlib - See COPYING file in this package.
6 
7 // [Guard]
8 #ifndef _ASMJIT_CORE_BUILD_H
9 #define _ASMJIT_CORE_BUILD_H
10 
11 // [Include]
12 #include "../Config.h"
13 
14 #if defined(ASMJIT_EXPORTS)
15 # if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
16 #  define _CRT_SECURE_NO_WARNINGS
17 # endif // _MSC_VER
18 #endif // ASMJIT_EXPORTS
19 
20 // Here should be optional include files that's needed fo successfuly
21 // use macros defined here. Remember, AsmJit uses only AsmJit namespace
22 // and all macros are used within it.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <new>
28 
29 // ============================================================================
30 // [AsmJit - OS]
31 // ============================================================================
32 
33 #if !defined(ASMJIT_WINDOWS) && !defined(ASMJIT_POSIX)
34 #if defined(WINDOWS) || defined(_WINDOWS) || defined(__WINDOWS__) || defined(_WIN32) || defined(_WIN64)
35 # define ASMJIT_WINDOWS
36 #elif defined(__linux__)     || defined(__unix__)    || \
37       defined(__OpenBSD__)   || defined(__FreeBSD__) || defined(__NetBSD__) || \
38       defined(__DragonFly__) || defined(__BSD__)     || defined(__FREEBSD__) || \
39       defined(__APPLE__)
40 # define ASMJIT_POSIX
41 #else
42 # warning "AsmJit - Can't match operating system, using ASMJIT_POSIX"
43 # define ASMJIT_POSIX
44 #endif
45 #endif
46 
47 // ============================================================================
48 // [AsmJit - Architecture]
49 // ============================================================================
50 
51 // define it only if it's not defined. In some systems we can
52 // use -D command in compiler to bypass this autodetection.
53 #if !defined(ASMJIT_X86) && !defined(ASMJIT_X64)
54 # if defined(__x86_64__) || defined(__LP64) || defined(__IA64__) || \
55      defined(_M_X64)     || defined(_WIN64)
56 #  define ASMJIT_X64 // x86-64
57 # else
58 // _M_IX86, __INTEL__, __i386__
59 #  define ASMJIT_X86
60 # endif
61 #endif
62 
63 // ============================================================================
64 // [AsmJit - API]
65 // ============================================================================
66 
67 // Make AsmJit as shared library by default.
68 #if !defined(ASMJIT_API)
69 # if defined(ASMJIT_WINDOWS)
70 #  if defined(__GNUC__)
71 #   if defined(ASMJIT_EXPORTS)
72 #    define ASMJIT_API __attribute__((dllexport))
73 #   else
74 #    define ASMJIT_API __attribute__((dllimport))
75 #   endif // ASMJIT_EXPORTS
76 #  else
77 #   if defined(ASMJIT_EXPORTS)
78 #    define ASMJIT_API __declspec(dllexport)
79 #   else
80 #    define ASMJIT_API __declspec(dllimport)
81 #   endif // ASMJIT_EXPORTS
82 #  endif // __GNUC__
83 # else
84 #  if defined(__GNUC__)
85 #   if __GNUC__ >= 4
86 #    define ASMJIT_API __attribute__((visibility("default")))
87 #    define ASMJIT_VAR extern ASMJIT_API
88 #   endif // __GNUC__ >= 4
89 #  endif // __GNUC__
90 # endif
91 #endif // ASMJIT_API
92 
93 #if !defined(ASMJIT_VAR)
94 # if defined(ASMJIT_API)
95 #  define ASMJIT_VAR extern ASMJIT_API
96 # else
97 #  define ASMJIT_VAR
98 # endif // ASMJIT_API
99 #endif // !ASMJIT_VAR
100 
101 // [AsmJit - Memory Management]
102 #if !defined(ASMJIT_MALLOC)
103 # define ASMJIT_MALLOC ::malloc
104 #endif // ASMJIT_MALLOC
105 
106 #if !defined(ASMJIT_REALLOC)
107 # define ASMJIT_REALLOC ::realloc
108 #endif // ASMJIT_REALLOC
109 
110 #if !defined(ASMJIT_FREE)
111 # define ASMJIT_FREE ::free
112 #endif // ASMJIT_FREE
113 
114 // ============================================================================
115 // [AsmJit - Calling Conventions]
116 // ============================================================================
117 
118 #if defined(ASMJIT_X86)
119 # if defined(__GNUC__)
120 #  define ASMJIT_REGPARM_1 __attribute__((regparm(1)))
121 #  define ASMJIT_REGPARM_2 __attribute__((regparm(2)))
122 #  define ASMJIT_REGPARM_3 __attribute__((regparm(3)))
123 #  define ASMJIT_FASTCALL  __attribute__((fastcall))
124 #  define ASMJIT_STDCALL   __attribute__((stdcall))
125 #  define ASMJIT_CDECL     __attribute__((cdecl))
126 # else
127 #  define ASMJIT_FASTCALL   __fastcall
128 #  define ASMJIT_STDCALL    __stdcall
129 #  define ASMJIT_CDECL      __cdecl
130 # endif
131 #else
132 # define ASMJIT_FASTCALL
133 # define ASMJIT_STDCALL
134 # define ASMJIT_CDECL
135 #endif // ASMJIT_X86
136 
137 #if !defined(ASMJIT_UNUSED)
138 # define ASMJIT_UNUSED(var) ((void)var)
139 #endif // ASMJIT_UNUSED
140 
141 #if !defined(ASMJIT_NOP)
142 # define ASMJIT_NOP() ((void)0)
143 #endif // ASMJIT_NOP
144 
145 // [AsmJit - C++ Compiler Support]
146 #define ASMJIT_TYPE_TO_TYPE(_Type_) _Type_
147 #define ASMJIT_HAS_STANDARD_DEFINE_OPTIONS
148 #define ASMJIT_HAS_PARTIAL_TEMPLATE_SPECIALIZATION
149 
150 // Support for VC6
151 #if defined(_MSC_VER) && (_MSC_VER < 1400)
152 
153 namespace AsmJit {
154   template<typename T>
155   struct _Type2Type { typedef T Type; };
156 }
157 
158 #undef ASMJIT_TYPE_TO_TYPE
159 #define ASMJIT_TYPE_TO_TYPE(_Type_) ::AsmJit::_Type2Type<_Type_>::Type
160 
161 #undef ASMJIT_HAS_STANDARD_DEFINE_OPTIONS
162 #undef ASMJIT_HAS_PARTIAL_TEMPLATE_SPECIALIZATION
163 
164 #endif
165 
166 // ============================================================================
167 // [AsmJit - Types]
168 // ============================================================================
169 
170 #if defined(__MINGW32__) || defined(__MINGW64__)
171 # include <sys/types.h>
172 #endif // __MINGW32__ || __MINGW64__
173 
174 #if defined(_MSC_VER) && (_MSC_VER < 1600)
175 # if (_MSC_VER < 1300)
176 typedef signed char int8_t;
177 typedef signed short int16_t;
178 typedef signed int int32_t;
179 typedef signed __int64 int64_t;
180 typedef unsigned char uint8_t;
181 typedef unsigned short uint16_t;
182 typedef unsigned int uint32_t;
183 typedef unsigned __int64 uint64_t;
184 # else
185 typedef signed __int8 int8_t;
186 typedef signed __int16 int16_t;
187 typedef signed __int32 int32_t;
188 typedef signed __int64 int64_t;
189 typedef unsigned __int8 uint8_t;
190 typedef unsigned __int16 uint16_t;
191 typedef unsigned __int32 uint32_t;
192 typedef unsigned __int64 uint64_t;
193 # endif // _MSC_VER
194 #else
195 # include <stdint.h>
196 # include <limits.h>
197 #endif
198 
199 typedef unsigned char uchar;
200 typedef unsigned short ushort;
201 typedef unsigned int uint;
202 typedef unsigned long ulong;
203 
204 #if defined(ASMJIT_X86)
205 typedef int32_t sysint_t;
206 typedef uint32_t sysuint_t;
207 #else
208 typedef int64_t sysint_t;
209 typedef uint64_t sysuint_t;
210 #endif
211 
212 #if defined(_MSC_VER)
213 # define ASMJIT_INT64_C(num) num##i64
214 # define ASMJIT_UINT64_C(num) num##ui64
215 #else
216 # define ASMJIT_INT64_C(num) num##LL
217 # define ASMJIT_UINT64_C(num) num##ULL
218 #endif
219 
220 // ============================================================================
221 // [AsmJit - C++ Macros]
222 // ============================================================================
223 
224 #define ASMJIT_ARRAY_SIZE(A) (sizeof(A) / sizeof(*A))
225 
226 #define ASMJIT_NO_COPY(__type__) \
227 private: \
228   inline __type__(const __type__& other); \
229   inline __type__& operator=(const __type__& other); \
230 public:
231 
232 // ============================================================================
233 // [AsmJit - Debug]
234 // ============================================================================
235 
236 // If ASMJIT_DEBUG and ASMJIT_NO_DEBUG is not defined then ASMJIT_DEBUG will be
237 // detected using the compiler specific macros. This enables to set the build
238 // type using IDE.
239 #if !defined(ASMJIT_DEBUG) && !defined(ASMJIT_NO_DEBUG)
240 
241 #if defined(_DEBUG)
242 #define ASMJIT_DEBUG
243 #endif // _DEBUG
244 
245 #endif // !ASMJIT_DEBUG && !ASMJIT_NO_DEBUG
246 
247 // ============================================================================
248 // [AsmJit - Initialize/DontInitialize]
249 // ============================================================================
250 
251 // TODO: This should be moved to AsmJit namespace!
252 
253 // Skip documenting this.
254 #if !defined(ASMJIT_NODOC)
255 struct _Initialize {};
256 struct _DontInitialize {};
257 #endif // !ASMJIT_NODOC
258 
259 // ============================================================================
260 // [AsmJit - Void]
261 // ============================================================================
262 
263 // TODO: This should be moved to AsmJit namespace!
264 
265 //! @brief Void type which can be used in @ref FunctionDeclaration templates.
266 struct Void {};
267 
268 // ============================================================================
269 // [asmjit_cast<>]
270 // ============================================================================
271 
272 //! @brief Cast used to cast pointer to function. It's like reinterpret_cast<>,
273 //! but uses internally C style cast to work with MinGW.
274 //!
275 //! If you are using single compiler and @c reinterpret_cast<> works for you,
276 //! there is no reason to use @c asmjit_cast<>. If you are writing
277 //! cross-platform software with various compiler support, consider using
278 //! @c asmjit_cast<> instead of @c reinterpret_cast<>.
279 template<typename T, typename Z>
asmjit_cast(Z * p)280 static inline T asmjit_cast(Z* p) { return (T)p; }
281 
282 // ============================================================================
283 // [AsmJit - OS Support]
284 // ============================================================================
285 
286 #if defined(ASMJIT_WINDOWS)
287 #include <windows.h>
288 #endif // ASMJIT_WINDOWS
289 
290 #if defined(__APPLE__)
291 #include <AvailabilityMacros.h>
292 
293 #ifndef MAC_OS_X_VERSION_10_7
294 // In Mac OS X, strnlen() is unsupported prior to v10.7, so define it here.
strnlen(const char * s,size_t n)295 static size_t strnlen(const char *s, size_t n)
296 {
297 	const char *p = (const char *)memchr(s, 0, n);
298 	return(p ? p-s : n);
299 }
300 #endif
301 
302 #endif // __APPLE__
303 
304 // [Guard]
305 #endif // _ASMJIT_CORE_BUILD_H
306