beta-0.89.2
[luatex.git] / source / libs / luajit / LuaJIT-src / src / lj_def.h
blob04bf00c89be451a326a876563c4fbf6d69a24730
1 /*
2 ** LuaJIT common internal definitions.
3 ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_DEF_H
7 #define _LJ_DEF_H
9 #include "lua.h"
11 #if defined(_MSC_VER)
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;
21 #ifdef _WIN64
22 typedef __int64 intptr_t;
23 typedef unsigned __int64 uintptr_t;
24 #else
25 typedef __int32 intptr_t;
26 typedef unsigned __int32 uintptr_t;
27 #endif
28 #elif defined(__symbian__)
29 /* Cough. */
30 typedef signed char int8_t;
31 typedef short int int16_t;
32 typedef int int32_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;
38 typedef int intptr_t;
39 typedef unsigned int uintptr_t;
40 #else
41 #include <stdint.h>
42 #endif
44 /* Needed everywhere. */
45 #include <string.h>
46 #include <stdlib.h>
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 249 /* Max. # of upvalues. */
71 #define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */
72 #define LJ_STACK_EXTRA (5+2*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. */
83 #define LJ_MIN_K64SZ 16 /* Min. size for chained K64Array. */
85 /* JIT compiler limits. */
86 #define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */
87 #define LJ_MAX_PHI 64 /* Max. # of PHIs for a loop. */
88 #define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */
90 /* Various macros. */
91 #ifndef UNUSED
92 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
93 #endif
95 #define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo)
96 #define i32ptr(p) ((int32_t)(intptr_t)(void *)(p))
97 #define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p))
99 #define checki8(x) ((x) == (int32_t)(int8_t)(x))
100 #define checku8(x) ((x) == (int32_t)(uint8_t)(x))
101 #define checki16(x) ((x) == (int32_t)(int16_t)(x))
102 #define checku16(x) ((x) == (int32_t)(uint16_t)(x))
103 #define checki32(x) ((x) == (int32_t)(x))
104 #define checku32(x) ((x) == (uint32_t)(x))
105 #define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x))
106 #define checkptr47(x) (((uint64_t)(x) >> 47) == 0)
107 #if LJ_GC64
108 #define checkptrGC(x) (checkptr47((x)))
109 #elif LJ_64
110 #define checkptrGC(x) (checkptr32((x)))
111 #else
112 #define checkptrGC(x) 1
113 #endif
115 /* Every half-decent C compiler transforms this into a rotate instruction. */
116 #define lj_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1))))
117 #define lj_ror(x, n) (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n)))
119 /* A really naive Bloom filter. But sufficient for our needs. */
120 typedef uintptr_t BloomFilter;
121 #define BLOOM_MASK (8*sizeof(BloomFilter) - 1)
122 #define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK))
123 #define bloomset(b, x) ((b) |= bloombit((x)))
124 #define bloomtest(b, x) ((b) & bloombit((x)))
126 #if defined(__GNUC__) || defined(__psp2__)
128 #define LJ_NORET __attribute__((noreturn))
129 #define LJ_ALIGN(n) __attribute__((aligned(n)))
130 #define LJ_INLINE inline
131 #define LJ_AINLINE inline __attribute__((always_inline))
132 #define LJ_NOINLINE __attribute__((noinline))
134 #if defined(__ELF__) || defined(__MACH__) || defined(__psp2__)
135 #if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__))
136 #define LJ_NOAPI extern __attribute__((visibility("hidden")))
137 #endif
138 #endif
140 /* Note: it's only beneficial to use fastcall on x86 and then only for up to
141 ** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only
142 ** indirect calls and related tail-called C functions are marked as fastcall.
144 #if defined(__i386__)
145 #define LJ_FASTCALL __attribute__((fastcall))
146 #endif
148 #define LJ_LIKELY(x) __builtin_expect(!!(x), 1)
149 #define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0)
151 #define lj_ffs(x) ((uint32_t)__builtin_ctz(x))
152 /* Don't ask ... */
153 #if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__))
154 static LJ_AINLINE uint32_t lj_fls(uint32_t x)
156 uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r;
158 #else
159 #define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31))
160 #endif
162 #if defined(__arm__)
163 static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
165 #if defined(__psp2__)
166 return __builtin_rev(x);
167 #else
168 uint32_t r;
169 #if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\
170 __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__
171 __asm__("rev %0, %1" : "=r" (r) : "r" (x));
172 return r;
173 #else
174 #ifdef __thumb__
175 r = x ^ lj_ror(x, 16);
176 #else
177 __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x));
178 #endif
179 return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8);
180 #endif
181 #endif
184 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
186 return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));
188 #elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
189 static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
191 return (uint32_t)__builtin_bswap32((int32_t)x);
194 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
196 return (uint64_t)__builtin_bswap64((int64_t)x);
198 #elif defined(__i386__) || defined(__x86_64__)
199 static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
201 uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;
204 #if defined(__i386__)
205 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
207 return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));
209 #else
210 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
212 uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;
214 #endif
215 #else
216 static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
218 return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24);
221 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
223 return (uint64_t)lj_bswap((uint32_t)(x >> 32)) |
224 ((uint64_t)lj_bswap((uint32_t)x) << 32);
226 #endif
228 typedef union __attribute__((packed)) Unaligned16 {
229 uint16_t u;
230 uint8_t b[2];
231 } Unaligned16;
233 typedef union __attribute__((packed)) Unaligned32 {
234 uint32_t u;
235 uint8_t b[4];
236 } Unaligned32;
238 /* Unaligned load of uint16_t. */
239 static LJ_AINLINE uint16_t lj_getu16(const void *p)
241 return ((const Unaligned16 *)p)->u;
244 /* Unaligned load of uint32_t. */
245 static LJ_AINLINE uint32_t lj_getu32(const void *p)
247 return ((const Unaligned32 *)p)->u;
250 #elif defined(_MSC_VER)
252 #define LJ_NORET __declspec(noreturn)
253 #define LJ_ALIGN(n) __declspec(align(n))
254 #define LJ_INLINE __inline
255 #define LJ_AINLINE __forceinline
256 #define LJ_NOINLINE __declspec(noinline)
257 #if defined(_M_IX86)
258 #define LJ_FASTCALL __fastcall
259 #endif
261 #ifdef _M_PPC
262 unsigned int _CountLeadingZeros(long);
263 #pragma intrinsic(_CountLeadingZeros)
264 static LJ_AINLINE uint32_t lj_fls(uint32_t x)
266 return _CountLeadingZeros(x) ^ 31;
268 #else
269 unsigned char _BitScanForward(uint32_t *, unsigned long);
270 unsigned char _BitScanReverse(uint32_t *, unsigned long);
271 #pragma intrinsic(_BitScanForward)
272 #pragma intrinsic(_BitScanReverse)
274 static LJ_AINLINE uint32_t lj_ffs(uint32_t x)
276 uint32_t r; _BitScanForward(&r, x); return r;
279 static LJ_AINLINE uint32_t lj_fls(uint32_t x)
281 uint32_t r; _BitScanReverse(&r, x); return r;
283 #endif
285 unsigned long _byteswap_ulong(unsigned long);
286 uint64_t _byteswap_uint64(uint64_t);
287 #define lj_bswap(x) (_byteswap_ulong((x)))
288 #define lj_bswap64(x) (_byteswap_uint64((x)))
290 #if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED)
292 ** Replacement for unaligned loads on Xbox 360. Disabled by default since it's
293 ** usually more costly than the occasional stall when crossing a cache-line.
295 static LJ_AINLINE uint16_t lj_getu16(const void *v)
297 const uint8_t *p = (const uint8_t *)v;
298 return (uint16_t)((p[0]<<8) | p[1]);
300 static LJ_AINLINE uint32_t lj_getu32(const void *v)
302 const uint8_t *p = (const uint8_t *)v;
303 return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]);
305 #else
306 /* Unaligned loads are generally ok on x86/x64. */
307 #define lj_getu16(p) (*(uint16_t *)(p))
308 #define lj_getu32(p) (*(uint32_t *)(p))
309 #endif
311 #else
312 #error "missing defines for your compiler"
313 #endif
315 /* Optional defines. */
316 #ifndef LJ_FASTCALL
317 #define LJ_FASTCALL
318 #endif
319 #ifndef LJ_NORET
320 #define LJ_NORET
321 #endif
322 #ifndef LJ_NOAPI
323 #define LJ_NOAPI extern
324 #endif
325 #ifndef LJ_LIKELY
326 #define LJ_LIKELY(x) (x)
327 #define LJ_UNLIKELY(x) (x)
328 #endif
330 /* Attributes for internal functions. */
331 #define LJ_DATA LJ_NOAPI
332 #define LJ_DATADEF
333 #define LJ_ASMF LJ_NOAPI
334 #define LJ_FUNCA LJ_NOAPI
335 #if defined(ljamalg_c)
336 #define LJ_FUNC static
337 #else
338 #define LJ_FUNC LJ_NOAPI
339 #endif
340 #define LJ_FUNC_NORET LJ_FUNC LJ_NORET
341 #define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET
342 #define LJ_ASMF_NORET LJ_ASMF LJ_NORET
344 /* Runtime assertions. */
345 #ifdef lua_assert
346 #define check_exp(c, e) (lua_assert(c), (e))
347 #define api_check(l, e) lua_assert(e)
348 #else
349 #define lua_assert(c) ((void)0)
350 #define check_exp(c, e) (e)
351 #define api_check luai_apicheck
352 #endif
354 /* Static assertions. */
355 #define LJ_ASSERT_NAME2(name, line) name ## line
356 #define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line)
357 #ifdef __COUNTER__
358 #define LJ_STATIC_ASSERT(cond) \
359 extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
360 #else
361 #define LJ_STATIC_ASSERT(cond) \
362 extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
363 #endif
365 #endif