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