lj_def.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. ** LuaJIT common internal definitions.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_DEF_H
  6. #define _LJ_DEF_H
  7. #include "lua.h"
  8. #if defined(_MSC_VER) && (_MSC_VER < 1700)
  9. /* Old MSVC is stuck in the last century and doesn't have C99's stdint.h. */
  10. typedef __int8 int8_t;
  11. typedef __int16 int16_t;
  12. typedef __int32 int32_t;
  13. typedef __int64 int64_t;
  14. typedef unsigned __int8 uint8_t;
  15. typedef unsigned __int16 uint16_t;
  16. typedef unsigned __int32 uint32_t;
  17. typedef unsigned __int64 uint64_t;
  18. #ifdef _WIN64
  19. typedef __int64 intptr_t;
  20. typedef unsigned __int64 uintptr_t;
  21. #else
  22. typedef __int32 intptr_t;
  23. typedef unsigned __int32 uintptr_t;
  24. #endif
  25. #elif defined(__symbian__)
  26. /* Cough. */
  27. typedef signed char int8_t;
  28. typedef short int int16_t;
  29. typedef int int32_t;
  30. typedef long long int64_t;
  31. typedef unsigned char uint8_t;
  32. typedef unsigned short int uint16_t;
  33. typedef unsigned int uint32_t;
  34. typedef unsigned long long uint64_t;
  35. typedef int intptr_t;
  36. typedef unsigned int uintptr_t;
  37. #else
  38. #include <stdint.h>
  39. #endif
  40. /* Needed everywhere. */
  41. #include <string.h>
  42. #include <stdlib.h>
  43. /* Various VM limits. */
  44. #define LJ_MAX_MEM32 0x7fffff00 /* Max. 32 bit memory allocation. */
  45. #define LJ_MAX_MEM64 ((uint64_t)1<<47) /* Max. 64 bit memory allocation. */
  46. /* Max. total memory allocation. */
  47. #define LJ_MAX_MEM (LJ_GC64 ? LJ_MAX_MEM64 : LJ_MAX_MEM32)
  48. #define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */
  49. #define LJ_MAX_STR LJ_MAX_MEM32 /* Max. string length. */
  50. #define LJ_MAX_BUF LJ_MAX_MEM32 /* Max. buffer length. */
  51. #define LJ_MAX_UDATA LJ_MAX_MEM32 /* Max. userdata length. */
  52. #define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */
  53. #define LJ_MAX_HBITS 26 /* Max. hash bits. */
  54. #define LJ_MAX_ABITS 28 /* Max. bits of array key. */
  55. #define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */
  56. #define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */
  57. #define LJ_MAX_LINE LJ_MAX_MEM32 /* Max. source code line number. */
  58. #define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */
  59. #define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */
  60. #define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */
  61. #define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */
  62. #define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */
  63. #define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */
  64. #define LJ_STACK_EXTRA (5+3*LJ_FR2) /* Extra stack space (metamethods). */
  65. #define LJ_NUM_CBPAGE 1 /* Number of FFI callback pages. */
  66. /* Minimum table/buffer sizes. */
  67. #define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */
  68. #define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). */
  69. #define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */
  70. #define LJ_MIN_SBUF 32 /* Min. string buffer length. */
  71. #define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */
  72. #define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */
  73. /* JIT compiler limits. */
  74. #define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */
  75. #define LJ_MAX_PHI 64 /* Max. # of PHIs for a loop. */
  76. #define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */
  77. /* Various macros. */
  78. #ifndef UNUSED
  79. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  80. #endif
  81. #define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo)
  82. #define i32ptr(p) ((int32_t)(intptr_t)(void *)(p))
  83. #define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p))
  84. #define i64ptr(p) ((int64_t)(intptr_t)(void *)(p))
  85. #define u64ptr(p) ((uint64_t)(intptr_t)(void *)(p))
  86. #define igcptr(p) (LJ_GC64 ? i64ptr(p) : i32ptr(p))
  87. #define checki8(x) ((x) == (int32_t)(int8_t)(x))
  88. #define checku8(x) ((x) == (int32_t)(uint8_t)(x))
  89. #define checki16(x) ((x) == (int32_t)(int16_t)(x))
  90. #define checku16(x) ((x) == (int32_t)(uint16_t)(x))
  91. #define checki32(x) ((x) == (int32_t)(x))
  92. #define checku32(x) ((x) == (uint32_t)(x))
  93. #define checkptr31(x) (((uint64_t)(uintptr_t)(x) >> 31) == 0)
  94. #define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x))
  95. #define checkptr47(x) (((uint64_t)(uintptr_t)(x) >> 47) == 0)
  96. #define checkptrGC(x) (LJ_GC64 ? checkptr47((x)) : LJ_64 ? checkptr31((x)) :1)
  97. /* Every half-decent C compiler transforms this into a rotate instruction. */
  98. #define lj_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1))))
  99. #define lj_ror(x, n) (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n)))
  100. /* A really naive Bloom filter. But sufficient for our needs. */
  101. typedef uintptr_t BloomFilter;
  102. #define BLOOM_MASK (8*sizeof(BloomFilter) - 1)
  103. #define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK))
  104. #define bloomset(b, x) ((b) |= bloombit((x)))
  105. #define bloomtest(b, x) ((b) & bloombit((x)))
  106. #if defined(__GNUC__) || defined(__clang__) || defined(__psp2__)
  107. #define LJ_NORET __attribute__((noreturn))
  108. #define LJ_ALIGN(n) __attribute__((aligned(n)))
  109. #define LJ_INLINE inline
  110. #define LJ_AINLINE inline __attribute__((always_inline))
  111. #define LJ_NOINLINE __attribute__((noinline))
  112. #if defined(__ELF__) || defined(__MACH__) || defined(__psp2__)
  113. #if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__))
  114. #define LJ_NOAPI extern __attribute__((visibility("hidden")))
  115. #endif
  116. #endif
  117. /* Note: it's only beneficial to use fastcall on x86 and then only for up to
  118. ** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only
  119. ** indirect calls and related tail-called C functions are marked as fastcall.
  120. */
  121. #if defined(__i386__)
  122. #define LJ_FASTCALL __attribute__((fastcall))
  123. #endif
  124. #define LJ_LIKELY(x) __builtin_expect(!!(x), 1)
  125. #define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0)
  126. #define lj_ffs(x) ((uint32_t)__builtin_ctz(x))
  127. #define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31))
  128. #define lj_ffs64(x) ((uint32_t)__builtin_ctzll(x))
  129. #define lj_fls64(x) ((uint32_t)(__builtin_clzll(x)^63))
  130. #if defined(__arm__)
  131. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  132. {
  133. #if defined(__psp2__)
  134. return __builtin_rev(x);
  135. #else
  136. uint32_t r;
  137. #if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\
  138. __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__
  139. __asm__("rev %0, %1" : "=r" (r) : "r" (x));
  140. return r;
  141. #else
  142. #ifdef __thumb__
  143. r = x ^ lj_ror(x, 16);
  144. #else
  145. __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x));
  146. #endif
  147. return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8);
  148. #endif
  149. #endif
  150. }
  151. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  152. {
  153. return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));
  154. }
  155. #elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __clang__
  156. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  157. {
  158. return (uint32_t)__builtin_bswap32((int32_t)x);
  159. }
  160. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  161. {
  162. return (uint64_t)__builtin_bswap64((int64_t)x);
  163. }
  164. #elif defined(__i386__) || defined(__x86_64__)
  165. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  166. {
  167. uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;
  168. }
  169. #if defined(__i386__)
  170. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  171. {
  172. return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));
  173. }
  174. #else
  175. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  176. {
  177. uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;
  178. }
  179. #endif
  180. #else
  181. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  182. {
  183. return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24);
  184. }
  185. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  186. {
  187. return (uint64_t)lj_bswap((uint32_t)(x >> 32)) |
  188. ((uint64_t)lj_bswap((uint32_t)x) << 32);
  189. }
  190. #endif
  191. typedef union __attribute__((packed)) Unaligned16 {
  192. uint16_t u;
  193. uint8_t b[2];
  194. } Unaligned16;
  195. typedef union __attribute__((packed)) Unaligned32 {
  196. uint32_t u;
  197. uint8_t b[4];
  198. } Unaligned32;
  199. /* Unaligned load of uint16_t. */
  200. static LJ_AINLINE uint16_t lj_getu16(const void *p)
  201. {
  202. return ((const Unaligned16 *)p)->u;
  203. }
  204. /* Unaligned load of uint32_t. */
  205. static LJ_AINLINE uint32_t lj_getu32(const void *p)
  206. {
  207. return ((const Unaligned32 *)p)->u;
  208. }
  209. #elif defined(_MSC_VER)
  210. #define LJ_NORET __declspec(noreturn)
  211. #define LJ_ALIGN(n) __declspec(align(n))
  212. #define LJ_INLINE __inline
  213. #define LJ_AINLINE __forceinline
  214. #define LJ_NOINLINE __declspec(noinline)
  215. #if defined(_M_IX86)
  216. #define LJ_FASTCALL __fastcall
  217. #endif
  218. #ifdef _M_PPC
  219. unsigned int _CountLeadingZeros(long);
  220. #pragma intrinsic(_CountLeadingZeros)
  221. static LJ_AINLINE uint32_t lj_fls(uint32_t x)
  222. {
  223. return _CountLeadingZeros(x) ^ 31;
  224. }
  225. #else
  226. unsigned char _BitScanForward(unsigned long *, unsigned long);
  227. unsigned char _BitScanReverse(unsigned long *, unsigned long);
  228. #pragma intrinsic(_BitScanForward)
  229. #pragma intrinsic(_BitScanReverse)
  230. static LJ_AINLINE uint32_t lj_ffs(uint32_t x)
  231. {
  232. unsigned long r; _BitScanForward(&r, x); return (uint32_t)r;
  233. }
  234. static LJ_AINLINE uint32_t lj_fls(uint32_t x)
  235. {
  236. unsigned long r; _BitScanReverse(&r, x); return (uint32_t)r;
  237. }
  238. #if defined(_M_X64) || defined(_M_ARM64)
  239. unsigned char _BitScanForward64(unsigned long *, uint64_t);
  240. unsigned char _BitScanReverse64(unsigned long *, uint64_t);
  241. #pragma intrinsic(_BitScanForward64)
  242. #pragma intrinsic(_BitScanReverse64)
  243. static LJ_AINLINE uint32_t lj_ffs64(uint64_t x)
  244. {
  245. unsigned long r; _BitScanForward64(&r, x); return (uint32_t)r;
  246. }
  247. static LJ_AINLINE uint32_t lj_fls64(uint64_t x)
  248. {
  249. unsigned long r; _BitScanReverse64(&r, x); return (uint32_t)r;
  250. }
  251. #endif
  252. #endif
  253. unsigned long _byteswap_ulong(unsigned long);
  254. uint64_t _byteswap_uint64(uint64_t);
  255. #define lj_bswap(x) (_byteswap_ulong((x)))
  256. #define lj_bswap64(x) (_byteswap_uint64((x)))
  257. #if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED)
  258. /*
  259. ** Replacement for unaligned loads on Xbox 360. Disabled by default since it's
  260. ** usually more costly than the occasional stall when crossing a cache-line.
  261. */
  262. static LJ_AINLINE uint16_t lj_getu16(const void *v)
  263. {
  264. const uint8_t *p = (const uint8_t *)v;
  265. return (uint16_t)((p[0]<<8) | p[1]);
  266. }
  267. static LJ_AINLINE uint32_t lj_getu32(const void *v)
  268. {
  269. const uint8_t *p = (const uint8_t *)v;
  270. return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]);
  271. }
  272. #else
  273. /* Unaligned loads are generally ok on x86/x64. */
  274. #define lj_getu16(p) (*(uint16_t *)(p))
  275. #define lj_getu32(p) (*(uint32_t *)(p))
  276. #endif
  277. #else
  278. #error "missing defines for your compiler"
  279. #endif
  280. /* Optional defines. */
  281. #ifndef LJ_FASTCALL
  282. #define LJ_FASTCALL
  283. #endif
  284. #ifndef LJ_NORET
  285. #define LJ_NORET
  286. #endif
  287. #ifndef LJ_NOAPI
  288. #define LJ_NOAPI extern
  289. #endif
  290. #ifndef LJ_LIKELY
  291. #define LJ_LIKELY(x) (x)
  292. #define LJ_UNLIKELY(x) (x)
  293. #endif
  294. /* Attributes for internal functions. */
  295. #define LJ_DATA LJ_NOAPI
  296. #define LJ_DATADEF
  297. #define LJ_ASMF LJ_NOAPI
  298. #define LJ_FUNCA LJ_NOAPI
  299. #if defined(ljamalg_c)
  300. #define LJ_FUNC static
  301. #else
  302. #define LJ_FUNC LJ_NOAPI
  303. #endif
  304. #define LJ_FUNC_NORET LJ_FUNC LJ_NORET
  305. #define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET
  306. #define LJ_ASMF_NORET LJ_ASMF LJ_NORET
  307. /* Internal assertions. */
  308. #if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK)
  309. #define lj_assert_check(g, c, ...) \
  310. ((c) ? (void)0 : \
  311. (lj_assert_fail((g), __FILE__, __LINE__, __func__, __VA_ARGS__), 0))
  312. #define lj_checkapi(c, ...) lj_assert_check(G(L), (c), __VA_ARGS__)
  313. #else
  314. #define lj_checkapi(c, ...) ((void)L)
  315. #endif
  316. #ifdef LUA_USE_ASSERT
  317. #define lj_assertG_(g, c, ...) lj_assert_check((g), (c), __VA_ARGS__)
  318. #define lj_assertG(c, ...) lj_assert_check(g, (c), __VA_ARGS__)
  319. #define lj_assertL(c, ...) lj_assert_check(G(L), (c), __VA_ARGS__)
  320. #define lj_assertX(c, ...) lj_assert_check(NULL, (c), __VA_ARGS__)
  321. #define check_exp(c, e) (lj_assertX((c), #c), (e))
  322. #else
  323. #define lj_assertG_(g, c, ...) ((void)0)
  324. #define lj_assertG(c, ...) ((void)g)
  325. #define lj_assertL(c, ...) ((void)L)
  326. #define lj_assertX(c, ...) ((void)0)
  327. #define check_exp(c, e) (e)
  328. #endif
  329. /* Static assertions. */
  330. #define LJ_ASSERT_NAME2(name, line) name ## line
  331. #define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line)
  332. #ifdef __COUNTER__
  333. #define LJ_STATIC_ASSERT(cond) \
  334. extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
  335. #else
  336. #define LJ_STATIC_ASSERT(cond) \
  337. extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
  338. #endif
  339. /* PRNG state. Need this here, details in lj_prng.h. */
  340. typedef struct PRNGState {
  341. uint64_t u[4];
  342. } PRNGState;
  343. #endif