Home | History | Annotate | Line # | Download | only in sljit_src
sljitConfigInternal.h revision 1.5
      1 /*
      2  *    Stack-less Just-In-Time compiler
      3  *
      4  *    Copyright 2009-2012 Zoltan Herczeg (hzmester (at) 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 macros depending on the target architecture:
     32 
     33    Feature detection (boolean) macros:
     34    SLJIT_32BIT_ARCHITECTURE : 32 bit architecture
     35    SLJIT_64BIT_ARCHITECTURE : 64 bit architecture
     36    SLJIT_WORD_SHIFT : the shift required to apply when accessing a sljit_w/sljit_uw array by index
     37    SLJIT_FLOAT_SHIFT : the shift required to apply when accessing a double array by index
     38    SLJIT_LITTLE_ENDIAN : little endian architecture
     39    SLJIT_BIG_ENDIAN : big endian architecture
     40    SLJIT_UNALIGNED : allows unaligned memory accesses for non-fpu operations (only!)
     41    SLJIT_INDIRECT_CALL : see SLJIT_FUNC_OFFSET() for more information
     42    SLJIT_RETURN_ADDRESS_OFFSET : a return instruction always adds this offset to the return address
     43 
     44    Types and useful macros:
     45    sljit_b, sljit_ub : signed and unsigned 8 bit byte
     46    sljit_h, sljit_uh : signed and unsigned 16 bit half-word (short) type
     47    sljit_i, sljit_ui : signed and unsigned 32 bit integer type
     48    sljit_w, sljit_uw : signed and unsigned machine word, enough to store a pointer (same as intptr_t)
     49    SLJIT_CALL : C calling convention define for both calling JIT form C and C callbacks for JIT
     50    SLJIT_W(number) : defining 64 bit constants on 64 bit architectures (compiler independent helper)
     51 */
     52 
     53 #if !((defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
     54 	|| (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
     55 	|| (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) \
     56 	|| (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
     57 	|| (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
     58 	|| (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
     59 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
     60 	|| (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
     61 	|| (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) \
     62 	|| (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO) \
     63 	|| (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED))
     64 #error "An architecture must be selected"
     65 #endif
     66 
     67 /* Sanity check. */
     68 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
     69 	+ (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
     70 	+ (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) \
     71 	+ (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
     72 	+ (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
     73 	+ (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
     74 	+ (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
     75 	+ (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) \
     76 	+ (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) \
     77 	+ (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO) \
     78 	+ (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED) >= 2
     79 #error "Multiple architectures are selected"
     80 #endif
     81 
     82 /* Auto select option (requires compiler support) */
     83 #if (defined SLJIT_CONFIG_AUTO && SLJIT_CONFIG_AUTO)
     84 
     85 #ifndef _WIN32
     86 
     87 #if defined(__i386__) || defined(__i386)
     88 #define SLJIT_CONFIG_X86_32 1
     89 #elif defined(__x86_64__)
     90 #define SLJIT_CONFIG_X86_64 1
     91 #elif defined(__arm__) || defined(__ARM__)
     92 #ifdef __thumb2__
     93 #define SLJIT_CONFIG_ARM_THUMB2 1
     94 #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__)
     95 #define SLJIT_CONFIG_ARM_V7 1
     96 #else
     97 #define SLJIT_CONFIG_ARM_V5 1
     98 #endif
     99 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(_ARCH_PPC64) || (defined(_POWER) && defined(__64BIT__))
    100 #define SLJIT_CONFIG_PPC_64 1
    101 #elif defined(__ppc__) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(_ARCH_PWR) || defined(_ARCH_PWR2) || defined(_POWER)
    102 #define SLJIT_CONFIG_PPC_32 1
    103 #elif defined(__mips__)
    104 #define SLJIT_CONFIG_MIPS_32 1
    105 #elif defined(__sparc__) || defined(__sparc)
    106 #define SLJIT_CONFIG_SPARC_32 1
    107 #else
    108 /* Unsupported architecture */
    109 #define SLJIT_CONFIG_UNSUPPORTED 1
    110 #endif
    111 
    112 #else /* !_WIN32 */
    113 
    114 #if defined(_M_X64) || defined(__x86_64__)
    115 #define SLJIT_CONFIG_X86_64 1
    116 #elif defined(_ARM_)
    117 #define SLJIT_CONFIG_ARM_V5 1
    118 #else
    119 #define SLJIT_CONFIG_X86_32 1
    120 #endif
    121 
    122 #endif /* !WIN32 */
    123 #endif /* SLJIT_CONFIG_AUTO */
    124 
    125 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
    126 #undef SLJIT_EXECUTABLE_ALLOCATOR
    127 #endif
    128 
    129 #if !(defined SLJIT_STD_MACROS_DEFINED && SLJIT_STD_MACROS_DEFINED)
    130 
    131 #ifndef _KERNEL
    132 /* These libraries are needed for the macros below. */
    133 #include <stdlib.h>
    134 #include <string.h>
    135 #endif
    136 
    137 #endif /* STD_MACROS_DEFINED */
    138 
    139 /* General macros:
    140    Note: SLJIT is designed to be independent from them as possible.
    141 
    142    In release mode (SLJIT_DEBUG is not defined) only the following macros are needed:
    143 */
    144 
    145 #ifndef SLJIT_MALLOC
    146 #define SLJIT_MALLOC(size) malloc(size)
    147 #endif
    148 
    149 #ifndef SLJIT_FREE
    150 #define SLJIT_FREE(ptr) free(ptr)
    151 #endif
    152 
    153 #ifndef SLJIT_MEMMOVE
    154 #define SLJIT_MEMMOVE(dest, src, len) memmove(dest, src, len)
    155 #endif
    156 
    157 #ifndef SLJIT_ZEROMEM
    158 #define SLJIT_ZEROMEM(dest, len) memset(dest, 0, len)
    159 #endif
    160 
    161 #if !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY)
    162 
    163 #if defined(__GNUC__) && (__GNUC__ >= 3)
    164 #define SLJIT_LIKELY(x)		__builtin_expect((x), 1)
    165 #define SLJIT_UNLIKELY(x)	__builtin_expect((x), 0)
    166 #else
    167 #define SLJIT_LIKELY(x)		(x)
    168 #define SLJIT_UNLIKELY(x)	(x)
    169 #endif
    170 
    171 #endif /* !defined(SLJIT_LIKELY) && !defined(SLJIT_UNLIKELY) */
    172 
    173 #ifndef SLJIT_INLINE
    174 /* Inline functions. */
    175 #define SLJIT_INLINE __inline
    176 #endif
    177 
    178 #ifndef SLJIT_CONST
    179 /* Const variables. */
    180 #define SLJIT_CONST const
    181 #endif
    182 
    183 #ifndef SLJIT_UNUSED_ARG
    184 /* Unused arguments. */
    185 #define SLJIT_UNUSED_ARG(arg) (void)arg
    186 #endif
    187 
    188 #if (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC)
    189 /* Static ABI functions. For all-in-one programs. */
    190 
    191 #if defined(__GNUC__)
    192 /* Disable unused warnings in gcc. */
    193 #define SLJIT_API_FUNC_ATTRIBUTE static __attribute__((unused))
    194 #else
    195 #define SLJIT_API_FUNC_ATTRIBUTE static
    196 #endif
    197 
    198 #else
    199 #define SLJIT_API_FUNC_ATTRIBUTE
    200 #endif /* (defined SLJIT_CONFIG_STATIC && SLJIT_CONFIG_STATIC) */
    201 
    202 #ifndef SLJIT_CACHE_FLUSH
    203 
    204 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
    205 
    206 /* Not required to implement on archs with unified caches. */
    207 #define SLJIT_CACHE_FLUSH(from, to)
    208 
    209 #elif defined __APPLE__
    210 
    211 /* Supported by all macs since Mac OS 10.5.
    212    However, it does not work on non-jailbroken iOS devices,
    213    although the compilation is successful. */
    214 
    215 #define SLJIT_CACHE_FLUSH(from, to) \
    216 	sys_icache_invalidate((char*)(from), (char*)(to) - (char*)(from))
    217 
    218 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
    219 
    220 /* The __clear_cache() implementation of GCC is a dummy function on PowerPC. */
    221 #define SLJIT_CACHE_FLUSH(from, to) \
    222 	ppc_cache_flush((from), (to))
    223 
    224 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
    225 
    226 /* The __clear_cache() implementation of GCC is a dummy function on Sparc. */
    227 #define SLJIT_CACHE_FLUSH(from, to) \
    228 	sparc_cache_flush((from), (to))
    229 
    230 #else
    231 
    232 /* Calls __ARM_NR_cacheflush on ARM-Linux. */
    233 #define SLJIT_CACHE_FLUSH(from, to) \
    234 	__clear_cache((char*)(from), (char*)(to))
    235 
    236 #endif
    237 
    238 #endif /* !SLJIT_CACHE_FLUSH */
    239 
    240 /* 8 bit byte type. */
    241 typedef unsigned char sljit_ub;
    242 typedef signed char sljit_b;
    243 
    244 /* 16 bit half-word type. */
    245 typedef unsigned short int sljit_uh;
    246 typedef signed short int sljit_h;
    247 
    248 /* 32 bit integer type. */
    249 typedef unsigned int sljit_ui;
    250 typedef signed int sljit_i;
    251 
    252 /* Machine word type. Can encapsulate a pointer.
    253      32 bit for 32 bit machines.
    254      64 bit for 64 bit machines. */
    255 #if (defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
    256 /* Just to have something. */
    257 #define SLJIT_WORD_SHIFT 0
    258 typedef unsigned long int sljit_uw;
    259 typedef long int sljit_w;
    260 #elif !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) && !(defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
    261 #define SLJIT_32BIT_ARCHITECTURE 1
    262 #define SLJIT_WORD_SHIFT 2
    263 typedef unsigned int sljit_uw;
    264 typedef int sljit_w;
    265 #else
    266 #define SLJIT_64BIT_ARCHITECTURE 1
    267 #define SLJIT_WORD_SHIFT 3
    268 #ifdef _WIN32
    269 typedef unsigned __int64 sljit_uw;
    270 typedef __int64 sljit_w;
    271 #else
    272 typedef unsigned long int sljit_uw;
    273 typedef long int sljit_w;
    274 #endif
    275 #endif
    276 
    277 /* Double precision. */
    278 #define SLJIT_FLOAT_SHIFT 3
    279 
    280 #ifndef SLJIT_W
    281 
    282 /* Defining long constants. */
    283 #if (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
    284 #define SLJIT_W(w)	(w##ll)
    285 #else
    286 #define SLJIT_W(w)	(w)
    287 #endif
    288 
    289 #endif /* !SLJIT_W */
    290 
    291 #ifndef SLJIT_CALL
    292 
    293 /* ABI (Application Binary Interface) types. */
    294 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
    295 
    296 #if defined(__GNUC__)
    297 
    298 #define SLJIT_CALL __attribute__ ((fastcall))
    299 #define SLJIT_X86_32_FASTCALL 1
    300 
    301 #elif defined(_WIN32)
    302 
    303 #ifdef __BORLANDC__
    304 #define SLJIT_CALL __msfastcall
    305 #else /* __BORLANDC__ */
    306 #define SLJIT_CALL __fastcall
    307 #endif /* __BORLANDC__ */
    308 #define SLJIT_X86_32_FASTCALL 1
    309 
    310 #else /* defined(_WIN32) */
    311 #define SLJIT_CALL __stdcall
    312 #endif
    313 
    314 #else /* Other architectures. */
    315 
    316 #define SLJIT_CALL
    317 
    318 #endif /* SLJIT_CONFIG_X86_32 */
    319 
    320 #endif /* !SLJIT_CALL */
    321 
    322 #if !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN)
    323 
    324 /* These macros are useful for the application. */
    325 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
    326 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) \
    327 	|| (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
    328 #define SLJIT_BIG_ENDIAN 1
    329 
    330 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
    331 
    332 #ifdef __MIPSEL__
    333 #define SLJIT_LITTLE_ENDIAN 1
    334 #else
    335 #define SLJIT_BIG_ENDIAN 1
    336 #endif
    337 
    338 #else
    339 #define SLJIT_LITTLE_ENDIAN 1
    340 #endif
    341 
    342 #endif /* !defined(SLJIT_BIG_ENDIAN) && !defined(SLJIT_LITTLE_ENDIAN) */
    343 
    344 /* Sanity check. */
    345 #if (defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
    346 #error "Exactly one endianness must be selected"
    347 #endif
    348 
    349 #if !(defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN) && !(defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
    350 #error "Exactly one endianness must be selected"
    351 #endif
    352 
    353 #ifndef SLJIT_INDIRECT_CALL
    354 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) || (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32 && defined _AIX)
    355 /* It seems certain ppc compilers use an indirect addressing for functions
    356    which makes things complicated. */
    357 #define SLJIT_INDIRECT_CALL 1
    358 #endif
    359 #endif /* SLJIT_INDIRECT_CALL */
    360 
    361 #ifndef SLJIT_RETURN_ADDRESS_OFFSET
    362 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
    363 #define SLJIT_RETURN_ADDRESS_OFFSET 8
    364 #else
    365 #define SLJIT_RETURN_ADDRESS_OFFSET 0
    366 #endif
    367 #endif /* SLJIT_RETURN_ADDRESS_OFFSET */
    368 
    369 #ifndef SLJIT_SSE2
    370 
    371 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
    372 /* Turn on SSE2 support on x86. */
    373 #define SLJIT_SSE2 1
    374 
    375 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
    376 /* Auto detect SSE2 support using CPUID.
    377    On 64 bit x86 cpus, sse2 must be present. */
    378 #define SLJIT_DETECT_SSE2 1
    379 #endif
    380 
    381 #endif /* (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) */
    382 
    383 #endif /* !SLJIT_SSE2 */
    384 
    385 #ifndef SLJIT_UNALIGNED
    386 
    387 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) \
    388 	|| (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) \
    389 	|| (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) \
    390 	|| (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) \
    391 	|| (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) \
    392 	|| (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
    393 #define SLJIT_UNALIGNED 1
    394 #endif
    395 
    396 #endif /* !SLJIT_UNALIGNED */
    397 
    398 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
    399 SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size);
    400 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr);
    401 #define SLJIT_MALLOC_EXEC(size) sljit_malloc_exec(size)
    402 #define SLJIT_FREE_EXEC(ptr) sljit_free_exec(ptr)
    403 #endif
    404 
    405 #if (defined SLJIT_DEBUG && SLJIT_DEBUG) || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
    406 #ifdef _KERNEL
    407 #include <sys/systm.h>
    408 #else
    409 #include <stdio.h>
    410 #endif
    411 #endif
    412 
    413 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
    414 
    415 /* Feel free to redefine these two macros. */
    416 #ifndef SLJIT_ASSERT
    417 
    418 #include <assert.h>
    419 #include <stdlib.h>
    420 
    421 #define SLJIT_HALT_PROCESS() abort()
    422 
    423 #define SLJIT_ASSERT(x) assert(x)
    424 
    425 #endif /* !SLJIT_ASSERT */
    426 
    427 #ifndef SLJIT_ASSERT_STOP
    428 
    429 #define SLJIT_ASSERT_STOP() \
    430 	do { \
    431 		printf("Should never been reached " __FILE__ ":%d\n", __LINE__); \
    432 		SLJIT_HALT_PROCESS(); \
    433 	} while (0)
    434 
    435 #endif /* !SLJIT_ASSERT_STOP */
    436 
    437 #else /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
    438 
    439 #undef SLJIT_ASSERT
    440 #undef SLJIT_ASSERT_STOP
    441 
    442 #define SLJIT_ASSERT(x) \
    443 	do { } while (0)
    444 #define SLJIT_ASSERT_STOP() \
    445 	do { } while (0)
    446 
    447 #endif /* (defined SLJIT_DEBUG && SLJIT_DEBUG) */
    448 
    449 #ifndef SLJIT_COMPILE_ASSERT
    450 
    451 /* Should be improved eventually. */
    452 #define SLJIT_COMPILE_ASSERT(x, description) \
    453 	SLJIT_ASSERT(x)
    454 
    455 #endif /* !SLJIT_COMPILE_ASSERT */
    456 
    457 #endif
    458