OSDN Git Service

* configure.ac: Tidy target case.
[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 typedef union {
197   ffi_sarg  sint;
198   ffi_arg   uint;
199   float     flt;
200   char      data[FFI_SIZEOF_ARG];
201   void*     ptr;
202 } ffi_raw;
203
204 void ffi_raw_call (ffi_cif *cif,
205                    void (*fn)(),
206                    void *rvalue,
207                    ffi_raw *avalue);
208
209 void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
210 void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
211 size_t ffi_raw_size (ffi_cif *cif);
212
213 /* This is analogous to the raw API, except it uses Java parameter      */
214 /* packing, even on 64-bit machines.  I.e. on 64-bit machines           */
215 /* longs and doubles are followed by an empty 64-bit word.              */
216
217 void ffi_java_raw_call (ffi_cif *cif,
218                         void (*fn)(),
219                         void *rvalue,
220                         ffi_raw *avalue);
221
222 void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
223 void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
224 size_t ffi_java_raw_size (ffi_cif *cif);
225
226 /* ---- Definitions for closures ----------------------------------------- */
227
228 #if FFI_CLOSURES
229
230 typedef struct {
231   char tramp[FFI_TRAMPOLINE_SIZE];
232   ffi_cif   *cif;
233   void     (*fun)(ffi_cif*,void*,void**,void*);
234   void      *user_data;
235 } ffi_closure __attribute__((aligned (8)));
236
237 void *ffi_closure_alloc (size_t size, void **code);
238 void ffi_closure_free (void *);
239
240 ffi_status
241 ffi_prep_closure (ffi_closure*,
242                   ffi_cif *,
243                   void (*fun)(ffi_cif*,void*,void**,void*),
244                   void *user_data);
245
246 ffi_status
247 ffi_prep_closure_loc (ffi_closure*,
248                       ffi_cif *,
249                       void (*fun)(ffi_cif*,void*,void**,void*),
250                       void *user_data,
251                       void*codeloc);
252
253 typedef struct {
254   char tramp[FFI_TRAMPOLINE_SIZE];
255
256   ffi_cif   *cif;
257
258 #if !FFI_NATIVE_RAW_API
259
260   /* if this is enabled, then a raw closure has the same layout 
261      as a regular closure.  We use this to install an intermediate 
262      handler to do the transaltion, void** -> ffi_raw*. */
263
264   void     (*translate_args)(ffi_cif*,void*,void**,void*);
265   void      *this_closure;
266
267 #endif
268
269   void     (*fun)(ffi_cif*,void*,ffi_raw*,void*);
270   void      *user_data;
271
272 } ffi_raw_closure;
273
274 ffi_status
275 ffi_prep_raw_closure (ffi_raw_closure*,
276                       ffi_cif *cif,
277                       void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
278                       void *user_data);
279
280 ffi_status
281 ffi_prep_raw_closure_loc (ffi_raw_closure*,
282                           ffi_cif *cif,
283                           void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
284                           void *user_data,
285                           void *codeloc);
286
287 ffi_status
288 ffi_prep_java_raw_closure (ffi_raw_closure*,
289                            ffi_cif *cif,
290                            void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
291                            void *user_data);
292
293 ffi_status
294 ffi_prep_java_raw_closure_loc (ffi_raw_closure*,
295                                ffi_cif *cif,
296                                void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
297                                void *user_data,
298                                void *codeloc);
299
300 #endif /* FFI_CLOSURES */
301
302 /* ---- Public interface definition -------------------------------------- */
303
304 ffi_status ffi_prep_cif(ffi_cif *cif,
305                         ffi_abi abi,
306                         unsigned int nargs,
307                         ffi_type *rtype,
308                         ffi_type **atypes);
309
310 void ffi_call(ffi_cif *cif,
311               void (*fn)(),
312               void *rvalue,
313               void **avalue);
314
315 /* Useful for eliminating compiler warnings */
316 #define FFI_FN(f) ((void (*)())f)
317
318 /* ---- Definitions shared with assembly code ---------------------------- */
319
320 #endif
321
322 /* If these change, update src/mips/ffitarget.h. */
323 #define FFI_TYPE_VOID       0    
324 #define FFI_TYPE_INT        1
325 #define FFI_TYPE_FLOAT      2    
326 #define FFI_TYPE_DOUBLE     3
327 #if @HAVE_LONG_DOUBLE@
328 #define FFI_TYPE_LONGDOUBLE 4
329 #else
330 #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
331 #endif
332 #define FFI_TYPE_UINT8      5   
333 #define FFI_TYPE_SINT8      6
334 #define FFI_TYPE_UINT16     7 
335 #define FFI_TYPE_SINT16     8
336 #define FFI_TYPE_UINT32     9
337 #define FFI_TYPE_SINT32     10
338 #define FFI_TYPE_UINT64     11
339 #define FFI_TYPE_SINT64     12
340 #define FFI_TYPE_STRUCT     13
341 #define FFI_TYPE_POINTER    14
342
343 /* This should always refer to the last type code (for sanity checks) */
344 #define FFI_TYPE_LAST       FFI_TYPE_POINTER
345
346 #ifdef __cplusplus
347 }
348 #endif
349
350 #endif