1// android -- copy of liffi.h.in with '@' values replaced
2/* -----------------------------------------------------------------*-C-*-
3   libffi @VERSION@ - Copyright (c) 1996-2003, 2007, 2008  Red Hat, Inc.
4
5   Permission is hereby granted, free of charge, to any person obtaining
6   a copy of this software and associated documentation files (the
7   ``Software''), to deal in the Software without restriction, including
8   without limitation the rights to use, copy, modify, merge, publish,
9   distribute, sublicense, and/or sell copies of the Software, and to
10   permit persons to whom the Software is furnished to do so, subject to
11   the following conditions:
12
13   The above copyright notice and this permission notice shall be included
14   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@          // android changed
62//#define @TARGET@          // android changed
63//#endif                    // android changed
64
65/* ---- System configuration information --------------------------------- */
66
67//#include <ffitarget.h>        // android changed -- included earlier
68
69#ifndef LIBFFI_ASM
70
71#include <stddef.h>
72#include <limits.h>
73
74/* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
75   But we can find it either under the correct ANSI name, or under GNU
76   C's internal name.  */
77#ifdef LONG_LONG_MAX
78# define FFI_LONG_LONG_MAX LONG_LONG_MAX
79#else
80# ifdef LLONG_MAX
81#  define FFI_LONG_LONG_MAX LLONG_MAX
82# else
83#  ifdef __GNUC__
84#   define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
85#  endif
86# endif
87#endif
88
89/* The closure code assumes that this works on pointers, i.e. a size_t	*/
90/* can hold a pointer.							*/
91
92typedef struct _ffi_type
93{
94  size_t size;
95  unsigned short alignment;
96  unsigned short type;
97  struct _ffi_type **elements;
98} ffi_type;
99
100#ifndef LIBFFI_HIDE_BASIC_TYPES
101#if SCHAR_MAX == 127
102# define ffi_type_uchar                ffi_type_uint8
103# define ffi_type_schar                ffi_type_sint8
104#else
105 #error "char size not supported"
106#endif
107
108#if SHRT_MAX == 32767
109# define ffi_type_ushort       ffi_type_uint16
110# define ffi_type_sshort       ffi_type_sint16
111#elif SHRT_MAX == 2147483647
112# define ffi_type_ushort       ffi_type_uint32
113# define ffi_type_sshort       ffi_type_sint32
114#else
115 #error "short size not supported"
116#endif
117
118#if INT_MAX == 32767
119# define ffi_type_uint         ffi_type_uint16
120# define ffi_type_sint         ffi_type_sint16
121#elif INT_MAX == 2147483647
122# define ffi_type_uint         ffi_type_uint32
123# define ffi_type_sint         ffi_type_sint32
124#elif INT_MAX == 9223372036854775807
125# define ffi_type_uint         ffi_type_uint64
126# define ffi_type_sint         ffi_type_sint64
127#else
128 #error "int size not supported"
129#endif
130
131#if LONG_MAX == 2147483647
132# if FFI_LONG_LONG_MAX != 9223372036854775807
133 #error "no 64-bit data type supported"
134# endif
135#elif LONG_MAX != 9223372036854775807
136 #error "long size not supported"
137#endif
138
139#if LONG_MAX == 2147483647
140# define ffi_type_ulong        ffi_type_uint32
141# define ffi_type_slong        ffi_type_sint32
142#elif LONG_MAX == 9223372036854775807
143# define ffi_type_ulong        ffi_type_uint64
144# define ffi_type_slong        ffi_type_sint64
145#else
146 #error "long size not supported"
147#endif
148
149/* These are defined in types.c */
150extern ffi_type ffi_type_void;
151extern ffi_type ffi_type_uint8;
152extern ffi_type ffi_type_sint8;
153extern ffi_type ffi_type_uint16;
154extern ffi_type ffi_type_sint16;
155extern ffi_type ffi_type_uint32;
156extern ffi_type ffi_type_sint32;
157extern ffi_type ffi_type_uint64;
158extern ffi_type ffi_type_sint64;
159extern ffi_type ffi_type_float;
160extern ffi_type ffi_type_double;
161extern ffi_type ffi_type_pointer;
162
163#if CONF_HAVE_LONG_DOUBLE       // android changed
164extern ffi_type ffi_type_longdouble;
165#else
166#define ffi_type_longdouble ffi_type_double
167#endif
168#endif /* LIBFFI_HIDE_BASIC_TYPES */
169
170typedef enum {
171  FFI_OK = 0,
172  FFI_BAD_TYPEDEF,
173  FFI_BAD_ABI
174} ffi_status;
175
176typedef unsigned FFI_TYPE;
177
178typedef struct {
179  ffi_abi abi;
180  unsigned nargs;
181  ffi_type **arg_types;
182  ffi_type *rtype;
183  unsigned bytes;
184  unsigned flags;
185#ifdef FFI_EXTRA_CIF_FIELDS
186  FFI_EXTRA_CIF_FIELDS;
187#endif
188} ffi_cif;
189
190/* ---- Definitions for the raw API -------------------------------------- */
191
192#ifndef FFI_SIZEOF_ARG
193# if LONG_MAX == 2147483647
194#  define FFI_SIZEOF_ARG        4
195# elif LONG_MAX == 9223372036854775807
196#  define FFI_SIZEOF_ARG        8
197# endif
198#endif
199
200#ifndef FFI_SIZEOF_JAVA_RAW
201#  define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
202#endif
203
204typedef union {
205  ffi_sarg  sint;
206  ffi_arg   uint;
207  float	    flt;
208  char      data[FFI_SIZEOF_ARG];
209  void*     ptr;
210} ffi_raw;
211
212#if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
213/* This is a special case for mips64/n32 ABI (and perhaps others) where
214   sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8.  */
215typedef union {
216  signed int	sint;
217  unsigned int	uint;
218  float		flt;
219  char		data[FFI_SIZEOF_JAVA_RAW];
220  void*		ptr;
221} ffi_java_raw;
222#else
223typedef ffi_raw ffi_java_raw;
224#endif
225
226
227void ffi_raw_call (ffi_cif *cif,
228		   void (*fn)(void),
229		   void *rvalue,
230		   ffi_raw *avalue);
231
232void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
233void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
234size_t ffi_raw_size (ffi_cif *cif);
235
236/* This is analogous to the raw API, except it uses Java parameter	*/
237/* packing, even on 64-bit machines.  I.e. on 64-bit machines		*/
238/* longs and doubles are followed by an empty 64-bit word.		*/
239
240void ffi_java_raw_call (ffi_cif *cif,
241			void (*fn)(void),
242			void *rvalue,
243			ffi_java_raw *avalue);
244
245void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw);
246void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args);
247size_t ffi_java_raw_size (ffi_cif *cif);
248
249/* ---- Definitions for closures ----------------------------------------- */
250
251#if FFI_CLOSURES
252
253typedef struct {
254  char tramp[FFI_TRAMPOLINE_SIZE];
255  ffi_cif   *cif;
256  void     (*fun)(ffi_cif*,void*,void**,void*);
257  void      *user_data;
258} ffi_closure __attribute__((aligned (8)));
259
260void *ffi_closure_alloc (size_t size, void **code);
261void ffi_closure_free (void *);
262
263ffi_status
264ffi_prep_closure (ffi_closure*,
265		  ffi_cif *,
266		  void (*fun)(ffi_cif*,void*,void**,void*),
267		  void *user_data);
268
269ffi_status
270ffi_prep_closure_loc (ffi_closure*,
271		      ffi_cif *,
272		      void (*fun)(ffi_cif*,void*,void**,void*),
273		      void *user_data,
274		      void*codeloc);
275
276typedef struct {
277  char tramp[FFI_TRAMPOLINE_SIZE];
278
279  ffi_cif   *cif;
280
281#if !FFI_NATIVE_RAW_API
282
283  /* if this is enabled, then a raw closure has the same layout
284     as a regular closure.  We use this to install an intermediate
285     handler to do the transaltion, void** -> ffi_raw*. */
286
287  void     (*translate_args)(ffi_cif*,void*,void**,void*);
288  void      *this_closure;
289
290#endif
291
292  void     (*fun)(ffi_cif*,void*,ffi_raw*,void*);
293  void      *user_data;
294
295} ffi_raw_closure;
296
297typedef struct {
298  char tramp[FFI_TRAMPOLINE_SIZE];
299
300  ffi_cif   *cif;
301
302#if !FFI_NATIVE_RAW_API
303
304  /* if this is enabled, then a raw closure has the same layout
305     as a regular closure.  We use this to install an intermediate
306     handler to do the transaltion, void** -> ffi_raw*. */
307
308  void     (*translate_args)(ffi_cif*,void*,void**,void*);
309  void      *this_closure;
310
311#endif
312
313  void     (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
314  void      *user_data;
315
316} ffi_java_raw_closure;
317
318ffi_status
319ffi_prep_raw_closure (ffi_raw_closure*,
320		      ffi_cif *cif,
321		      void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
322		      void *user_data);
323
324ffi_status
325ffi_prep_raw_closure_loc (ffi_raw_closure*,
326			  ffi_cif *cif,
327			  void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
328			  void *user_data,
329			  void *codeloc);
330
331ffi_status
332ffi_prep_java_raw_closure (ffi_java_raw_closure*,
333		           ffi_cif *cif,
334		           void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
335		           void *user_data);
336
337ffi_status
338ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
339			       ffi_cif *cif,
340			       void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
341			       void *user_data,
342			       void *codeloc);
343
344#endif /* FFI_CLOSURES */
345
346/* ---- Public interface definition -------------------------------------- */
347
348ffi_status ffi_prep_cif(ffi_cif *cif,
349			ffi_abi abi,
350			unsigned int nargs,
351			ffi_type *rtype,
352			ffi_type **atypes);
353
354void ffi_call(ffi_cif *cif,
355	      void (*fn)(void),
356	      void *rvalue,
357	      void **avalue);
358
359/* Useful for eliminating compiler warnings */
360#define FFI_FN(f) ((void (*)(void))f)
361
362/* ---- Definitions shared with assembly code ---------------------------- */
363
364#endif
365
366/* If these change, update src/mips/ffitarget.h. */
367#define FFI_TYPE_VOID       0
368#define FFI_TYPE_INT        1
369#define FFI_TYPE_FLOAT      2
370#define FFI_TYPE_DOUBLE     3
371#if CONF_HAVE_LONG_DOUBLE       // android changed
372#define FFI_TYPE_LONGDOUBLE 4
373#else
374#define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
375#endif
376#define FFI_TYPE_UINT8      5
377#define FFI_TYPE_SINT8      6
378#define FFI_TYPE_UINT16     7
379#define FFI_TYPE_SINT16     8
380#define FFI_TYPE_UINT32     9
381#define FFI_TYPE_SINT32     10
382#define FFI_TYPE_UINT64     11
383#define FFI_TYPE_SINT64     12
384#define FFI_TYPE_STRUCT     13
385#define FFI_TYPE_POINTER    14
386
387/* This should always refer to the last type code (for sanity checks) */
388#define FFI_TYPE_LAST       FFI_TYPE_POINTER
389
390#ifdef __cplusplus
391}
392#endif
393
394#endif
395