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