1/* -----------------------------------------------------------------*-C-*- 2 libffi PyOBJC - Copyright (c) 1996-2003 Red Hat, Inc. 3 4 Permission is hereby granted, free of charge, to any person obtaining 5 a copy of this software and associated documentation files (the 6 ``Software''), to deal in the Software without restriction, including 7 without limitation the rights to use, copy, modify, merge, publish, 8 distribute, sublicense, and/or sell copies of the Software, and to 9 permit persons to whom the Software is furnished to do so, subject to 10 the following conditions: 11 12 The above copyright notice and this permission notice shall be included 13 in all copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 OTHER DEALINGS IN THE SOFTWARE. 22 23 ----------------------------------------------------------------------- */ 24 25/* ------------------------------------------------------------------- 26 The basic API is described in the README file. 27 28 The raw API is designed to bypass some of the argument packing 29 and unpacking on architectures for which it can be avoided. 30 31 The closure API allows interpreted functions to be packaged up 32 inside a C function pointer, so that they can be called as C functions, 33 with no understanding on the client side that they are interpreted. 34 It can also be used in other cases in which it is necessary to package 35 up a user specified parameter and a function pointer as a single 36 function pointer. 37 38 The closure API must be implemented in order to get its functionality, 39 e.g. for use by gij. Routines are provided to emulate the raw API 40 if the underlying platform doesn't allow faster implementation. 41 42 More details on the raw and closure API can be found in: 43 44 http://gcc.gnu.org/ml/java/1999-q3/msg00138.html 45 46 and 47 48 http://gcc.gnu.org/ml/java/1999-q3/msg00174.html 49 -------------------------------------------------------------------- */ 50 51#ifndef LIBFFI_H 52#define LIBFFI_H 53 54#ifdef __cplusplus 55extern "C" { 56#endif 57 58#include "fficonfig.h" 59 60/* Specify which architecture libffi is configured for. */ 61#ifdef MACOSX 62# if defined(__i386__) || defined(__x86_64__) 63# define X86_DARWIN 64# elif defined(__ppc__) || defined(__ppc64__) 65# define POWERPC_DARWIN 66# else 67# error "Unsupported MacOS X CPU type" 68# endif 69#else 70#error "Unsupported OS type" 71#endif 72 73/* ---- System configuration information --------------------------------- */ 74 75#include "ffitarget.h" 76 77#ifndef LIBFFI_ASM 78 79#include <stddef.h> 80#include <limits.h> 81 82/* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example). 83 But we can find it either under the correct ANSI name, or under GNU 84 C's internal name. */ 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# else 91# ifdef __GNUC__ 92# define FFI_LONG_LONG_MAX __LONG_LONG_MAX__ 93# endif 94# endif 95#endif 96 97#if SCHAR_MAX == 127 98# define ffi_type_uchar ffi_type_uint8 99# define ffi_type_schar ffi_type_sint8 100#else 101#error "char size not supported" 102#endif 103 104#if SHRT_MAX == 32767 105# define ffi_type_ushort ffi_type_uint16 106# define ffi_type_sshort ffi_type_sint16 107#elif SHRT_MAX == 2147483647 108# define ffi_type_ushort ffi_type_uint32 109# define ffi_type_sshort ffi_type_sint32 110#else 111#error "short size not supported" 112#endif 113 114#if INT_MAX == 32767 115# define ffi_type_uint ffi_type_uint16 116# define ffi_type_sint ffi_type_sint16 117#elif INT_MAX == 2147483647 118# define ffi_type_uint ffi_type_uint32 119# define ffi_type_sint ffi_type_sint32 120#elif INT_MAX == 9223372036854775807 121# define ffi_type_uint ffi_type_uint64 122# define ffi_type_sint ffi_type_sint64 123#else 124#error "int size not supported" 125#endif 126 127#define ffi_type_ulong ffi_type_uint64 128#define ffi_type_slong ffi_type_sint64 129 130#if LONG_MAX == 2147483647 131# if FFI_LONG_LONG_MAX != 9223372036854775807 132# error "no 64-bit data type supported" 133# endif 134#elif LONG_MAX != 9223372036854775807 135#error "long size not supported" 136#endif 137 138/* The closure code assumes that this works on pointers, i.e. a size_t 139 can hold a pointer. */ 140 141typedef struct _ffi_type { 142 size_t size; 143 unsigned short alignment; 144 unsigned short type; 145/*@null@*/ struct _ffi_type** elements; 146} ffi_type; 147 148/* These are defined in types.c */ 149extern ffi_type ffi_type_void; 150extern ffi_type ffi_type_uint8; 151extern ffi_type ffi_type_sint8; 152extern ffi_type ffi_type_uint16; 153extern ffi_type ffi_type_sint16; 154extern ffi_type ffi_type_uint32; 155extern ffi_type ffi_type_sint32; 156extern ffi_type ffi_type_uint64; 157extern ffi_type ffi_type_sint64; 158extern ffi_type ffi_type_float; 159extern ffi_type ffi_type_double; 160extern ffi_type ffi_type_longdouble; 161extern ffi_type ffi_type_pointer; 162 163typedef enum ffi_status { 164 FFI_OK = 0, 165 FFI_BAD_TYPEDEF, 166 FFI_BAD_ABI 167} ffi_status; 168 169typedef unsigned FFI_TYPE; 170 171typedef struct ffi_cif { 172 ffi_abi abi; 173 unsigned nargs; 174/*@dependent@*/ ffi_type** arg_types; 175/*@dependent@*/ ffi_type* rtype; 176 unsigned bytes; 177 unsigned flags; 178#ifdef FFI_EXTRA_CIF_FIELDS 179 FFI_EXTRA_CIF_FIELDS; 180#endif 181} ffi_cif; 182 183/* ---- Definitions for the raw API -------------------------------------- */ 184 185#ifndef FFI_SIZEOF_ARG 186# if LONG_MAX == 2147483647 187# define FFI_SIZEOF_ARG 4 188# elif LONG_MAX == 9223372036854775807 189# define FFI_SIZEOF_ARG 8 190# endif 191#endif 192 193typedef union { 194 ffi_sarg sint; 195 ffi_arg uint; 196 float flt; 197 char data[FFI_SIZEOF_ARG]; 198 void* ptr; 199} ffi_raw; 200 201void 202ffi_raw_call( 203/*@dependent@*/ ffi_cif* cif, 204 void (*fn)(void), 205/*@out@*/ void* rvalue, 206/*@dependent@*/ ffi_raw* avalue); 207 208void 209ffi_ptrarray_to_raw( 210 ffi_cif* cif, 211 void** args, 212 ffi_raw* raw); 213 214void 215ffi_raw_to_ptrarray( 216 ffi_cif* cif, 217 ffi_raw* raw, 218 void** args); 219 220size_t 221ffi_raw_size( 222 ffi_cif* cif); 223 224/* This is analogous to the raw API, except it uses Java parameter 225 packing, even on 64-bit machines. I.e. on 64-bit machines 226 longs and doubles are followed by an empty 64-bit word. */ 227void 228ffi_java_raw_call( 229/*@dependent@*/ ffi_cif* cif, 230 void (*fn)(void), 231/*@out@*/ void* rvalue, 232/*@dependent@*/ ffi_raw* avalue); 233 234void 235ffi_java_ptrarray_to_raw( 236 ffi_cif* cif, 237 void** args, 238 ffi_raw* raw); 239 240void 241ffi_java_raw_to_ptrarray( 242 ffi_cif* cif, 243 ffi_raw* raw, 244 void** args); 245 246size_t 247ffi_java_raw_size( 248 ffi_cif* cif); 249 250/* ---- Definitions for closures ----------------------------------------- */ 251 252#if FFI_CLOSURES 253 254typedef struct ffi_closure { 255 char tramp[FFI_TRAMPOLINE_SIZE]; 256 ffi_cif* cif; 257 void (*fun)(ffi_cif*,void*,void**,void*); 258 void* user_data; 259} ffi_closure; 260 261ffi_status 262ffi_prep_closure( 263 ffi_closure* closure, 264 ffi_cif* cif, 265 void (*fun)(ffi_cif*,void*,void**,void*), 266 void* user_data); 267 268typedef struct ffi_raw_closure { 269 char tramp[FFI_TRAMPOLINE_SIZE]; 270 ffi_cif* cif; 271 272#if !FFI_NATIVE_RAW_API 273 /* if this is enabled, then a raw closure has the same layout 274 as a regular closure. We use this to install an intermediate 275 handler to do the transaltion, void** -> ffi_raw*. */ 276 void (*translate_args)(ffi_cif*,void*,void**,void*); 277 void* this_closure; 278#endif 279 280 void (*fun)(ffi_cif*,void*,ffi_raw*,void*); 281 void* user_data; 282} ffi_raw_closure; 283 284ffi_status 285ffi_prep_raw_closure( 286 ffi_raw_closure* closure, 287 ffi_cif* cif, 288 void (*fun)(ffi_cif*,void*,ffi_raw*,void*), 289 void* user_data); 290 291ffi_status 292ffi_prep_java_raw_closure( 293 ffi_raw_closure* closure, 294 ffi_cif* cif, 295 void (*fun)(ffi_cif*,void*,ffi_raw*,void*), 296 void* user_data); 297 298#elif 299#error FFI_CLOSURES not defined 300#endif 301 302/* ---- Public interface definition -------------------------------------- */ 303 304ffi_status 305ffi_prep_cif( 306/*@out@*/ /*@partial@*/ ffi_cif* cif, 307 ffi_abi abi, 308 unsigned int nargs, 309/*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type* rtype, 310/*@dependent@*/ ffi_type** atypes); 311 312void 313ffi_call( 314/*@dependent@*/ ffi_cif* cif, 315 void (*fn)(void), 316/*@out@*/ void* rvalue, 317/*@dependent@*/ void** avalue); 318 319/* Useful for eliminating compiler warnings */ 320#define FFI_FN(f) ((void (*)(void))f) 321 322#endif // #ifndef LIBFFI_ASM 323/* ---- Definitions shared with assembly code ---------------------------- */ 324 325/* If these change, update src/mips/ffitarget.h. */ 326#define FFI_TYPE_VOID 0 327#define FFI_TYPE_INT 1 328#define FFI_TYPE_FLOAT 2 329#define FFI_TYPE_DOUBLE 3 330 331#ifdef HAVE_LONG_DOUBLE 332# define FFI_TYPE_LONGDOUBLE 4 333#else 334# define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE 335#endif 336 337#define FFI_TYPE_UINT8 5 338#define FFI_TYPE_SINT8 6 339#define FFI_TYPE_UINT16 7 340#define FFI_TYPE_SINT16 8 341#define FFI_TYPE_UINT32 9 342#define FFI_TYPE_SINT32 10 343#define FFI_TYPE_UINT64 11 344#define FFI_TYPE_SINT64 12 345#define FFI_TYPE_STRUCT 13 346#define FFI_TYPE_POINTER 14 347 348/* This should always refer to the last type code (for sanity checks) */ 349#define FFI_TYPE_LAST FFI_TYPE_POINTER 350 351#ifdef __cplusplus 352} 353#endif 354 355#endif // #ifndef LIBFFI_H