OSDN Git Service

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