| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi_common.h - Copyright (C) 2011, 2012, 2013 Anthony Green |
| 3 |
Copyright (C) 2007 Free Software Foundation, Inc |
| 4 |
Copyright (c) 1996 Red Hat, Inc. |
| 5 |
|
| 6 |
Common internal definitions and macros. Only necessary for building |
| 7 |
libffi. |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person |
| 10 |
obtaining a copy of this software and associated documentation |
| 11 |
files (the ``Software''), to deal in the Software without |
| 12 |
restriction, including without limitation the rights to use, copy, |
| 13 |
modify, merge, publish, distribute, sublicense, and/or sell copies |
| 14 |
of the Software, and to permit persons to whom the Software is |
| 15 |
furnished to do so, subject to the following conditions: |
| 16 |
|
| 17 |
The above copyright notice and this permission notice shall be |
| 18 |
included in all copies or substantial portions of the Software. |
| 19 |
|
| 20 |
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
| 21 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 27 |
DEALINGS IN THE SOFTWARE. |
| 28 |
|
| 29 |
----------------------------------------------------------------------- */ |
| 30 |
|
| 31 |
#ifndef FFI_COMMON_H |
| 32 |
#define FFI_COMMON_H |
| 33 |
|
| 34 |
#ifdef __cplusplus |
| 35 |
extern "C" { |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <fficonfig.h> |
| 39 |
|
| 40 |
/* Do not move this. Some versions of AIX are very picky about where |
| 41 |
this is positioned. */ |
| 42 |
#ifdef __GNUC__ |
| 43 |
# if HAVE_ALLOCA_H |
| 44 |
# include <alloca.h> |
| 45 |
# else |
| 46 |
/* mingw64 defines this already in malloc.h. */ |
| 47 |
# ifndef alloca |
| 48 |
# define alloca __builtin_alloca |
| 49 |
# endif |
| 50 |
# endif |
| 51 |
# define MAYBE_UNUSED __attribute__((__unused__)) |
| 52 |
# define NORETURN __attribute__((__noreturn__)) |
| 53 |
#else |
| 54 |
# define MAYBE_UNUSED |
| 55 |
# define NORETURN |
| 56 |
# if HAVE_ALLOCA_H |
| 57 |
# include <alloca.h> |
| 58 |
# else |
| 59 |
# ifdef _AIX |
| 60 |
# pragma alloca |
| 61 |
# else |
| 62 |
# ifndef alloca /* predefined by HP cc +Olibcalls */ |
| 63 |
# ifdef _MSC_VER |
| 64 |
# define alloca _alloca |
| 65 |
# else |
| 66 |
char *alloca (); |
| 67 |
# endif |
| 68 |
# endif |
| 69 |
# endif |
| 70 |
# endif |
| 71 |
#endif |
| 72 |
|
| 73 |
/* Check for the existence of memcpy. */ |
| 74 |
#if STDC_HEADERS |
| 75 |
# include <string.h> |
| 76 |
#else |
| 77 |
# ifndef HAVE_MEMCPY |
| 78 |
# define memcpy(d, s, n) bcopy ((s), (d), (n)) |
| 79 |
# endif |
| 80 |
#endif |
| 81 |
|
| 82 |
#if defined(FFI_DEBUG) |
| 83 |
#include <stdio.h> |
| 84 |
#endif |
| 85 |
|
| 86 |
#ifndef __SANITIZE_ADDRESS__ |
| 87 |
# ifdef __clang__ |
| 88 |
# if __has_feature(address_sanitizer) |
| 89 |
# define FFI_ASAN |
| 90 |
# endif |
| 91 |
# endif |
| 92 |
#endif |
| 93 |
#ifdef __SANITIZE_ADDRESS__ |
| 94 |
#define FFI_ASAN |
| 95 |
#endif |
| 96 |
|
| 97 |
#ifdef FFI_ASAN |
| 98 |
#define FFI_ASAN_NO_SANITIZE __attribute__((no_sanitize_address)) |
| 99 |
#else |
| 100 |
#define FFI_ASAN_NO_SANITIZE |
| 101 |
#endif |
| 102 |
|
| 103 |
#ifdef FFI_DEBUG |
| 104 |
NORETURN void ffi_assert(const char *expr, const char *file, int line); |
| 105 |
void ffi_stop_here(void); |
| 106 |
void ffi_type_test(ffi_type *a, const char *file, int line); |
| 107 |
|
| 108 |
#define FFI_ASSERT(x) ((x) ? (void)0 : ffi_assert(#x, __FILE__,__LINE__)) |
| 109 |
#define FFI_ASSERT_AT(x, f, l) ((x) ? 0 : ffi_assert(#x, (f), (l))) |
| 110 |
#define FFI_ASSERT_VALID_TYPE(x) ffi_type_test (x, __FILE__, __LINE__) |
| 111 |
#else |
| 112 |
#define FFI_ASSERT(x) |
| 113 |
#define FFI_ASSERT_AT(x, f, l) |
| 114 |
#define FFI_ASSERT_VALID_TYPE(x) |
| 115 |
#endif |
| 116 |
|
| 117 |
/* v cast to size_t and aligned up to a multiple of a */ |
| 118 |
#define FFI_ALIGN(v, a) (((((size_t) (v))-1) | ((a)-1))+1) |
| 119 |
/* v cast to size_t and aligned down to a multiple of a */ |
| 120 |
#define FFI_ALIGN_DOWN(v, a) (((size_t) (v)) & -a) |
| 121 |
|
| 122 |
/* Perform machine dependent cif processing */ |
| 123 |
ffi_status ffi_prep_cif_machdep(ffi_cif *cif); |
| 124 |
ffi_status ffi_prep_cif_machdep_var(ffi_cif *cif, |
| 125 |
unsigned int nfixedargs, unsigned int ntotalargs); |
| 126 |
|
| 127 |
|
| 128 |
#if HAVE_LONG_DOUBLE_VARIANT |
| 129 |
/* Used to adjust size/alignment of ffi types. */ |
| 130 |
void ffi_prep_types (ffi_abi abi); |
| 131 |
#endif |
| 132 |
|
| 133 |
/* Used internally, but overridden by some architectures */ |
| 134 |
ffi_status ffi_prep_cif_core(ffi_cif *cif, |
| 135 |
ffi_abi abi, |
| 136 |
unsigned int isvariadic, |
| 137 |
unsigned int nfixedargs, |
| 138 |
unsigned int ntotalargs, |
| 139 |
ffi_type *rtype, |
| 140 |
ffi_type **atypes); |
| 141 |
|
| 142 |
/* Translate a data pointer to a code pointer. Needed for closures on |
| 143 |
some targets. */ |
| 144 |
void *ffi_data_to_code_pointer (void *data) FFI_HIDDEN; |
| 145 |
|
| 146 |
/* The arch code calls this to determine if a given closure has a |
| 147 |
static trampoline. */ |
| 148 |
int ffi_tramp_is_present (void *closure) FFI_HIDDEN; |
| 149 |
|
| 150 |
/* Return a file descriptor of a temporary zero-sized file in a |
| 151 |
writable and executable filesystem. */ |
| 152 |
int open_temp_exec_file(void) FFI_HIDDEN; |
| 153 |
|
| 154 |
/* Extended cif, used in callback from assembly routine */ |
| 155 |
typedef struct |
| 156 |
{ |
| 157 |
ffi_cif *cif; |
| 158 |
void *rvalue; |
| 159 |
void **avalue; |
| 160 |
} extended_cif; |
| 161 |
|
| 162 |
/* Terse sized type definitions. */ |
| 163 |
#if defined(_MSC_VER) || defined(__sgi) || defined(__SUNPRO_C) |
| 164 |
typedef unsigned char UINT8; |
| 165 |
typedef signed char SINT8; |
| 166 |
typedef unsigned short UINT16; |
| 167 |
typedef signed short SINT16; |
| 168 |
typedef unsigned int UINT32; |
| 169 |
typedef signed int SINT32; |
| 170 |
# ifdef _MSC_VER |
| 171 |
typedef unsigned __int64 UINT64; |
| 172 |
typedef signed __int64 SINT64; |
| 173 |
# else |
| 174 |
# include <inttypes.h> |
| 175 |
typedef uint64_t UINT64; |
| 176 |
typedef int64_t SINT64; |
| 177 |
# endif |
| 178 |
#else |
| 179 |
typedef unsigned int UINT8 __attribute__((__mode__(__QI__))); |
| 180 |
typedef signed int SINT8 __attribute__((__mode__(__QI__))); |
| 181 |
typedef unsigned int UINT16 __attribute__((__mode__(__HI__))); |
| 182 |
typedef signed int SINT16 __attribute__((__mode__(__HI__))); |
| 183 |
typedef unsigned int UINT32 __attribute__((__mode__(__SI__))); |
| 184 |
typedef signed int SINT32 __attribute__((__mode__(__SI__))); |
| 185 |
typedef unsigned int UINT64 __attribute__((__mode__(__DI__))); |
| 186 |
typedef signed int SINT64 __attribute__((__mode__(__DI__))); |
| 187 |
#endif |
| 188 |
|
| 189 |
typedef float FLOAT32; |
| 190 |
|
| 191 |
#ifndef __GNUC__ |
| 192 |
#define __builtin_expect(x, expected_value) (x) |
| 193 |
#endif |
| 194 |
#define LIKELY(x) __builtin_expect(!!(x),1) |
| 195 |
#define UNLIKELY(x) __builtin_expect((x)!=0,0) |
| 196 |
|
| 197 |
#ifdef __cplusplus |
| 198 |
} |
| 199 |
#endif |
| 200 |
|
| 201 |
#endif |