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 cloure 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/* Specify which architecture libffi is configured for. */ 59#ifdef MACOSX 60 61#ifdef __i386__ 62# define X86_DARWIN 63#elif defined(__ppc__) 64# define POWERPC_DARWIN 65#else 66# error "Unsupported MacOS X CPU type" 67#endif 68 69#else 70 71#error "Unsupported OS type" 72 73#endif 74 75/* ---- System configuration information --------------------------------- */ 76 77#include <ffitarget.h> 78#include "fficonfig.h" 79 80#ifndef LIBFFI_ASM 81 82#include <stddef.h> 83#include <limits.h> 84 85/* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example). 86 But we can find it either under the correct ANSI name, or under GNU 87 C's internal name. */ 88#ifdef LONG_LONG_MAX 89# define FFI_LONG_LONG_MAX LONG_LONG_MAX 90#else 91# ifdef LLONG_MAX 92# define FFI_LONG_LONG_MAX LLONG_MAX 93# else 94# ifdef __GNUC__ 95# define FFI_LONG_LONG_MAX __LONG_LONG_MAX__ 96# endif 97# endif 98#endif 99 100#if SCHAR_MAX == 127 101# define ffi_type_uchar ffi_type_uint8 102# define ffi_type_schar ffi_type_sint8 103#else 104 #error "char size not supported" 105#endif 106 107#if SHRT_MAX == 32767 108# define ffi_type_ushort ffi_type_uint16 109# define ffi_type_sshort ffi_type_sint16 110#elif SHRT_MAX == 2147483647 111# define ffi_type_ushort ffi_type_uint32 112# define ffi_type_sshort ffi_type_sint32 113#else 114 #error "short size not supported" 115#endif 116 117#if INT_MAX == 32767 118# define ffi_type_uint ffi_type_uint16 119# define ffi_type_sint ffi_type_sint16 120#elif INT_MAX == 2147483647 121# define ffi_type_uint ffi_type_uint32 122# define ffi_type_sint ffi_type_sint32 123#elif INT_MAX == 9223372036854775807 124# define ffi_type_uint ffi_type_uint64 125# define ffi_type_sint ffi_type_sint64 126#else 127 #error "int size not supported" 128#endif 129 130#define ffi_type_ulong ffi_type_uint64 131#define ffi_type_slong ffi_type_sint64 132#if LONG_MAX == 2147483647 133# if FFI_LONG_LONG_MAX != 9223372036854775807 134 #error "no 64-bit data type supported" 135# endif 136#elif LONG_MAX != 9223372036854775807 137 #error "long size not supported" 138#endif 139 140/* The closure code assumes that this works on pointers, i.e. a size_t */ 141/* can hold a pointer. */ 142 143typedef struct _ffi_type 144{ 145 size_t size; 146 unsigned short alignment; 147 unsigned short type; 148 /*@null@*/ struct _ffi_type **elements; 149} ffi_type; 150 151/* These are defined in types.c */ 152extern ffi_type ffi_type_void; 153extern ffi_type ffi_type_uint8; 154extern ffi_type ffi_type_sint8; 155extern ffi_type ffi_type_uint16; 156extern ffi_type ffi_type_sint16; 157extern ffi_type ffi_type_uint32; 158extern ffi_type ffi_type_sint32; 159extern ffi_type ffi_type_uint64; 160extern ffi_type ffi_type_sint64; 161extern ffi_type ffi_type_float; 162extern ffi_type ffi_type_double; 163extern ffi_type ffi_type_longdouble; 164extern ffi_type ffi_type_pointer; 165 166 167typedef enum { 168 FFI_OK = 0, 169 FFI_BAD_TYPEDEF, 170 FFI_BAD_ABI 171} ffi_status; 172 173typedef unsigned FFI_TYPE; 174 175typedef struct { 176 ffi_abi abi; 177 unsigned nargs; 178 /*@dependent@*/ ffi_type **arg_types; 179 /*@dependent@*/ ffi_type *rtype; 180 unsigned bytes; 181 unsigned flags; 182#ifdef FFI_EXTRA_CIF_FIELDS 183 FFI_EXTRA_CIF_FIELDS; 184#endif 185} ffi_cif; 186 187/* ---- Definitions for the raw API -------------------------------------- */ 188 189#ifndef FFI_SIZEOF_ARG 190# if LONG_MAX == 2147483647 191# define FFI_SIZEOF_ARG 4 192# elif LONG_MAX == 9223372036854775807 193# define FFI_SIZEOF_ARG 8 194# endif 195#endif 196 197typedef union { 198 ffi_sarg sint; 199 ffi_arg uint; 200 float flt; 201 char data[FFI_SIZEOF_ARG]; 202 void* ptr; 203} ffi_raw; 204 205void ffi_raw_call (/*@dependent@*/ ffi_cif *cif, 206 void (*fn)(void), 207 /*@out@*/ void *rvalue, 208 /*@dependent@*/ ffi_raw *avalue); 209 210void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw); 211void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args); 212size_t ffi_raw_size (ffi_cif *cif); 213 214/* This is analogous to the raw API, except it uses Java parameter */ 215/* packing, even on 64-bit machines. I.e. on 64-bit machines */ 216/* longs and doubles are followed by an empty 64-bit word. */ 217 218void ffi_java_raw_call (/*@dependent@*/ ffi_cif *cif, 219 void (*fn)(void), 220 /*@out@*/ void *rvalue, 221 /*@dependent@*/ ffi_raw *avalue); 222 223void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw); 224void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args); 225size_t ffi_java_raw_size (ffi_cif *cif); 226 227/* ---- Definitions for closures ----------------------------------------- */ 228 229#if FFI_CLOSURES 230 231typedef struct { 232 char tramp[FFI_TRAMPOLINE_SIZE]; 233 ffi_cif *cif; 234 void (*fun)(ffi_cif*,void*,void**,void*); 235 void *user_data; 236} ffi_closure; 237 238ffi_status 239ffi_prep_closure (ffi_closure*, 240 ffi_cif *, 241 void (*fun)(ffi_cif*,void*,void**,void*), 242 void *user_data); 243 244typedef struct { 245 char tramp[FFI_TRAMPOLINE_SIZE]; 246 247 ffi_cif *cif; 248 249#if !FFI_NATIVE_RAW_API 250 251 /* if this is enabled, then a raw closure has the same layout 252 as a regular closure. We use this to install an intermediate 253 handler to do the transaltion, void** -> ffi_raw*. */ 254 255 void (*translate_args)(ffi_cif*,void*,void**,void*); 256 void *this_closure; 257 258#endif 259 260 void (*fun)(ffi_cif*,void*,ffi_raw*,void*); 261 void *user_data; 262 263} ffi_raw_closure; 264 265ffi_status 266ffi_prep_raw_closure (ffi_raw_closure*, 267 ffi_cif *cif, 268 void (*fun)(ffi_cif*,void*,ffi_raw*,void*), 269 void *user_data); 270 271ffi_status 272ffi_prep_java_raw_closure (ffi_raw_closure*, 273 ffi_cif *cif, 274 void (*fun)(ffi_cif*,void*,ffi_raw*,void*), 275 void *user_data); 276 277#endif /* FFI_CLOSURES */ 278 279/* ---- Public interface definition -------------------------------------- */ 280 281ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif, 282 ffi_abi abi, 283 unsigned int nargs, 284 /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype, 285 /*@dependent@*/ ffi_type **atypes); 286 287void ffi_call(/*@dependent@*/ ffi_cif *cif, 288 void (*fn)(void), 289 /*@out@*/ void *rvalue, 290 /*@dependent@*/ void **avalue); 291 292/* Useful for eliminating compiler warnings */ 293#define FFI_FN(f) ((void (*)(void))f) 294 295/* ---- Definitions shared with assembly code ---------------------------- */ 296 297#endif 298 299/* If these change, update src/mips/ffitarget.h. */ 300#define FFI_TYPE_VOID 0 301#define FFI_TYPE_INT 1 302#define FFI_TYPE_FLOAT 2 303#define FFI_TYPE_DOUBLE 3 304#ifdef HAVE_LONG_DOUBLE 305#define FFI_TYPE_LONGDOUBLE 4 306#else 307#define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE 308#endif 309#define FFI_TYPE_UINT8 5 310#define FFI_TYPE_SINT8 6 311#define FFI_TYPE_UINT16 7 312#define FFI_TYPE_SINT16 8 313#define FFI_TYPE_UINT32 9 314#define FFI_TYPE_SINT32 10 315#define FFI_TYPE_UINT64 11 316#define FFI_TYPE_SINT64 12 317#define FFI_TYPE_STRUCT 13 318#define FFI_TYPE_POINTER 14 319 320/* This should always refer to the last type code (for sanity checks) */ 321#define FFI_TYPE_LAST FFI_TYPE_POINTER 322 323#ifdef __cplusplus 324} 325#endif 326 327#endif 328 329