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