Ticket #31459: ffi.h

File ffi.h, 11.4 KB (added by herzog@…, 13 years ago)
Line 
1/* -----------------------------------------------------------------*-C-*-
2   libffi 3.0.10 - Copyright (c) 2011 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#if (defined(__ppc__) || defined(__ppc64__))
62#ifndef POWERPC_DARWIN
63#define POWERPC_DARWIN
64#else
65#ifndef X86_DARWIN
66#define X86_DARWIN
67#endif
68#endif
69
70/* ---- System configuration information --------------------------------- */
71
72#include <ffitarget.h>
73
74#ifndef LIBFFI_ASM
75
76#ifdef _MSC_VER
77#define __attribute__(X)
78#endif
79
80#include <stddef.h>
81#include <limits.h>
82
83/* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
84   But we can find it either under the correct ANSI name, or under GNU
85   C's internal name.  */
86
87#define FFI_64_BIT_MAX 9223372036854775807
88
89#ifdef LONG_LONG_MAX
90# define FFI_LONG_LONG_MAX LONG_LONG_MAX
91#else
92# ifdef LLONG_MAX
93#  define FFI_LONG_LONG_MAX LLONG_MAX
94#  ifdef _AIX52 /* or newer has C99 LLONG_MAX */
95#   undef FFI_64_BIT_MAX
96#   define FFI_64_BIT_MAX 9223372036854775807LL
97#  endif /* _AIX52 or newer */
98# else
99#  ifdef __GNUC__
100#   define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
101#  endif
102#  ifdef _AIX /* AIX 5.1 and earlier have LONGLONG_MAX */
103#   ifndef __PPC64__
104#    if defined (__IBMC__) || defined (__IBMCPP__)
105#     define FFI_LONG_LONG_MAX LONGLONG_MAX
106#    endif
107#   endif /* __PPC64__ */
108#   undef  FFI_64_BIT_MAX
109#   define FFI_64_BIT_MAX 9223372036854775807LL
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/* These are defined in types.c */
175extern ffi_type ffi_type_void;
176extern ffi_type ffi_type_uint8;
177extern ffi_type ffi_type_sint8;
178extern ffi_type ffi_type_uint16;
179extern ffi_type ffi_type_sint16;
180extern ffi_type ffi_type_uint32;
181extern ffi_type ffi_type_sint32;
182extern ffi_type ffi_type_uint64;
183extern ffi_type ffi_type_sint64;
184extern ffi_type ffi_type_float;
185extern ffi_type ffi_type_double;
186extern ffi_type ffi_type_pointer;
187
188#if 1
189extern ffi_type ffi_type_longdouble;
190#else
191#define ffi_type_longdouble ffi_type_double
192#endif
193#endif /* LIBFFI_HIDE_BASIC_TYPES */
194
195typedef enum {
196  FFI_OK = 0,
197  FFI_BAD_TYPEDEF,
198  FFI_BAD_ABI
199} ffi_status;
200
201typedef unsigned FFI_TYPE;
202
203typedef struct {
204  ffi_abi abi;
205  unsigned nargs;
206  ffi_type **arg_types;
207  ffi_type *rtype;
208  unsigned bytes;
209  unsigned flags;
210#ifdef FFI_EXTRA_CIF_FIELDS
211  FFI_EXTRA_CIF_FIELDS;
212#endif
213} ffi_cif;
214
215/* ---- Definitions for the raw API -------------------------------------- */
216
217#ifndef FFI_SIZEOF_ARG
218# if LONG_MAX == 2147483647
219#  define FFI_SIZEOF_ARG        4
220# elif LONG_MAX == FFI_64_BIT_MAX
221#  define FFI_SIZEOF_ARG        8
222# endif
223#endif
224
225#ifndef FFI_SIZEOF_JAVA_RAW
226#  define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
227#endif
228
229typedef union {
230  ffi_sarg  sint;
231  ffi_arg   uint;
232  float     flt;
233  char      data[FFI_SIZEOF_ARG];
234  void*     ptr;
235} ffi_raw;
236
237#if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
238/* This is a special case for mips64/n32 ABI (and perhaps others) where
239   sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8.  */
240typedef union {
241  signed int    sint;
242  unsigned int  uint;
243  float         flt;
244  char          data[FFI_SIZEOF_JAVA_RAW];
245  void*         ptr;
246} ffi_java_raw;
247#else
248typedef ffi_raw ffi_java_raw;
249#endif
250
251
252void ffi_raw_call (ffi_cif *cif,
253                   void (*fn)(void),
254                   void *rvalue,
255                   ffi_raw *avalue);
256
257void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
258void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
259size_t ffi_raw_size (ffi_cif *cif);
260
261/* This is analogous to the raw API, except it uses Java parameter      */
262/* packing, even on 64-bit machines.  I.e. on 64-bit machines           */
263/* longs and doubles are followed by an empty 64-bit word.              */
264
265void ffi_java_raw_call (ffi_cif *cif,
266                        void (*fn)(void),
267                        void *rvalue,
268                        ffi_java_raw *avalue);
269
270void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw);
271void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args);
272size_t ffi_java_raw_size (ffi_cif *cif);
273
274/* ---- Definitions for closures ----------------------------------------- */
275
276#if FFI_CLOSURES
277
278#ifdef _MSC_VER
279__declspec(align(8))
280#endif
281typedef struct {
282  char tramp[FFI_TRAMPOLINE_SIZE];
283  ffi_cif   *cif;
284  void     (*fun)(ffi_cif*,void*,void**,void*);
285  void      *user_data;
286#ifdef __GNUC__
287} ffi_closure __attribute__((aligned (8)));
288#else
289} ffi_closure;
290# ifdef __sgi
291#  pragma pack 0
292# endif
293#endif
294
295void *ffi_closure_alloc (size_t size, void **code);
296void ffi_closure_free (void *);
297
298ffi_status
299ffi_prep_closure (ffi_closure*,
300                  ffi_cif *,
301                  void (*fun)(ffi_cif*,void*,void**,void*),
302                  void *user_data);
303
304ffi_status
305ffi_prep_closure_loc (ffi_closure*,
306                      ffi_cif *,
307                      void (*fun)(ffi_cif*,void*,void**,void*),
308                      void *user_data,
309                      void*codeloc);
310
311#ifdef __sgi
312# pragma pack 8
313#endif
314typedef struct {
315  char tramp[FFI_TRAMPOLINE_SIZE];
316
317  ffi_cif   *cif;
318
319#if !FFI_NATIVE_RAW_API
320
321  /* if this is enabled, then a raw closure has the same layout
322     as a regular closure.  We use this to install an intermediate
323     handler to do the transaltion, void** -> ffi_raw*. */
324
325  void     (*translate_args)(ffi_cif*,void*,void**,void*);
326  void      *this_closure;
327
328#endif
329
330  void     (*fun)(ffi_cif*,void*,ffi_raw*,void*);
331  void      *user_data;
332
333} ffi_raw_closure;
334
335typedef struct {
336  char tramp[FFI_TRAMPOLINE_SIZE];
337
338  ffi_cif   *cif;
339
340#if !FFI_NATIVE_RAW_API
341
342  /* if this is enabled, then a raw closure has the same layout
343     as a regular closure.  We use this to install an intermediate
344     handler to do the transaltion, void** -> ffi_raw*. */
345
346  void     (*translate_args)(ffi_cif*,void*,void**,void*);
347  void      *this_closure;
348
349#endif
350
351  void     (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
352  void      *user_data;
353
354} ffi_java_raw_closure;
355
356ffi_status
357ffi_prep_raw_closure (ffi_raw_closure*,
358                      ffi_cif *cif,
359                      void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
360                      void *user_data);
361
362ffi_status
363ffi_prep_raw_closure_loc (ffi_raw_closure*,
364                          ffi_cif *cif,
365                          void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
366                          void *user_data,
367                          void *codeloc);
368
369ffi_status
370ffi_prep_java_raw_closure (ffi_java_raw_closure*,
371                           ffi_cif *cif,
372                           void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
373                           void *user_data);
374
375ffi_status
376ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
377                               ffi_cif *cif,
378                               void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
379                               void *user_data,
380                               void *codeloc);
381
382#endif /* FFI_CLOSURES */
383
384/* ---- Public interface definition -------------------------------------- */
385
386ffi_status ffi_prep_cif(ffi_cif *cif,
387                        ffi_abi abi,
388                        unsigned int nargs,
389                        ffi_type *rtype,
390                        ffi_type **atypes);
391
392void ffi_call(ffi_cif *cif,
393              void (*fn)(void),
394              void *rvalue,
395              void **avalue);
396
397/* Useful for eliminating compiler warnings */
398#define FFI_FN(f) ((void (*)(void))f)
399
400/* ---- Definitions shared with assembly code ---------------------------- */
401
402#endif
403
404/* If these change, update src/mips/ffitarget.h. */
405#define FFI_TYPE_VOID       0   
406#define FFI_TYPE_INT        1
407#define FFI_TYPE_FLOAT      2   
408#define FFI_TYPE_DOUBLE     3
409#if 1
410#define FFI_TYPE_LONGDOUBLE 4
411#else
412#define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
413#endif
414#define FFI_TYPE_UINT8      5   
415#define FFI_TYPE_SINT8      6
416#define FFI_TYPE_UINT16     7
417#define FFI_TYPE_SINT16     8
418#define FFI_TYPE_UINT32     9
419#define FFI_TYPE_SINT32     10
420#define FFI_TYPE_UINT64     11
421#define FFI_TYPE_SINT64     12
422#define FFI_TYPE_STRUCT     13
423#define FFI_TYPE_POINTER    14
424
425/* This should always refer to the last type code (for sanity checks) */
426#define FFI_TYPE_LAST       FFI_TYPE_POINTER
427
428#ifdef __cplusplus
429}
430#endif
431
432#endif