lj_def.h (11046B)
1 /* 2 ** LuaJIT common internal definitions. 3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h 4 */ 5 6 #ifndef _LJ_DEF_H 7 #define _LJ_DEF_H 8 9 #include "lua.h" 10 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 43 44 /* Needed everywhere. */ 45 #include <string.h> 46 #include <stdlib.h> 47 48 /* Various VM limits. */ 49 #define LJ_MAX_MEM32 0xffff0000 /* 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. */ 57 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. */ 63 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_MSBT 200000 /* Max. string.match() backtracks. */ 67 #define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */ 68 #define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */ 69 #define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */ 70 #define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */ 71 72 #define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */ 73 #define LJ_STACK_EXTRA (5+2*LJ_FR2) /* Extra stack space (metamethods). */ 74 75 #define LJ_NUM_CBPAGE 1 /* Number of FFI callback pages. */ 76 77 /* Minimum table/buffer sizes. */ 78 #define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */ 79 #define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). ORDER LUA_RIDX_COUNT. */ 80 #define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */ 81 #define LJ_MIN_SBUF 32 /* Min. string buffer length. */ 82 #define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */ 83 #define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */ 84 #define LJ_MIN_K64SZ 16 /* Min. size for chained K64Array. */ 85 86 /* JIT compiler limits. */ 87 #define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */ 88 #define LJ_MAX_PHI 64 /* Max. # of PHIs for a loop. */ 89 #define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */ 90 91 /* Various macros. */ 92 #ifndef UNUSED 93 #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 94 #endif 95 96 #define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo) 97 #define i32ptr(p) ((int32_t)(intptr_t)(void *)(p)) 98 #define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p)) 99 #define i64ptr(p) ((int64_t)(intptr_t)(void *)(p)) 100 #define u64ptr(p) ((uint64_t)(intptr_t)(void *)(p)) 101 102 #define checki8(x) ((x) == (int32_t)(int8_t)(x)) 103 #define checku8(x) ((x) == (int32_t)(uint8_t)(x)) 104 #define checki16(x) ((x) == (int32_t)(int16_t)(x)) 105 #define checku16(x) ((x) == (int32_t)(uint16_t)(x)) 106 #define checki32(x) ((x) == (int32_t)(x)) 107 #define checku32(x) ((x) == (uint32_t)(x)) 108 #define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x)) 109 #define checkptr47(x) (((uint64_t)(x) >> 47) == 0) 110 #if LJ_GC64 111 #define checkptrGC(x) (checkptr47((x))) 112 #elif LJ_64 113 #define checkptrGC(x) (checkptr32((x))) 114 #else 115 #define checkptrGC(x) 1 116 #endif 117 118 /* Every half-decent C compiler transforms this into a rotate instruction. */ 119 #define lj_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1)))) 120 #define lj_ror(x, n) (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n))) 121 122 /* A really naive Bloom filter. But sufficient for our needs. */ 123 typedef uintptr_t BloomFilter; 124 #define BLOOM_MASK (8*sizeof(BloomFilter) - 1) 125 #define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK)) 126 #define bloomset(b, x) ((b) |= bloombit((x))) 127 #define bloomtest(b, x) ((b) & bloombit((x))) 128 129 #if defined(__GNUC__) || defined(__psp2__) 130 131 #define LJ_NORET __attribute__((noreturn)) 132 #define LJ_ALIGN(n) __attribute__((aligned(n))) 133 #define LJ_INLINE inline 134 #define LJ_AINLINE inline __attribute__((always_inline)) 135 #define LJ_NOINLINE __attribute__((noinline)) 136 137 #if defined(__ELF__) || defined(__MACH__) || defined(__psp2__) 138 #if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__)) 139 #define LJ_NOAPI extern __attribute__((visibility("hidden"))) 140 #endif 141 #endif 142 143 /* Note: it's only beneficial to use fastcall on x86 and then only for up to 144 ** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only 145 ** indirect calls and related tail-called C functions are marked as fastcall. 146 */ 147 #if defined(__i386__) 148 #define LJ_FASTCALL __attribute__((fastcall)) 149 #endif 150 151 #define LJ_LIKELY(x) __builtin_expect(!!(x), 1) 152 #define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0) 153 154 #define LJ_RESTRICT 155 156 #define lj_ffs(x) ((uint32_t)__builtin_ctz(x)) 157 /* Don't ask ... */ 158 #if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__)) 159 static LJ_AINLINE uint32_t lj_fls(uint32_t x) 160 { 161 uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r; 162 } 163 #else 164 #define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31)) 165 #endif 166 167 #if defined(__arm__) 168 static LJ_AINLINE uint32_t lj_bswap(uint32_t x) 169 { 170 #if defined(__psp2__) 171 return __builtin_rev(x); 172 #else 173 uint32_t r; 174 #if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\ 175 __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ 176 __asm__("rev %0, %1" : "=r" (r) : "r" (x)); 177 return r; 178 #else 179 #ifdef __thumb__ 180 r = x ^ lj_ror(x, 16); 181 #else 182 __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x)); 183 #endif 184 return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8); 185 #endif 186 #endif 187 } 188 189 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) 190 { 191 return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); 192 } 193 #elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) 194 static LJ_AINLINE uint32_t lj_bswap(uint32_t x) 195 { 196 return (uint32_t)__builtin_bswap32((int32_t)x); 197 } 198 199 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) 200 { 201 return (uint64_t)__builtin_bswap64((int64_t)x); 202 } 203 #elif defined(__i386__) || defined(__x86_64__) 204 static LJ_AINLINE uint32_t lj_bswap(uint32_t x) 205 { 206 uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; 207 } 208 209 #if defined(__i386__) 210 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) 211 { 212 return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); 213 } 214 #else 215 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) 216 { 217 uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; 218 } 219 #endif 220 #else 221 static LJ_AINLINE uint32_t lj_bswap(uint32_t x) 222 { 223 return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24); 224 } 225 226 static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) 227 { 228 return (uint64_t)lj_bswap((uint32_t)(x >> 32)) | 229 ((uint64_t)lj_bswap((uint32_t)x) << 32); 230 } 231 #endif 232 233 typedef union __attribute__((packed)) Unaligned16 { 234 uint16_t u; 235 uint8_t b[2]; 236 } Unaligned16; 237 238 typedef union __attribute__((packed)) Unaligned32 { 239 uint32_t u; 240 uint8_t b[4]; 241 } Unaligned32; 242 243 /* Unaligned load of uint16_t. */ 244 static LJ_AINLINE uint16_t lj_getu16(const void *p) 245 { 246 return ((const Unaligned16 *)p)->u; 247 } 248 249 /* Unaligned load of uint32_t. */ 250 static LJ_AINLINE uint32_t lj_getu32(const void *p) 251 { 252 return ((const Unaligned32 *)p)->u; 253 } 254 255 #elif defined(_MSC_VER) 256 257 #define LJ_NORET __declspec(noreturn) 258 #define LJ_ALIGN(n) __declspec(align(n)) 259 #define LJ_INLINE __inline 260 #define LJ_AINLINE __forceinline 261 #define LJ_NOINLINE __declspec(noinline) 262 #if defined(_M_IX86) 263 #define LJ_FASTCALL __fastcall 264 #endif 265 266 #ifdef _M_PPC 267 unsigned int _CountLeadingZeros(long); 268 #pragma intrinsic(_CountLeadingZeros) 269 static LJ_AINLINE uint32_t lj_fls(uint32_t x) 270 { 271 return _CountLeadingZeros(x) ^ 31; 272 } 273 #else 274 unsigned char _BitScanForward(uint32_t *, unsigned long); 275 unsigned char _BitScanReverse(uint32_t *, unsigned long); 276 #pragma intrinsic(_BitScanForward) 277 #pragma intrinsic(_BitScanReverse) 278 279 static LJ_AINLINE uint32_t lj_ffs(uint32_t x) 280 { 281 uint32_t r; _BitScanForward(&r, x); return r; 282 } 283 284 static LJ_AINLINE uint32_t lj_fls(uint32_t x) 285 { 286 uint32_t r; _BitScanReverse(&r, x); return r; 287 } 288 #endif 289 290 unsigned long _byteswap_ulong(unsigned long); 291 uint64_t _byteswap_uint64(uint64_t); 292 #define lj_bswap(x) (_byteswap_ulong((x))) 293 #define lj_bswap64(x) (_byteswap_uint64((x))) 294 295 #if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED) 296 /* 297 ** Replacement for unaligned loads on Xbox 360. Disabled by default since it's 298 ** usually more costly than the occasional stall when crossing a cache-line. 299 */ 300 static LJ_AINLINE uint16_t lj_getu16(const void *v) 301 { 302 const uint8_t *p = (const uint8_t *)v; 303 return (uint16_t)((p[0]<<8) | p[1]); 304 } 305 static LJ_AINLINE uint32_t lj_getu32(const void *v) 306 { 307 const uint8_t *p = (const uint8_t *)v; 308 return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]); 309 } 310 #else 311 /* Unaligned loads are generally ok on x86/x64. */ 312 #define lj_getu16(p) (*(uint16_t *)(p)) 313 #define lj_getu32(p) (*(uint32_t *)(p)) 314 #endif 315 316 #else 317 #error "missing defines for your compiler" 318 #endif 319 320 /* Optional defines. */ 321 #ifndef LJ_FASTCALL 322 #define LJ_FASTCALL 323 #endif 324 #ifndef LJ_NORET 325 #define LJ_NORET 326 #endif 327 #ifndef LJ_NOAPI 328 #define LJ_NOAPI extern 329 #endif 330 #ifndef LJ_LIKELY 331 #define LJ_LIKELY(x) (x) 332 #define LJ_UNLIKELY(x) (x) 333 #endif 334 335 /* Attributes for internal functions. */ 336 #define LJ_DATA LJ_NOAPI 337 #define LJ_DATADEF 338 #define LJ_ASMF LJ_NOAPI 339 #define LJ_FUNCA LJ_NOAPI 340 #if defined(ljamalg_c) 341 #define LJ_FUNC static 342 #else 343 #define LJ_FUNC LJ_NOAPI 344 #endif 345 #define LJ_FUNC_NORET LJ_FUNC LJ_NORET 346 #define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET 347 #define LJ_ASMF_NORET LJ_ASMF LJ_NORET 348 349 /* Runtime assertions. */ 350 #ifdef lua_assert 351 #define check_exp(c, e) (lua_assert(c), (e)) 352 #define api_check(l, e) lua_assert(e) 353 #else 354 #define lua_assert(c) ((void)0) 355 #define check_exp(c, e) (e) 356 #define api_check luai_apicheck 357 #endif 358 359 /* Static assertions. */ 360 #define LJ_ASSERT_NAME2(name, line) name ## line 361 #define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line) 362 #ifdef __COUNTER__ 363 #define LJ_STATIC_ASSERT(cond) \ 364 extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) 365 #else 366 #define LJ_STATIC_ASSERT(cond) \ 367 extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) 368 #endif 369 370 #endif