src/lj_def.h - luajit-2.0-src

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** LuaJIT common internal definitions.
  3. ** Copyright (C) 2005-2015 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)
  9. /* 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+2*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. #define LJ_MIN_K64SZ        16                /* Min. size for chained K64Array. */

  74. /* JIT compiler limits. */
  75. #define LJ_MAX_JSLOTS        250                /* Max. # of stack slots for a trace. */
  76. #define LJ_MAX_PHI        64                /* Max. # of PHIs for a loop. */
  77. #define LJ_MAX_EXITSTUBGR        16        /* Max. # of exit stub groups. */

  78. /* Various macros. */
  79. #ifndef UNUSED
  80. #define UNUSED(x)        ((void)(x))        /* to avoid warnings */
  81. #endif

  82. #define U64x(hi, lo)        (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo)
  83. #define i32ptr(p)        ((int32_t)(intptr_t)(void *)(p))
  84. #define u32ptr(p)        ((uint32_t)(intptr_t)(void *)(p))

  85. #define checki8(x)        ((x) == (int32_t)(int8_t)(x))
  86. #define checku8(x)        ((x) == (int32_t)(uint8_t)(x))
  87. #define checki16(x)        ((x) == (int32_t)(int16_t)(x))
  88. #define checku16(x)        ((x) == (int32_t)(uint16_t)(x))
  89. #define checki32(x)        ((x) == (int32_t)(x))
  90. #define checku32(x)        ((x) == (uint32_t)(x))
  91. #define checkptr32(x)        ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x))
  92. #define checkptr47(x)        (((uint64_t)(x) >> 47) == 0)
  93. #if LJ_GC64
  94. #define checkptrGC(x)        (checkptr47((x)))
  95. #elif LJ_64
  96. #define checkptrGC(x)        (checkptr32((x)))
  97. #else
  98. #define checkptrGC(x)        1
  99. #endif

  100. /* Every half-decent C compiler transforms this into a rotate instruction. */
  101. #define lj_rol(x, n)        (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1))))
  102. #define lj_ror(x, n)        (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n)))

  103. /* A really naive Bloom filter. But sufficient for our needs. */
  104. typedef uintptr_t BloomFilter;
  105. #define BLOOM_MASK        (8*sizeof(BloomFilter) - 1)
  106. #define bloombit(x)        ((uintptr_t)1 << ((x) & BLOOM_MASK))
  107. #define bloomset(b, x)        ((b) |= bloombit((x)))
  108. #define bloomtest(b, x)        ((b) & bloombit((x)))

  109. #if defined(__GNUC__) || defined(__psp2__)

  110. #define LJ_NORET        __attribute__((noreturn))
  111. #define LJ_ALIGN(n)        __attribute__((aligned(n)))
  112. #define LJ_INLINE        inline
  113. #define LJ_AINLINE        inline __attribute__((always_inline))
  114. #define LJ_NOINLINE        __attribute__((noinline))

  115. #if defined(__ELF__) || defined(__MACH__) || defined(__psp2__)
  116. #if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__))
  117. #define LJ_NOAPI        extern __attribute__((visibility("hidden")))
  118. #endif
  119. #endif

  120. /* Note: it's only beneficial to use fastcall on x86 and then only for up to
  121. ** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only
  122. ** indirect calls and related tail-called C functions are marked as fastcall.
  123. */
  124. #if defined(__i386__)
  125. #define LJ_FASTCALL        __attribute__((fastcall))
  126. #endif

  127. #define LJ_LIKELY(x)        __builtin_expect(!!(x), 1)
  128. #define LJ_UNLIKELY(x)        __builtin_expect(!!(x), 0)

  129. #define lj_ffs(x)        ((uint32_t)__builtin_ctz(x))
  130. /* Don't ask ... */
  131. #if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__))
  132. static LJ_AINLINE uint32_t lj_fls(uint32_t x)
  133. {
  134.   uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r;
  135. }
  136. #else
  137. #define lj_fls(x)        ((uint32_t)(__builtin_clz(x)^31))
  138. #endif

  139. #if defined(__arm__)
  140. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  141. {
  142. #if defined(__psp2__)
  143.   return __builtin_rev(x);
  144. #else
  145.   uint32_t r;
  146. #if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\
  147.     __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__
  148.   __asm__("rev %0, %1" : "=r" (r) : "r" (x));
  149.   return r;
  150. #else
  151. #ifdef __thumb__
  152.   r = x ^ lj_ror(x, 16);
  153. #else
  154.   __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x));
  155. #endif
  156.   return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8);
  157. #endif
  158. #endif
  159. }

  160. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  161. {
  162.   return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));
  163. }
  164. #elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
  165. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  166. {
  167.   return (uint32_t)__builtin_bswap32((int32_t)x);
  168. }

  169. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  170. {
  171.   return (uint64_t)__builtin_bswap64((int64_t)x);
  172. }
  173. #elif defined(__i386__) || defined(__x86_64__)
  174. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  175. {
  176.   uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;
  177. }

  178. #if defined(__i386__)
  179. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  180. {
  181.   return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));
  182. }
  183. #else
  184. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  185. {
  186.   uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;
  187. }
  188. #endif
  189. #else
  190. static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
  191. {
  192.   return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24);
  193. }

  194. static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
  195. {
  196.   return (uint64_t)lj_bswap((uint32_t)(x >> 32)) |
  197.          ((uint64_t)lj_bswap((uint32_t)x) << 32);
  198. }
  199. #endif

  200. typedef union __attribute__((packed)) Unaligned16 {
  201.   uint16_t u;
  202.   uint8_t b[2];
  203. } Unaligned16;

  204. typedef union __attribute__((packed)) Unaligned32 {
  205.   uint32_t u;
  206.   uint8_t b[4];
  207. } Unaligned32;

  208. /* Unaligned load of uint16_t. */
  209. static LJ_AINLINE uint16_t lj_getu16(const void *p)
  210. {
  211.   return ((const Unaligned16 *)p)->u;
  212. }

  213. /* Unaligned load of uint32_t. */
  214. static LJ_AINLINE uint32_t lj_getu32(const void *p)
  215. {
  216.   return ((const Unaligned32 *)p)->u;
  217. }

  218. #elif defined(_MSC_VER)

  219. #define LJ_NORET        __declspec(noreturn)
  220. #define LJ_ALIGN(n)        __declspec(align(n))
  221. #define LJ_INLINE        __inline
  222. #define LJ_AINLINE        __forceinline
  223. #define LJ_NOINLINE        __declspec(noinline)
  224. #if defined(_M_IX86)
  225. #define LJ_FASTCALL        __fastcall
  226. #endif

  227. #ifdef _M_PPC
  228. unsigned int _CountLeadingZeros(long);
  229. #pragma intrinsic(_CountLeadingZeros)
  230. static LJ_AINLINE uint32_t lj_fls(uint32_t x)
  231. {
  232.   return _CountLeadingZeros(x) ^ 31;
  233. }
  234. #else
  235. unsigned char _BitScanForward(uint32_t *, unsigned long);
  236. unsigned char _BitScanReverse(uint32_t *, unsigned long);
  237. #pragma intrinsic(_BitScanForward)
  238. #pragma intrinsic(_BitScanReverse)

  239. static LJ_AINLINE uint32_t lj_ffs(uint32_t x)
  240. {
  241.   uint32_t r; _BitScanForward(&r, x); return r;
  242. }

  243. static LJ_AINLINE uint32_t lj_fls(uint32_t x)
  244. {
  245.   uint32_t r; _BitScanReverse(&r, x); return r;
  246. }
  247. #endif

  248. unsigned long _byteswap_ulong(unsigned long);
  249. uint64_t _byteswap_uint64(uint64_t);
  250. #define lj_bswap(x)        (_byteswap_ulong((x)))
  251. #define lj_bswap64(x)        (_byteswap_uint64((x)))

  252. #if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED)
  253. /*
  254. ** Replacement for unaligned loads on Xbox 360. Disabled by default since it's
  255. ** usually more costly than the occasional stall when crossing a cache-line.
  256. */
  257. static LJ_AINLINE uint16_t lj_getu16(const void *v)
  258. {
  259.   const uint8_t *p = (const uint8_t *)v;
  260.   return (uint16_t)((p[0]<<8) | p[1]);
  261. }
  262. static LJ_AINLINE uint32_t lj_getu32(const void *v)
  263. {
  264.   const uint8_t *p = (const uint8_t *)v;
  265.   return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]);
  266. }
  267. #else
  268. /* Unaligned loads are generally ok on x86/x64. */
  269. #define lj_getu16(p)        (*(uint16_t *)(p))
  270. #define lj_getu32(p)        (*(uint32_t *)(p))
  271. #endif

  272. #else
  273. #error "missing defines for your compiler"
  274. #endif

  275. /* Optional defines. */
  276. #ifndef LJ_FASTCALL
  277. #define LJ_FASTCALL
  278. #endif
  279. #ifndef LJ_NORET
  280. #define LJ_NORET
  281. #endif
  282. #ifndef LJ_NOAPI
  283. #define LJ_NOAPI        extern
  284. #endif
  285. #ifndef LJ_LIKELY
  286. #define LJ_LIKELY(x)        (x)
  287. #define LJ_UNLIKELY(x)        (x)
  288. #endif

  289. /* Attributes for internal functions. */
  290. #define LJ_DATA                LJ_NOAPI
  291. #define LJ_DATADEF
  292. #define LJ_ASMF                LJ_NOAPI
  293. #define LJ_FUNCA        LJ_NOAPI
  294. #if defined(ljamalg_c)
  295. #define LJ_FUNC                static
  296. #else
  297. #define LJ_FUNC                LJ_NOAPI
  298. #endif
  299. #define LJ_FUNC_NORET        LJ_FUNC LJ_NORET
  300. #define LJ_FUNCA_NORET        LJ_FUNCA LJ_NORET
  301. #define LJ_ASMF_NORET        LJ_ASMF LJ_NORET

  302. /* Runtime assertions. */
  303. #ifdef lua_assert
  304. #define check_exp(c, e)                (lua_assert(c), (e))
  305. #define api_check(l, e)                lua_assert(e)
  306. #else
  307. #define lua_assert(c)                ((void)0)
  308. #define check_exp(c, e)                (e)
  309. #define api_check                luai_apicheck
  310. #endif

  311. /* Static assertions. */
  312. #define LJ_ASSERT_NAME2(name, line)        name ## line
  313. #define LJ_ASSERT_NAME(line)                LJ_ASSERT_NAME2(lj_assert_, line)
  314. #ifdef __COUNTER__
  315. #define LJ_STATIC_ASSERT(cond) \
  316.   extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
  317. #else
  318. #define LJ_STATIC_ASSERT(cond) \
  319.   extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
  320. #endif

  321. #endif