sljitConfigInternal.h 27 KB

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