1 /* -----------------------------------------------------------------*-C-*-
2 libffi @VERSION@ - Copyright (c) 1996-2003 Cygnus Solutions
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:
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
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.
23 ----------------------------------------------------------------------- */
25 /* -------------------------------------------------------------------
26 The basic API is described in the README file.
28 The raw API is designed to bypass some of the argument packing
29 and unpacking on architectures for which it can be avoided.
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
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.
42 More details on the raw and cloure API can be found in:
44 http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
48 http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
49 -------------------------------------------------------------------- */
58 /* Specify which architecture libffi is configured for. */
61 /* ---- System configuration information --------------------------------- */
63 #include <fficonfig.h>
65 #if !defined(LIBFFI_ASM)
67 #if defined(FFI_DEBUG)
72 /* ---- Generic type definitions ----------------------------------------- */
75 #define FLOAT64 double
76 #define FLOAT80 long double
78 #define UINT8 unsigned char
79 #define SINT8 signed char
83 #define UINT16 unsigned int
85 #define ffi_type_uint ffi_type_uint16
86 #define ffi_type_sint ffi_type_sint16
91 #define UINT16 unsigned short
93 #define ffi_type_ushort ffi_type_uint16
94 #define ffi_type_sshort ffi_type_sint16
101 #define UINT32 unsigned int
103 #define ffi_type_uint ffi_type_uint32
104 #define ffi_type_sint ffi_type_sint32
107 #if SIZEOF_SHORT == 4
109 #define UINT32 unsigned short
111 #define ffi_type_ushort ffi_type_uint32
112 #define ffi_type_sshort ffi_type_sint32
117 #define UINT32 unsigned long
119 #define ffi_type_ulong ffi_type_uint32
120 #define ffi_type_slong ffi_type_sint32
128 #define UINT64 unsigned int
130 #define ffi_type_uint ffi_type_uint64
131 #define ffi_type_sint ffi_type_sint64
136 #define UINT64 unsigned long
138 #define ffi_type_ulong ffi_type_uint64
139 #define ffi_type_slong ffi_type_sint64
142 #if SIZEOF_LONG_LONG == 8
144 #define UINT64 unsigned long long
145 #define SINT64 long long
146 #define ffi_type_ulong ffi_type_uint64
147 #define ffi_type_slong ffi_type_sint64
153 /* ---- System specific configurations ----------------------------------- */
156 #include <ffi_mips.h>
158 #define SIZEOF_ARG SIZEOF_VOID_P
162 #if defined(__arch64__) || defined(__sparcv9)
168 #if defined (__s390x__)
175 /* ---- Generic type definitions ----------------------------------------- */
177 #define ALIGN(v, a) (((((size_t) (v))-1) | ((a)-1))+1)
178 /* The closure code assumes that this works on pointers, i.e. a size_t */
179 /* can hold a pointer. */
181 typedef enum ffi_abi
{
183 /* Leave this for debugging purposes */
186 /* ---- Sparc -------------------- */
192 FFI_DEFAULT_ABI
= FFI_V9
,
194 FFI_DEFAULT_ABI
= FFI_V8
,
198 /* ---- Intel x86 Win32 ---------- */
202 /* TODO: Add fastcall support for the sake of completeness */
203 FFI_DEFAULT_ABI
= FFI_SYSV
,
206 /* ---- Intel x86 and AMD x86-64 - */
207 #if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__))
209 FFI_UNIX64
, /* Unix variants all use the same ABI for x86-64 */
211 FFI_DEFAULT_ABI
= FFI_SYSV
,
213 FFI_DEFAULT_ABI
= FFI_UNIX64
,
217 /* ---- Intel ia64 ---------------- */
219 FFI_UNIX
, /* Linux and all Unix variants use the same conventions */
220 FFI_DEFAULT_ABI
= FFI_UNIX
,
223 /* ---- Mips --------------------- */
230 /* ---- Alpha -------------------- */
233 FFI_DEFAULT_ABI
= FFI_OSF
,
236 /* ---- Motorola m68k ------------ */
239 FFI_DEFAULT_ABI
= FFI_SYSV
,
242 /* ---- PowerPC ------------------ */
246 FFI_DEFAULT_ABI
= FFI_GCC_SYSV
,
252 FFI_DEFAULT_ABI
= FFI_AIX
,
255 #ifdef POWERPC_DARWIN
258 FFI_DEFAULT_ABI
= FFI_DARWIN
,
261 /* ---- ARM --------------------- */
264 FFI_DEFAULT_ABI
= FFI_SYSV
,
267 /* ---- S390 --------------------- */
270 FFI_DEFAULT_ABI
= FFI_SYSV
,
273 /* ---- SuperH ------------------- */
276 FFI_DEFAULT_ABI
= FFI_SYSV
,
279 /* Leave this for debugging purposes */
284 typedef struct _ffi_type
287 unsigned short alignment
;
289 /*@null@*/ struct _ffi_type
**elements
;
292 /* These are defined in types.c */
293 extern ffi_type ffi_type_void
;
294 extern ffi_type ffi_type_uint8
;
295 extern ffi_type ffi_type_sint8
;
296 extern ffi_type ffi_type_uint16
;
297 extern ffi_type ffi_type_sint16
;
298 extern ffi_type ffi_type_uint32
;
299 extern ffi_type ffi_type_sint32
;
300 extern ffi_type ffi_type_uint64
;
301 extern ffi_type ffi_type_sint64
;
302 extern ffi_type ffi_type_float
;
303 extern ffi_type ffi_type_double
;
304 extern ffi_type ffi_type_longdouble
;
305 extern ffi_type ffi_type_pointer
;
307 /* Characters are 8 bit integral types */
308 #define ffi_type_schar ffi_type_sint8
309 #define ffi_type_uchar ffi_type_uint8
317 typedef unsigned FFI_TYPE
;
322 /*@dependent@*/ ffi_type
**arg_types
;
323 /*@dependent@*/ ffi_type
*rtype
;
328 #if _MIPS_SIM == _ABIN32
329 unsigned rstruct_flag
;
336 typedef UINT32 ffi_arg
;
339 typedef UINT64 ffi_arg
;
341 -- unsupported configuration
345 /* ---- Definitions for the raw API -------------------------------------- */
351 #define UINT_ARG UINT32
352 #define SINT_ARG SINT32
358 #define UINT_ARG UINT64
359 #define SINT_ARG SINT64
367 char data
[SIZEOF_ARG
];
371 void ffi_raw_call (/*@dependent@*/ ffi_cif
*cif
,
373 /*@out@*/ void *rvalue
,
374 /*@dependent@*/ ffi_raw
*avalue
);
376 void ffi_ptrarray_to_raw (ffi_cif
*cif
, void **args
, ffi_raw
*raw
);
377 void ffi_raw_to_ptrarray (ffi_cif
*cif
, ffi_raw
*raw
, void **args
);
378 size_t ffi_raw_size (ffi_cif
*cif
);
382 /* This is analogous to the raw API, except it uses Java parameter */
383 /* packing, even on 64-bit machines. I.e. on 64-bit machines */
384 /* longs and doubles are followed by an empty 64-bit word. */
386 void ffi_java_raw_call (/*@dependent@*/ ffi_cif
*cif
,
388 /*@out@*/ void *rvalue
,
389 /*@dependent@*/ ffi_raw
*avalue
);
391 void ffi_java_ptrarray_to_raw (ffi_cif
*cif
, void **args
, ffi_raw
*raw
);
392 void ffi_java_raw_to_ptrarray (ffi_cif
*cif
, ffi_raw
*raw
, void **args
);
393 size_t ffi_java_raw_size (ffi_cif
*cif
);
395 #endif /* !NO_JAVA_RAW_API */
397 #endif /* !FFI_NO_RAW_API */
399 /* ---- Definitions for closures ----------------------------------------- */
403 #define FFI_CLOSURES 1 /* x86 supports closures */
404 #define FFI_TRAMPOLINE_SIZE 10
405 #define FFI_NATIVE_RAW_API 1 /* and has native raw api support */
409 #define FFI_CLOSURES 1
410 #define FFI_TRAMPOLINE_SIZE 24 /* Really the following struct, which */
411 /* can be interpreted as a C function */
414 struct ffi_ia64_trampoline_struct
{
415 void * code_pointer
; /* Pointer to ffi_closure_UNIX */
416 void * fake_gp
; /* Pointer to closure, installed as gp */
417 void * real_gp
; /* Real gp value, reinstalled by */
418 /* ffi_closure_UNIX. */
420 #define FFI_NATIVE_RAW_API 0
424 #define FFI_CLOSURES 1
425 #define FFI_TRAMPOLINE_SIZE 24
426 #define FFI_NATIVE_RAW_API 0
428 #elif defined(POWERPC)
430 #define FFI_CLOSURES 1
431 #define FFI_TRAMPOLINE_SIZE 40
432 #define FFI_NATIVE_RAW_API 0
434 #elif defined(POWERPC_DARWIN)
436 #define FFI_CLOSURES 1
437 #define FFI_TRAMPOLINE_SIZE 40
438 #define FFI_NATIVE_RAW_API 0
440 #elif defined(POWERPC_AIX)
442 #define FFI_CLOSURES 1
443 #define FFI_TRAMPOLINE_SIZE 24 /* see struct below */
444 #define FFI_NATIVE_RAW_API 0
446 #elif defined(SPARC64)
448 #define FFI_CLOSURES 1
449 #define FFI_TRAMPOLINE_SIZE 24
450 #define FFI_NATIVE_RAW_API 0
454 #define FFI_CLOSURES 1
455 #define FFI_TRAMPOLINE_SIZE 16
456 #define FFI_NATIVE_RAW_API 0
460 #define FFI_CLOSURES 1
462 #define FFI_TRAMPOLINE_SIZE 32
464 #define FFI_TRAMPOLINE_SIZE 16
466 #define FFI_NATIVE_RAW_API 0
470 #define FFI_CLOSURES 1
471 #define FFI_TRAMPOLINE_SIZE 16
472 #define FFI_NATIVE_RAW_API 0
476 #define FFI_CLOSURES 0
477 #define FFI_NATIVE_RAW_API 0
481 #if defined(POWERPC_DARWIN) || defined(POWERPC_AIX)
483 struct ffi_aix_trampoline_struct
{
484 void * code_pointer
; /* Pointer to ffi_closure_ASM */
485 void * toc
; /* TOC */
486 void * static_chain
; /* Pointer to closure */
496 char tramp
[FFI_TRAMPOLINE_SIZE
];
498 void (*fun
)(ffi_cif
*,void*,void**,void*);
503 ffi_prep_closure (ffi_closure
*,
505 void (*fun
)(ffi_cif
*,void*,void**,void*),
511 char tramp
[FFI_TRAMPOLINE_SIZE
];
515 #if !FFI_NATIVE_RAW_API
517 /* if this is enabled, then a raw closure has the same layout
518 as a regular closure. We use this to install an intermediate
519 handler to do the transaltion, void** -> ffi_raw*. */
521 void (*translate_args
)(ffi_cif
*,void*,void**,void*);
526 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*);
532 ffi_prep_raw_closure (ffi_raw_closure
*,
534 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
537 #ifndef NO_JAVA_RAW_API
539 ffi_prep_java_raw_closure (ffi_raw_closure
*,
541 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
545 #endif /* !FFI_NO_RAW_API */
546 #endif /* FFI_CLOSURES */
548 /* ---- Public interface definition -------------------------------------- */
550 ffi_status
ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif
*cif
,
553 /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type
*rtype
,
554 /*@dependent@*/ ffi_type
**atypes
);
556 void ffi_call(/*@dependent@*/ ffi_cif
*cif
,
558 /*@out@*/ void *rvalue
,
559 /*@dependent@*/ void **avalue
);
561 /* Useful for eliminating compiler warnings */
562 #define FFI_FN(f) ((void (*)())f)
564 /* ---- Definitions shared with assembly code ---------------------------- */
568 #define FFI_TYPE_VOID 0
569 #define FFI_TYPE_INT 1
570 #define FFI_TYPE_FLOAT 2
571 #define FFI_TYPE_DOUBLE 3
572 #if SIZEOF_LONG_DOUBLE == SIZEOF_DOUBLE
573 #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
575 #define FFI_TYPE_LONGDOUBLE 4
578 #define FFI_TYPE_UINT8 5 /* If this changes, update ffi_mips.h. */
579 #define FFI_TYPE_SINT8 6 /* If this changes, update ffi_mips.h. */
580 #define FFI_TYPE_UINT16 7
581 #define FFI_TYPE_SINT16 8
582 #define FFI_TYPE_UINT32 9
583 #define FFI_TYPE_SINT32 10
584 #define FFI_TYPE_UINT64 11
585 #define FFI_TYPE_SINT64 12
586 #define FFI_TYPE_STRUCT 13 /* If this changes, update ffi_mips.h. */
587 #define FFI_TYPE_POINTER 14
589 /* This should always refer to the last type code (for sanity checks) */
590 #define FFI_TYPE_LAST FFI_TYPE_POINTER