blob: ad0be19e2aed8f3e1119be6f177d459ecfc230fc [file] [log] [blame]
Tristan Matthews04616462013-11-14 16:09:34 -05001/*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright 2009-2010 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef _SLJIT_CONFIG_INTERNAL_H_
28#define _SLJIT_CONFIG_INTERNAL_H_
29
30/*
31 SLJIT defines the following variables itself depending on the configuration:
32 sljit_b, sljit_ub : signed and unsigned 8 bit byte
33 sljit_h, sljit_uh : signed and unsigned 16 bit half-word (short) type
34 sljit_i, sljit_ui : signed and unsigned 32 bit integer type
35 sljit_w, sljit_uw : signed and unsigned machine word, enough to store a pointer (same as intptr_t)
36 SLJIT_CALL : C calling convention for both calling JIT and C callbacks from JIT
37 SLJIT_32BIT_ARCHITECTURE : 32 bit architecture
38 SLJIT_64BIT_ARCHITECTURE : 64 bit architecture
39 SLJIT_WORD_SHIFT : the shift required to apply when accessing a sljit_w/sljit_uw array by index
40 SLJIT_FLOAT_SHIFT : the shift required to apply when accessing a double array by index
41 SLJIT_BIG_ENDIAN : big endian architecture
42 SLJIT_LITTLE_ENDIAN : little endian architecture
43 SLJIT_INDIRECT_CALL : see SLJIT_FUNC_OFFSET()
44 SLJIT_W : for defining 64 bit constants on 64 bit architectures (compiler workaround)
45 SLJIT_UNALIGNED : allows unaligned memory accesses for integer arithmetic (only!)
46*/
47
48#if !((defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
49 || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
50 || (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) \
51 || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
52 || (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
53 || (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
54 || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
55 || (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
56 || (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO) \
57 || (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED))
58#error "An architecture must be selected"
59#endif
60
61/* Sanity check. */
62#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
63 + (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
64 + (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) \
65 + (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
66 + (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
67 + (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
68 + (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
69 + (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
70 + (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO) \
71 + (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED) >= 2
72#error "Multiple architectures are selected"
73#endif
74
75/* Auto select option (requires compiler support) */
76#if (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO)
77
78#ifndef _WIN32
79
80#if defined(__i386__) || defined(__i386)
81#define SLJIT_CONFIG_X86_32 1
82#elif defined(__x86_64__)
83#define SLJIT_CONFIG_X86_64 1
84#elif defined(__arm__) || defined(__ARM__)
85#ifdef __thumb2__
86#define SLJIT_CONFIG_ARM_THUMB2 1
87#elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__)
88#define SLJIT_CONFIG_ARM_V7 1
89#else
90#define SLJIT_CONFIG_ARM_V5 1
91#endif
92#elif defined(__ppc64__) || defined(__powerpc64__)
93#define SLJIT_CONFIG_PPC_64 1
94#elif defined(__ppc__) || defined(__powerpc__)
95#define SLJIT_CONFIG_PPC_32 1
96#elif defined(__mips__)
97#define SLJIT_CONFIG_MIPS_32 1
98#else
99/* Unsupported architecture */
100#define SLJIT_CONFIG_UNSUPPORTED 1
101#endif
102
103#else /* !_WIN32 */
104
105#if defined(_M_X64) || defined(__x86_64__)
106#define SLJIT_CONFIG_X86_64 1
107#elif defined(_ARM_)
108#define SLJIT_CONFIG_ARM_V5 1
109#else
110#define SLJIT_CONFIG_X86_32 1
111#endif
112
113#endif /* !WIN32 */
114#endif /* SLJIT_CONFIG_AUTO */
115
116#if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
117#undef SLJIT_EXECUTABLE_ALLOCATOR
118#endif
119
120#if !(defined SLJIT_STD_MACROS_DEFINED && SLJIT_STD_MACROS_DEFINED)
121
122/* These libraries are needed for the macros below. */
123#include <stdlib.h>
124#include <string.h>
125
126#endif /* STD_MACROS_DEFINED */
127
128/* General macros:
129 Note: SLJIT is designed to be independent from them as possible.
130
131 In release mode (SLJIT_DEBUG is not defined) only the following macros are needed:
132*/
133
134#ifndef SLJIT_MALLOC
135#define SLJIT_MALLOC(size) malloc(size)
136#endif
137
138#ifndef SLJIT_FREE
139#define SLJIT_FREE(ptr) free(ptr)
140#endif
141
142#ifndef SLJIT_MEMMOVE
143#define SLJIT_MEMMOVE(dest, src, len) memmove(dest, src, len)
144#endif
145
146#ifndef SLJIT_ZEROMEM
147#define SLJIT_ZEROMEM(dest, len) memset(dest, 0, len)
148#endif
149
150#if !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY)
151
152#if defined(__GNUC__) && (__GNUC__ >= 3)
153#define SLJIT_LIKELY(x) __builtin_expect((x), 1)
154#define SLJIT_UNLIKELY(x) __builtin_expect((x), 0)
155#else
156#define SLJIT_LIKELY(x) (x)
157#define SLJIT_UNLIKELY(x) (x)
158#endif
159
160#endif /* !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY) */
161
162#ifndef SLJIT_INLINE
163/* Inline functions. */
164#define SLJIT_INLINE __inline
165#endif
166
167#ifndef SLJIT_CONST
168/* Const variables. */
169#define SLJIT_CONST const
170#endif
171
172#ifndef SLJIT_UNUSED_ARG
173/* Unused arguments. */
174#define SLJIT_UNUSED_ARG(arg) (void)arg
175#endif
176
177#if (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC)
178/* Static ABI functions. For all-in-one programs. */
179
180#if defined(__GNUC__)
181/* Disable unused warnings in gcc. */
182#define SLJIT_API_FUNC_ATTRIBUTE static __attribute__((unused))
183#else
184#define SLJIT_API_FUNC_ATTRIBUTE static
185#endif
186
187#else
188#define SLJIT_API_FUNC_ATTRIBUTE
189#endif /* (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC) */
190
191#ifndef SLJIT_CACHE_FLUSH
192
193#if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
194
195/* The __clear_cache() implementation of GCC is a dummy function on PowerPC. */
196#define SLJIT_CACHE_FLUSH(from, to) \
197 ppc_cache_flush((from), (to))
198
199#elif (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
200
201/* Not required to implement on archs with unified caches. */
202#define SLJIT_CACHE_FLUSH(from, to)
203
204#else
205
206/* Calls __ARM_NR_cacheflush on ARM-Linux. */
207#define SLJIT_CACHE_FLUSH(from, to) \
208 __clear_cache((char*)(from), (char*)(to))
209
210#endif
211
212#endif /* !SLJIT_CACHE_FLUSH */
213
214/* 8 bit byte type. */
215typedef unsigned char sljit_ub;
216typedef signed char sljit_b;
217
218/* 16 bit half-word type. */
219typedef unsigned short int sljit_uh;
220typedef signed short int sljit_h;
221
222/* 32 bit integer type. */
223typedef unsigned int sljit_ui;
224typedef signed int sljit_i;
225
226/* Machine word type. Can encapsulate a pointer.
227 32 bit for 32 bit machines.
228 64 bit for 64 bit machines. */
229#if !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) && !(defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
230#define SLJIT_32BIT_ARCHITECTURE 1
231#define SLJIT_WORD_SHIFT 2
232typedef unsigned int sljit_uw;
233typedef int sljit_w;
234#else
235#define SLJIT_64BIT_ARCHITECTURE 1
236#define SLJIT_WORD_SHIFT 3
237#ifdef _WIN32
238typedef unsigned __int64 sljit_uw;
239typedef __int64 sljit_w;
240#else
241typedef unsigned long int sljit_uw;
242typedef long int sljit_w;
243#endif
244#endif
245
246/* Double precision. */
247#define SLJIT_FLOAT_SHIFT 3
248
249#ifndef SLJIT_W
250
251/* Defining long constants. */
252#if (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
253#define SLJIT_W(w) (w##ll)
254#else
255#define SLJIT_W(w) (w)
256#endif
257
258#endif /* !SLJIT_W */
259
260#ifndef SLJIT_CALL
261
262/* ABI (Application Binary Interface) types. */
263#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
264
265#if defined(__GNUC__)
266
267#define SLJIT_CALL __attribute__ ((fastcall))
268#define SLJIT_X86_32_FASTCALL 1
269
270#elif defined(_WIN32)
271
272#ifdef __BORLANDC__
273#define SLJIT_CALL __msfastcall
274#else /* __BORLANDC__ */
275#define SLJIT_CALL __fastcall
276#endif /* __BORLANDC__ */
277#define SLJIT_X86_32_FASTCALL 1
278
279#else /* defined(_WIN32) */
280#define SLJIT_CALL __stdcall
281#endif
282
283#else /* Other architectures. */
284
285#define SLJIT_CALL
286
287#endif /* SLJIT_CONFIG_X86_32 */
288
289#endif /* !SLJIT_CALL */
290
291#if !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN)
292
293/* These macros are useful for the application. */
294#if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
295#define SLJIT_BIG_ENDIAN 1
296
297#elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
298
299#ifdef __MIPSEL__
300#define SLJIT_LITTLE_ENDIAN 1
301#else
302#define SLJIT_BIG_ENDIAN 1
303#endif
304
305#else
306#define SLJIT_LITTLE_ENDIAN 1
307#endif
308
309#endif /* !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN) */
310
311/* Sanity check. */
312#if (defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
313#error "Exactly one endianness must be selected"
314#endif
315
316#if !(defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && !(defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
317#error "Exactly one endianness must be selected"
318#endif
319
320#if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
321/* It seems ppc64 compilers use an indirect addressing for functions.
322 It makes things really complicated. */
323#define SLJIT_INDIRECT_CALL 1
324#endif
325
326#ifndef SLJIT_SSE2
327
328#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
329/* Turn on SSE2 support on x86 (operating on doubles).
330 (Better performance than legacy fpu instructions). */
331#define SLJIT_SSE2 1
332
333#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
334/* Auto detect SSE2 support using CPUID.
335 On 64 bit x86 cpus, sse2 must be present. */
336#define SLJIT_SSE2_AUTO 1
337#endif
338
339#endif /* (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) */
340
341#endif /* !SLJIT_SSE2 */
342
343#ifndef SLJIT_UNALIGNED
344
345#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
346 || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
347 || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
348 || (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
349 || (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
350 || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
351#define SLJIT_UNALIGNED 1
352#endif
353
354#endif /* !SLJIT_UNALIGNED */
355
356#if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
357SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size);
358SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr);
359#define SLJIT_MALLOC_EXEC(size) sljit_malloc_exec(size)
360#define SLJIT_FREE_EXEC(ptr) sljit_free_exec(ptr)
361#endif
362
363#if (defined SLJIT_DEBUG && SLJIT_DEBUG) || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
364#include <stdio.h>
365#endif
366
367#if (defined SLJIT_DEBUG && SLJIT_DEBUG)
368
369/* Feel free to redefine these two macros. */
370#ifndef SLJIT_ASSERT
371
372#define SLJIT_HALT_PROCESS() \
373 *((int*)0) = 0
374
375#define SLJIT_ASSERT(x) \
376 do { \
377 if (SLJIT_UNLIKELY(!(x))) { \
378 printf("Assertion failed at " __FILE__ ":%d\n", __LINE__); \
379 SLJIT_HALT_PROCESS(); \
380 } \
381 } while (0)
382
383#endif /* !SLJIT_ASSERT */
384
385#ifndef SLJIT_ASSERT_STOP
386
387#define SLJIT_ASSERT_STOP() \
388 do { \
389 printf("Should never been reached " __FILE__ ":%d\n", __LINE__); \
390 SLJIT_HALT_PROCESS(); \
391 } while (0)
392
393#endif /* !SLJIT_ASSERT_STOP */
394
395#else /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
396
397#undef SLJIT_ASSERT
398#undef SLJIT_ASSERT_STOP
399
400#define SLJIT_ASSERT(x) \
401 do { } while (0)
402#define SLJIT_ASSERT_STOP() \
403 do { } while (0)
404
405#endif /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
406
407#ifndef SLJIT_COMPILE_ASSERT
408
409/* Should be improved eventually. */
410#define SLJIT_COMPILE_ASSERT(x, description) \
411 SLJIT_ASSERT(x)
412
413#endif /* !SLJIT_COMPILE_ASSERT */
414
415#endif