sljitConfigInternal.h 24 KB

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