1 | /* |
2 | ** Target architecture selection. |
3 | ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h |
4 | */ |
5 | |
6 | #ifndef _LJ_ARCH_H |
7 | #define _LJ_ARCH_H |
8 | |
9 | #include "lua.h" |
10 | |
11 | /* Target endianess. */ |
12 | #define LUAJIT_LE 0 |
13 | #define LUAJIT_BE 1 |
14 | |
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 |
28 | |
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 |
36 | |
37 | /* Select native target if no target defined. */ |
38 | #ifndef LUAJIT_TARGET |
39 | |
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 |
57 | |
58 | #endif |
59 | |
60 | /* Select native OS if no target OS defined. */ |
61 | #ifndef LUAJIT_OS |
62 | |
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__)) && !defined(__ORBIS__) |
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 |
77 | |
78 | #endif |
79 | |
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 |
94 | |
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 |
101 | |
102 | #ifdef __CELLOS_LV2__ |
103 | #define LJ_TARGET_PS3 1 |
104 | #define LJ_TARGET_CONSOLE 1 |
105 | #endif |
106 | |
107 | #ifdef __ORBIS__ |
108 | #define LJ_TARGET_PS4 1 |
109 | #define LJ_TARGET_CONSOLE 1 |
110 | #undef NULL |
111 | #define NULL ((void*)0) |
112 | #endif |
113 | |
114 | #if _XBOX_VER >= 200 |
115 | #define LJ_TARGET_XBOX360 1 |
116 | #define LJ_TARGET_CONSOLE 1 |
117 | #endif |
118 | |
119 | #define LJ_NUMMODE_SINGLE 0 /* Single-number mode only. */ |
120 | #define LJ_NUMMODE_SINGLE_DUAL 1 /* Default to single-number mode. */ |
121 | #define LJ_NUMMODE_DUAL 2 /* Dual-number mode only. */ |
122 | #define LJ_NUMMODE_DUAL_SINGLE 3 /* Default to dual-number mode. */ |
123 | |
124 | /* Set target architecture properties. */ |
125 | #if LUAJIT_TARGET == LUAJIT_ARCH_X86 |
126 | |
127 | #define LJ_ARCH_NAME "x86" |
128 | #define LJ_ARCH_BITS 32 |
129 | #define LJ_ARCH_ENDIAN LUAJIT_LE |
130 | #if LJ_TARGET_WINDOWS || __CYGWIN__ |
131 | #define LJ_ABI_WIN 1 |
132 | #else |
133 | #define LJ_ABI_WIN 0 |
134 | #endif |
135 | #define LJ_TARGET_X86 1 |
136 | #define LJ_TARGET_X86ORX64 1 |
137 | #define LJ_TARGET_EHRETREG 0 |
138 | #define LJ_TARGET_MASKSHIFT 1 |
139 | #define LJ_TARGET_MASKROT 1 |
140 | #define LJ_TARGET_UNALIGNED 1 |
141 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL |
142 | |
143 | #elif LUAJIT_TARGET == LUAJIT_ARCH_X64 |
144 | |
145 | #define LJ_ARCH_NAME "x64" |
146 | #define LJ_ARCH_BITS 64 |
147 | #define LJ_ARCH_ENDIAN LUAJIT_LE |
148 | #define LJ_ABI_WIN LJ_TARGET_WINDOWS |
149 | #define LJ_TARGET_X64 1 |
150 | #define LJ_TARGET_X86ORX64 1 |
151 | #define LJ_TARGET_EHRETREG 0 |
152 | #define LJ_TARGET_JUMPRANGE 31 /* +-2^31 = +-2GB */ |
153 | #define LJ_TARGET_MASKSHIFT 1 |
154 | #define LJ_TARGET_MASKROT 1 |
155 | #define LJ_TARGET_UNALIGNED 1 |
156 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL |
157 | |
158 | #elif LUAJIT_TARGET == LUAJIT_ARCH_ARM |
159 | |
160 | #define LJ_ARCH_NAME "arm" |
161 | #define LJ_ARCH_BITS 32 |
162 | #define LJ_ARCH_ENDIAN LUAJIT_LE |
163 | #if !defined(LJ_ARCH_HASFPU) && __SOFTFP__ |
164 | #define LJ_ARCH_HASFPU 0 |
165 | #endif |
166 | #if !defined(LJ_ABI_SOFTFP) && !__ARM_PCS_VFP |
167 | #define LJ_ABI_SOFTFP 1 |
168 | #endif |
169 | #define LJ_ABI_EABI 1 |
170 | #define LJ_TARGET_ARM 1 |
171 | #define LJ_TARGET_EHRETREG 0 |
172 | #define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ |
173 | #define LJ_TARGET_MASKSHIFT 0 |
174 | #define LJ_TARGET_MASKROT 1 |
175 | #define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ |
176 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL |
177 | |
178 | #if __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH_7S__ |
179 | #define LJ_ARCH_VERSION 70 |
180 | #elif __ARM_ARCH_6T2__ |
181 | #define LJ_ARCH_VERSION 61 |
182 | #elif __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6K__ || __ARM_ARCH_6Z__ || __ARM_ARCH_6ZK__ |
183 | #define LJ_ARCH_VERSION 60 |
184 | #else |
185 | #define LJ_ARCH_VERSION 50 |
186 | #endif |
187 | |
188 | #elif LUAJIT_TARGET == LUAJIT_ARCH_PPC |
189 | |
190 | #define LJ_ARCH_NAME "ppc" |
191 | #if _LP64 |
192 | #define LJ_ARCH_BITS 64 |
193 | #else |
194 | #define LJ_ARCH_BITS 32 |
195 | #endif |
196 | #define LJ_ARCH_ENDIAN LUAJIT_BE |
197 | #define LJ_TARGET_PPC 1 |
198 | #define LJ_TARGET_EHRETREG 3 |
199 | #define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ |
200 | #define LJ_TARGET_MASKSHIFT 0 |
201 | #define LJ_TARGET_MASKROT 1 |
202 | #define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ |
203 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL_SINGLE |
204 | |
205 | #if _ARCH_PWR7 |
206 | #define LJ_ARCH_VERSION 70 |
207 | #elif _ARCH_PWR6 |
208 | #define LJ_ARCH_VERSION 60 |
209 | #elif _ARCH_PWR5X |
210 | #define LJ_ARCH_VERSION 51 |
211 | #elif _ARCH_PWR5 |
212 | #define LJ_ARCH_VERSION 50 |
213 | #elif _ARCH_PWR4 |
214 | #define LJ_ARCH_VERSION 40 |
215 | #else |
216 | #define LJ_ARCH_VERSION 0 |
217 | #endif |
218 | #if __PPC64__ || __powerpc64__ || LJ_TARGET_CONSOLE |
219 | #define LJ_ARCH_PPC64 1 |
220 | #define LJ_ARCH_NOFFI 1 |
221 | #endif |
222 | #if _ARCH_PPCSQ |
223 | #define LJ_ARCH_SQRT 1 |
224 | #endif |
225 | #if _ARCH_PWR5X |
226 | #define LJ_ARCH_ROUND 1 |
227 | #endif |
228 | #if __PPU__ |
229 | #define LJ_ARCH_CELL 1 |
230 | #endif |
231 | #if LJ_TARGET_XBOX360 |
232 | #define LJ_ARCH_XENON 1 |
233 | #endif |
234 | |
235 | #elif LUAJIT_TARGET == LUAJIT_ARCH_PPCSPE |
236 | |
237 | #define LJ_ARCH_NAME "ppcspe" |
238 | #define LJ_ARCH_BITS 32 |
239 | #define LJ_ARCH_ENDIAN LUAJIT_BE |
240 | #ifndef LJ_ABI_SOFTFP |
241 | #define LJ_ABI_SOFTFP 1 |
242 | #endif |
243 | #define LJ_ABI_EABI 1 |
244 | #define LJ_TARGET_PPCSPE 1 |
245 | #define LJ_TARGET_EHRETREG 3 |
246 | #define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ |
247 | #define LJ_TARGET_MASKSHIFT 0 |
248 | #define LJ_TARGET_MASKROT 1 |
249 | #define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ |
250 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE |
251 | #define LJ_ARCH_NOFFI 1 /* NYI: comparisons, calls. */ |
252 | #define LJ_ARCH_NOJIT 1 |
253 | |
254 | #elif LUAJIT_TARGET == LUAJIT_ARCH_MIPS |
255 | |
256 | #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) |
257 | #define LJ_ARCH_NAME "mipsel" |
258 | #define LJ_ARCH_ENDIAN LUAJIT_LE |
259 | #else |
260 | #define LJ_ARCH_NAME "mips" |
261 | #define LJ_ARCH_ENDIAN LUAJIT_BE |
262 | #endif |
263 | #define LJ_ARCH_BITS 32 |
264 | #define LJ_TARGET_MIPS 1 |
265 | #define LJ_TARGET_EHRETREG 4 |
266 | #define LJ_TARGET_JUMPRANGE 27 /* 2*2^27 = 256MB-aligned region */ |
267 | #define LJ_TARGET_MASKSHIFT 1 |
268 | #define LJ_TARGET_MASKROT 1 |
269 | #define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ |
270 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE |
271 | |
272 | #if _MIPS_ARCH_MIPS32R2 |
273 | #define LJ_ARCH_VERSION 20 |
274 | #else |
275 | #define LJ_ARCH_VERSION 10 |
276 | #endif |
277 | |
278 | #else |
279 | #error "No target architecture defined" |
280 | #endif |
281 | |
282 | #ifndef LJ_PAGESIZE |
283 | #define LJ_PAGESIZE 4096 |
284 | #endif |
285 | |
286 | /* Check for minimum required compiler versions. */ |
287 | #if defined(__GNUC__) |
288 | #if LJ_TARGET_X86 |
289 | #if (__GNUC__ < 3) || ((__GNUC__ == 3) && __GNUC_MINOR__ < 4) |
290 | #error "Need at least GCC 3.4 or newer" |
291 | #endif |
292 | #elif LJ_TARGET_X64 |
293 | #if __GNUC__ < 4 |
294 | #error "Need at least GCC 4.0 or newer" |
295 | #endif |
296 | #elif LJ_TARGET_ARM |
297 | #if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 2) |
298 | #error "Need at least GCC 4.2 or newer" |
299 | #endif |
300 | #elif !LJ_TARGET_PS3 |
301 | #if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 3) |
302 | #error "Need at least GCC 4.3 or newer" |
303 | #endif |
304 | #endif |
305 | #endif |
306 | |
307 | /* Check target-specific constraints. */ |
308 | #ifndef _BUILDVM_H |
309 | #if LJ_TARGET_X64 |
310 | #if __USING_SJLJ_EXCEPTIONS__ |
311 | #error "Need a C compiler with native exception handling on x64" |
312 | #endif |
313 | #elif LJ_TARGET_ARM |
314 | #if defined(__ARMEB__) |
315 | #error "No support for big-endian ARM" |
316 | #endif |
317 | #if __ARM_ARCH_6M__ || __ARM_ARCH_7M__ || __ARM_ARCH_7EM__ |
318 | #error "No support for Cortex-M CPUs" |
319 | #endif |
320 | #if !(__ARM_EABI__ || LJ_TARGET_IOS) |
321 | #error "Only ARM EABI or iOS 3.0+ ABI is supported" |
322 | #endif |
323 | #elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE |
324 | #if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE) |
325 | #error "No support for PowerPC CPUs without double-precision FPU" |
326 | #endif |
327 | #if defined(_LITTLE_ENDIAN) |
328 | #error "No support for little-endian PowerPC" |
329 | #endif |
330 | #if defined(_LP64) |
331 | #error "No support for PowerPC 64 bit mode" |
332 | #endif |
333 | #elif LJ_TARGET_MIPS |
334 | #if defined(__mips_soft_float) |
335 | #error "No support for MIPS CPUs without FPU" |
336 | #endif |
337 | #endif |
338 | #endif |
339 | |
340 | /* Enable or disable the dual-number mode for the VM. */ |
341 | #if (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE && LUAJIT_NUMMODE == 2) || \ |
342 | (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL && LUAJIT_NUMMODE == 1) |
343 | #error "No support for this number mode on this architecture" |
344 | #endif |
345 | #if LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL || \ |
346 | (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL_SINGLE && LUAJIT_NUMMODE != 1) || \ |
347 | (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE_DUAL && LUAJIT_NUMMODE == 2) |
348 | #define LJ_DUALNUM 1 |
349 | #else |
350 | #define LJ_DUALNUM 0 |
351 | #endif |
352 | |
353 | #if LJ_TARGET_IOS || LJ_TARGET_CONSOLE |
354 | /* Runtime code generation is restricted on iOS. Complain to Apple, not me. */ |
355 | /* Ditto for the consoles. Complain to Sony or MS, not me. */ |
356 | #ifndef LUAJIT_ENABLE_JIT |
357 | #define LJ_OS_NOJIT 1 |
358 | #endif |
359 | #endif |
360 | |
361 | /* Disable or enable the JIT compiler. */ |
362 | #if defined(LUAJIT_DISABLE_JIT) || defined(LJ_ARCH_NOJIT) || defined(LJ_OS_NOJIT) |
363 | #define LJ_HASJIT 0 |
364 | #else |
365 | #define LJ_HASJIT 1 |
366 | #endif |
367 | |
368 | /* Disable or enable the FFI extension. */ |
369 | #if defined(LUAJIT_DISABLE_FFI) || defined(LJ_ARCH_NOFFI) |
370 | #define LJ_HASFFI 0 |
371 | #else |
372 | #define LJ_HASFFI 1 |
373 | #endif |
374 | |
375 | #ifndef LJ_ARCH_HASFPU |
376 | #define LJ_ARCH_HASFPU 1 |
377 | #endif |
378 | #ifndef LJ_ABI_SOFTFP |
379 | #define LJ_ABI_SOFTFP 0 |
380 | #endif |
381 | #define LJ_SOFTFP (!LJ_ARCH_HASFPU) |
382 | |
383 | #if LJ_ARCH_ENDIAN == LUAJIT_BE |
384 | #define LJ_LE 0 |
385 | #define LJ_BE 1 |
386 | #define LJ_ENDIAN_SELECT(le, be) be |
387 | #define LJ_ENDIAN_LOHI(lo, hi) hi lo |
388 | #else |
389 | #define LJ_LE 1 |
390 | #define LJ_BE 0 |
391 | #define LJ_ENDIAN_SELECT(le, be) le |
392 | #define LJ_ENDIAN_LOHI(lo, hi) lo hi |
393 | #endif |
394 | |
395 | #if LJ_ARCH_BITS == 32 |
396 | #define LJ_32 1 |
397 | #define LJ_64 0 |
398 | #else |
399 | #define LJ_32 0 |
400 | #define LJ_64 1 |
401 | #endif |
402 | |
403 | #ifndef LJ_TARGET_UNALIGNED |
404 | #define LJ_TARGET_UNALIGNED 0 |
405 | #endif |
406 | |
407 | /* Various workarounds for embedded operating systems. */ |
408 | #if (defined(__ANDROID__) && !defined(LJ_TARGET_X86ORX64)) || defined(__symbian__) || LJ_TARGET_XBOX360 |
409 | #define LUAJIT_NO_LOG2 |
410 | #endif |
411 | #if defined(__symbian__) |
412 | #define LUAJIT_NO_EXP2 |
413 | #endif |
414 | |
415 | #if defined(LUAJIT_NO_UNWIND) || defined(__symbian__) || LJ_TARGET_IOS || LJ_TARGET_PS3 |
416 | #define LJ_NO_UNWIND 1 |
417 | #endif |
418 | |
419 | /* Compatibility with Lua 5.1 vs. 5.2. */ |
420 | #ifdef LUAJIT_ENABLE_LUA52COMPAT |
421 | #define LJ_52 1 |
422 | #else |
423 | #define LJ_52 0 |
424 | #endif |
425 | |
426 | #endif |
427 | |