2 ** LuaJIT common internal definitions.
3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
11 #if defined(_MSC_VER) && (_MSC_VER < 1700)
12 /* Old MSVC is stuck in the last century and doesn't have C99's stdint.h. */
13 typedef __int8
int8_t;
14 typedef __int16
int16_t;
15 typedef __int32
int32_t;
16 typedef __int64
int64_t;
17 typedef unsigned __int8
uint8_t;
18 typedef unsigned __int16
uint16_t;
19 typedef unsigned __int32
uint32_t;
20 typedef unsigned __int64
uint64_t;
22 typedef __int64
intptr_t;
23 typedef unsigned __int64
uintptr_t;
25 typedef __int32
intptr_t;
26 typedef unsigned __int32
uintptr_t;
28 #elif defined(__symbian__)
30 typedef signed char int8_t;
31 typedef short int int16_t;
33 typedef long long int64_t;
34 typedef unsigned char uint8_t;
35 typedef unsigned short int uint16_t;
36 typedef unsigned int uint32_t;
37 typedef unsigned long long uint64_t;
39 typedef unsigned int uintptr_t;
44 /* Needed everywhere. */
48 /* Various VM limits. */
49 #define LJ_MAX_MEM32 0x7fffff00 /* Max. 32 bit memory allocation. */
50 #define LJ_MAX_MEM64 ((uint64_t)1<<47) /* Max. 64 bit memory allocation. */
51 /* Max. total memory allocation. */
52 #define LJ_MAX_MEM (LJ_GC64 ? LJ_MAX_MEM64 : LJ_MAX_MEM32)
53 #define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */
54 #define LJ_MAX_STR LJ_MAX_MEM32 /* Max. string length. */
55 #define LJ_MAX_BUF LJ_MAX_MEM32 /* Max. buffer length. */
56 #define LJ_MAX_UDATA LJ_MAX_MEM32 /* Max. userdata length. */
58 #define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */
59 #define LJ_MAX_HBITS 26 /* Max. hash bits. */
60 #define LJ_MAX_ABITS 28 /* Max. bits of array key. */
61 #define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */
62 #define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */
64 #define LJ_MAX_LINE LJ_MAX_MEM32 /* Max. source code line number. */
65 #define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */
66 #define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */
67 #define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */
68 #define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */
69 #define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */
71 #define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */
72 #define LJ_STACK_EXTRA (5+3*LJ_FR2) /* Extra stack space (metamethods). */
74 #define LJ_NUM_CBPAGE 1 /* Number of FFI callback pages. */
76 /* Minimum table/buffer sizes. */
77 #define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */
78 #define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). */
79 #define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */
80 #define LJ_MIN_SBUF 32 /* Min. string buffer length. */
81 #define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */
82 #define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */
84 /* JIT compiler limits. */
85 #define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */
86 #define LJ_MAX_PHI 64 /* Max. # of PHIs for a loop. */
87 #define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */
91 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
94 #define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo)
95 #define i32ptr(p) ((int32_t)(intptr_t)(void *)(p))
96 #define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p))
97 #define i64ptr(p) ((int64_t)(intptr_t)(void *)(p))
98 #define u64ptr(p) ((uint64_t)(intptr_t)(void *)(p))
99 #define igcptr(p) (LJ_GC64 ? i64ptr(p) : i32ptr(p))
101 #define checki8(x) ((x) == (int32_t)(int8_t)(x))
102 #define checku8(x) ((x) == (int32_t)(uint8_t)(x))
103 #define checki16(x) ((x) == (int32_t)(int16_t)(x))
104 #define checku16(x) ((x) == (int32_t)(uint16_t)(x))
105 #define checki32(x) ((x) == (int32_t)(x))
106 #define checku32(x) ((x) == (uint32_t)(x))
107 #define checkptr31(x) (((uint64_t)(uintptr_t)(x) >> 31) == 0)
108 #define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x))
109 #define checkptr47(x) (((uint64_t)(uintptr_t)(x) >> 47) == 0)
110 #define checkptrGC(x) (LJ_GC64 ? checkptr47((x)) : LJ_64 ? checkptr31((x)) :1)
112 /* Every half-decent C compiler transforms this into a rotate instruction. */
113 #define lj_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1))))
114 #define lj_ror(x, n) (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n)))
116 /* A really naive Bloom filter. But sufficient for our needs. */
117 typedef uintptr_t BloomFilter
;
118 #define BLOOM_MASK (8*sizeof(BloomFilter) - 1)
119 #define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK))
120 #define bloomset(b, x) ((b) |= bloombit((x)))
121 #define bloomtest(b, x) ((b) & bloombit((x)))
123 #if defined(__GNUC__) || defined(__clang__) || defined(__psp2__)
125 #define LJ_NORET __attribute__((noreturn))
126 #define LJ_ALIGN(n) __attribute__((aligned(n)))
127 #define LJ_INLINE inline
128 #define LJ_AINLINE inline __attribute__((always_inline))
129 #define LJ_NOINLINE __attribute__((noinline))
131 #if defined(__ELF__) || defined(__MACH__) || defined(__psp2__)
132 #if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__))
133 #define LJ_NOAPI extern __attribute__((visibility("hidden")))
137 /* Note: it's only beneficial to use fastcall on x86 and then only for up to
138 ** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only
139 ** indirect calls and related tail-called C functions are marked as fastcall.
141 #if defined(__i386__)
142 #define LJ_FASTCALL __attribute__((fastcall))
145 #define LJ_LIKELY(x) __builtin_expect(!!(x), 1)
146 #define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0)
148 #define lj_ffs(x) ((uint32_t)__builtin_ctz(x))
149 #define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31))
150 #define lj_ffs64(x) ((uint32_t)__builtin_ctzll(x))
151 #define lj_fls64(x) ((uint32_t)(__builtin_clzll(x)^63))
154 static LJ_AINLINE
uint32_t lj_bswap(uint32_t x
)
156 #if defined(__psp2__)
157 return __builtin_rev(x
);
160 #if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\
161 __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__
162 __asm__("rev %0, %1" : "=r" (r
) : "r" (x
));
166 r
= x
^ lj_ror(x
, 16);
168 __asm__("eor %0, %1, %1, ror #16" : "=r" (r
) : "r" (x
));
170 return ((r
& 0xff00ffffu
) >> 8) ^ lj_ror(x
, 8);
175 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
177 return ((uint64_t)lj_bswap((uint32_t)x
)<<32) | lj_bswap((uint32_t)(x
>>32));
179 #elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __clang__
180 static LJ_AINLINE
uint32_t lj_bswap(uint32_t x
)
182 return (uint32_t)__builtin_bswap32((int32_t)x
);
185 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
187 return (uint64_t)__builtin_bswap64((int64_t)x
);
189 #elif defined(__i386__) || defined(__x86_64__)
190 static LJ_AINLINE
uint32_t lj_bswap(uint32_t x
)
192 uint32_t r
; __asm__("bswap %0" : "=r" (r
) : "0" (x
)); return r
;
195 #if defined(__i386__)
196 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
198 return ((uint64_t)lj_bswap((uint32_t)x
)<<32) | lj_bswap((uint32_t)(x
>>32));
201 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
203 uint64_t r
; __asm__("bswap %0" : "=r" (r
) : "0" (x
)); return r
;
207 static LJ_AINLINE
uint32_t lj_bswap(uint32_t x
)
209 return (x
<< 24) | ((x
& 0xff00) << 8) | ((x
>> 8) & 0xff00) | (x
>> 24);
212 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
214 return (uint64_t)lj_bswap((uint32_t)(x
>> 32)) |
215 ((uint64_t)lj_bswap((uint32_t)x
) << 32);
219 typedef union __attribute__((packed
)) Unaligned16
{
224 typedef union __attribute__((packed
)) Unaligned32
{
229 /* Unaligned load of uint16_t. */
230 static LJ_AINLINE
uint16_t lj_getu16(const void *p
)
232 return ((const Unaligned16
*)p
)->u
;
235 /* Unaligned load of uint32_t. */
236 static LJ_AINLINE
uint32_t lj_getu32(const void *p
)
238 return ((const Unaligned32
*)p
)->u
;
241 #elif defined(_MSC_VER)
243 #define LJ_NORET __declspec(noreturn)
244 #define LJ_ALIGN(n) __declspec(align(n))
245 #define LJ_INLINE __inline
246 #define LJ_AINLINE __forceinline
247 #define LJ_NOINLINE __declspec(noinline)
249 #define LJ_FASTCALL __fastcall
253 unsigned int _CountLeadingZeros(long);
254 #pragma intrinsic(_CountLeadingZeros)
255 static LJ_AINLINE
uint32_t lj_fls(uint32_t x
)
257 return _CountLeadingZeros(x
) ^ 31;
260 unsigned char _BitScanForward(unsigned long *, unsigned long);
261 unsigned char _BitScanReverse(unsigned long *, unsigned long);
262 #pragma intrinsic(_BitScanForward)
263 #pragma intrinsic(_BitScanReverse)
265 static LJ_AINLINE
uint32_t lj_ffs(uint32_t x
)
267 unsigned long r
; _BitScanForward(&r
, x
); return (uint32_t)r
;
270 static LJ_AINLINE
uint32_t lj_fls(uint32_t x
)
272 unsigned long r
; _BitScanReverse(&r
, x
); return (uint32_t)r
;
275 #if defined(_M_X64) || defined(_M_ARM64)
276 unsigned char _BitScanForward64(unsigned long *, uint64_t);
277 unsigned char _BitScanReverse64(unsigned long *, uint64_t);
278 #pragma intrinsic(_BitScanForward64)
279 #pragma intrinsic(_BitScanReverse64)
281 static LJ_AINLINE
uint32_t lj_ffs64(uint64_t x
)
283 unsigned long r
; _BitScanForward64(&r
, x
); return (uint32_t)r
;
286 static LJ_AINLINE
uint32_t lj_fls64(uint64_t x
)
288 unsigned long r
; _BitScanReverse64(&r
, x
); return (uint32_t)r
;
293 unsigned long _byteswap_ulong(unsigned long);
294 uint64_t _byteswap_uint64(uint64_t);
295 #define lj_bswap(x) (_byteswap_ulong((x)))
296 #define lj_bswap64(x) (_byteswap_uint64((x)))
298 #if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED)
300 ** Replacement for unaligned loads on Xbox 360. Disabled by default since it's
301 ** usually more costly than the occasional stall when crossing a cache-line.
303 static LJ_AINLINE
uint16_t lj_getu16(const void *v
)
305 const uint8_t *p
= (const uint8_t *)v
;
306 return (uint16_t)((p
[0]<<8) | p
[1]);
308 static LJ_AINLINE
uint32_t lj_getu32(const void *v
)
310 const uint8_t *p
= (const uint8_t *)v
;
311 return (uint32_t)((p
[0]<<24) | (p
[1]<<16) | (p
[2]<<8) | p
[3]);
314 /* Unaligned loads are generally ok on x86/x64. */
315 #define lj_getu16(p) (*(uint16_t *)(p))
316 #define lj_getu32(p) (*(uint32_t *)(p))
320 #error "missing defines for your compiler"
323 /* Optional defines. */
331 #define LJ_NOAPI extern
334 #define LJ_LIKELY(x) (x)
335 #define LJ_UNLIKELY(x) (x)
338 /* Attributes for internal functions. */
339 #define LJ_DATA LJ_NOAPI
341 #define LJ_ASMF LJ_NOAPI
342 #define LJ_FUNCA LJ_NOAPI
343 #if defined(ljamalg_c)
344 #define LJ_FUNC static
346 #define LJ_FUNC LJ_NOAPI
348 #define LJ_FUNC_NORET LJ_FUNC LJ_NORET
349 #define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET
350 #define LJ_ASMF_NORET LJ_ASMF LJ_NORET
352 /* Internal assertions. */
353 #if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK)
354 #define lj_assert_check(g, c, ...) \
356 (lj_assert_fail((g), __FILE__, __LINE__, __func__, __VA_ARGS__), 0))
357 #define lj_checkapi(c, ...) lj_assert_check(G(L), (c), __VA_ARGS__)
359 #define lj_checkapi(c, ...) ((void)L)
362 #ifdef LUA_USE_ASSERT
363 #define lj_assertG_(g, c, ...) lj_assert_check((g), (c), __VA_ARGS__)
364 #define lj_assertG(c, ...) lj_assert_check(g, (c), __VA_ARGS__)
365 #define lj_assertL(c, ...) lj_assert_check(G(L), (c), __VA_ARGS__)
366 #define lj_assertX(c, ...) lj_assert_check(NULL, (c), __VA_ARGS__)
367 #define check_exp(c, e) (lj_assertX((c), #c), (e))
369 #define lj_assertG_(g, c, ...) ((void)0)
370 #define lj_assertG(c, ...) ((void)g)
371 #define lj_assertL(c, ...) ((void)L)
372 #define lj_assertX(c, ...) ((void)0)
373 #define check_exp(c, e) (e)
376 /* Static assertions. */
377 #define LJ_ASSERT_NAME2(name, line) name ## line
378 #define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line)
380 #define LJ_STATIC_ASSERT(cond) \
381 extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
383 #define LJ_STATIC_ASSERT(cond) \
384 extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
387 /* PRNG state. Need this here, details in lj_prng.h. */
388 typedef struct PRNGState
{