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 #else
162 /* Unsupported architecture */
163 #define SLJIT_CONFIG_UNSUPPORTED 1
164 #endif
165 
166 #else /* _WIN32 */
167 
168 #if defined(_M_X64) || defined(__x86_64__)
169 #define SLJIT_CONFIG_X86_64 1
170 #elif (defined(_M_ARM) && _M_ARM >= 7 && defined(_M_ARMT)) || defined(__thumb2__)
171 #define SLJIT_CONFIG_ARM_THUMB2 1
172 #elif (defined(_M_ARM) && _M_ARM >= 7)
173 #define SLJIT_CONFIG_ARM_V7 1
174 #elif defined(_ARM_)
175 #define SLJIT_CONFIG_ARM_V5 1
176 #elif defined(_M_ARM64) || defined(__aarch64__)
177 #define SLJIT_CONFIG_ARM_64 1
178 #else
179 #define SLJIT_CONFIG_X86_32 1
180 #endif
181 
182 #endif /* !_WIN32 */
183 #endif /* SLJIT_CONFIG_AUTO */
184 
185 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
186 #undef SLJIT_EXECUTABLE_ALLOCATOR
187 #endif
188 
189 /******************************/
190 /* CPU family type detection. */
191 /******************************/
192 
193 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
194 	|| (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
195 #define SLJIT_CONFIG_ARM_32 1
196 #endif
197 
198 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
199 #define SLJIT_CONFIG_X86 1
200 #elif (defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) || (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
201 #define SLJIT_CONFIG_ARM 1
202 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
203 #define SLJIT_CONFIG_PPC 1
204 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) || (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
205 #define SLJIT_CONFIG_MIPS 1
206 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) || (defined SLJIT_CONFIG_SPARC_64 && SLJIT_CONFIG_SPARC_64)
207 #define SLJIT_CONFIG_SPARC 1
208 #endif
209 
210 /***********************************************************/
211 /* Intel Control-flow Enforcement Technology (CET) spport. */
212 /***********************************************************/
213 
214 #ifdef SLJIT_CONFIG_X86
215 
216 #if defined(__CET__) && !(defined SLJIT_CONFIG_X86_CET && SLJIT_CONFIG_X86_CET)
217 #define SLJIT_CONFIG_X86_CET 1
218 #endif
219 
220 #if (defined SLJIT_CONFIG_X86_CET && SLJIT_CONFIG_X86_CET) && defined(__GNUC__)
221 #include <x86intrin.h>
222 #endif
223 
224 #endif /* SLJIT_CONFIG_X86 */
225 
226 /**********************************/
227 /* External function definitions. */
228 /**********************************/
229 
230 /* General macros:
231    Note: SLJIT is designed to be independent from them as possible.
232 
233    In release mode (SLJIT_DEBUG is not defined) only the following
234    external functions are needed:
235 */
236 
237 #ifndef SLJIT_MALLOC
238 #define SLJIT_MALLOC(size, allocator_data) malloc(size)
239 #endif
240 
241 #ifndef SLJIT_FREE
242 #define SLJIT_FREE(ptr, allocator_data) free(ptr)
243 #endif
244 
245 #ifndef SLJIT_MEMCPY
246 #define SLJIT_MEMCPY(dest, src, len) memcpy(dest, src, len)
247 #endif
248 
249 #ifndef SLJIT_MEMMOVE
250 #define SLJIT_MEMMOVE(dest, src, len) memmove(dest, src, len)
251 #endif
252 
253 #ifndef SLJIT_ZEROMEM
254 #define SLJIT_ZEROMEM(dest, len) memset(dest, 0, len)
255 #endif
256 
257 /***************************/
258 /* Compiler helper macros. */
259 /***************************/
260 
261 #if !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY)
262 
263 #if defined(__GNUC__) && (__GNUC__ >= 3)
264 #define SLJIT_LIKELY(x)		__builtin_expect((x), 1)
265 #define SLJIT_UNLIKELY(x)	__builtin_expect((x), 0)
266 #else
267 #define SLJIT_LIKELY(x)		(x)
268 #define SLJIT_UNLIKELY(x)	(x)
269 #endif
270 
271 #endif /* !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY) */
272 
273 #ifndef SLJIT_INLINE
274 /* Inline functions. Some old compilers do not support them. */
275 #if defined(__SUNPRO_C) && __SUNPRO_C <= 0x510
276 #define SLJIT_INLINE
277 #else
278 #define SLJIT_INLINE __inline
279 #endif
280 #endif /* !SLJIT_INLINE */
281 
282 #ifndef SLJIT_NOINLINE
283 /* Not inline functions. */
284 #if defined(__GNUC__)
285 #define SLJIT_NOINLINE __attribute__ ((noinline))
286 #else
287 #define SLJIT_NOINLINE
288 #endif
289 #endif /* !SLJIT_INLINE */
290 
291 #ifndef SLJIT_UNUSED_ARG
292 /* Unused arguments. */
293 #define SLJIT_UNUSED_ARG(arg) (void)arg
294 #endif
295 
296 /*********************************/
297 /* Type of public API functions. */
298 /*********************************/
299 
300 #ifndef SLJIT_API_FUNC_ATTRIBUTE
301 #if (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC)
302 /* Static ABI functions. For all-in-one programs. */
303 
304 #if defined(__GNUC__)
305 /* Disable unused warnings in gcc. */
306 #define SLJIT_API_FUNC_ATTRIBUTE static __attribute__((unused))
307 #else
308 #define SLJIT_API_FUNC_ATTRIBUTE static
309 #endif
310 
311 #else
312 #define SLJIT_API_FUNC_ATTRIBUTE
313 #endif /* (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC) */
314 #endif /* defined SLJIT_API_FUNC_ATTRIBUTE */
315 
316 /****************************/
317 /* Instruction cache flush. */
318 /****************************/
319 
320 #if (!defined SLJIT_CACHE_FLUSH && defined __has_builtin)
321 #if __has_builtin(__builtin___clear_cache)
322 
323 #define SLJIT_CACHE_FLUSH(from, to) \
324 	__builtin___clear_cache((char*)(from), (char*)(to))
325 
326 #endif /* __has_builtin(__builtin___clear_cache) */
327 #endif /* (!defined SLJIT_CACHE_FLUSH && defined __has_builtin) */
328 
329 #ifndef SLJIT_CACHE_FLUSH
330 
331 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
332 
333 /* Not required to implement on archs with unified caches. */
334 #define SLJIT_CACHE_FLUSH(from, to)
335 
336 #elif defined __APPLE__
337 
338 /* Supported by all macs since Mac OS 10.5.
339    However, it does not work on non-jailbroken iOS devices,
340    although the compilation is successful. */
341 
342 #define SLJIT_CACHE_FLUSH(from, to) \
343 	sys_icache_invalidate((char*)(from), (char*)(to) - (char*)(from))
344 
345 #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
346 
347 /* The __clear_cache() implementation of GCC is a dummy function on PowerPC. */
348 #define SLJIT_CACHE_FLUSH(from, to) \
349 	ppc_cache_flush((from), (to))
350 #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
351 
352 #elif (defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
353 
354 #define SLJIT_CACHE_FLUSH(from, to) \
355 	__builtin___clear_cache((char*)(from), (char*)(to))
356 
357 #elif defined __ANDROID__
358 
359 /* Android lacks __clear_cache; instead, cacheflush should be used. */
360 
361 #define SLJIT_CACHE_FLUSH(from, to) \
362     cacheflush((long)(from), (long)(to), 0)
363 
364 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
365 
366 /* The __clear_cache() implementation of GCC is a dummy function on Sparc. */
367 #define SLJIT_CACHE_FLUSH(from, to) \
368 	sparc_cache_flush((from), (to))
369 #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
370 
371 #elif defined _WIN32
372 
373 #define SLJIT_CACHE_FLUSH(from, to) \
374 	FlushInstructionCache(GetCurrentProcess(), (char*)(from), (char*)(to) - (char*)(from))
375 
376 #else
377 
378 /* Calls __ARM_NR_cacheflush on ARM-Linux. */
379 #define SLJIT_CACHE_FLUSH(from, to) \
380 	__clear_cache((char*)(from), (char*)(to))
381 
382 #endif
383 
384 #endif /* !SLJIT_CACHE_FLUSH */
385 
386 /******************************************************/
387 /*    Integer and floating point type definitions.    */
388 /******************************************************/
389 
390 /* 8 bit byte type. */
391 typedef unsigned char sljit_u8;
392 typedef signed char sljit_s8;
393 
394 /* 16 bit half-word type. */
395 typedef unsigned short int sljit_u16;
396 typedef signed short int sljit_s16;
397 
398 /* 32 bit integer type. */
399 typedef unsigned int sljit_u32;
400 typedef signed int sljit_s32;
401 
402 /* Machine word type. Enough for storing a pointer.
403      32 bit for 32 bit machines.
404      64 bit for 64 bit machines. */
405 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
406 /* Just to have something. */
407 #define SLJIT_WORD_SHIFT 0
408 typedef unsigned long int sljit_uw;
409 typedef long int sljit_sw;
410 #elif !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
411 	&& !(defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
412 	&& !(defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
413 	&& !(defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64) \
414 	&& !(defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
415 #define SLJIT_32BIT_ARCHITECTURE 1
416 #define SLJIT_WORD_SHIFT 2
417 typedef unsigned int sljit_uw;
418 typedef int sljit_sw;
419 #else
420 #define SLJIT_64BIT_ARCHITECTURE 1
421 #define SLJIT_WORD_SHIFT 3
422 #ifdef _WIN32
423 #ifdef __GNUC__
424 /* These types do not require windows.h */
425 typedef unsigned long long sljit_uw;
426 typedef long long sljit_sw;
427 #else
428 typedef unsigned __int64 sljit_uw;
429 typedef __int64 sljit_sw;
430 #endif
431 #else /* !_WIN32 */
432 typedef unsigned long int sljit_uw;
433 typedef long int sljit_sw;
434 #endif /* _WIN32 */
435 #endif
436 
437 typedef sljit_uw sljit_p;
438 
439 /* Floating point types. */
440 typedef float sljit_f32;
441 typedef double sljit_f64;
442 
443 /* Shift for pointer sized data. */
444 #define SLJIT_POINTER_SHIFT SLJIT_WORD_SHIFT
445 
446 /* Shift for double precision sized data. */
447 #define SLJIT_F32_SHIFT 2
448 #define SLJIT_F64_SHIFT 3
449 
450 #ifndef SLJIT_W
451 
452 /* Defining long constants. */
453 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
454 #define SLJIT_W(w)	(w##l)
455 #elif (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
456 #ifdef _WIN64
457 #define SLJIT_W(w)	(w##ll)
458 #else /* !windows */
459 #define SLJIT_W(w)	(w##l)
460 #endif /* windows */
461 #else /* 32 bit */
462 #define SLJIT_W(w)	(w)
463 #endif /* unknown */
464 
465 #endif /* !SLJIT_W */
466 
467 /*************************/
468 /* Endianness detection. */
469 /*************************/
470 
471 #if !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN)
472 
473 /* These macros are mostly useful for the applications. */
474 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
475 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
476 
477 #ifdef __LITTLE_ENDIAN__
478 #define SLJIT_LITTLE_ENDIAN 1
479 #else
480 #define SLJIT_BIG_ENDIAN 1
481 #endif
482 
483 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
484 	|| (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
485 
486 #ifdef __MIPSEL__
487 #define SLJIT_LITTLE_ENDIAN 1
488 #else
489 #define SLJIT_BIG_ENDIAN 1
490 #endif
491 
492 #ifndef SLJIT_MIPS_REV
493 
494 /* Auto detecting mips revision. */
495 #if (defined __mips_isa_rev) && (__mips_isa_rev >= 6)
496 #define SLJIT_MIPS_REV 6
497 #elif (defined __mips_isa_rev && __mips_isa_rev >= 1) \
498 	|| (defined __clang__ && defined _MIPS_ARCH_OCTEON) \
499 	|| (defined __clang__ && defined _MIPS_ARCH_P5600)
500 /* clang either forgets to define (clang-7) __mips_isa_rev at all
501  * or sets it to zero (clang-8,-9) for -march=octeon (MIPS64 R2+)
502  * and -march=p5600 (MIPS32 R5).
503  * It also sets the __mips macro to 64 or 32 for -mipsN when N <= 5
504  * (should be set to N exactly) so we cannot rely on this too.
505  */
506 #define SLJIT_MIPS_REV 1
507 #endif
508 
509 #endif /* !SLJIT_MIPS_REV */
510 
511 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) \
512 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
513 
514 #define SLJIT_BIG_ENDIAN 1
515 
516 #else
517 #define SLJIT_LITTLE_ENDIAN 1
518 #endif
519 
520 #endif /* !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN) */
521 
522 /* Sanity check. */
523 #if (defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
524 #error "Exactly one endianness must be selected"
525 #endif
526 
527 #if !(defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && !(defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
528 #error "Exactly one endianness must be selected"
529 #endif
530 
531 #ifndef SLJIT_UNALIGNED
532 
533 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
534 	|| (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
535 	|| (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
536 	|| (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
537 	|| (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
538 	|| (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
539 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
540 	|| (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
541 #define SLJIT_UNALIGNED 1
542 #endif
543 
544 #endif /* !SLJIT_UNALIGNED */
545 
546 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
547 /* Auto detect SSE2 support using CPUID.
548    On 64 bit x86 cpus, sse2 must be present. */
549 #define SLJIT_DETECT_SSE2 1
550 #endif
551 
552 /*****************************************************************************************/
553 /* Calling convention of functions generated by SLJIT or called from the generated code. */
554 /*****************************************************************************************/
555 
556 #ifndef SLJIT_FUNC
557 
558 #if (defined SLJIT_USE_CDECL_CALLING_CONVENTION && SLJIT_USE_CDECL_CALLING_CONVENTION) \
559 	|| !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
560 
561 #define SLJIT_FUNC
562 
563 #elif defined(__GNUC__) && !defined(__APPLE__)
564 
565 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
566 #define SLJIT_FUNC __attribute__ ((fastcall))
567 #define SLJIT_X86_32_FASTCALL 1
568 #else
569 #define SLJIT_FUNC
570 #endif /* gcc >= 3.4 */
571 
572 #elif defined(_MSC_VER)
573 
574 #define SLJIT_FUNC __fastcall
575 #define SLJIT_X86_32_FASTCALL 1
576 
577 #elif defined(__BORLANDC__)
578 
579 #define SLJIT_FUNC __msfastcall
580 #define SLJIT_X86_32_FASTCALL 1
581 
582 #else /* Unknown compiler. */
583 
584 /* The cdecl calling convention is usually the x86 default. */
585 #define SLJIT_FUNC
586 
587 #endif /* SLJIT_USE_CDECL_CALLING_CONVENTION */
588 
589 #endif /* !SLJIT_FUNC */
590 
591 #ifndef SLJIT_INDIRECT_CALL
592 #if ((defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) && (!defined _CALL_ELF || _CALL_ELF == 1)) \
593 	|| ((defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) && defined _AIX)
594 /* It seems certain ppc compilers use an indirect addressing for functions
595    which makes things complicated. */
596 #define SLJIT_INDIRECT_CALL 1
597 #endif
598 #endif /* SLJIT_INDIRECT_CALL */
599 
600 /* The offset which needs to be substracted from the return address to
601 determine the next executed instruction after return. */
602 #ifndef SLJIT_RETURN_ADDRESS_OFFSET
603 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
604 #define SLJIT_RETURN_ADDRESS_OFFSET 8
605 #else
606 #define SLJIT_RETURN_ADDRESS_OFFSET 0
607 #endif
608 #endif /* SLJIT_RETURN_ADDRESS_OFFSET */
609 
610 /***************************************************/
611 /* Functions of the built-in executable allocator. */
612 /***************************************************/
613 
614 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
615 SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size);
616 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr);
617 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
618 #define SLJIT_BUILTIN_MALLOC_EXEC(size, exec_allocator_data) sljit_malloc_exec(size)
619 #define SLJIT_BUILTIN_FREE_EXEC(ptr, exec_allocator_data) sljit_free_exec(ptr)
620 
621 #ifndef SLJIT_MALLOC_EXEC
622 #define SLJIT_MALLOC_EXEC(size, exec_allocator_data) SLJIT_BUILTIN_MALLOC_EXEC((size), (exec_allocator_data))
623 #endif /* SLJIT_MALLOC_EXEC */
624 
625 #ifndef SLJIT_FREE_EXEC
626 #define SLJIT_FREE_EXEC(ptr, exec_allocator_data) SLJIT_BUILTIN_FREE_EXEC((ptr), (exec_allocator_data))
627 #endif /* SLJIT_FREE_EXEC */
628 
629 #if (defined SLJIT_PROT_EXECUTABLE_ALLOCATOR && SLJIT_PROT_EXECUTABLE_ALLOCATOR)
630 SLJIT_API_FUNC_ATTRIBUTE sljit_sw sljit_exec_offset(void* ptr);
631 #define SLJIT_EXEC_OFFSET(ptr) sljit_exec_offset(ptr)
632 #else
633 #define SLJIT_EXEC_OFFSET(ptr) 0
634 #endif
635 
636 #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
637 
638 /**********************************************/
639 /* Registers and locals offset determination. */
640 /**********************************************/
641 
642 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
643 
644 #define SLJIT_NUMBER_OF_REGISTERS 12
645 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 9
646 #define SLJIT_LOCALS_OFFSET_BASE (compiler->locals_offset)
647 #define SLJIT_PREF_SHIFT_REG SLJIT_R2
648 
649 #elif (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
650 
651 #define SLJIT_NUMBER_OF_REGISTERS 13
652 #ifndef _WIN64
653 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 6
654 #define SLJIT_LOCALS_OFFSET_BASE 0
655 #else /* _WIN64 */
656 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
657 #define SLJIT_LOCALS_OFFSET_BASE (compiler->locals_offset)
658 #endif /* !_WIN64 */
659 #define SLJIT_PREF_SHIFT_REG SLJIT_R3
660 
661 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
662 
663 #define SLJIT_NUMBER_OF_REGISTERS 12
664 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
665 #define SLJIT_LOCALS_OFFSET_BASE 0
666 
667 #elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
668 
669 #define SLJIT_NUMBER_OF_REGISTERS 12
670 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
671 #define SLJIT_LOCALS_OFFSET_BASE 0
672 
673 #elif (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
674 
675 #define SLJIT_NUMBER_OF_REGISTERS 26
676 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 10
677 #define SLJIT_LOCALS_OFFSET_BASE 0
678 
679 #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
680 
681 #define SLJIT_NUMBER_OF_REGISTERS 23
682 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 17
683 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) || (defined _AIX)
684 #define SLJIT_LOCALS_OFFSET_BASE ((6 + 8) * sizeof(sljit_sw))
685 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
686 /* Add +1 for double alignment. */
687 #define SLJIT_LOCALS_OFFSET_BASE ((3 + 1) * sizeof(sljit_sw))
688 #else
689 #define SLJIT_LOCALS_OFFSET_BASE (3 * sizeof(sljit_sw))
690 #endif /* SLJIT_CONFIG_PPC_64 || _AIX */
691 
692 #elif (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
693 
694 #define SLJIT_NUMBER_OF_REGISTERS 21
695 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
696 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
697 #define SLJIT_LOCALS_OFFSET_BASE (4 * sizeof(sljit_sw))
698 #else
699 #define SLJIT_LOCALS_OFFSET_BASE 0
700 #endif
701 
702 #elif (defined SLJIT_CONFIG_SPARC && SLJIT_CONFIG_SPARC)
703 
704 #define SLJIT_NUMBER_OF_REGISTERS 18
705 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 14
706 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
707 /* saved registers (16), return struct pointer (1), space for 6 argument words (1),
708    4th double arg (2), double alignment (1). */
709 #define SLJIT_LOCALS_OFFSET_BASE ((16 + 1 + 6 + 2 + 1) * sizeof(sljit_sw))
710 #endif
711 
712 #elif (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
713 
714 /*
715  * https://refspecs.linuxbase.org/ELF/zSeries/lzsabi0_zSeries.html#STACKFRAME
716  *
717  * 160
718  *  .. FR6
719  *  .. FR4
720  *  .. FR2
721  * 128 FR0
722  * 120 R15 (used for SP)
723  * 112 R14
724  * 104 R13
725  *  96 R12
726  *  ..
727  *  48 R6
728  *  ..
729  *  16 R2
730  *   8 RESERVED
731  *   0 SP
732  */
733 #define SLJIT_S390X_DEFAULT_STACK_FRAME_SIZE 160
734 
735 #define SLJIT_NUMBER_OF_REGISTERS 12
736 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
737 #define SLJIT_LOCALS_OFFSET_BASE SLJIT_S390X_DEFAULT_STACK_FRAME_SIZE
738 
739 #elif (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
740 
741 #define SLJIT_NUMBER_OF_REGISTERS 0
742 #define SLJIT_NUMBER_OF_SAVED_REGISTERS 0
743 #define SLJIT_LOCALS_OFFSET_BASE 0
744 
745 #endif
746 
747 #define SLJIT_LOCALS_OFFSET (SLJIT_LOCALS_OFFSET_BASE)
748 
749 #define SLJIT_NUMBER_OF_SCRATCH_REGISTERS \
750 	(SLJIT_NUMBER_OF_REGISTERS - SLJIT_NUMBER_OF_SAVED_REGISTERS)
751 
752 #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 6
753 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) && (defined _WIN64)
754 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 1
755 #else
756 #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 0
757 #endif
758 
759 #define SLJIT_NUMBER_OF_SCRATCH_FLOAT_REGISTERS \
760 	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS)
761 
762 /*************************************/
763 /* Debug and verbose related macros. */
764 /*************************************/
765 
766 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
767 
768 #if !defined(SLJIT_ASSERT) || !defined(SLJIT_UNREACHABLE)
769 
770 /* SLJIT_HALT_PROCESS must halt the process. */
771 #ifndef SLJIT_HALT_PROCESS
772 #define SLJIT_HALT_PROCESS() \
773 	abort();
774 #endif /* !SLJIT_HALT_PROCESS */
775 
776 #endif /* !SLJIT_ASSERT || !SLJIT_UNREACHABLE */
777 
778 /* Feel free to redefine these two macros. */
779 #ifndef SLJIT_ASSERT
780 
781 #define SLJIT_ASSERT(x) \
782 	do { \
783 		if (SLJIT_UNLIKELY(!(x))) { \
784 			printf("Assertion failed at " __FILE__ ":%d\n", __LINE__); \
785 			SLJIT_HALT_PROCESS(); \
786 		} \
787 	} while (0)
788 
789 #endif /* !SLJIT_ASSERT */
790 
791 #ifndef SLJIT_UNREACHABLE
792 
793 #define SLJIT_UNREACHABLE() \
794 	do { \
795 		printf("Should never been reached " __FILE__ ":%d\n", __LINE__); \
796 		SLJIT_HALT_PROCESS(); \
797 	} while (0)
798 
799 #endif /* !SLJIT_UNREACHABLE */
800 
801 #else /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
802 
803 /* Forcing empty, but valid statements. */
804 #undef SLJIT_ASSERT
805 #undef SLJIT_UNREACHABLE
806 
807 #define SLJIT_ASSERT(x) \
808 	do { } while (0)
809 #define SLJIT_UNREACHABLE() \
810 	do { } while (0)
811 
812 #endif /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
813 
814 #ifndef SLJIT_COMPILE_ASSERT
815 
816 #define SLJIT_COMPILE_ASSERT(x, description) \
817 	switch(0) { case 0: case ((x) ? 1 : 0): break; }
818 
819 #endif /* !SLJIT_COMPILE_ASSERT */
820 
821 #ifdef __cplusplus
822 } /* extern "C" */
823 #endif
824 
825 #endif /* SLJIT_CONFIG_INTERNAL_H_ */
826