Home | History | Annotate | Download | only in libffi
      1 /* ffi.h. Auto generated from ffi.h.in by configure. */
      2 /* -----------------------------------------------------------------*-C-*-
      3    libffi 3.1 - Copyright (c) 2011 Anthony Green
      4                     - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc.
      5 
      6    Permission is hereby granted, free of charge, to any person
      7    obtaining a copy of this software and associated documentation
      8    files (the ``Software''), to deal in the Software without
      9    restriction, including without limitation the rights to use, copy,
     10    modify, merge, publish, distribute, sublicense, and/or sell copies
     11    of the Software, and to permit persons to whom the Software is
     12    furnished to do so, subject to the following conditions:
     13 
     14    The above copyright notice and this permission notice shall be
     15    included in all copies or substantial portions of the Software.
     16 
     17    THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
     18    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     19    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     20    NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     21    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     22    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     23    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     24    DEALINGS IN THE SOFTWARE.
     25 
     26    ----------------------------------------------------------------------- */
     27 
     28 /* -------------------------------------------------------------------
     29    The basic API is described in the README file.
     30 
     31    The raw API is designed to bypass some of the argument packing
     32    and unpacking on architectures for which it can be avoided.
     33 
     34    The closure API allows interpreted functions to be packaged up
     35    inside a C function pointer, so that they can be called as C functions,
     36    with no understanding on the client side that they are interpreted.
     37    It can also be used in other cases in which it is necessary to package
     38    up a user specified parameter and a function pointer as a single
     39    function pointer.
     40 
     41    The closure API must be implemented in order to get its functionality,
     42    e.g. for use by gij.  Routines are provided to emulate the raw API
     43    if the underlying platform doesn't allow faster implementation.
     44 
     45    More details on the raw and cloure API can be found in:
     46 
     47    http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
     48 
     49    and
     50 
     51    http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
     52    -------------------------------------------------------------------- */
     53 
     54 #ifndef LIBFFI_H
     55 #define LIBFFI_H
     56 
     57 #ifdef __cplusplus
     58 extern "C" {
     59 #endif
     60 
     61 /* Specify which architecture libffi is configured for. */
     62 #ifndef X86_64
     63 #define X86_64
     64 #endif
     65 
     66 /* ---- System configuration information --------------------------------- */
     67 
     68 #include <ffitarget.h>
     69 
     70 #ifndef LIBFFI_ASM
     71 
     72 #ifdef _MSC_VER
     73 #define __attribute__(X)
     74 #endif
     75 
     76 #include <stddef.h>
     77 #include <limits.h>
     78 
     79 /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
     80    But we can find it either under the correct ANSI name, or under GNU
     81    C's internal name.  */
     82 
     83 #define FFI_64_BIT_MAX 9223372036854775807
     84 
     85 #ifdef LONG_LONG_MAX
     86 # define FFI_LONG_LONG_MAX LONG_LONG_MAX
     87 #else
     88 # ifdef LLONG_MAX
     89 #  define FFI_LONG_LONG_MAX LLONG_MAX
     90 #  ifdef _AIX52 /* or newer has C99 LLONG_MAX */
     91 #   undef FFI_64_BIT_MAX
     92 #   define FFI_64_BIT_MAX 9223372036854775807LL
     93 #  endif /* _AIX52 or newer */
     94 # else
     95 #  ifdef __GNUC__
     96 #   define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
     97 #  endif
     98 #  ifdef _AIX /* AIX 5.1 and earlier have LONGLONG_MAX */
     99 #   ifndef __PPC64__
    100 #    if defined (__IBMC__) || defined (__IBMCPP__)
    101 #     define FFI_LONG_LONG_MAX LONGLONG_MAX
    102 #    endif
    103 #   endif /* __PPC64__ */
    104 #   undef  FFI_64_BIT_MAX
    105 #   define FFI_64_BIT_MAX 9223372036854775807LL
    106 #  endif
    107 # endif
    108 #endif
    109 
    110 /* The closure code assumes that this works on pointers, i.e. a size_t	*/
    111 /* can hold a pointer.							*/
    112 
    113 typedef struct _ffi_type
    114 {
    115   size_t size;
    116   unsigned short alignment;
    117   unsigned short type;
    118   struct _ffi_type **elements;
    119 } ffi_type;
    120 
    121 #ifndef LIBFFI_HIDE_BASIC_TYPES
    122 #if SCHAR_MAX == 127
    123 # define ffi_type_uchar                ffi_type_uint8
    124 # define ffi_type_schar                ffi_type_sint8
    125 #else
    126  #error "char size not supported"
    127 #endif
    128 
    129 #if SHRT_MAX == 32767
    130 # define ffi_type_ushort       ffi_type_uint16
    131 # define ffi_type_sshort       ffi_type_sint16
    132 #elif SHRT_MAX == 2147483647
    133 # define ffi_type_ushort       ffi_type_uint32
    134 # define ffi_type_sshort       ffi_type_sint32
    135 #else
    136  #error "short size not supported"
    137 #endif
    138 
    139 #if INT_MAX == 32767
    140 # define ffi_type_uint         ffi_type_uint16
    141 # define ffi_type_sint         ffi_type_sint16
    142 #elif INT_MAX == 2147483647
    143 # define ffi_type_uint         ffi_type_uint32
    144 # define ffi_type_sint         ffi_type_sint32
    145 #elif INT_MAX == 9223372036854775807
    146 # define ffi_type_uint         ffi_type_uint64
    147 # define ffi_type_sint         ffi_type_sint64
    148 #else
    149  #error "int size not supported"
    150 #endif
    151 
    152 #if LONG_MAX == 2147483647
    153 # if FFI_LONG_LONG_MAX != FFI_64_BIT_MAX
    154  #error "no 64-bit data type supported"
    155 # endif
    156 #elif LONG_MAX != FFI_64_BIT_MAX
    157  #error "long size not supported"
    158 #endif
    159 
    160 #if LONG_MAX == 2147483647
    161 # define ffi_type_ulong        ffi_type_uint32
    162 # define ffi_type_slong        ffi_type_sint32
    163 #elif LONG_MAX == FFI_64_BIT_MAX
    164 # define ffi_type_ulong        ffi_type_uint64
    165 # define ffi_type_slong        ffi_type_sint64
    166 #else
    167  #error "long size not supported"
    168 #endif
    169 
    170 /* Need minimal decorations for DLLs to works on Windows. */
    171 /* GCC has autoimport and autoexport.  Rely on Libtool to */
    172 /* help MSVC export from a DLL, but always declare data   */
    173 /* to be imported for MSVC clients.  This costs an extra  */
    174 /* indirection for MSVC clients using the static version  */
    175 /* of the library, but don't worry about that.  Besides,  */
    176 /* as a workaround, they can define FFI_BUILDING if they  */
    177 /* *know* they are going to link with the static library. */
    178 #if defined _MSC_VER && !defined FFI_BUILDING
    179 #define FFI_EXTERN extern __declspec(dllimport)
    180 #else
    181 #define FFI_EXTERN extern
    182 #endif
    183 
    184 /* These are defined in types.c */
    185 FFI_EXTERN ffi_type ffi_type_void;
    186 FFI_EXTERN ffi_type ffi_type_uint8;
    187 FFI_EXTERN ffi_type ffi_type_sint8;
    188 FFI_EXTERN ffi_type ffi_type_uint16;
    189 FFI_EXTERN ffi_type ffi_type_sint16;
    190 FFI_EXTERN ffi_type ffi_type_uint32;
    191 FFI_EXTERN ffi_type ffi_type_sint32;
    192 FFI_EXTERN ffi_type ffi_type_uint64;
    193 FFI_EXTERN ffi_type ffi_type_sint64;
    194 FFI_EXTERN ffi_type ffi_type_float;
    195 FFI_EXTERN ffi_type ffi_type_double;
    196 FFI_EXTERN ffi_type ffi_type_pointer;
    197 
    198 #if 1
    199 FFI_EXTERN ffi_type ffi_type_longdouble;
    200 #else
    201 #define ffi_type_longdouble ffi_type_double
    202 #endif
    203 #endif /* LIBFFI_HIDE_BASIC_TYPES */
    204 
    205 typedef enum {
    206   FFI_OK = 0,
    207   FFI_BAD_TYPEDEF,
    208   FFI_BAD_ABI
    209 } ffi_status;
    210 
    211 typedef unsigned FFI_TYPE;
    212 
    213 typedef struct {
    214   ffi_abi abi;
    215   unsigned nargs;
    216   ffi_type **arg_types;
    217   ffi_type *rtype;
    218   unsigned bytes;
    219   unsigned flags;
    220 #ifdef FFI_EXTRA_CIF_FIELDS
    221   FFI_EXTRA_CIF_FIELDS;
    222 #endif
    223 } ffi_cif;
    224 
    225 #if HAVE_LONG_DOUBLE_VARIANT
    226 /* Used to adjust size/alignment of ffi types.  */
    227 void ffi_prep_types (ffi_abi abi);
    228 # endif
    229 
    230 /* Used internally, but overridden by some architectures */
    231 ffi_status ffi_prep_cif_core(ffi_cif *cif,
    232 			     ffi_abi abi,
    233 			     unsigned int isvariadic,
    234 			     unsigned int nfixedargs,
    235 			     unsigned int ntotalargs,
    236 			     ffi_type *rtype,
    237 			     ffi_type **atypes);
    238 
    239 /* ---- Definitions for the raw API -------------------------------------- */
    240 
    241 #ifndef FFI_SIZEOF_ARG
    242 # if LONG_MAX == 2147483647
    243 #  define FFI_SIZEOF_ARG        4
    244 # elif LONG_MAX == FFI_64_BIT_MAX
    245 #  define FFI_SIZEOF_ARG        8
    246 # endif
    247 #endif
    248 
    249 #ifndef FFI_SIZEOF_JAVA_RAW
    250 #  define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
    251 #endif
    252 
    253 typedef union {
    254   ffi_sarg  sint;
    255   ffi_arg   uint;
    256   float	    flt;
    257   char      data[FFI_SIZEOF_ARG];
    258   void*     ptr;
    259 } ffi_raw;
    260 
    261 #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
    262 /* This is a special case for mips64/n32 ABI (and perhaps others) where
    263    sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8.  */
    264 typedef union {
    265   signed int	sint;
    266   unsigned int	uint;
    267   float		flt;
    268   char		data[FFI_SIZEOF_JAVA_RAW];
    269   void*		ptr;
    270 } ffi_java_raw;
    271 #else
    272 typedef ffi_raw ffi_java_raw;
    273 #endif
    274 
    275 
    276 void ffi_raw_call (ffi_cif *cif,
    277 		   void (*fn)(void),
    278 		   void *rvalue,
    279 		   ffi_raw *avalue);
    280 
    281 void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
    282 void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
    283 size_t ffi_raw_size (ffi_cif *cif);
    284 
    285 /* This is analogous to the raw API, except it uses Java parameter	*/
    286 /* packing, even on 64-bit machines.  I.e. on 64-bit machines		*/
    287 /* longs and doubles are followed by an empty 64-bit word.		*/
    288 
    289 void ffi_java_raw_call (ffi_cif *cif,
    290 			void (*fn)(void),
    291 			void *rvalue,
    292 			ffi_java_raw *avalue);
    293 
    294 void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw);
    295 void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args);
    296 size_t ffi_java_raw_size (ffi_cif *cif);
    297 
    298 /* ---- Definitions for closures ----------------------------------------- */
    299 
    300 #if FFI_CLOSURES
    301 
    302 #ifdef _MSC_VER
    303 __declspec(align(8))
    304 #endif
    305 typedef struct {
    306 #if 0
    307   void *trampoline_table;
    308   void *trampoline_table_entry;
    309 #else
    310   char tramp[FFI_TRAMPOLINE_SIZE];
    311 #endif
    312   ffi_cif   *cif;
    313   void     (*fun)(ffi_cif*,void*,void**,void*);
    314   void      *user_data;
    315 #ifdef __GNUC__
    316 } ffi_closure __attribute__((aligned (8)));
    317 #else
    318 } ffi_closure;
    319 # ifdef __sgi
    320 #  pragma pack 0
    321 # endif
    322 #endif
    323 
    324 void *ffi_closure_alloc (size_t size, void **code);
    325 void ffi_closure_free (void *);
    326 
    327 ffi_status
    328 ffi_prep_closure (ffi_closure*,
    329 		  ffi_cif *,
    330 		  void (*fun)(ffi_cif*,void*,void**,void*),
    331 		  void *user_data);
    332 
    333 ffi_status
    334 ffi_prep_closure_loc (ffi_closure*,
    335 		      ffi_cif *,
    336 		      void (*fun)(ffi_cif*,void*,void**,void*),
    337 		      void *user_data,
    338 		      void*codeloc);
    339 
    340 #ifdef __sgi
    341 # pragma pack 8
    342 #endif
    343 typedef struct {
    344 #if 0
    345   void *trampoline_table;
    346   void *trampoline_table_entry;
    347 #else
    348   char tramp[FFI_TRAMPOLINE_SIZE];
    349 #endif
    350   ffi_cif   *cif;
    351 
    352 #if !FFI_NATIVE_RAW_API
    353 
    354   /* if this is enabled, then a raw closure has the same layout
    355      as a regular closure.  We use this to install an intermediate
    356      handler to do the transaltion, void** -> ffi_raw*. */
    357 
    358   void     (*translate_args)(ffi_cif*,void*,void**,void*);
    359   void      *this_closure;
    360 
    361 #endif
    362 
    363   void     (*fun)(ffi_cif*,void*,ffi_raw*,void*);
    364   void      *user_data;
    365 
    366 } ffi_raw_closure;
    367 
    368 typedef struct {
    369 #if 0
    370   void *trampoline_table;
    371   void *trampoline_table_entry;
    372 #else
    373   char tramp[FFI_TRAMPOLINE_SIZE];
    374 #endif
    375 
    376   ffi_cif   *cif;
    377 
    378 #if !FFI_NATIVE_RAW_API
    379 
    380   /* if this is enabled, then a raw closure has the same layout
    381      as a regular closure.  We use this to install an intermediate
    382      handler to do the transaltion, void** -> ffi_raw*. */
    383 
    384   void     (*translate_args)(ffi_cif*,void*,void**,void*);
    385   void      *this_closure;
    386 
    387 #endif
    388 
    389   void     (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
    390   void      *user_data;
    391 
    392 } ffi_java_raw_closure;
    393 
    394 ffi_status
    395 ffi_prep_raw_closure (ffi_raw_closure*,
    396 		      ffi_cif *cif,
    397 		      void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
    398 		      void *user_data);
    399 
    400 ffi_status
    401 ffi_prep_raw_closure_loc (ffi_raw_closure*,
    402 			  ffi_cif *cif,
    403 			  void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
    404 			  void *user_data,
    405 			  void *codeloc);
    406 
    407 ffi_status
    408 ffi_prep_java_raw_closure (ffi_java_raw_closure*,
    409 		           ffi_cif *cif,
    410 		           void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
    411 		           void *user_data);
    412 
    413 ffi_status
    414 ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
    415 			       ffi_cif *cif,
    416 			       void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
    417 			       void *user_data,
    418 			       void *codeloc);
    419 
    420 #endif /* FFI_CLOSURES */
    421 
    422 /* ---- Public interface definition -------------------------------------- */
    423 
    424 ffi_status ffi_prep_cif(ffi_cif *cif,
    425 			ffi_abi abi,
    426 			unsigned int nargs,
    427 			ffi_type *rtype,
    428 			ffi_type **atypes);
    429 
    430 ffi_status ffi_prep_cif_var(ffi_cif *cif,
    431 			    ffi_abi abi,
    432 			    unsigned int nfixedargs,
    433 			    unsigned int ntotalargs,
    434 			    ffi_type *rtype,
    435 			    ffi_type **atypes);
    436 
    437 void ffi_call(ffi_cif *cif,
    438 	      void (*fn)(void),
    439 	      void *rvalue,
    440 	      void **avalue);
    441 
    442 /* Useful for eliminating compiler warnings */
    443 #define FFI_FN(f) ((void (*)(void))f)
    444 
    445 /* ---- Definitions shared with assembly code ---------------------------- */
    446 
    447 #endif
    448 
    449 /* If these change, update src/mips/ffitarget.h. */
    450 #define FFI_TYPE_VOID       0
    451 #define FFI_TYPE_INT        1
    452 #define FFI_TYPE_FLOAT      2
    453 #define FFI_TYPE_DOUBLE     3
    454 #if 1
    455 #define FFI_TYPE_LONGDOUBLE 4
    456 #else
    457 #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
    458 #endif
    459 #define FFI_TYPE_UINT8      5
    460 #define FFI_TYPE_SINT8      6
    461 #define FFI_TYPE_UINT16     7
    462 #define FFI_TYPE_SINT16     8
    463 #define FFI_TYPE_UINT32     9
    464 #define FFI_TYPE_SINT32     10
    465 #define FFI_TYPE_UINT64     11
    466 #define FFI_TYPE_SINT64     12
    467 #define FFI_TYPE_STRUCT     13
    468 #define FFI_TYPE_POINTER    14
    469 
    470 /* This should always refer to the last type code (for sanity checks) */
    471 #define FFI_TYPE_LAST       FFI_TYPE_POINTER
    472 
    473 #ifdef __cplusplus
    474 }
    475 #endif
    476 
    477 #endif
    478