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