]>
Commit | Line | Data |
---|---|---|
248c0b69 | 1 | /* -----------------------------------------------------------------*-C-*- |
b7b857bb | 2 | libffi @VERSION@ - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc. |
248c0b69 | 3 | |
248c0b69 AG |
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 | ||
5f933ef0 AH |
15 | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
22 | DEALINGS IN THE SOFTWARE. | |
248c0b69 AG |
23 | |
24 | ----------------------------------------------------------------------- */ | |
25 | ||
dc5de370 HB |
26 | /* ------------------------------------------------------------------- |
27 | The basic API is described in the README file. | |
28 | ||
29 | The raw API is designed to bypass some of the argument packing | |
30 | and unpacking on architectures for which it can be avoided. | |
31 | ||
32 | The closure API allows interpreted functions to be packaged up | |
33 | inside a C function pointer, so that they can be called as C functions, | |
34 | with no understanding on the client side that they are interpreted. | |
35 | It can also be used in other cases in which it is necessary to package | |
36 | up a user specified parameter and a function pointer as a single | |
37 | function pointer. | |
38 | ||
39 | The closure API must be implemented in order to get its functionality, | |
40 | e.g. for use by gij. Routines are provided to emulate the raw API | |
41 | if the underlying platform doesn't allow faster implementation. | |
42 | ||
43 | More details on the raw and cloure API can be found in: | |
44 | ||
7e5fd99f | 45 | http://gcc.gnu.org/ml/java/1999-q3/msg00138.html |
dc5de370 HB |
46 | |
47 | and | |
48 | ||
7e5fd99f | 49 | http://gcc.gnu.org/ml/java/1999-q3/msg00174.html |
dc5de370 HB |
50 | -------------------------------------------------------------------- */ |
51 | ||
248c0b69 AG |
52 | #ifndef LIBFFI_H |
53 | #define LIBFFI_H | |
54 | ||
55 | #ifdef __cplusplus | |
56 | extern "C" { | |
57 | #endif | |
58 | ||
59 | /* Specify which architecture libffi is configured for. */ | |
60 | #define @TARGET@ | |
61 | ||
62 | /* ---- System configuration information --------------------------------- */ | |
63 | ||
1450eb7a | 64 | #include <ffitarget.h> |
248c0b69 | 65 | |
1450eb7a | 66 | #ifndef LIBFFI_ASM |
248c0b69 | 67 | |
1450eb7a AT |
68 | #include <stddef.h> |
69 | #include <limits.h> | |
248c0b69 | 70 | |
1450eb7a AT |
71 | /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example). |
72 | But we can find it either under the correct ANSI name, or under GNU | |
73 | C's internal name. */ | |
74 | #ifdef LONG_LONG_MAX | |
75 | # define FFI_LONG_LONG_MAX LONG_LONG_MAX | |
248c0b69 | 76 | #else |
1450eb7a AT |
77 | # ifdef LLONG_MAX |
78 | # define FFI_LONG_LONG_MAX LLONG_MAX | |
79 | # else | |
80 | # ifdef __GNUC__ | |
81 | # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__ | |
82 | # endif | |
83 | # endif | |
cd41c847 ZD |
84 | #endif |
85 | ||
bf8da5fc RH |
86 | /* The closure code assumes that this works on pointers, i.e. a size_t */ |
87 | /* can hold a pointer. */ | |
88 | ||
89 | typedef struct _ffi_type | |
90 | { | |
91 | size_t size; | |
92 | unsigned short alignment; | |
93 | unsigned short type; | |
94 | struct _ffi_type **elements; | |
95 | } ffi_type; | |
96 | ||
97 | #ifndef LIBFFI_HIDE_BASIC_TYPES | |
1450eb7a AT |
98 | #if SCHAR_MAX == 127 |
99 | # define ffi_type_uchar ffi_type_uint8 | |
100 | # define ffi_type_schar ffi_type_sint8 | |
a733b15e | 101 | #else |
1450eb7a | 102 | #error "char size not supported" |
a733b15e JJ |
103 | #endif |
104 | ||
1450eb7a AT |
105 | #if SHRT_MAX == 32767 |
106 | # define ffi_type_ushort ffi_type_uint16 | |
107 | # define ffi_type_sshort ffi_type_sint16 | |
108 | #elif SHRT_MAX == 2147483647 | |
109 | # define ffi_type_ushort ffi_type_uint32 | |
110 | # define ffi_type_sshort ffi_type_sint32 | |
a733b15e | 111 | #else |
1450eb7a AT |
112 | #error "short size not supported" |
113 | #endif | |
114 | ||
115 | #if INT_MAX == 32767 | |
116 | # define ffi_type_uint ffi_type_uint16 | |
117 | # define ffi_type_sint ffi_type_sint16 | |
118 | #elif INT_MAX == 2147483647 | |
119 | # define ffi_type_uint ffi_type_uint32 | |
120 | # define ffi_type_sint ffi_type_sint32 | |
121 | #elif INT_MAX == 9223372036854775807 | |
122 | # define ffi_type_uint ffi_type_uint64 | |
123 | # define ffi_type_sint ffi_type_sint64 | |
3791773c | 124 | #else |
1450eb7a | 125 | #error "int size not supported" |
248c0b69 AG |
126 | #endif |
127 | ||
1450eb7a AT |
128 | #if LONG_MAX == 2147483647 |
129 | # if FFI_LONG_LONG_MAX != 9223372036854775807 | |
cb4132fe | 130 | #error "no 64-bit data type supported" |
e9b84181 | 131 | # endif |
1450eb7a AT |
132 | #elif LONG_MAX != 9223372036854775807 |
133 | #error "long size not supported" | |
248c0b69 AG |
134 | #endif |
135 | ||
cb4132fe TS |
136 | #if LONG_MAX == 2147483647 |
137 | # define ffi_type_ulong ffi_type_uint32 | |
138 | # define ffi_type_slong ffi_type_sint32 | |
139 | #elif LONG_MAX == 9223372036854775807 | |
140 | # define ffi_type_ulong ffi_type_uint64 | |
141 | # define ffi_type_slong ffi_type_sint64 | |
142 | #else | |
143 | #error "long size not supported" | |
144 | #endif | |
145 | ||
eb3c46a1 | 146 | /* These are defined in types.c */ |
248c0b69 AG |
147 | extern ffi_type ffi_type_void; |
148 | extern ffi_type ffi_type_uint8; | |
149 | extern ffi_type ffi_type_sint8; | |
150 | extern ffi_type ffi_type_uint16; | |
151 | extern ffi_type ffi_type_sint16; | |
152 | extern ffi_type ffi_type_uint32; | |
153 | extern ffi_type ffi_type_sint32; | |
154 | extern ffi_type ffi_type_uint64; | |
155 | extern ffi_type ffi_type_sint64; | |
156 | extern ffi_type ffi_type_float; | |
157 | extern ffi_type ffi_type_double; | |
248c0b69 AG |
158 | extern ffi_type ffi_type_pointer; |
159 | ||
bf8da5fc RH |
160 | #if @HAVE_LONG_DOUBLE@ |
161 | extern ffi_type ffi_type_longdouble; | |
162 | #else | |
163 | #define ffi_type_longdouble ffi_type_double | |
164 | #endif | |
165 | #endif /* LIBFFI_HIDE_BASIC_TYPES */ | |
248c0b69 AG |
166 | |
167 | typedef enum { | |
168 | FFI_OK = 0, | |
169 | FFI_BAD_TYPEDEF, | |
8e5fde87 | 170 | FFI_BAD_ABI |
248c0b69 AG |
171 | } ffi_status; |
172 | ||
173 | typedef unsigned FFI_TYPE; | |
174 | ||
175 | typedef struct { | |
176 | ffi_abi abi; | |
177 | unsigned nargs; | |
8e5fde87 AT |
178 | ffi_type **arg_types; |
179 | ffi_type *rtype; | |
248c0b69 AG |
180 | unsigned bytes; |
181 | unsigned flags; | |
1450eb7a AT |
182 | #ifdef FFI_EXTRA_CIF_FIELDS |
183 | FFI_EXTRA_CIF_FIELDS; | |
e860ed6d | 184 | #endif |
248c0b69 AG |
185 | } ffi_cif; |
186 | ||
187 | /* ---- Definitions for the raw API -------------------------------------- */ | |
188 | ||
1450eb7a AT |
189 | #ifndef FFI_SIZEOF_ARG |
190 | # if LONG_MAX == 2147483647 | |
191 | # define FFI_SIZEOF_ARG 4 | |
192 | # elif LONG_MAX == 9223372036854775807 | |
193 | # define FFI_SIZEOF_ARG 8 | |
194 | # endif | |
248c0b69 AG |
195 | #endif |
196 | ||
4c42b3d8 DD |
197 | #ifndef FFI_SIZEOF_JAVA_RAW |
198 | # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG | |
199 | #endif | |
200 | ||
248c0b69 | 201 | typedef union { |
1450eb7a AT |
202 | ffi_sarg sint; |
203 | ffi_arg uint; | |
204 | float flt; | |
205 | char data[FFI_SIZEOF_ARG]; | |
206 | void* ptr; | |
248c0b69 AG |
207 | } ffi_raw; |
208 | ||
4c42b3d8 DD |
209 | #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8 |
210 | /* This is a special case for mips64/n32 ABI (and perhaps others) where | |
211 | sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */ | |
212 | typedef union { | |
213 | signed int sint; | |
214 | unsigned int uint; | |
215 | float flt; | |
216 | char data[FFI_SIZEOF_JAVA_RAW]; | |
217 | void* ptr; | |
218 | } ffi_java_raw; | |
219 | #else | |
220 | typedef ffi_raw ffi_java_raw; | |
221 | #endif | |
222 | ||
223 | ||
8e5fde87 | 224 | void ffi_raw_call (ffi_cif *cif, |
96baa251 | 225 | void (*fn)(void), |
8e5fde87 AT |
226 | void *rvalue, |
227 | ffi_raw *avalue); | |
248c0b69 AG |
228 | |
229 | void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw); | |
230 | void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args); | |
231 | size_t ffi_raw_size (ffi_cif *cif); | |
232 | ||
dc5de370 HB |
233 | /* This is analogous to the raw API, except it uses Java parameter */ |
234 | /* packing, even on 64-bit machines. I.e. on 64-bit machines */ | |
235 | /* longs and doubles are followed by an empty 64-bit word. */ | |
248c0b69 | 236 | |
8e5fde87 | 237 | void ffi_java_raw_call (ffi_cif *cif, |
96baa251 | 238 | void (*fn)(void), |
8e5fde87 | 239 | void *rvalue, |
4c42b3d8 | 240 | ffi_java_raw *avalue); |
dc5de370 | 241 | |
4c42b3d8 DD |
242 | void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw); |
243 | void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args); | |
dc5de370 HB |
244 | size_t ffi_java_raw_size (ffi_cif *cif); |
245 | ||
248c0b69 AG |
246 | /* ---- Definitions for closures ----------------------------------------- */ |
247 | ||
248c0b69 AG |
248 | #if FFI_CLOSURES |
249 | ||
250 | typedef struct { | |
251 | char tramp[FFI_TRAMPOLINE_SIZE]; | |
252 | ffi_cif *cif; | |
253 | void (*fun)(ffi_cif*,void*,void**,void*); | |
254 | void *user_data; | |
88d8c771 | 255 | } ffi_closure __attribute__((aligned (8))); |
248c0b69 | 256 | |
18fa3240 AO |
257 | void *ffi_closure_alloc (size_t size, void **code); |
258 | void ffi_closure_free (void *); | |
259 | ||
248c0b69 AG |
260 | ffi_status |
261 | ffi_prep_closure (ffi_closure*, | |
262 | ffi_cif *, | |
263 | void (*fun)(ffi_cif*,void*,void**,void*), | |
264 | void *user_data); | |
265 | ||
18fa3240 AO |
266 | ffi_status |
267 | ffi_prep_closure_loc (ffi_closure*, | |
268 | ffi_cif *, | |
269 | void (*fun)(ffi_cif*,void*,void**,void*), | |
270 | void *user_data, | |
271 | void*codeloc); | |
272 | ||
248c0b69 AG |
273 | typedef struct { |
274 | char tramp[FFI_TRAMPOLINE_SIZE]; | |
275 | ||
276 | ffi_cif *cif; | |
277 | ||
278 | #if !FFI_NATIVE_RAW_API | |
279 | ||
280 | /* if this is enabled, then a raw closure has the same layout | |
281 | as a regular closure. We use this to install an intermediate | |
282 | handler to do the transaltion, void** -> ffi_raw*. */ | |
283 | ||
284 | void (*translate_args)(ffi_cif*,void*,void**,void*); | |
285 | void *this_closure; | |
286 | ||
287 | #endif | |
288 | ||
289 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*); | |
290 | void *user_data; | |
291 | ||
292 | } ffi_raw_closure; | |
293 | ||
4c42b3d8 DD |
294 | typedef struct { |
295 | char tramp[FFI_TRAMPOLINE_SIZE]; | |
296 | ||
297 | ffi_cif *cif; | |
298 | ||
299 | #if !FFI_NATIVE_RAW_API | |
300 | ||
301 | /* if this is enabled, then a raw closure has the same layout | |
302 | as a regular closure. We use this to install an intermediate | |
303 | handler to do the transaltion, void** -> ffi_raw*. */ | |
304 | ||
305 | void (*translate_args)(ffi_cif*,void*,void**,void*); | |
306 | void *this_closure; | |
307 | ||
308 | #endif | |
309 | ||
310 | void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*); | |
311 | void *user_data; | |
312 | ||
313 | } ffi_java_raw_closure; | |
314 | ||
248c0b69 AG |
315 | ffi_status |
316 | ffi_prep_raw_closure (ffi_raw_closure*, | |
317 | ffi_cif *cif, | |
318 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), | |
319 | void *user_data); | |
320 | ||
18fa3240 AO |
321 | ffi_status |
322 | ffi_prep_raw_closure_loc (ffi_raw_closure*, | |
323 | ffi_cif *cif, | |
324 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), | |
325 | void *user_data, | |
326 | void *codeloc); | |
327 | ||
dc5de370 | 328 | ffi_status |
4c42b3d8 | 329 | ffi_prep_java_raw_closure (ffi_java_raw_closure*, |
dc5de370 | 330 | ffi_cif *cif, |
4c42b3d8 | 331 | void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*), |
dc5de370 | 332 | void *user_data); |
dc5de370 | 333 | |
18fa3240 | 334 | ffi_status |
4c42b3d8 | 335 | ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*, |
18fa3240 | 336 | ffi_cif *cif, |
4c42b3d8 | 337 | void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*), |
18fa3240 AO |
338 | void *user_data, |
339 | void *codeloc); | |
340 | ||
248c0b69 AG |
341 | #endif /* FFI_CLOSURES */ |
342 | ||
343 | /* ---- Public interface definition -------------------------------------- */ | |
344 | ||
8e5fde87 | 345 | ffi_status ffi_prep_cif(ffi_cif *cif, |
248c0b69 | 346 | ffi_abi abi, |
8e5fde87 AT |
347 | unsigned int nargs, |
348 | ffi_type *rtype, | |
349 | ffi_type **atypes); | |
350 | ||
351 | void ffi_call(ffi_cif *cif, | |
96baa251 | 352 | void (*fn)(void), |
8e5fde87 AT |
353 | void *rvalue, |
354 | void **avalue); | |
248c0b69 AG |
355 | |
356 | /* Useful for eliminating compiler warnings */ | |
b7b857bb | 357 | #define FFI_FN(f) ((void (*)(void))f) |
248c0b69 AG |
358 | |
359 | /* ---- Definitions shared with assembly code ---------------------------- */ | |
360 | ||
361 | #endif | |
362 | ||
1450eb7a | 363 | /* If these change, update src/mips/ffitarget.h. */ |
248c0b69 AG |
364 | #define FFI_TYPE_VOID 0 |
365 | #define FFI_TYPE_INT 1 | |
366 | #define FFI_TYPE_FLOAT 2 | |
367 | #define FFI_TYPE_DOUBLE 3 | |
1450eb7a | 368 | #if @HAVE_LONG_DOUBLE@ |
248c0b69 | 369 | #define FFI_TYPE_LONGDOUBLE 4 |
1450eb7a AT |
370 | #else |
371 | #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE | |
248c0b69 | 372 | #endif |
1450eb7a AT |
373 | #define FFI_TYPE_UINT8 5 |
374 | #define FFI_TYPE_SINT8 6 | |
248c0b69 AG |
375 | #define FFI_TYPE_UINT16 7 |
376 | #define FFI_TYPE_SINT16 8 | |
377 | #define FFI_TYPE_UINT32 9 | |
378 | #define FFI_TYPE_SINT32 10 | |
379 | #define FFI_TYPE_UINT64 11 | |
380 | #define FFI_TYPE_SINT64 12 | |
1450eb7a | 381 | #define FFI_TYPE_STRUCT 13 |
248c0b69 AG |
382 | #define FFI_TYPE_POINTER 14 |
383 | ||
384 | /* This should always refer to the last type code (for sanity checks) */ | |
385 | #define FFI_TYPE_LAST FFI_TYPE_POINTER | |
386 | ||
387 | #ifdef __cplusplus | |
388 | } | |
389 | #endif | |
390 | ||
391 | #endif |