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_ADDR() 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(__64BIT__)) || (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)) && !defined(_LP64)
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 #ifdef __SUNPRO_C
278 #if __SUNPRO_C < 0x560
279 #define SLJIT_INLINE
280 #else
281 #define SLJIT_INLINE inline
282 #endif /* __SUNPRO_C */
283 #else
284 #define SLJIT_INLINE __inline
285 #endif
286 #endif /* !SLJIT_INLINE */
287 
288 #ifndef SLJIT_NOINLINE
289 /* Not inline functions. */
290 #if defined(__GNUC__)
291 #define SLJIT_NOINLINE __attribute__ ((noinline))
292 #else
293 #define SLJIT_NOINLINE
294 #endif
295 #endif /* !SLJIT_INLINE */
296 
297 #ifndef SLJIT_UNUSED_ARG
298 /* Unused arguments. */
299 #define SLJIT_UNUSED_ARG(arg) (void)arg
300 #endif
301 
302 /*********************************/
303 /* Type of public API functions. */
304 /*********************************/
305 
306 #ifndef SLJIT_API_FUNC_ATTRIBUTE
307 #if (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC)
308 /* Static ABI functions. For all-in-one programs. */
309 
310 #if defined(__GNUC__)
311 /* Disable unused warnings in gcc. */
312 #define SLJIT_API_FUNC_ATTRIBUTE static __attribute__((unused))
313 #else
314 #define SLJIT_API_FUNC_ATTRIBUTE static
315 #endif
316 
317 #else
318 #define SLJIT_API_FUNC_ATTRIBUTE
319 #endif /* (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC) */
320 #endif /* defined SLJIT_API_FUNC_ATTRIBUTE */
321 
322 /****************************/
323 /* Instruction cache flush. */
324 /****************************/
325 
326 /*
327  * TODO:
328  *
329  * clang >= 15 could be safe to enable below
330  * older versions are known to abort in some targets
331  * https://github.com/PhilipHazel/pcre2/issues/92
332  *
333  * beware APPLE is known to have removed the code in iOS so
334  * it will need to be excempted or result in broken builds
335  */
336 #if (!defined SLJIT_CACHE_FLUSH && defined __has_builtin)
337 #if __has_builtin(__builtin___clear_cache) && !defined(__clang__)
338 
339 /*
340  * https://gcc.gnu.org/bugzilla//show_bug.cgi?id=91248
341  * https://gcc.gnu.org/bugzilla//show_bug.cgi?id=93811
342  * gcc's clear_cache builtin for power and sparc are broken
343  */
344 #if !defined(SLJIT_CONFIG_PPC) && !defined(SLJIT_CONFIG_SPARC_32)
345 #define SLJIT_CACHE_FLUSH(from, to) \
346 	__builtin___clear_cache((char*)(from), (char*)(to))
347 #endif
348 
349 #endif /* gcc >= 10 */
350 #endif /* (!defined SLJIT_CACHE_FLUSH && defined __has_builtin) */
351 
352 #ifndef SLJIT_CACHE_FLUSH
353 
354 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) \
355 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
356 
357 /* Not required to implement on archs with unified caches. */
358 #define SLJIT_CACHE_FLUSH(from, to)
359 
360 #elif defined __APPLE__
361 
362 /* Supported by all macs since Mac OS 10.5.
363    However, it does not work on non-jailbroken iOS devices,
364    although the compilation is successful. */
365 #include <libkern/OSCacheControl.h>
366 #define SLJIT_CACHE_FLUSH(from, to) \
367 	sys_icache_invalidate((void*)(from), (size_t)((char*)(to) - (char*)(from)))
368 
369 #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
370 
371 /* The __clear_cache() implementation of GCC is a dummy function on PowerPC. */
372 #define SLJIT_CACHE_FLUSH(from, to) \
373 	ppc_cache_flush((from), (to))
374 #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
375 
376 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
377 
378 /* The __clear_cache() implementation of GCC is a dummy function on Sparc. */
379 #define SLJIT_CACHE_FLUSH(from, to) \
380 	sparc_cache_flush((from), (to))
381 #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
382 
383 #elif (defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) || defined(__clang__)
384 
385 #define SLJIT_CACHE_FLUSH(from, to) \
386 	__builtin___clear_cache((char*)(from), (char*)(to))
387 
388 #elif defined __ANDROID__
389 
390 /* Android ARMv7 with gcc lacks __clear_cache; use cacheflush instead. */
391 #include <sys/cachectl.h>
392 #define SLJIT_CACHE_FLUSH(from, to) \
393 	cacheflush((long)(from), (long)(to), 0)
394 
395 #elif defined _WIN32
396 
397 #define SLJIT_CACHE_FLUSH(from, to) \
398 	FlushInstructionCache(GetCurrentProcess(), (void*)(from), (char*)(to) - (char*)(from))
399 
400 #else
401 
402 /* Call __ARM_NR_cacheflush on ARM-Linux or the corresponding MIPS syscall. */
403 #define SLJIT_CACHE_FLUSH(from, to) \
404 	__clear_cache((char*)(from), (char*)(to))
405 
406 #endif
407 
408 #endif /* !SLJIT_CACHE_FLUSH */
409 
410 /******************************************************/
411 /*    Integer and floating point type definitions.    */
412 /******************************************************/
413 
414 /* 8 bit byte type. */
415 typedef unsigned char sljit_u8;
416 typedef signed char sljit_s8;
417 
418 /* 16 bit half-word type. */
419 typedef unsigned short int sljit_u16;
420 typedef signed short int sljit_s16;
421 
422 /* 32 bit integer type. */
423 typedef unsigned int sljit_u32;
424 typedef signed int sljit_s32;
425 
426 /* Machine word type. Enough for storing a pointer.
427      32 bit for 32 bit machines.
428      64 bit for 64 bit machines. */
429 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
430 /* Just to have something. */
431 #define SLJIT_WORD_SHIFT 0
432 typedef unsigned long int sljit_uw;
433 typedef long int sljit_sw;
434 #elif !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
435 	&& !(defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
436 	&& !(defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
437 	&& !(defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64) \
438 	&& !(defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
439 #define SLJIT_32BIT_ARCHITECTURE 1
440 #define SLJIT_WORD_SHIFT 2
441 typedef unsigned int sljit_uw;
442 typedef int sljit_sw;
443 #else
444 #define SLJIT_64BIT_ARCHITECTURE 1
445 #define SLJIT_WORD_SHIFT 3
446 #ifdef _WIN32
447 #ifdef __GNUC__
448 /* These types do not require windows.h */
449 typedef unsigned long long sljit_uw;
450 typedef long long sljit_sw;
451 #else
452 typedef unsigned __int64 sljit_uw;
453 typedef __int64 sljit_sw;
454 #endif
455 #else /* !_WIN32 */
456 typedef unsigned long int sljit_uw;
457 typedef long int sljit_sw;
458 #endif /* _WIN32 */
459 #endif
460 
461 typedef sljit_uw sljit_p;
462 
463 /* Floating point types. */
464 typedef float sljit_f32;
465 typedef double sljit_f64;
466 
467 /* Shift for pointer sized data. */
468 #define SLJIT_POINTER_SHIFT SLJIT_WORD_SHIFT
469 
470 /* Shift for double precision sized data. */
471 #define SLJIT_F32_SHIFT 2
472 #define SLJIT_F64_SHIFT 3
473 
474 #ifndef SLJIT_W
475 
476 /* Defining long constants. */
477 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
478 #define SLJIT_W(w)	(w##l)
479 #elif (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
480 #ifdef _WIN64
481 #define SLJIT_W(w)	(w##ll)
482 #else /* !windows */
483 #define SLJIT_W(w)	(w##l)
484 #endif /* windows */
485 #else /* 32 bit */
486 #define SLJIT_W(w)	(w)
487 #endif /* unknown */
488 
489 #endif /* !SLJIT_W */
490 
491 /*************************/
492 /* Endianness detection. */
493 /*************************/
494 
495 #if !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN)
496 
497 /* These macros are mostly useful for the applications. */
498 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
499 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
500 
501 #ifdef __LITTLE_ENDIAN__
502 #define SLJIT_LITTLE_ENDIAN 1
503 #else
504 #define SLJIT_BIG_ENDIAN 1
505 #endif
506 
507 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
508 	|| (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
509 
510 #ifdef __MIPSEL__
511 #define SLJIT_LITTLE_ENDIAN 1
512 #else
513 #define SLJIT_BIG_ENDIAN 1
514 #endif
515 
516 #ifndef SLJIT_MIPS_REV
517 
518 /* Auto detecting mips revision. */
519 #if (defined __mips_isa_rev) && (__mips_isa_rev >= 6)
520 #define SLJIT_MIPS_REV 6
521 #elif (defined __mips_isa_rev && __mips_isa_rev >= 1) \
522 	|| (defined __clang__ && defined _MIPS_ARCH_OCTEON) \
523 	|| (defined __clang__ && defined _MIPS_ARCH_P5600)
524 /* clang either forgets to define (clang-7) __mips_isa_rev at all
525  * or sets it to zero (clang-8,-9) for -march=octeon (MIPS64 R2+)
526  * and -march=p5600 (MIPS32 R5).
527  * It also sets the __mips macro to 64 or 32 for -mipsN when N <= 5
528  * (should be set to N exactly) so we cannot rely on this too.
529  */
530 #define SLJIT_MIPS_REV 1
531 #endif
532 
533 #endif /* !SLJIT_MIPS_REV */
534 
535 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) \
536 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
537 
538 #define SLJIT_BIG_ENDIAN 1
539 
540 #else
541 #define SLJIT_LITTLE_ENDIAN 1
542 #endif
543 
544 #endif /* !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN) */
545 
546 /* Sanity check. */
547 #if (defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
548 #error "Exactly one endianness must be selected"
549 #endif
550 
551 #if !(defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && !(defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
552 #error "Exactly one endianness must be selected"
553 #endif
554 
555 #ifndef SLJIT_UNALIGNED
556 
557 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
558 	|| (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
559 	|| (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
560 	|| (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
561 	|| (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
562 	|| (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
563 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
564 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
565 #define SLJIT_UNALIGNED 1
566 #endif
567 
568 #endif /* !SLJIT_UNALIGNED */
569 
570 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
571 /* Auto detect SSE2 support using CPUID.
572    On 64 bit x86 cpus, sse2 must be present. */
573 #define SLJIT_DETECT_SSE2 1
574 #endif
575 
576 /*****************************************************************************************/
577 /* Calling convention of functions generated by SLJIT or called from the generated code. */
578 /*****************************************************************************************/
579 
580 #ifndef SLJIT_FUNC
581 
582 #if (defined SLJIT_USE_CDECL_CALLING_CONVENTION && SLJIT_USE_CDECL_CALLING_CONVENTION) \
583 	|| !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
584 
585 #define SLJIT_FUNC
586 
587 #elif defined(__GNUC__) && !defined(__APPLE__)
588 
589 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
590 #define SLJIT_FUNC __attribute__ ((fastcall))
591 #define SLJIT_X86_32_FASTCALL 1
592 #else
593 #define SLJIT_FUNC
594 #endif /* gcc >= 3.4 */
595 
596 #elif defined(_MSC_VER)
597 
598 #define SLJIT_FUNC __fastcall
599 #define SLJIT_X86_32_FASTCALL 1
600 
601 #elif defined(__BORLANDC__)
602 
603 #define SLJIT_FUNC __msfastcall
604 #define SLJIT_X86_32_FASTCALL 1
605 
606 #else /* Unknown compiler. */
607 
608 /* The cdecl calling convention is usually the x86 default. */
609 #define SLJIT_FUNC
610 
611 #endif /* SLJIT_USE_CDECL_CALLING_CONVENTION */
612 
613 #endif /* !SLJIT_FUNC */
614 
615 #ifndef SLJIT_INDIRECT_CALL
616 #if ((defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) && (!defined _CALL_ELF || _CALL_ELF == 1)) \
617 	|| ((defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) && defined _AIX)
618 /* It seems certain ppc compilers use an indirect addressing for functions
619    which makes things complicated. */
620 #define SLJIT_INDIRECT_CALL 1
621 #endif
622 #endif /* SLJIT_INDIRECT_CALL */
623 
624 /* The offset which needs to be substracted from the return address to
625 determine the next executed instruction after return. */
626 #ifndef SLJIT_RETURN_ADDRESS_OFFSET
627 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
628 #define SLJIT_RETURN_ADDRESS_OFFSET 8
629 #else
630 #define SLJIT_RETURN_ADDRESS_OFFSET 0
631 #endif
632 #endif /* SLJIT_RETURN_ADDRESS_OFFSET */
633 
634 /***************************************************/
635 /* Functions of the built-in executable allocator. */
636 /***************************************************/
637 
638 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
639 SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size);
640 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr);
641 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
642 #define SLJIT_BUILTIN_MALLOC_EXEC(size, exec_allocator_data) sljit_malloc_exec(size)
643 #define SLJIT_BUILTIN_FREE_EXEC(ptr, exec_allocator_data) sljit_free_exec(ptr)
644 
645 #ifndef SLJIT_MALLOC_EXEC
646 #define SLJIT_MALLOC_EXEC(size, exec_allocator_data) SLJIT_BUILTIN_MALLOC_EXEC((size), (exec_allocator_data))
647 #endif /* SLJIT_MALLOC_EXEC */
648 
649 #ifndef SLJIT_FREE_EXEC
650 #define SLJIT_FREE_EXEC(ptr, exec_allocator_data) SLJIT_BUILTIN_FREE_EXEC((ptr), (exec_allocator_data))
651 #endif /* SLJIT_FREE_EXEC */
652 
653 #if (defined SLJIT_PROT_EXECUTABLE_ALLOCATOR && SLJIT_PROT_EXECUTABLE_ALLOCATOR)
654 SLJIT_API_FUNC_ATTRIBUTE sljit_sw sljit_exec_offset(void* ptr);
655 #define SLJIT_EXEC_OFFSET(ptr) sljit_exec_offset(ptr)
656 #else
657 #define SLJIT_EXEC_OFFSET(ptr) 0
658 #endif
659 
660 #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
661 
662 /**********************************************/
663 /* Registers and locals offset determination. */
664 /**********************************************/
665 
666 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
667 
668 #define SLJIT_NUMBER_OF_REGISTERS 12
669 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 9
670 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 7
671 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 0
672 #define SLJIT_LOCALS_OFFSET_BASE (compiler->locals_offset)
673 #define SLJIT_PREF_SHIFT_REG SLJIT_R2
674 
675 #elif (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
676 
677 #define SLJIT_NUMBER_OF_REGISTERS 13
678 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 15
679 #ifndef _WIN64
680 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 6
681 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 0
682 #define SLJIT_LOCALS_OFFSET_BASE 0
683 #else /* _WIN64 */
684 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
685 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 10
686 #define SLJIT_LOCALS_OFFSET_BASE (4 * (sljit_s32)sizeof(sljit_sw))
687 #endif /* !_WIN64 */
688 #define SLJIT_PREF_SHIFT_REG SLJIT_R3
689 
690 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
691 
692 #define SLJIT_NUMBER_OF_REGISTERS 12
693 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
694 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 14
695 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 8
696 #define SLJIT_LOCALS_OFFSET_BASE 0
697 
698 #elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
699 
700 #define SLJIT_NUMBER_OF_REGISTERS 12
701 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
702 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 14
703 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 8
704 #define SLJIT_LOCALS_OFFSET_BASE 0
705 
706 #elif (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
707 
708 #define SLJIT_NUMBER_OF_REGISTERS 26
709 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 10
710 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 30
711 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 8
712 #define SLJIT_LOCALS_OFFSET_BASE (2 * (sljit_s32)sizeof(sljit_sw))
713 
714 #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
715 
716 #define SLJIT_NUMBER_OF_REGISTERS 23
717 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 17
718 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 30
719 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 18
720 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) || (defined _AIX)
721 #define SLJIT_LOCALS_OFFSET_BASE ((6 + 8) * (sljit_s32)sizeof(sljit_sw))
722 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
723 /* Add +1 for double alignment. */
724 #define SLJIT_LOCALS_OFFSET_BASE ((3 + 1) * (sljit_s32)sizeof(sljit_sw))
725 #else
726 #define SLJIT_LOCALS_OFFSET_BASE (3 * (sljit_s32)sizeof(sljit_sw))
727 #endif /* SLJIT_CONFIG_PPC_64 || _AIX */
728 
729 #elif (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
730 
731 #define SLJIT_NUMBER_OF_REGISTERS 21
732 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
733 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
734 #define SLJIT_LOCALS_OFFSET_BASE (4 * (sljit_s32)sizeof(sljit_sw))
735 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 13
736 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 6
737 #else
738 #define SLJIT_LOCALS_OFFSET_BASE 0
739 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 29
740 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 8
741 #endif
742 
743 #elif (defined SLJIT_CONFIG_SPARC && SLJIT_CONFIG_SPARC)
744 
745 #define SLJIT_NUMBER_OF_REGISTERS 18
746 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 14
747 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 14
748 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 0
749 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
750 /* saved registers (16), return struct pointer (1), space for 6 argument words (1),
751    4th double arg (2), double alignment (1). */
752 #define SLJIT_LOCALS_OFFSET_BASE ((16 + 1 + 6 + 2 + 1) * (sljit_s32)sizeof(sljit_sw))
753 #endif
754 
755 #elif (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
756 
757 /*
758  * https://refspecs.linuxbase.org/ELF/zSeries/lzsabi0_zSeries.html#STACKFRAME
759  *
760  * 160
761  *  .. FR6
762  *  .. FR4
763  *  .. FR2
764  * 128 FR0
765  * 120 R15 (used for SP)
766  * 112 R14
767  * 104 R13
768  *  96 R12
769  *  ..
770  *  48 R6
771  *  ..
772  *  16 R2
773  *   8 RESERVED
774  *   0 SP
775  */
776 #define SLJIT_S390X_DEFAULT_STACK_FRAME_SIZE 160
777 
778 #define SLJIT_NUMBER_OF_REGISTERS 12
779 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
780 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 15
781 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 8
782 #define SLJIT_LOCALS_OFFSET_BASE SLJIT_S390X_DEFAULT_STACK_FRAME_SIZE
783 
784 #elif (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
785 
786 #define SLJIT_NUMBER_OF_REGISTERS 0
787 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 0
788 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 0
789 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 0
790 #define SLJIT_LOCALS_OFFSET_BASE 0
791 
792 #endif
793 
794 #define SLJIT_LOCALS_OFFSET (SLJIT_LOCALS_OFFSET_BASE)
795 
796 #define SLJIT_NUMBER_OF_SCRATCH_REGISTERS \
797 	(SLJIT_NUMBER_OF_REGISTERS - SLJIT_NUMBER_OF_SAVED_REGISTERS)
798 
799 #define SLJIT_NUMBER_OF_SCRATCH_FLOAT_REGISTERS \
800 	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS)
801 
802 /********************************/
803 /* CPU status flags management. */
804 /********************************/
805 
806 #if (defined SLJIT_CONFIG_ARM && SLJIT_CONFIG_ARM) \
807 	|| (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC) \
808 	|| (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS) \
809 	|| (defined SLJIT_CONFIG_SPARC && SLJIT_CONFIG_SPARC) \
810 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
811 #define SLJIT_HAS_STATUS_FLAGS_STATE 1
812 #endif
813 
814 /*************************************/
815 /* Debug and verbose related macros. */
816 /*************************************/
817 
818 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
819 
820 #if !defined(SLJIT_ASSERT) || !defined(SLJIT_UNREACHABLE)
821 
822 /* SLJIT_HALT_PROCESS must halt the process. */
823 #ifndef SLJIT_HALT_PROCESS
824 #define SLJIT_HALT_PROCESS() \
825 	abort();
826 #endif /* !SLJIT_HALT_PROCESS */
827 
828 #endif /* !SLJIT_ASSERT || !SLJIT_UNREACHABLE */
829 
830 /* Feel free to redefine these two macros. */
831 #ifndef SLJIT_ASSERT
832 
833 #define SLJIT_ASSERT(x) \
834 	do { \
835 		if (SLJIT_UNLIKELY(!(x))) { \
836 			printf("Assertion failed at " __FILE__ ":%d\n", __LINE__); \
837 			SLJIT_HALT_PROCESS(); \
838 		} \
839 	} while (0)
840 
841 #endif /* !SLJIT_ASSERT */
842 
843 #ifndef SLJIT_UNREACHABLE
844 
845 #define SLJIT_UNREACHABLE() \
846 	do { \
847 		printf("Should never been reached " __FILE__ ":%d\n", __LINE__); \
848 		SLJIT_HALT_PROCESS(); \
849 	} while (0)
850 
851 #endif /* !SLJIT_UNREACHABLE */
852 
853 #else /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
854 
855 /* Forcing empty, but valid statements. */
856 #undef SLJIT_ASSERT
857 #undef SLJIT_UNREACHABLE
858 
859 #define SLJIT_ASSERT(x) \
860 	do { } while (0)
861 #define SLJIT_UNREACHABLE() \
862 	do { } while (0)
863 
864 #endif /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
865 
866 #ifndef SLJIT_COMPILE_ASSERT
867 
868 #define SLJIT_COMPILE_ASSERT(x, description) \
869 	switch(0) { case 0: case ((x) ? 1 : 0): break; }
870 
871 #endif /* !SLJIT_COMPILE_ASSERT */
872 
873 #ifdef __cplusplus
874 } /* extern "C" */
875 #endif
876 
877 #endif /* SLJIT_CONFIG_INTERNAL_H_ */
878