summaryrefslogtreecommitdiff
path: root/src/lj_def.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_def.h')
-rw-r--r--src/lj_def.h226
1 files changed, 226 insertions, 0 deletions
diff --git a/src/lj_def.h b/src/lj_def.h
new file mode 100644
index 00000000..dbfd5bf5
--- /dev/null
+++ b/src/lj_def.h
@@ -0,0 +1,226 @@
1/*
2** LuaJIT common internal definitions.
3** Copyright (C) 2005-2009 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#ifdef _MSC_VER
12/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
13typedef __int8 int8_t;
14typedef __int16 int16_t;
15typedef __int32 int32_t;
16typedef __int64 int64_t;
17typedef unsigned __int8 uint8_t;
18typedef unsigned __int16 uint16_t;
19typedef unsigned __int32 uint32_t;
20typedef unsigned __int64 uint64_t;
21#ifdef _WIN64
22typedef __int64 intptr_t;
23typedef unsigned __int64 uintptr_t;
24#else
25typedef __int32 intptr_t;
26typedef unsigned __int32 uintptr_t;
27#endif
28#else
29#include <stdint.h>
30#endif
31
32/* Needed everywhere. */
33#include <string.h>
34#include <stdlib.h>
35
36/* Various VM limits. */
37#define LJ_MAX_MEM 0x7fffff00 /* Max. total memory allocation. */
38#define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */
39#define LJ_MAX_STR LJ_MAX_MEM /* Max. string length. */
40#define LJ_MAX_UDATA LJ_MAX_MEM /* Max. userdata length. */
41
42#define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */
43#define LJ_MAX_HBITS 26 /* Max. hash bits. */
44#define LJ_MAX_ABITS 28 /* Max. bits of array key. */
45#define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */
46#define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */
47
48#define LJ_MAX_LINE LJ_MAX_MEM /* Max. source code line number. */
49#define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */
50#define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */
51#define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */
52#define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */
53#define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */
54
55#define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */
56#define LJ_STACK_EXTRA 5 /* Extra stack space (metamethods). */
57
58/* Minimum table/buffer sizes. */
59#define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */
60#define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). */
61#define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */
62#define LJ_MIN_SBUF 32 /* Min. string buffer length. */
63#define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */
64#define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */
65#define LJ_MIN_KNUMSZ 16 /* Min. size for chained KNUM array. */
66
67/* JIT compiler limits. */
68#define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */
69#define LJ_MAX_PHI 32 /* Max. # of PHIs for a loop. */
70#define LJ_MAX_EXITSTUBGR 8 /* Max. # of exit stub groups. */
71
72/* Various macros. */
73#ifndef UNUSED
74#define UNUSED(x) ((void)(x)) /* to avoid warnings */
75#endif
76
77#ifndef cast
78#define cast(t, exp) ((t)(exp))
79#endif
80
81#define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo)
82#define cast_byte(i) cast(uint8_t, (i))
83#define cast_num(i) cast(lua_Number, (i))
84#define cast_int(i) cast(int, (i))
85#define i32ptr(p) ((int32_t)(intptr_t)(void *)(p))
86#define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p))
87
88#define checki8(x) ((x) == (int32_t)(int8_t)(x))
89#define checku8(x) ((x) == (int32_t)(uint8_t)(x))
90#define checki16(x) ((x) == (int32_t)(int16_t)(x))
91
92/* Every half-decent C compiler transforms this into a rotate instruction. */
93#define lj_rol(x, n) (((x)<<(n)) | ((x)>>(32-(n))))
94#define lj_ror(x, n) (((x)<<(32-(n))) | ((x)>>(n)))
95
96/* A really naive Bloom filter. But sufficient for our needs. */
97typedef uintptr_t BloomFilter;
98#define BLOOM_MASK (8*sizeof(BloomFilter) - 1)
99#define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK))
100#define bloomset(b, x) ((b) |= bloombit((x)))
101#define bloomtest(b, x) ((b) & bloombit((x)))
102
103#if defined(__GNUC__)
104
105#if (__GNUC__ < 3) || ((__GNUC__ == 3) && __GNUC_MINOR__ < 4)
106#error "sorry, need GCC 3.4 or newer"
107#endif
108
109#define LJ_NORET __attribute__((noreturn))
110#define LJ_ALIGN(n) __attribute__((aligned(n)))
111#define LJ_INLINE inline
112#define LJ_AINLINE inline __attribute__((always_inline))
113#define LJ_NOINLINE __attribute__((noinline))
114
115#if defined(__ELF__) || defined(__MACH__)
116#define LJ_NOAPI extern __attribute__((visibility("hidden")))
117#endif
118
119/* Note: it's only beneficial to use fastcall on x86 and then only for up to
120** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only
121** indirect calls and related tail-called C functions are marked as fastcall.
122*/
123#if defined(__i386__)
124#define LJ_FASTCALL __attribute__((fastcall))
125#endif
126
127#define LJ_LIKELY(x) __builtin_expect(!!(x), 1)
128#define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0)
129
130#define lj_ffs(x) ((uint32_t)__builtin_ctz(x))
131/* Don't ask ... */
132#if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__))
133static LJ_AINLINE uint32_t lj_fls(uint32_t x)
134{
135 uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r;
136}
137#else
138#define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31))
139#endif
140
141#if defined(__i386__) || defined(__x86_64__)
142static LJ_AINLINE uint32_t lj_bswap(uint32_t x)
143{
144 uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;
145}
146#else
147#error "missing define for lj_bswap()"
148#endif
149
150#elif defined(_MSC_VER)
151
152#define LJ_NORET __declspec(noreturn)
153#define LJ_ALIGN(n) __declspec(align(n))
154#define LJ_INLINE __inline
155#define LJ_AINLINE __forceinline
156#define LJ_NOINLINE __declspec(noinline)
157#if defined(_M_IX86)
158#define LJ_FASTCALL __fastcall
159#endif
160
161static LJ_AINLINE uint32_t lj_ffs(uint32_t x)
162{
163 uint32_t r; _BitScanForward(&r, x); return r;
164}
165
166static LJ_AINLINE uint32_t lj_fls(uint32_t x)
167{
168 uint32_t r; _BitScanReverse(&r, x); return r;
169}
170
171#define lj_bswap(x) (_byteswap_ulong((x)))
172
173#else
174#error "missing defines for your compiler"
175#endif
176
177/* Optional defines. */
178#ifndef LJ_FASTCALL
179#define LJ_FASTCALL
180#endif
181#ifndef LJ_NORET
182#define LJ_NORET
183#endif
184#ifndef LJ_NOAPI
185#define LJ_NOAPI extern
186#endif
187#ifndef LJ_LIKELY
188#define LJ_LIKELY(x) (x)
189#define LJ_UNLIKELY(x) (x)
190#endif
191
192/* Attributes for internal functions. */
193#if defined(ljamalg_c)
194#define LJ_DATA static
195#define LJ_DATADEF static
196#define LJ_FUNC static
197#define LJ_ASMF LJ_NOAPI
198#define LJ_FUNCA LJ_NOAPI
199#else
200#define LJ_DATA LJ_NOAPI
201#define LJ_DATADEF
202#define LJ_FUNC LJ_NOAPI
203#define LJ_ASMF LJ_NOAPI
204#define LJ_FUNCA LJ_NOAPI
205#endif
206#define LJ_FUNC_NORET LJ_FUNC LJ_NORET
207#define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET
208#define LJ_ASMF_NORET LJ_ASMF LJ_NORET
209
210/* Runtime assertions. */
211#ifdef lua_assert
212#define check_exp(c, e) (lua_assert(c), (e))
213#define api_check(l, e) lua_assert(e)
214#else
215#define lua_assert(c) ((void)0)
216#define check_exp(c, e) (e)
217#define api_check luai_apicheck
218#endif
219
220/* Static assertions. */
221#define LJ_ASSERT_NAME2(name, line) name ## line
222#define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line)
223#define LJ_STATIC_ASSERT(cond) \
224 extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
225
226#endif