sljitConfigInternal.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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_ZEROMEM
  191. #define SLJIT_ZEROMEM(dest, len) memset(dest, 0, len)
  192. #endif
  193. /***************************/
  194. /* Compiler helper macros. */
  195. /***************************/
  196. #if !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY)
  197. #if defined(__GNUC__) && (__GNUC__ >= 3)
  198. #define SLJIT_LIKELY(x) __builtin_expect((x), 1)
  199. #define SLJIT_UNLIKELY(x) __builtin_expect((x), 0)
  200. #else
  201. #define SLJIT_LIKELY(x) (x)
  202. #define SLJIT_UNLIKELY(x) (x)
  203. #endif
  204. #endif /* !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY) */
  205. #ifndef SLJIT_INLINE
  206. /* Inline functions. Some old compilers do not support them. */
  207. #if defined(__SUNPRO_C) && __SUNPRO_C <= 0x510
  208. #define SLJIT_INLINE
  209. #else
  210. #define SLJIT_INLINE __inline
  211. #endif
  212. #endif /* !SLJIT_INLINE */
  213. #ifndef SLJIT_NOINLINE
  214. /* Not inline functions. */
  215. #if defined(__GNUC__)
  216. #define SLJIT_NOINLINE __attribute__ ((noinline))
  217. #else
  218. #define SLJIT_NOINLINE
  219. #endif
  220. #endif /* !SLJIT_INLINE */
  221. #ifndef SLJIT_UNUSED_ARG
  222. /* Unused arguments. */
  223. #define SLJIT_UNUSED_ARG(arg) (void)arg
  224. #endif
  225. /*********************************/
  226. /* Type of public API functions. */
  227. /*********************************/
  228. #if (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC)
  229. /* Static ABI functions. For all-in-one programs. */
  230. #if defined(__GNUC__)
  231. /* Disable unused warnings in gcc. */
  232. #define SLJIT_API_FUNC_ATTRIBUTE static __attribute__((unused))
  233. #else
  234. #define SLJIT_API_FUNC_ATTRIBUTE static
  235. #endif
  236. #else
  237. #define SLJIT_API_FUNC_ATTRIBUTE
  238. #endif /* (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC) */
  239. /****************************/
  240. /* Instruction cache flush. */
  241. /****************************/
  242. #if (!defined SLJIT_CACHE_FLUSH && defined __has_builtin)
  243. #if __has_builtin(__builtin___clear_cache)
  244. #define SLJIT_CACHE_FLUSH(from, to) \
  245. __builtin___clear_cache((char*)from, (char*)to)
  246. #endif /* __has_builtin(__builtin___clear_cache) */
  247. #endif /* (!defined SLJIT_CACHE_FLUSH && defined __has_builtin) */
  248. #ifndef SLJIT_CACHE_FLUSH
  249. #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
  250. /* Not required to implement on archs with unified caches. */
  251. #define SLJIT_CACHE_FLUSH(from, to)
  252. #elif defined __APPLE__
  253. /* Supported by all macs since Mac OS 10.5.
  254. However, it does not work on non-jailbroken iOS devices,
  255. although the compilation is successful. */
  256. #define SLJIT_CACHE_FLUSH(from, to) \
  257. sys_icache_invalidate((char*)(from), (char*)(to) - (char*)(from))
  258. #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
  259. /* The __clear_cache() implementation of GCC is a dummy function on PowerPC. */
  260. #define SLJIT_CACHE_FLUSH(from, to) \
  261. ppc_cache_flush((from), (to))
  262. #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
  263. #elif (defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  264. #define SLJIT_CACHE_FLUSH(from, to) \
  265. __builtin___clear_cache((char*)from, (char*)to)
  266. #elif defined __ANDROID__
  267. /* Android lacks __clear_cache; instead, cacheflush should be used. */
  268. #define SLJIT_CACHE_FLUSH(from, to) \
  269. cacheflush((long)(from), (long)(to), 0)
  270. #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
  271. /* The __clear_cache() implementation of GCC is a dummy function on Sparc. */
  272. #define SLJIT_CACHE_FLUSH(from, to) \
  273. sparc_cache_flush((from), (to))
  274. #define SLJIT_CACHE_FLUSH_OWN_IMPL 1
  275. #elif defined _WIN32
  276. #define SLJIT_CACHE_FLUSH(from, to) \
  277. FlushInstructionCache(GetCurrentProcess(), (char*)(from), (char*)(to) - (char*)(from))
  278. #else
  279. /* Calls __ARM_NR_cacheflush on ARM-Linux. */
  280. #define SLJIT_CACHE_FLUSH(from, to) \
  281. __clear_cache((char*)(from), (char*)(to))
  282. #endif
  283. #endif /* !SLJIT_CACHE_FLUSH */
  284. /******************************************************/
  285. /* Integer and floating point type definitions. */
  286. /******************************************************/
  287. /* 8 bit byte type. */
  288. typedef unsigned char sljit_u8;
  289. typedef signed char sljit_s8;
  290. /* 16 bit half-word type. */
  291. typedef unsigned short int sljit_u16;
  292. typedef signed short int sljit_s16;
  293. /* 32 bit integer type. */
  294. typedef unsigned int sljit_u32;
  295. typedef signed int sljit_s32;
  296. /* Machine word type. Enough for storing a pointer.
  297. 32 bit for 32 bit machines.
  298. 64 bit for 64 bit machines. */
  299. #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
  300. /* Just to have something. */
  301. #define SLJIT_WORD_SHIFT 0
  302. typedef unsigned long int sljit_uw;
  303. typedef long int sljit_sw;
  304. #elif !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
  305. && !(defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
  306. && !(defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
  307. && !(defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64) \
  308. && !(defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
  309. #define SLJIT_32BIT_ARCHITECTURE 1
  310. #define SLJIT_WORD_SHIFT 2
  311. typedef unsigned int sljit_uw;
  312. typedef int sljit_sw;
  313. #else
  314. #define SLJIT_64BIT_ARCHITECTURE 1
  315. #define SLJIT_WORD_SHIFT 3
  316. #ifdef _WIN32
  317. #ifdef __GNUC__
  318. /* These types do not require windows.h */
  319. typedef unsigned long long sljit_uw;
  320. typedef long long sljit_sw;
  321. #else
  322. typedef unsigned __int64 sljit_uw;
  323. typedef __int64 sljit_sw;
  324. #endif
  325. #else /* !_WIN32 */
  326. typedef unsigned long int sljit_uw;
  327. typedef long int sljit_sw;
  328. #endif /* _WIN32 */
  329. #endif
  330. typedef sljit_uw sljit_p;
  331. /* Floating point types. */
  332. typedef float sljit_f32;
  333. typedef double sljit_f64;
  334. /* Shift for pointer sized data. */
  335. #define SLJIT_POINTER_SHIFT SLJIT_WORD_SHIFT
  336. /* Shift for double precision sized data. */
  337. #define SLJIT_F32_SHIFT 2
  338. #define SLJIT_F64_SHIFT 3
  339. #ifndef SLJIT_W
  340. /* Defining long constants. */
  341. #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
  342. #define SLJIT_W(w) (w##l)
  343. #elif (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
  344. #define SLJIT_W(w) (w##ll)
  345. #else
  346. #define SLJIT_W(w) (w)
  347. #endif
  348. #endif /* !SLJIT_W */
  349. /*************************/
  350. /* Endianness detection. */
  351. /*************************/
  352. #if !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN)
  353. /* These macros are mostly useful for the applications. */
  354. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
  355. || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  356. #ifdef __LITTLE_ENDIAN__
  357. #define SLJIT_LITTLE_ENDIAN 1
  358. #else
  359. #define SLJIT_BIG_ENDIAN 1
  360. #endif
  361. #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
  362. || (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  363. #ifdef __MIPSEL__
  364. #define SLJIT_LITTLE_ENDIAN 1
  365. #else
  366. #define SLJIT_BIG_ENDIAN 1
  367. #endif
  368. #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
  369. #define SLJIT_BIG_ENDIAN 1
  370. #else
  371. #define SLJIT_LITTLE_ENDIAN 1
  372. #endif
  373. #endif /* !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN) */
  374. /* Sanity check. */
  375. #if (defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
  376. #error "Exactly one endianness must be selected"
  377. #endif
  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. #ifndef SLJIT_UNALIGNED
  382. #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
  383. || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
  384. || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
  385. || (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
  386. || (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) \
  387. || (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
  388. || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  389. #define SLJIT_UNALIGNED 1
  390. #endif
  391. #endif /* !SLJIT_UNALIGNED */
  392. #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
  393. /* Auto detect SSE2 support using CPUID.
  394. On 64 bit x86 cpus, sse2 must be present. */
  395. #define SLJIT_DETECT_SSE2 1
  396. #endif
  397. /*****************************************************************************************/
  398. /* Calling convention of functions generated by SLJIT or called from the generated code. */
  399. /*****************************************************************************************/
  400. #ifndef SLJIT_FUNC
  401. #if (defined SLJIT_USE_CDECL_CALLING_CONVENTION && SLJIT_USE_CDECL_CALLING_CONVENTION)
  402. /* Force cdecl. */
  403. #define SLJIT_FUNC
  404. #elif (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
  405. #if defined(__GNUC__) && !defined(__APPLE__)
  406. #define SLJIT_FUNC __attribute__ ((fastcall))
  407. #define SLJIT_X86_32_FASTCALL 1
  408. #elif defined(_MSC_VER)
  409. #define SLJIT_FUNC __fastcall
  410. #define SLJIT_X86_32_FASTCALL 1
  411. #elif defined(__BORLANDC__)
  412. #define SLJIT_FUNC __msfastcall
  413. #define SLJIT_X86_32_FASTCALL 1
  414. #else /* Unknown compiler. */
  415. /* The cdecl attribute is the default. */
  416. #define SLJIT_FUNC
  417. #endif
  418. #else /* Non x86-32 architectures. */
  419. #define SLJIT_FUNC
  420. #endif /* SLJIT_CONFIG_X86_32 */
  421. #endif /* !SLJIT_FUNC */
  422. #ifndef SLJIT_INDIRECT_CALL
  423. #if ((defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) && (!defined _CALL_ELF || _CALL_ELF == 1)) \
  424. || ((defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) && defined _AIX)
  425. /* It seems certain ppc compilers use an indirect addressing for functions
  426. which makes things complicated. */
  427. #define SLJIT_INDIRECT_CALL 1
  428. #endif
  429. #endif /* SLJIT_INDIRECT_CALL */
  430. /* The offset which needs to be substracted from the return address to
  431. determine the next executed instruction after return. */
  432. #ifndef SLJIT_RETURN_ADDRESS_OFFSET
  433. #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
  434. #define SLJIT_RETURN_ADDRESS_OFFSET 8
  435. #else
  436. #define SLJIT_RETURN_ADDRESS_OFFSET 0
  437. #endif
  438. #endif /* SLJIT_RETURN_ADDRESS_OFFSET */
  439. /***************************************************/
  440. /* Functions of the built-in executable allocator. */
  441. /***************************************************/
  442. #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
  443. SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size);
  444. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr);
  445. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
  446. #define SLJIT_MALLOC_EXEC(size) sljit_malloc_exec(size)
  447. #define SLJIT_FREE_EXEC(ptr) sljit_free_exec(ptr)
  448. #if (defined SLJIT_PROT_EXECUTABLE_ALLOCATOR && SLJIT_PROT_EXECUTABLE_ALLOCATOR)
  449. SLJIT_API_FUNC_ATTRIBUTE sljit_sw sljit_exec_offset(void* ptr);
  450. #define SLJIT_EXEC_OFFSET(ptr) sljit_exec_offset(ptr)
  451. #else
  452. #define SLJIT_EXEC_OFFSET(ptr) 0
  453. #endif
  454. #endif
  455. /**********************************************/
  456. /* Registers and locals offset determination. */
  457. /**********************************************/
  458. #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
  459. #define SLJIT_NUMBER_OF_REGISTERS 12
  460. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 9
  461. #define SLJIT_LOCALS_OFFSET_BASE (compiler->locals_offset)
  462. #define SLJIT_PREF_SHIFT_REG SLJIT_R2
  463. #elif (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
  464. #define SLJIT_NUMBER_OF_REGISTERS 13
  465. #ifndef _WIN64
  466. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 6
  467. #define SLJIT_LOCALS_OFFSET_BASE 0
  468. #else /* _WIN64 */
  469. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
  470. #define SLJIT_LOCALS_OFFSET_BASE (compiler->locals_offset)
  471. #endif /* !_WIN64 */
  472. #define SLJIT_PREF_SHIFT_REG SLJIT_R3
  473. #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  474. #define SLJIT_NUMBER_OF_REGISTERS 12
  475. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
  476. #define SLJIT_LOCALS_OFFSET_BASE 0
  477. #elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
  478. #define SLJIT_NUMBER_OF_REGISTERS 12
  479. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
  480. #define SLJIT_LOCALS_OFFSET_BASE 0
  481. #elif (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
  482. #define SLJIT_NUMBER_OF_REGISTERS 26
  483. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 10
  484. #define SLJIT_LOCALS_OFFSET_BASE 0
  485. #elif (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
  486. #define SLJIT_NUMBER_OF_REGISTERS 23
  487. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 17
  488. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) || (defined _AIX)
  489. #define SLJIT_LOCALS_OFFSET_BASE ((6 + 8) * sizeof(sljit_sw))
  490. #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  491. /* Add +1 for double alignment. */
  492. #define SLJIT_LOCALS_OFFSET_BASE ((3 + 1) * sizeof(sljit_sw))
  493. #else
  494. #define SLJIT_LOCALS_OFFSET_BASE (3 * sizeof(sljit_sw))
  495. #endif /* SLJIT_CONFIG_PPC_64 || _AIX */
  496. #elif (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
  497. #define SLJIT_NUMBER_OF_REGISTERS 21
  498. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 8
  499. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  500. #define SLJIT_LOCALS_OFFSET_BASE (4 * sizeof(sljit_sw))
  501. #else
  502. #define SLJIT_LOCALS_OFFSET_BASE 0
  503. #endif
  504. #elif (defined SLJIT_CONFIG_SPARC && SLJIT_CONFIG_SPARC)
  505. #define SLJIT_NUMBER_OF_REGISTERS 18
  506. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 14
  507. #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
  508. /* saved registers (16), return struct pointer (1), space for 6 argument words (1),
  509. 4th double arg (2), double alignment (1). */
  510. #define SLJIT_LOCALS_OFFSET_BASE ((16 + 1 + 6 + 2 + 1) * sizeof(sljit_sw))
  511. #endif
  512. #elif (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
  513. #define SLJIT_NUMBER_OF_REGISTERS 10
  514. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 5
  515. #define SLJIT_LOCALS_OFFSET_BASE 0
  516. #elif (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
  517. #define SLJIT_NUMBER_OF_REGISTERS 0
  518. #define SLJIT_NUMBER_OF_SAVED_REGISTERS 0
  519. #define SLJIT_LOCALS_OFFSET_BASE 0
  520. #endif
  521. #define SLJIT_LOCALS_OFFSET (SLJIT_LOCALS_OFFSET_BASE)
  522. #define SLJIT_NUMBER_OF_SCRATCH_REGISTERS \
  523. (SLJIT_NUMBER_OF_REGISTERS - SLJIT_NUMBER_OF_SAVED_REGISTERS)
  524. #define SLJIT_NUMBER_OF_FLOAT_REGISTERS 6
  525. #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) && (defined _WIN64)
  526. #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 1
  527. #else
  528. #define SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS 0
  529. #endif
  530. #define SLJIT_NUMBER_OF_SCRATCH_FLOAT_REGISTERS \
  531. (SLJIT_NUMBER_OF_FLOAT_REGISTERS - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS)
  532. /*************************************/
  533. /* Debug and verbose related macros. */
  534. /*************************************/
  535. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
  536. #include <stdio.h>
  537. #endif
  538. #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
  539. #if !defined(SLJIT_ASSERT) || !defined(SLJIT_UNREACHABLE)
  540. /* SLJIT_HALT_PROCESS must halt the process. */
  541. #ifndef SLJIT_HALT_PROCESS
  542. #include <stdlib.h>
  543. #define SLJIT_HALT_PROCESS() \
  544. abort();
  545. #endif /* !SLJIT_HALT_PROCESS */
  546. #include <stdio.h>
  547. #endif /* !SLJIT_ASSERT || !SLJIT_UNREACHABLE */
  548. /* Feel free to redefine these two macros. */
  549. #ifndef SLJIT_ASSERT
  550. #define SLJIT_ASSERT(x) \
  551. do { \
  552. if (SLJIT_UNLIKELY(!(x))) { \
  553. printf("Assertion failed at " __FILE__ ":%d\n", __LINE__); \
  554. SLJIT_HALT_PROCESS(); \
  555. } \
  556. } while (0)
  557. #endif /* !SLJIT_ASSERT */
  558. #ifndef SLJIT_UNREACHABLE
  559. #define SLJIT_UNREACHABLE() \
  560. do { \
  561. printf("Should never been reached " __FILE__ ":%d\n", __LINE__); \
  562. SLJIT_HALT_PROCESS(); \
  563. } while (0)
  564. #endif /* !SLJIT_UNREACHABLE */
  565. #else /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
  566. /* Forcing empty, but valid statements. */
  567. #undef SLJIT_ASSERT
  568. #undef SLJIT_UNREACHABLE
  569. #define SLJIT_ASSERT(x) \
  570. do { } while (0)
  571. #define SLJIT_UNREACHABLE() \
  572. do { } while (0)
  573. #endif /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
  574. #ifndef SLJIT_COMPILE_ASSERT
  575. #define SLJIT_COMPILE_ASSERT(x, description) \
  576. switch(0) { case 0: case ((x) ? 1 : 0): break; }
  577. #endif /* !SLJIT_COMPILE_ASSERT */
  578. #endif