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