1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification, are
7  * permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright notice, this list of
10  *      conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
13  *      of conditions and the following disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef SLJIT_CONFIG_INTERNAL_H_
28 #define SLJIT_CONFIG_INTERNAL_H_
29 
30 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
31 	|| (defined SLJIT_DEBUG && SLJIT_DEBUG && (!defined(SLJIT_ASSERT) || !defined(SLJIT_UNREACHABLE)))
32 #include <stdio.h>
33 #endif
34 
35 #if (defined SLJIT_DEBUG && SLJIT_DEBUG \
36 	&& (!defined(SLJIT_ASSERT) || !defined(SLJIT_UNREACHABLE) || !defined(SLJIT_HALT_PROCESS)))
37 #include <stdlib.h>
38 #endif
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /*
45    SLJIT defines the following architecture dependent types and macros:
46 
47    Types:
48      sljit_s8, sljit_u8   : signed and unsigned 8 bit integer type
49      sljit_s16, sljit_u16 : signed and unsigned 16 bit integer type
50      sljit_s32, sljit_u32 : signed and unsigned 32 bit integer type
51      sljit_sw, sljit_uw   : signed and unsigned machine word, enough to store a pointer
52      sljit_p              : unsgined pointer value (usually the same as sljit_uw, but
53                             some 64 bit ABIs may use 32 bit pointers)
54      sljit_f32            : 32 bit single precision floating point value
55      sljit_f64            : 64 bit double precision floating point value
56 
57    Macros for feature detection (boolean):
58      SLJIT_32BIT_ARCHITECTURE : 32 bit architecture
59      SLJIT_64BIT_ARCHITECTURE : 64 bit architecture
60      SLJIT_LITTLE_ENDIAN : little endian architecture
61      SLJIT_BIG_ENDIAN : big endian architecture
62      SLJIT_UNALIGNED : allows unaligned memory accesses for non-fpu operations (only!)
63      SLJIT_INDIRECT_CALL : see SLJIT_FUNC_OFFSET() for more information
64 
65    Constants:
66      SLJIT_NUMBER_OF_REGISTERS : number of available registers
67      SLJIT_NUMBER_OF_SCRATCH_REGISTERS : number of available scratch registers
68      SLJIT_NUMBER_OF_SAVED_REGISTERS : number of available saved registers
69      SLJIT_NUMBER_OF_FLOAT_REGISTERS : number of available floating point registers
70      SLJIT_NUMBER_OF_SCRATCH_FLOAT_REGISTERS : number of available floating point scratch registers
71      SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS : number of available floating point saved registers
72      SLJIT_WORD_SHIFT : the shift required to apply when accessing a sljit_sw/sljit_uw array by index
73      SLJIT_F32_SHIFT : the shift required to apply when accessing
74                        a single precision floating point array by index
75      SLJIT_F64_SHIFT : the shift required to apply when accessing
76                        a double precision floating point array by index
77      SLJIT_PREF_SHIFT_REG : x86 systems prefers ecx for shifting by register
78                             the scratch register index of ecx is stored in this variable
79      SLJIT_LOCALS_OFFSET : local space starting offset (SLJIT_SP + SLJIT_LOCALS_OFFSET)
80      SLJIT_RETURN_ADDRESS_OFFSET : a return instruction always adds this offset to the return address
81 
82    Other macros:
83      SLJIT_FUNC : calling convention attribute for both calling JIT from C and C calling back from JIT
84      SLJIT_W(number) : defining 64 bit constants on 64 bit architectures (platform independent helper)
85 */
86 
87 /*****************/
88 /* Sanity check. */
89 /*****************/
90 
91 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
92 	+ (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
93 	+ (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) \
94 	+ (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
95 	+ (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
96 	+ (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
97 	+ (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
98 	+ (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
99 	+ (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
100 	+ (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64) \
101 	+ (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) \
102 	+ (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X) \
103 	+ (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO) \
104 	+ (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED) >= 2
105 #error "Multiple architectures are selected"
106 #endif
107 
108 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
109 	&& !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
110 	&& !(defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) \
111 	&& !(defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
112 	&& !(defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
113 	&& !(defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
114 	&& !(defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
115 	&& !(defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
116 	&& !(defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
117 	&& !(defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64) \
118 	&& !(defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) \
119 	&& !(defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X) \
120 	&& !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED) \
121 	&& !(defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO)
122 #if defined SLJIT_CONFIG_AUTO && !SLJIT_CONFIG_AUTO
123 #error "An architecture must be selected"
124 #else /* SLJIT_CONFIG_AUTO */
125 #define SLJIT_CONFIG_AUTO 1
126 #endif /* !SLJIT_CONFIG_AUTO */
127 #endif /* !SLJIT_CONFIG */
128 
129 /********************************************************/
130 /* Automatic CPU detection (requires compiler support). */
131 /********************************************************/
132 
133 #if (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO)
134 
135 #ifndef _WIN32
136 
137 #if defined(__i386__) || defined(__i386)
138 #define SLJIT_CONFIG_X86_32 1
139 #elif defined(__x86_64__)
140 #define SLJIT_CONFIG_X86_64 1
141 #elif defined(__arm__) || defined(__ARM__)
142 #ifdef __thumb2__
143 #define SLJIT_CONFIG_ARM_THUMB2 1
144 #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__)
145 #define SLJIT_CONFIG_ARM_V7 1
146 #else
147 #define SLJIT_CONFIG_ARM_V5 1
148 #endif
149 #elif defined (__aarch64__)
150 #define SLJIT_CONFIG_ARM_64 1
151 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(_ARCH_PPC64) || (defined(_POWER) && defined(__64BIT__))
152 #define SLJIT_CONFIG_PPC_64 1
153 #elif defined(__ppc__) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(_ARCH_PWR) || defined(_ARCH_PWR2) || defined(_POWER)
154 #define SLJIT_CONFIG_PPC_32 1
155 #elif defined(__mips__) && !defined(_LP64)
156 #define SLJIT_CONFIG_MIPS_32 1
157 #elif defined(__mips64)
158 #define SLJIT_CONFIG_MIPS_64 1
159 #elif defined(__sparc__) || defined(__sparc)
160 #define SLJIT_CONFIG_SPARC_32 1
161 #elif defined(__s390x__)
162 #define SLJIT_CONFIG_S390X 1
163 #else
164 /* Unsupported architecture */
165 #define SLJIT_CONFIG_UNSUPPORTED 1
166 #endif
167 
168 #else /* _WIN32 */
169 
170 #if defined(_M_X64) || defined(__x86_64__)
171 #define SLJIT_CONFIG_X86_64 1
172 #elif (defined(_M_ARM) && _M_ARM >= 7 && defined(_M_ARMT)) || defined(__thumb2__)
173 #define SLJIT_CONFIG_ARM_THUMB2 1
174 #elif (defined(_M_ARM) && _M_ARM >= 7)
175 #define SLJIT_CONFIG_ARM_V7 1
176 #elif defined(_ARM_)
177 #define SLJIT_CONFIG_ARM_V5 1
178 #elif defined(_M_ARM64) || defined(__aarch64__)
179 #define SLJIT_CONFIG_ARM_64 1
180 #else
181 #define SLJIT_CONFIG_X86_32 1
182 #endif
183 
184 #endif /* !_WIN32 */
185 #endif /* SLJIT_CONFIG_AUTO */
186 
187 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
188 #undef SLJIT_EXECUTABLE_ALLOCATOR
189 #endif
190 
191 /******************************/
192 /* CPU family type detection. */
193 /******************************/
194 
195 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
196 	|| (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
197 #define SLJIT_CONFIG_ARM_32 1
198 #endif
199 
200 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
201 #define SLJIT_CONFIG_X86 1
202 #elif (defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) || (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
203 #define SLJIT_CONFIG_ARM 1
204 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
205 #define SLJIT_CONFIG_PPC 1
206 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) || (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
207 #define SLJIT_CONFIG_MIPS 1
208 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) || (defined SLJIT_CONFIG_SPARC_64 && SLJIT_CONFIG_SPARC_64)
209 #define SLJIT_CONFIG_SPARC 1
210 #endif
211 
212 /***********************************************************/
213 /* Intel Control-flow Enforcement Technology (CET) spport. */
214 /***********************************************************/
215 
216 #ifdef SLJIT_CONFIG_X86
217 
218 #if defined(__CET__) && !(defined SLJIT_CONFIG_X86_CET && SLJIT_CONFIG_X86_CET)
219 #define SLJIT_CONFIG_X86_CET 1
220 #endif
221 
222 #if (defined SLJIT_CONFIG_X86_CET && SLJIT_CONFIG_X86_CET) && defined(__GNUC__)
223 #include <x86intrin.h>
224 #endif
225 
226 #endif /* SLJIT_CONFIG_X86 */
227 
228 /**********************************/
229 /* External function definitions. */
230 /**********************************/
231 
232 /* General macros:
233    Note: SLJIT is designed to be independent from them as possible.
234 
235    In release mode (SLJIT_DEBUG is not defined) only the following
236    external functions are needed:
237 */
238 
239 #ifndef SLJIT_MALLOC
240 #define SLJIT_MALLOC(size, allocator_data) malloc(size)
241 #endif
242 
243 #ifndef SLJIT_FREE
244 #define SLJIT_FREE(ptr, allocator_data) free(ptr)
245 #endif
246 
247 #ifndef SLJIT_MEMCPY
248 #define SLJIT_MEMCPY(dest, src, len) memcpy(dest, src, len)
249 #endif
250 
251 #ifndef SLJIT_MEMMOVE
252 #define SLJIT_MEMMOVE(dest, src, len) memmove(dest, src, len)
253 #endif
254 
255 #ifndef SLJIT_ZEROMEM
256 #define SLJIT_ZEROMEM(dest, len) memset(dest, 0, len)
257 #endif
258 
259 /***************************/
260 /* Compiler helper macros. */
261 /***************************/
262 
263 #if !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY)
264 
265 #if defined(__GNUC__) && (__GNUC__ >= 3)
266 #define SLJIT_LIKELY(x)		__builtin_expect((x), 1)
267 #define SLJIT_UNLIKELY(x)	__builtin_expect((x), 0)
268 #else
269 #define SLJIT_LIKELY(x)		(x)
270 #define SLJIT_UNLIKELY(x)	(x)
271 #endif
272 
273 #endif /* !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY) */
274 
275 #ifndef SLJIT_INLINE
276 /* Inline functions. Some old compilers do not support them. */
277 #if defined(__SUNPRO_C) && __SUNPRO_C <= 0x510
278 #define SLJIT_INLINE
279 #else
280 #define SLJIT_INLINE __inline
281 #endif
282 #endif /* !SLJIT_INLINE */
283 
284 #ifndef SLJIT_NOINLINE
285 /* Not inline functions. */
286 #if defined(__GNUC__)
287 #define SLJIT_NOINLINE __attribute__ ((noinline))
288 #else
289 #define SLJIT_NOINLINE
290 #endif
291 #endif /* !SLJIT_INLINE */
292 
293 #ifndef SLJIT_UNUSED_ARG
294 /* Unused arguments. */
295 #define SLJIT_UNUSED_ARG(arg) (void)arg
296 #endif
297 
298 /*********************************/
299 /* Type of public API functions. */
300 /*********************************/
301 
302 #ifndef SLJIT_API_FUNC_ATTRIBUTE
303 #if (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC)
304 /* Static ABI functions. For all-in-one programs. */
305 
306 #if defined(__GNUC__)
307 /* Disable unused warnings in gcc. */
308 #define SLJIT_API_FUNC_ATTRIBUTE static __attribute__((unused))
309 #else
310 #define SLJIT_API_FUNC_ATTRIBUTE static
311 #endif
312 
313 #else
314 #define SLJIT_API_FUNC_ATTRIBUTE
315 #endif /* (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC) */
316 #endif /* defined SLJIT_API_FUNC_ATTRIBUTE */
317 
318 /****************************/
319 /* Instruction cache flush. */
320 /****************************/
321 
322 #if (!defined SLJIT_CACHE_FLUSH && defined __has_builtin)
323 #if __has_builtin(__builtin___clear_cache)
324 
325 #define SLJIT_CACHE_FLUSH(from, to) \
326 	__builtin___clear_cache((char*)(from), (char*)(to))
327 
328 #endif /* __has_builtin(__builtin___clear_cache) */
329 #endif /* (!defined SLJIT_CACHE_FLUSH && defined __has_builtin) */
330 
331 #ifndef SLJIT_CACHE_FLUSH
332 
333 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
334 
335 /* Not required to implement on archs with unified caches. */
336 #define SLJIT_CACHE_FLUSH(from, to)
337 
338 #elif defined __APPLE__
339 
340 /* Supported by all macs since Mac OS 10.5.
341    However, it does not work on non-jailbroken iOS devices,
342    although the compilation is successful. */
343 
344 #define SLJIT_CACHE_FLUSH(from, to) \
345 	sys_icache_invalidate((char*)(from), (char*)(to) - (char*)(from))
346 
347 #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
348 
349 /* The __clear_cache() implementation of GCC is a dummy function on PowerPC. */
350 #define SLJIT_CACHE_FLUSH(from, to) \
351 	ppc_cache_flush((from), (to))
352 #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
353 
354 #elif (defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
355 
356 #define SLJIT_CACHE_FLUSH(from, to) \
357 	__builtin___clear_cache((char*)(from), (char*)(to))
358 
359 #elif defined __ANDROID__
360 
361 /* Android lacks __clear_cache; instead, cacheflush should be used. */
362 
363 #define SLJIT_CACHE_FLUSH(from, to) \
364     cacheflush((long)(from), (long)(to), 0)
365 
366 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
367 
368 /* The __clear_cache() implementation of GCC is a dummy function on Sparc. */
369 #define SLJIT_CACHE_FLUSH(from, to) \
370 	sparc_cache_flush((from), (to))
371 #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
372 
373 #elif defined _WIN32
374 
375 #define SLJIT_CACHE_FLUSH(from, to) \
376 	FlushInstructionCache(GetCurrentProcess(), (char*)(from), (char*)(to) - (char*)(from))
377 
378 #else
379 
380 /* Calls __ARM_NR_cacheflush on ARM-Linux. */
381 #define SLJIT_CACHE_FLUSH(from, to) \
382 	__clear_cache((char*)(from), (char*)(to))
383 
384 #endif
385 
386 #endif /* !SLJIT_CACHE_FLUSH */
387 
388 /******************************************************/
389 /*    Integer and floating point type definitions.    */
390 /******************************************************/
391 
392 /* 8 bit byte type. */
393 typedef unsigned char sljit_u8;
394 typedef signed char sljit_s8;
395 
396 /* 16 bit half-word type. */
397 typedef unsigned short int sljit_u16;
398 typedef signed short int sljit_s16;
399 
400 /* 32 bit integer type. */
401 typedef unsigned int sljit_u32;
402 typedef signed int sljit_s32;
403 
404 /* Machine word type. Enough for storing a pointer.
405      32 bit for 32 bit machines.
406      64 bit for 64 bit machines. */
407 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
408 /* Just to have something. */
409 #define SLJIT_WORD_SHIFT 0
410 typedef unsigned long int sljit_uw;
411 typedef long int sljit_sw;
412 #elif !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
413 	&& !(defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
414 	&& !(defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
415 	&& !(defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64) \
416 	&& !(defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
417 #define SLJIT_32BIT_ARCHITECTURE 1
418 #define SLJIT_WORD_SHIFT 2
419 typedef unsigned int sljit_uw;
420 typedef int sljit_sw;
421 #else
422 #define SLJIT_64BIT_ARCHITECTURE 1
423 #define SLJIT_WORD_SHIFT 3
424 #ifdef _WIN32
425 #ifdef __GNUC__
426 /* These types do not require windows.h */
427 typedef unsigned long long sljit_uw;
428 typedef long long sljit_sw;
429 #else
430 typedef unsigned __int64 sljit_uw;
431 typedef __int64 sljit_sw;
432 #endif
433 #else /* !_WIN32 */
434 typedef unsigned long int sljit_uw;
435 typedef long int sljit_sw;
436 #endif /* _WIN32 */
437 #endif
438 
439 typedef sljit_uw sljit_p;
440 
441 /* Floating point types. */
442 typedef float sljit_f32;
443 typedef double sljit_f64;
444 
445 /* Shift for pointer sized data. */
446 #define SLJIT_POINTER_SHIFT SLJIT_WORD_SHIFT
447 
448 /* Shift for double precision sized data. */
449 #define SLJIT_F32_SHIFT 2
450 #define SLJIT_F64_SHIFT 3
451 
452 #ifndef SLJIT_W
453 
454 /* Defining long constants. */
455 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
456 #define SLJIT_W(w)	(w##l)
457 #elif (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
458 #ifdef _WIN64
459 #define SLJIT_W(w)	(w##ll)
460 #else /* !windows */
461 #define SLJIT_W(w)	(w##l)
462 #endif /* windows */
463 #else /* 32 bit */
464 #define SLJIT_W(w)	(w)
465 #endif /* unknown */
466 
467 #endif /* !SLJIT_W */
468 
469 /*************************/
470 /* Endianness detection. */
471 /*************************/
472 
473 #if !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN)
474 
475 /* These macros are mostly useful for the applications. */
476 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
477 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
478 
479 #ifdef __LITTLE_ENDIAN__
480 #define SLJIT_LITTLE_ENDIAN 1
481 #else
482 #define SLJIT_BIG_ENDIAN 1
483 #endif
484 
485 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
486 	|| (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
487 
488 #ifdef __MIPSEL__
489 #define SLJIT_LITTLE_ENDIAN 1
490 #else
491 #define SLJIT_BIG_ENDIAN 1
492 #endif
493 
494 #ifndef SLJIT_MIPS_REV
495 
496 /* Auto detecting mips revision. */
497 #if (defined __mips_isa_rev) && (__mips_isa_rev >= 6)
498 #define SLJIT_MIPS_REV 6
499 #elif (defined __mips_isa_rev && __mips_isa_rev >= 1) \
500 	|| (defined __clang__ && defined _MIPS_ARCH_OCTEON) \
501 	|| (defined __clang__ && defined _MIPS_ARCH_P5600)
502 /* clang either forgets to define (clang-7) __mips_isa_rev at all
503  * or sets it to zero (clang-8,-9) for -march=octeon (MIPS64 R2+)
504  * and -march=p5600 (MIPS32 R5).
505  * It also sets the __mips macro to 64 or 32 for -mipsN when N <= 5
506  * (should be set to N exactly) so we cannot rely on this too.
507  */
508 #define SLJIT_MIPS_REV 1
509 #endif
510 
511 #endif /* !SLJIT_MIPS_REV */
512 
513 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) \
514 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
515 
516 #define SLJIT_BIG_ENDIAN 1
517 
518 #else
519 #define SLJIT_LITTLE_ENDIAN 1
520 #endif
521 
522 #endif /* !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN) */
523 
524 /* Sanity check. */
525 #if (defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
526 #error "Exactly one endianness must be selected"
527 #endif
528 
529 #if !(defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && !(defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
530 #error "Exactly one endianness must be selected"
531 #endif
532 
533 #ifndef SLJIT_UNALIGNED
534 
535 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
536 	|| (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
537 	|| (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
538 	|| (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
539 	|| (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
540 	|| (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
541 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
542 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
543 #define SLJIT_UNALIGNED 1
544 #endif
545 
546 #endif /* !SLJIT_UNALIGNED */
547 
548 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
549 /* Auto detect SSE2 support using CPUID.
550    On 64 bit x86 cpus, sse2 must be present. */
551 #define SLJIT_DETECT_SSE2 1
552 #endif
553 
554 /*****************************************************************************************/
555 /* Calling convention of functions generated by SLJIT or called from the generated code. */
556 /*****************************************************************************************/
557 
558 #ifndef SLJIT_FUNC
559 
560 #if (defined SLJIT_USE_CDECL_CALLING_CONVENTION && SLJIT_USE_CDECL_CALLING_CONVENTION) \
561 	|| !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
562 
563 #define SLJIT_FUNC
564 
565 #elif defined(__GNUC__) && !defined(__APPLE__)
566 
567 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
568 #define SLJIT_FUNC __attribute__ ((fastcall))
569 #define SLJIT_X86_32_FASTCALL 1
570 #else
571 #define SLJIT_FUNC
572 #endif /* gcc >= 3.4 */
573 
574 #elif defined(_MSC_VER)
575 
576 #define SLJIT_FUNC __fastcall
577 #define SLJIT_X86_32_FASTCALL 1
578 
579 #elif defined(__BORLANDC__)
580 
581 #define SLJIT_FUNC __msfastcall
582 #define SLJIT_X86_32_FASTCALL 1
583 
584 #else /* Unknown compiler. */
585 
586 /* The cdecl calling convention is usually the x86 default. */
587 #define SLJIT_FUNC
588 
589 #endif /* SLJIT_USE_CDECL_CALLING_CONVENTION */
590 
591 #endif /* !SLJIT_FUNC */
592 
593 #ifndef SLJIT_INDIRECT_CALL
594 #if ((defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) && (!defined _CALL_ELF || _CALL_ELF == 1)) \
595 	|| ((defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) && defined _AIX)
596 /* It seems certain ppc compilers use an indirect addressing for functions
597    which makes things complicated. */
598 #define SLJIT_INDIRECT_CALL 1
599 #endif
600 #endif /* SLJIT_INDIRECT_CALL */
601 
602 /* The offset which needs to be substracted from the return address to
603 determine the next executed instruction after return. */
604 #ifndef SLJIT_RETURN_ADDRESS_OFFSET
605 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
606 #define SLJIT_RETURN_ADDRESS_OFFSET 8
607 #else
608 #define SLJIT_RETURN_ADDRESS_OFFSET 0
609 #endif
610 #endif /* SLJIT_RETURN_ADDRESS_OFFSET */
611 
612 /***************************************************/
613 /* Functions of the built-in executable allocator. */
614 /***************************************************/
615 
616 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
617 SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size);
618 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr);
619 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
620 #define SLJIT_BUILTIN_MALLOC_EXEC(size, exec_allocator_data) sljit_malloc_exec(size)
621 #define SLJIT_BUILTIN_FREE_EXEC(ptr, exec_allocator_data) sljit_free_exec(ptr)
622 
623 #ifndef SLJIT_MALLOC_EXEC
624 #define SLJIT_MALLOC_EXEC(size, exec_allocator_data) SLJIT_BUILTIN_MALLOC_EXEC((size), (exec_allocator_data))
625 #endif /* SLJIT_MALLOC_EXEC */
626 
627 #ifndef SLJIT_FREE_EXEC
628 #define SLJIT_FREE_EXEC(ptr, exec_allocator_data) SLJIT_BUILTIN_FREE_EXEC((ptr), (exec_allocator_data))
629 #endif /* SLJIT_FREE_EXEC */
630 
631 #if (defined SLJIT_PROT_EXECUTABLE_ALLOCATOR && SLJIT_PROT_EXECUTABLE_ALLOCATOR)
632 SLJIT_API_FUNC_ATTRIBUTE sljit_sw sljit_exec_offset(void* ptr);
633 #define SLJIT_EXEC_OFFSET(ptr) sljit_exec_offset(ptr)
634 #else
635 #define SLJIT_EXEC_OFFSET(ptr) 0
636 #endif
637 
638 #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
639 
640 /**********************************************/
641 /* Registers and locals offset determination. */
642 /**********************************************/
643 
644 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
645 
646 #define SLJIT_NUMBER_OF_REGISTERS 12
647 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 9
648 #define SLJIT_LOCALS_OFFSET_BASE (compiler->locals_offset)
649 #define SLJIT_PREF_SHIFT_REG SLJIT_R2
650 
651 #elif (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
652 
653 #define SLJIT_NUMBER_OF_REGISTERS 13
654 #ifndef _WIN64
655 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 6
656 #define SLJIT_LOCALS_OFFSET_BASE 0
657 #else /* _WIN64 */
658 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
659 #define SLJIT_LOCALS_OFFSET_BASE (compiler->locals_offset)
660 #endif /* !_WIN64 */
661 #define SLJIT_PREF_SHIFT_REG SLJIT_R3
662 
663 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
664 
665 #define SLJIT_NUMBER_OF_REGISTERS 12
666 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
667 #define SLJIT_LOCALS_OFFSET_BASE 0
668 
669 #elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
670 
671 #define SLJIT_NUMBER_OF_REGISTERS 12
672 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
673 #define SLJIT_LOCALS_OFFSET_BASE 0
674 
675 #elif (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
676 
677 #define SLJIT_NUMBER_OF_REGISTERS 26
678 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 10
679 #define SLJIT_LOCALS_OFFSET_BASE 0
680 
681 #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
682 
683 #define SLJIT_NUMBER_OF_REGISTERS 23
684 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 17
685 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) || (defined _AIX)
686 #define SLJIT_LOCALS_OFFSET_BASE ((6 + 8) * sizeof(sljit_sw))
687 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
688 /* Add +1 for double alignment. */
689 #define SLJIT_LOCALS_OFFSET_BASE ((3 + 1) * sizeof(sljit_sw))
690 #else
691 #define SLJIT_LOCALS_OFFSET_BASE (3 * sizeof(sljit_sw))
692 #endif /* SLJIT_CONFIG_PPC_64 || _AIX */
693 
694 #elif (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
695 
696 #define SLJIT_NUMBER_OF_REGISTERS 21
697 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
698 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
699 #define SLJIT_LOCALS_OFFSET_BASE (4 * sizeof(sljit_sw))
700 #else
701 #define SLJIT_LOCALS_OFFSET_BASE 0
702 #endif
703 
704 #elif (defined SLJIT_CONFIG_SPARC && SLJIT_CONFIG_SPARC)
705 
706 #define SLJIT_NUMBER_OF_REGISTERS 18
707 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 14
708 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
709 /* saved registers (16), return struct pointer (1), space for 6 argument words (1),
710    4th double arg (2), double alignment (1). */
711 #define SLJIT_LOCALS_OFFSET_BASE ((16 + 1 + 6 + 2 + 1) * sizeof(sljit_sw))
712 #endif
713 
714 #elif (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
715 
716 /*
717  * https://refspecs.linuxbase.org/ELF/zSeries/lzsabi0_zSeries.html#STACKFRAME
718  *
719  * 160
720  *  .. FR6
721  *  .. FR4
722  *  .. FR2
723  * 128 FR0
724  * 120 R15 (used for SP)
725  * 112 R14
726  * 104 R13
727  *  96 R12
728  *  ..
729  *  48 R6
730  *  ..
731  *  16 R2
732  *   8 RESERVED
733  *   0 SP
734  */
735 #define SLJIT_S390X_DEFAULT_STACK_FRAME_SIZE 160
736 
737 #define SLJIT_NUMBER_OF_REGISTERS 12
738 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
739 #define SLJIT_LOCALS_OFFSET_BASE SLJIT_S390X_DEFAULT_STACK_FRAME_SIZE
740 
741 #elif (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
742 
743 #define SLJIT_NUMBER_OF_REGISTERS 0
744 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 0
745 #define SLJIT_LOCALS_OFFSET_BASE 0
746 
747 #endif
748 
749 #define SLJIT_LOCALS_OFFSET (SLJIT_LOCALS_OFFSET_BASE)
750 
751 #define SLJIT_NUMBER_OF_SCRATCH_REGISTERS \
752 	(SLJIT_NUMBER_OF_REGISTERS - SLJIT_NUMBER_OF_SAVED_REGISTERS)
753 
754 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 6
755 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) && (defined _WIN64)
756 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 1
757 #else
758 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 0
759 #endif
760 
761 #define SLJIT_NUMBER_OF_SCRATCH_FLOAT_REGISTERS \
762 	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS)
763 
764 /********************************/
765 /* CPU status flags management. */
766 /********************************/
767 
768 #if (defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) \
769 	|| (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
770 	|| (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS) \
771 	|| (defined SLJIT_CONFIG_SPARC && SLJIT_CONFIG_SPARC) \
772 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
773 #define SLJIT_HAS_STATUS_FLAGS_STATE 1
774 #endif
775 
776 /*************************************/
777 /* Debug and verbose related macros. */
778 /*************************************/
779 
780 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
781 
782 #if !defined(SLJIT_ASSERT) || !defined(SLJIT_UNREACHABLE)
783 
784 /* SLJIT_HALT_PROCESS must halt the process. */
785 #ifndef SLJIT_HALT_PROCESS
786 #define SLJIT_HALT_PROCESS() \
787 	abort();
788 #endif /* !SLJIT_HALT_PROCESS */
789 
790 #endif /* !SLJIT_ASSERT || !SLJIT_UNREACHABLE */
791 
792 /* Feel free to redefine these two macros. */
793 #ifndef SLJIT_ASSERT
794 
795 #define SLJIT_ASSERT(x) \
796 	do { \
797 		if (SLJIT_UNLIKELY(!(x))) { \
798 			printf("Assertion failed at " __FILE__ ":%d\n", __LINE__); \
799 			SLJIT_HALT_PROCESS(); \
800 		} \
801 	} while (0)
802 
803 #endif /* !SLJIT_ASSERT */
804 
805 #ifndef SLJIT_UNREACHABLE
806 
807 #define SLJIT_UNREACHABLE() \
808 	do { \
809 		printf("Should never been reached " __FILE__ ":%d\n", __LINE__); \
810 		SLJIT_HALT_PROCESS(); \
811 	} while (0)
812 
813 #endif /* !SLJIT_UNREACHABLE */
814 
815 #else /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
816 
817 /* Forcing empty, but valid statements. */
818 #undef SLJIT_ASSERT
819 #undef SLJIT_UNREACHABLE
820 
821 #define SLJIT_ASSERT(x) \
822 	do { } while (0)
823 #define SLJIT_UNREACHABLE() \
824 	do { } while (0)
825 
826 #endif /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
827 
828 #ifndef SLJIT_COMPILE_ASSERT
829 
830 #define SLJIT_COMPILE_ASSERT(x, description) \
831 	switch(0) { case 0: case ((x) ? 1 : 0): break; }
832 
833 #endif /* !SLJIT_COMPILE_ASSERT */
834 
835 #ifdef __cplusplus
836 } /* extern "C" */
837 #endif
838 
839 #endif /* SLJIT_CONFIG_INTERNAL_H_ */
840