2 ** LuaJIT common internal definitions.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
12 /* 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_MEM 0x7fffff00 /* Max. total memory allocation. */
50 #define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */
51 #define LJ_MAX_STR LJ_MAX_MEM /* Max. string length. */
52 #define LJ_MAX_UDATA LJ_MAX_MEM /* Max. userdata length. */
54 #define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */
55 #define LJ_MAX_HBITS 26 /* Max. hash bits. */
56 #define LJ_MAX_ABITS 28 /* Max. bits of array key. */
57 #define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */
58 #define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */
60 #define LJ_MAX_LINE LJ_MAX_MEM /* Max. source code line number. */
61 #define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */
62 #define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */
63 #define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */
64 #define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */
65 #define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */
67 #define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */
68 #define LJ_STACK_EXTRA 5 /* Extra stack space (metamethods). */
70 /* Minimum table/buffer sizes. */
71 #define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */
72 #define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). */
73 #define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */
74 #define LJ_MIN_SBUF 32 /* Min. string buffer length. */
75 #define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */
76 #define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */
77 #define LJ_MIN_K64SZ 16 /* Min. size for chained K64Array. */
79 /* JIT compiler limits. */
80 #define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */
81 #define LJ_MAX_PHI 32 /* Max. # of PHIs for a loop. */
82 #define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */
86 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
89 #define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo)
90 #define i32ptr(p) ((int32_t)(intptr_t)(void *)(p))
91 #define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p))
93 #define checki8(x) ((x) == (int32_t)(int8_t)(x))
94 #define checku8(x) ((x) == (int32_t)(uint8_t)(x))
95 #define checki16(x) ((x) == (int32_t)(int16_t)(x))
96 #define checku16(x) ((x) == (int32_t)(uint16_t)(x))
97 #define checki32(x) ((x) == (int32_t)(x))
98 #define checku32(x) ((x) == (uint32_t)(x))
99 #define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x))
101 /* Every half-decent C compiler transforms this into a rotate instruction. */
102 #define lj_rol(x, n) (((x)<<(n)) | ((x)>>(8*sizeof(x)-(n))))
103 #define lj_ror(x, n) (((x)<<(8*sizeof(x)-(n))) | ((x)>>(n)))
105 /* A really naive Bloom filter. But sufficient for our needs. */
106 typedef uintptr_t BloomFilter
;
107 #define BLOOM_MASK (8*sizeof(BloomFilter) - 1)
108 #define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK))
109 #define bloomset(b, x) ((b) |= bloombit((x)))
110 #define bloomtest(b, x) ((b) & bloombit((x)))
112 #if defined(__GNUC__)
114 #define LJ_NORET __attribute__((noreturn))
115 #define LJ_ALIGN(n) __attribute__((aligned(n)))
116 #define LJ_INLINE inline
117 #define LJ_AINLINE inline __attribute__((always_inline))
118 #define LJ_NOINLINE __attribute__((noinline))
120 #if defined(__ELF__) || defined(__MACH__)
121 #if !((defined(__sun__) && defined(__svr4__)) || defined(__solaris__))
122 #define LJ_NOAPI extern __attribute__((visibility("hidden")))
126 /* Note: it's only beneficial to use fastcall on x86 and then only for up to
127 ** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only
128 ** indirect calls and related tail-called C functions are marked as fastcall.
130 #if defined(__i386__)
131 #define LJ_FASTCALL __attribute__((fastcall))
134 #define LJ_LIKELY(x) __builtin_expect(!!(x), 1)
135 #define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0)
137 #define lj_ffs(x) ((uint32_t)__builtin_ctz(x))
139 #if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__))
140 static LJ_AINLINE
uint32_t lj_fls(uint32_t x
)
142 uint32_t r
; __asm__("bsrl %1, %0" : "=r" (r
) : "rm" (x
) : "cc"); return r
;
145 #define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31))
149 static LJ_AINLINE
uint32_t lj_bswap(uint32_t x
)
152 #if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\
153 __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__
154 __asm__("rev %0, %1" : "=r" (r
) : "r" (x
));
158 r
= x
^ lj_ror(x
, 16);
160 __asm__("eor %0, %1, %1, ror #16" : "=r" (r
) : "r" (x
));
162 return ((r
& 0xff00ffffu
) >> 8) ^ lj_ror(x
, 8);
166 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
168 return ((uint64_t)lj_bswap((uint32_t)x
)<<32) | lj_bswap((uint32_t)(x
>>32));
170 #elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
171 static LJ_AINLINE
uint32_t lj_bswap(uint32_t x
)
173 return (uint32_t)__builtin_bswap32((int32_t)x
);
176 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
178 return (uint64_t)__builtin_bswap64((int64_t)x
);
180 #elif defined(__i386__) || defined(__x86_64__)
181 static LJ_AINLINE
uint32_t lj_bswap(uint32_t x
)
183 uint32_t r
; __asm__("bswap %0" : "=r" (r
) : "0" (x
)); return r
;
186 #if defined(__i386__)
187 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
189 return ((uint64_t)lj_bswap((uint32_t)x
)<<32) | lj_bswap((uint32_t)(x
>>32));
192 static LJ_AINLINE
uint64_t lj_bswap64(uint64_t x
)
194 uint64_t r
; __asm__("bswap %0" : "=r" (r
) : "0" (x
)); return r
;
198 #error "missing define for lj_bswap()"
201 typedef union __attribute__((packed
)) Unaligned16
{
206 typedef union __attribute__((packed
)) Unaligned32
{
211 /* Unaligned load of uint16_t. */
212 static LJ_AINLINE
uint16_t lj_getu16(const void *p
)
214 return ((const Unaligned16
*)p
)->u
;
217 /* Unaligned load of uint32_t. */
218 static LJ_AINLINE
uint32_t lj_getu32(const void *p
)
220 return ((const Unaligned32
*)p
)->u
;
223 #elif defined(_MSC_VER)
225 #define LJ_NORET __declspec(noreturn)
226 #define LJ_ALIGN(n) __declspec(align(n))
227 #define LJ_INLINE __inline
228 #define LJ_AINLINE __forceinline
229 #define LJ_NOINLINE __declspec(noinline)
231 #define LJ_FASTCALL __fastcall
234 static LJ_AINLINE
uint32_t lj_ffs(uint32_t x
)
236 uint32_t r
; _BitScanForward(&r
, x
); return r
;
239 static LJ_AINLINE
uint32_t lj_fls(uint32_t x
)
241 uint32_t r
; _BitScanReverse(&r
, x
); return r
;
244 #define lj_bswap(x) (_byteswap_ulong((x)))
245 #define lj_bswap64(x) (_byteswap_uint64((x)))
247 /* MSVC is only supported on x86/x64, where unaligned loads are always ok. */
248 #define lj_getu16(p) (*(uint16_t *)(p))
249 #define lj_getu32(p) (*(uint32_t *)(p))
252 #error "missing defines for your compiler"
255 /* Optional defines. */
263 #define LJ_NOAPI extern
266 #define LJ_LIKELY(x) (x)
267 #define LJ_UNLIKELY(x) (x)
270 /* Attributes for internal functions. */
271 #define LJ_DATA LJ_NOAPI
273 #define LJ_ASMF LJ_NOAPI
274 #define LJ_FUNCA LJ_NOAPI
275 #if defined(ljamalg_c)
276 #define LJ_FUNC static
278 #define LJ_FUNC LJ_NOAPI
280 #define LJ_FUNC_NORET LJ_FUNC LJ_NORET
281 #define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET
282 #define LJ_ASMF_NORET LJ_ASMF LJ_NORET
284 /* Runtime assertions. */
286 #define check_exp(c, e) (lua_assert(c), (e))
287 #define api_check(l, e) lua_assert(e)
289 #define lua_assert(c) ((void)0)
290 #define check_exp(c, e) (e)
291 #define api_check luai_apicheck
294 /* Static assertions. */
295 #define LJ_ASSERT_NAME2(name, line) name ## line
296 #define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line)
298 #define LJ_STATIC_ASSERT(cond) \
299 extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
301 #define LJ_STATIC_ASSERT(cond) \
302 extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])