Fix call unroll checks in the presence of metamethod frames.
[luajit-2.0.git] / src / lj_arch.h
blob9ea10d0f5eb5a533e97e76251076ccefb1d4532d
1 /*
2 ** Target architecture selection.
3 ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_ARCH_H
7 #define _LJ_ARCH_H
9 #include "lua.h"
11 /* Target endianess. */
12 #define LUAJIT_LE 0
13 #define LUAJIT_BE 1
15 /* Target architectures. */
16 #define LUAJIT_ARCH_X86 1
17 #define LUAJIT_ARCH_x86 1
18 #define LUAJIT_ARCH_X64 2
19 #define LUAJIT_ARCH_x64 2
20 #define LUAJIT_ARCH_ARM 3
21 #define LUAJIT_ARCH_arm 3
22 #define LUAJIT_ARCH_PPC 4
23 #define LUAJIT_ARCH_ppc 4
24 #define LUAJIT_ARCH_PPCSPE 5
25 #define LUAJIT_ARCH_ppcspe 5
26 #define LUAJIT_ARCH_MIPS 6
27 #define LUAJIT_ARCH_mips 6
29 /* Target OS. */
30 #define LUAJIT_OS_OTHER 0
31 #define LUAJIT_OS_WINDOWS 1
32 #define LUAJIT_OS_LINUX 2
33 #define LUAJIT_OS_OSX 3
34 #define LUAJIT_OS_BSD 4
35 #define LUAJIT_OS_POSIX 5
37 /* Select native target if no target defined. */
38 #ifndef LUAJIT_TARGET
40 #if defined(__i386) || defined(__i386__) || defined(_M_IX86)
41 #define LUAJIT_TARGET LUAJIT_ARCH_X86
42 #elif defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
43 #define LUAJIT_TARGET LUAJIT_ARCH_X64
44 #elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM)
45 #define LUAJIT_TARGET LUAJIT_ARCH_ARM
46 #elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC)
47 #ifdef __NO_FPRS__
48 #define LUAJIT_TARGET LUAJIT_ARCH_PPCSPE
49 #else
50 #define LUAJIT_TARGET LUAJIT_ARCH_PPC
51 #endif
52 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__) || defined(__MIPS)
53 #define LUAJIT_TARGET LUAJIT_ARCH_MIPS
54 #else
55 #error "No support for this architecture (yet)"
56 #endif
58 #endif
60 /* Select native OS if no target OS defined. */
61 #ifndef LUAJIT_OS
63 #if defined(_WIN32) && !defined(_XBOX_VER)
64 #define LUAJIT_OS LUAJIT_OS_WINDOWS
65 #elif defined(__linux__)
66 #define LUAJIT_OS LUAJIT_OS_LINUX
67 #elif defined(__MACH__) && defined(__APPLE__)
68 #define LUAJIT_OS LUAJIT_OS_OSX
69 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
70 defined(__NetBSD__) || defined(__OpenBSD__)
71 #define LUAJIT_OS LUAJIT_OS_BSD
72 #elif (defined(__sun__) && defined(__svr4__)) || defined(__CYGWIN__)
73 #define LUAJIT_OS LUAJIT_OS_POSIX
74 #else
75 #define LUAJIT_OS LUAJIT_OS_OTHER
76 #endif
78 #endif
80 /* Set target OS properties. */
81 #if LUAJIT_OS == LUAJIT_OS_WINDOWS
82 #define LJ_OS_NAME "Windows"
83 #elif LUAJIT_OS == LUAJIT_OS_LINUX
84 #define LJ_OS_NAME "Linux"
85 #elif LUAJIT_OS == LUAJIT_OS_OSX
86 #define LJ_OS_NAME "OSX"
87 #elif LUAJIT_OS == LUAJIT_OS_BSD
88 #define LJ_OS_NAME "BSD"
89 #elif LUAJIT_OS == LUAJIT_OS_POSIX
90 #define LJ_OS_NAME "POSIX"
91 #else
92 #define LJ_OS_NAME "Other"
93 #endif
95 #define LJ_TARGET_WINDOWS (LUAJIT_OS == LUAJIT_OS_WINDOWS)
96 #define LJ_TARGET_LINUX (LUAJIT_OS == LUAJIT_OS_LINUX)
97 #define LJ_TARGET_OSX (LUAJIT_OS == LUAJIT_OS_OSX)
98 #define LJ_TARGET_IOS (LJ_TARGET_OSX && LUAJIT_TARGET == LUAJIT_ARCH_ARM)
99 #define LJ_TARGET_POSIX (LUAJIT_OS > LUAJIT_OS_WINDOWS)
100 #define LJ_TARGET_DLOPEN LJ_TARGET_POSIX
102 #ifdef __CELLOS_LV2__
103 #define LJ_TARGET_PS3 1
104 #define LJ_TARGET_CONSOLE 1
105 #endif
107 #if _XBOX_VER >= 200
108 #define LJ_TARGET_XBOX360 1
109 #define LJ_TARGET_CONSOLE 1
110 #endif
112 #define LJ_NUMMODE_SINGLE 0 /* Single-number mode only. */
113 #define LJ_NUMMODE_SINGLE_DUAL 1 /* Default to single-number mode. */
114 #define LJ_NUMMODE_DUAL 2 /* Dual-number mode only. */
115 #define LJ_NUMMODE_DUAL_SINGLE 3 /* Default to dual-number mode. */
117 /* Set target architecture properties. */
118 #if LUAJIT_TARGET == LUAJIT_ARCH_X86
120 #define LJ_ARCH_NAME "x86"
121 #define LJ_ARCH_BITS 32
122 #define LJ_ARCH_ENDIAN LUAJIT_LE
123 #if LJ_TARGET_WINDOWS || __CYGWIN__
124 #define LJ_ABI_WIN 1
125 #else
126 #define LJ_ABI_WIN 0
127 #endif
128 #define LJ_TARGET_X86 1
129 #define LJ_TARGET_X86ORX64 1
130 #define LJ_TARGET_EHRETREG 0
131 #define LJ_TARGET_MASKSHIFT 1
132 #define LJ_TARGET_MASKROT 1
133 #define LJ_TARGET_UNALIGNED 1
134 #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL
136 #elif LUAJIT_TARGET == LUAJIT_ARCH_X64
138 #define LJ_ARCH_NAME "x64"
139 #define LJ_ARCH_BITS 64
140 #define LJ_ARCH_ENDIAN LUAJIT_LE
141 #define LJ_ABI_WIN LJ_TARGET_WINDOWS
142 #define LJ_TARGET_X64 1
143 #define LJ_TARGET_X86ORX64 1
144 #define LJ_TARGET_EHRETREG 0
145 #define LJ_TARGET_JUMPRANGE 31 /* +-2^31 = +-2GB */
146 #define LJ_TARGET_MASKSHIFT 1
147 #define LJ_TARGET_MASKROT 1
148 #define LJ_TARGET_UNALIGNED 1
149 #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL
151 #elif LUAJIT_TARGET == LUAJIT_ARCH_ARM
153 #define LJ_ARCH_NAME "arm"
154 #define LJ_ARCH_BITS 32
155 #define LJ_ARCH_ENDIAN LUAJIT_LE
156 #if !defined(LJ_ARCH_HASFPU) && __SOFTFP__
157 #define LJ_ARCH_HASFPU 0
158 #endif
159 #if !defined(LJ_ABI_SOFTFP) && !__ARM_PCS_VFP
160 #define LJ_ABI_SOFTFP 1
161 #endif
162 #define LJ_ABI_EABI 1
163 #define LJ_TARGET_ARM 1
164 #define LJ_TARGET_EHRETREG 0
165 #define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */
166 #define LJ_TARGET_MASKSHIFT 0
167 #define LJ_TARGET_MASKROT 1
168 #define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */
169 #define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL
171 #if __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH_7S__
172 #define LJ_ARCH_VERSION 70
173 #elif __ARM_ARCH_6T2__
174 #define LJ_ARCH_VERSION 61
175 #elif __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6K__ || __ARM_ARCH_6Z__ || __ARM_ARCH_6ZK__
176 #define LJ_ARCH_VERSION 60
177 #else
178 #define LJ_ARCH_VERSION 50
179 #endif
181 #elif LUAJIT_TARGET == LUAJIT_ARCH_PPC
183 #define LJ_ARCH_NAME "ppc"
184 #if _LP64
185 #define LJ_ARCH_BITS 64
186 #else
187 #define LJ_ARCH_BITS 32
188 #endif
189 #define LJ_ARCH_ENDIAN LUAJIT_BE
190 #define LJ_TARGET_PPC 1
191 #define LJ_TARGET_EHRETREG 3
192 #define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */
193 #define LJ_TARGET_MASKSHIFT 0
194 #define LJ_TARGET_MASKROT 1
195 #define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */
196 #define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL_SINGLE
198 #if _ARCH_PWR7
199 #define LJ_ARCH_VERSION 70
200 #elif _ARCH_PWR6
201 #define LJ_ARCH_VERSION 60
202 #elif _ARCH_PWR5X
203 #define LJ_ARCH_VERSION 51
204 #elif _ARCH_PWR5
205 #define LJ_ARCH_VERSION 50
206 #elif _ARCH_PWR4
207 #define LJ_ARCH_VERSION 40
208 #else
209 #define LJ_ARCH_VERSION 0
210 #endif
211 #if __PPC64__ || __powerpc64__ || LJ_TARGET_CONSOLE
212 #define LJ_ARCH_PPC64 1
213 #define LJ_ARCH_NOFFI 1
214 #endif
215 #if _ARCH_PPCSQ
216 #define LJ_ARCH_SQRT 1
217 #endif
218 #if _ARCH_PWR5X
219 #define LJ_ARCH_ROUND 1
220 #endif
221 #if __PPU__
222 #define LJ_ARCH_CELL 1
223 #endif
224 #if LJ_TARGET_XBOX360
225 #define LJ_ARCH_XENON 1
226 #endif
228 #elif LUAJIT_TARGET == LUAJIT_ARCH_PPCSPE
230 #define LJ_ARCH_NAME "ppcspe"
231 #define LJ_ARCH_BITS 32
232 #define LJ_ARCH_ENDIAN LUAJIT_BE
233 #ifndef LJ_ABI_SOFTFP
234 #define LJ_ABI_SOFTFP 1
235 #endif
236 #define LJ_ABI_EABI 1
237 #define LJ_TARGET_PPCSPE 1
238 #define LJ_TARGET_EHRETREG 3
239 #define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */
240 #define LJ_TARGET_MASKSHIFT 0
241 #define LJ_TARGET_MASKROT 1
242 #define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */
243 #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE
244 #define LJ_ARCH_NOFFI 1 /* NYI: comparisons, calls. */
245 #define LJ_ARCH_NOJIT 1
247 #elif LUAJIT_TARGET == LUAJIT_ARCH_MIPS
249 #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)
250 #define LJ_ARCH_NAME "mipsel"
251 #define LJ_ARCH_ENDIAN LUAJIT_LE
252 #else
253 #define LJ_ARCH_NAME "mips"
254 #define LJ_ARCH_ENDIAN LUAJIT_BE
255 #endif
256 #define LJ_ARCH_BITS 32
257 #define LJ_TARGET_MIPS 1
258 #define LJ_TARGET_EHRETREG 4
259 #define LJ_TARGET_JUMPRANGE 27 /* 2*2^27 = 256MB-aligned region */
260 #define LJ_TARGET_MASKSHIFT 1
261 #define LJ_TARGET_MASKROT 1
262 #define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */
263 #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE
265 #if _MIPS_ARCH_MIPS32R2
266 #define LJ_ARCH_VERSION 20
267 #else
268 #define LJ_ARCH_VERSION 10
269 #endif
271 #else
272 #error "No target architecture defined"
273 #endif
275 #ifndef LJ_PAGESIZE
276 #define LJ_PAGESIZE 4096
277 #endif
279 /* Check for minimum required compiler versions. */
280 #if defined(__GNUC__)
281 #if LJ_TARGET_X86
282 #if (__GNUC__ < 3) || ((__GNUC__ == 3) && __GNUC_MINOR__ < 4)
283 #error "Need at least GCC 3.4 or newer"
284 #endif
285 #elif LJ_TARGET_X64
286 #if __GNUC__ < 4
287 #error "Need at least GCC 4.0 or newer"
288 #endif
289 #elif LJ_TARGET_ARM
290 #if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 2)
291 #error "Need at least GCC 4.2 or newer"
292 #endif
293 #elif !LJ_TARGET_PS3
294 #if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 3)
295 #error "Need at least GCC 4.3 or newer"
296 #endif
297 #endif
298 #endif
300 /* Check target-specific constraints. */
301 #ifndef _BUILDVM_H
302 #if LJ_TARGET_X64
303 #if __USING_SJLJ_EXCEPTIONS__
304 #error "Need a C compiler with native exception handling on x64"
305 #endif
306 #elif LJ_TARGET_ARM
307 #if defined(__ARMEB__)
308 #error "No support for big-endian ARM"
309 #endif
310 #if __ARM_ARCH_6M__ || __ARM_ARCH_7M__ || __ARM_ARCH_7EM__
311 #error "No support for Cortex-M CPUs"
312 #endif
313 #if !(__ARM_EABI__ || LJ_TARGET_IOS)
314 #error "Only ARM EABI or iOS 3.0+ ABI is supported"
315 #endif
316 #elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE
317 #if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE)
318 #error "No support for PowerPC CPUs without double-precision FPU"
319 #endif
320 #if defined(_LITTLE_ENDIAN)
321 #error "No support for little-endian PowerPC"
322 #endif
323 #if defined(_LP64)
324 #error "No support for PowerPC 64 bit mode"
325 #endif
326 #elif LJ_TARGET_MIPS
327 #if defined(__mips_soft_float)
328 #error "No support for MIPS CPUs without FPU"
329 #endif
330 #endif
331 #endif
333 /* Enable or disable the dual-number mode for the VM. */
334 #if (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE && LUAJIT_NUMMODE == 2) || \
335 (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL && LUAJIT_NUMMODE == 1)
336 #error "No support for this number mode on this architecture"
337 #endif
338 #if LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL || \
339 (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL_SINGLE && LUAJIT_NUMMODE != 1) || \
340 (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE_DUAL && LUAJIT_NUMMODE == 2)
341 #define LJ_DUALNUM 1
342 #else
343 #define LJ_DUALNUM 0
344 #endif
346 #if LJ_TARGET_IOS || LJ_TARGET_CONSOLE
347 /* Runtime code generation is restricted on iOS. Complain to Apple, not me. */
348 /* Ditto for the consoles. Complain to Sony or MS, not me. */
349 #ifndef LUAJIT_ENABLE_JIT
350 #define LJ_OS_NOJIT 1
351 #endif
352 #endif
354 /* Disable or enable the JIT compiler. */
355 #if defined(LUAJIT_DISABLE_JIT) || defined(LJ_ARCH_NOJIT) || defined(LJ_OS_NOJIT)
356 #define LJ_HASJIT 0
357 #else
358 #define LJ_HASJIT 1
359 #endif
361 /* Disable or enable the FFI extension. */
362 #if defined(LUAJIT_DISABLE_FFI) || defined(LJ_ARCH_NOFFI)
363 #define LJ_HASFFI 0
364 #else
365 #define LJ_HASFFI 1
366 #endif
368 #ifndef LJ_ARCH_HASFPU
369 #define LJ_ARCH_HASFPU 1
370 #endif
371 #ifndef LJ_ABI_SOFTFP
372 #define LJ_ABI_SOFTFP 0
373 #endif
374 #define LJ_SOFTFP (!LJ_ARCH_HASFPU)
376 #if LJ_ARCH_ENDIAN == LUAJIT_BE
377 #define LJ_LE 0
378 #define LJ_BE 1
379 #define LJ_ENDIAN_SELECT(le, be) be
380 #define LJ_ENDIAN_LOHI(lo, hi) hi lo
381 #else
382 #define LJ_LE 1
383 #define LJ_BE 0
384 #define LJ_ENDIAN_SELECT(le, be) le
385 #define LJ_ENDIAN_LOHI(lo, hi) lo hi
386 #endif
388 #if LJ_ARCH_BITS == 32
389 #define LJ_32 1
390 #define LJ_64 0
391 #else
392 #define LJ_32 0
393 #define LJ_64 1
394 #endif
396 #ifndef LJ_TARGET_UNALIGNED
397 #define LJ_TARGET_UNALIGNED 0
398 #endif
400 /* Various workarounds for embedded operating systems. */
401 #if (defined(__ANDROID__) && !defined(LJ_TARGET_X86ORX64)) || defined(__symbian__) || LJ_TARGET_XBOX360
402 #define LUAJIT_NO_LOG2
403 #endif
404 #if defined(__symbian__)
405 #define LUAJIT_NO_EXP2
406 #endif
408 #if defined(LUAJIT_NO_UNWIND) || defined(__symbian__) || LJ_TARGET_IOS || LJ_TARGET_PS3
409 #define LJ_NO_UNWIND 1
410 #endif
412 /* Compatibility with Lua 5.1 vs. 5.2. */
413 #ifdef LUAJIT_ENABLE_LUA52COMPAT
414 #define LJ_52 1
415 #else
416 #define LJ_52 0
417 #endif
419 #endif