]>
Commit | Line | Data |
---|---|---|
248c0b69 | 1 | /* -----------------------------------------------------------------*-C-*- |
18fa3240 | 2 | libffi @VERSION@ - Copyright (c) 1996-2003, 2007 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 | ||
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 | ||
dc5de370 HB |
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 | ||
7e5fd99f | 44 | http://gcc.gnu.org/ml/java/1999-q3/msg00138.html |
dc5de370 HB |
45 | |
46 | and | |
47 | ||
7e5fd99f | 48 | http://gcc.gnu.org/ml/java/1999-q3/msg00174.html |
dc5de370 HB |
49 | -------------------------------------------------------------------- */ |
50 | ||
248c0b69 AG |
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 | ||
1450eb7a | 63 | #include <ffitarget.h> |
248c0b69 | 64 | |
1450eb7a | 65 | #ifndef LIBFFI_ASM |
248c0b69 | 66 | |
1450eb7a AT |
67 | #include <stddef.h> |
68 | #include <limits.h> | |
248c0b69 | 69 | |
1450eb7a AT |
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 | |
248c0b69 | 75 | #else |
1450eb7a AT |
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 | |
cd41c847 ZD |
83 | #endif |
84 | ||
1450eb7a AT |
85 | #if SCHAR_MAX == 127 |
86 | # define ffi_type_uchar ffi_type_uint8 | |
87 | # define ffi_type_schar ffi_type_sint8 | |
a733b15e | 88 | #else |
1450eb7a | 89 | #error "char size not supported" |
a733b15e JJ |
90 | #endif |
91 | ||
1450eb7a AT |
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 | |
a733b15e | 98 | #else |
1450eb7a AT |
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 | |
3791773c | 111 | #else |
1450eb7a | 112 | #error "int size not supported" |
248c0b69 AG |
113 | #endif |
114 | ||
1450eb7a AT |
115 | #if LONG_MAX == 2147483647 |
116 | # if FFI_LONG_LONG_MAX != 9223372036854775807 | |
cb4132fe | 117 | #error "no 64-bit data type supported" |
e9b84181 | 118 | # endif |
1450eb7a AT |
119 | #elif LONG_MAX != 9223372036854775807 |
120 | #error "long size not supported" | |
248c0b69 AG |
121 | #endif |
122 | ||
cb4132fe TS |
123 | #if LONG_MAX == 2147483647 |
124 | # define ffi_type_ulong ffi_type_uint32 | |
125 | # define ffi_type_slong ffi_type_sint32 | |
126 | #elif LONG_MAX == 9223372036854775807 | |
127 | # define ffi_type_ulong ffi_type_uint64 | |
128 | # define ffi_type_slong ffi_type_sint64 | |
129 | #else | |
130 | #error "long size not supported" | |
131 | #endif | |
132 | ||
1450eb7a AT |
133 | /* The closure code assumes that this works on pointers, i.e. a size_t */ |
134 | /* can hold a pointer. */ | |
248c0b69 AG |
135 | |
136 | typedef struct _ffi_type | |
137 | { | |
138 | size_t size; | |
139 | unsigned short alignment; | |
140 | unsigned short type; | |
8e5fde87 | 141 | struct _ffi_type **elements; |
248c0b69 AG |
142 | } ffi_type; |
143 | ||
eb3c46a1 | 144 | /* These are defined in types.c */ |
248c0b69 AG |
145 | extern ffi_type ffi_type_void; |
146 | extern ffi_type ffi_type_uint8; | |
147 | extern ffi_type ffi_type_sint8; | |
148 | extern ffi_type ffi_type_uint16; | |
149 | extern ffi_type ffi_type_sint16; | |
150 | extern ffi_type ffi_type_uint32; | |
151 | extern ffi_type ffi_type_sint32; | |
152 | extern ffi_type ffi_type_uint64; | |
153 | extern ffi_type ffi_type_sint64; | |
154 | extern ffi_type ffi_type_float; | |
155 | extern ffi_type ffi_type_double; | |
156 | extern ffi_type ffi_type_longdouble; | |
157 | extern ffi_type ffi_type_pointer; | |
158 | ||
248c0b69 AG |
159 | |
160 | typedef enum { | |
161 | FFI_OK = 0, | |
162 | FFI_BAD_TYPEDEF, | |
8e5fde87 | 163 | FFI_BAD_ABI |
248c0b69 AG |
164 | } ffi_status; |
165 | ||
166 | typedef unsigned FFI_TYPE; | |
167 | ||
168 | typedef struct { | |
169 | ffi_abi abi; | |
170 | unsigned nargs; | |
8e5fde87 AT |
171 | ffi_type **arg_types; |
172 | ffi_type *rtype; | |
248c0b69 AG |
173 | unsigned bytes; |
174 | unsigned flags; | |
1450eb7a AT |
175 | #ifdef FFI_EXTRA_CIF_FIELDS |
176 | FFI_EXTRA_CIF_FIELDS; | |
e860ed6d | 177 | #endif |
248c0b69 AG |
178 | } ffi_cif; |
179 | ||
180 | /* ---- Definitions for the raw API -------------------------------------- */ | |
181 | ||
1450eb7a AT |
182 | #ifndef FFI_SIZEOF_ARG |
183 | # if LONG_MAX == 2147483647 | |
184 | # define FFI_SIZEOF_ARG 4 | |
185 | # elif LONG_MAX == 9223372036854775807 | |
186 | # define FFI_SIZEOF_ARG 8 | |
187 | # endif | |
248c0b69 AG |
188 | #endif |
189 | ||
190 | typedef union { | |
1450eb7a AT |
191 | ffi_sarg sint; |
192 | ffi_arg uint; | |
193 | float flt; | |
194 | char data[FFI_SIZEOF_ARG]; | |
195 | void* ptr; | |
248c0b69 AG |
196 | } ffi_raw; |
197 | ||
8e5fde87 AT |
198 | void ffi_raw_call (ffi_cif *cif, |
199 | void (*fn)(), | |
200 | void *rvalue, | |
201 | ffi_raw *avalue); | |
248c0b69 AG |
202 | |
203 | void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw); | |
204 | void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args); | |
205 | size_t ffi_raw_size (ffi_cif *cif); | |
206 | ||
dc5de370 HB |
207 | /* This is analogous to the raw API, except it uses Java parameter */ |
208 | /* packing, even on 64-bit machines. I.e. on 64-bit machines */ | |
209 | /* longs and doubles are followed by an empty 64-bit word. */ | |
248c0b69 | 210 | |
8e5fde87 AT |
211 | void ffi_java_raw_call (ffi_cif *cif, |
212 | void (*fn)(), | |
213 | void *rvalue, | |
214 | ffi_raw *avalue); | |
dc5de370 HB |
215 | |
216 | void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw); | |
217 | void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args); | |
218 | size_t ffi_java_raw_size (ffi_cif *cif); | |
219 | ||
248c0b69 AG |
220 | /* ---- Definitions for closures ----------------------------------------- */ |
221 | ||
248c0b69 AG |
222 | #if FFI_CLOSURES |
223 | ||
224 | typedef struct { | |
225 | char tramp[FFI_TRAMPOLINE_SIZE]; | |
226 | ffi_cif *cif; | |
227 | void (*fun)(ffi_cif*,void*,void**,void*); | |
228 | void *user_data; | |
88d8c771 | 229 | } ffi_closure __attribute__((aligned (8))); |
248c0b69 | 230 | |
18fa3240 AO |
231 | void *ffi_closure_alloc (size_t size, void **code); |
232 | void ffi_closure_free (void *); | |
233 | ||
248c0b69 AG |
234 | ffi_status |
235 | ffi_prep_closure (ffi_closure*, | |
236 | ffi_cif *, | |
237 | void (*fun)(ffi_cif*,void*,void**,void*), | |
238 | void *user_data); | |
239 | ||
18fa3240 AO |
240 | ffi_status |
241 | ffi_prep_closure_loc (ffi_closure*, | |
242 | ffi_cif *, | |
243 | void (*fun)(ffi_cif*,void*,void**,void*), | |
244 | void *user_data, | |
245 | void*codeloc); | |
246 | ||
248c0b69 AG |
247 | typedef struct { |
248 | char tramp[FFI_TRAMPOLINE_SIZE]; | |
249 | ||
250 | ffi_cif *cif; | |
251 | ||
252 | #if !FFI_NATIVE_RAW_API | |
253 | ||
254 | /* if this is enabled, then a raw closure has the same layout | |
255 | as a regular closure. We use this to install an intermediate | |
256 | handler to do the transaltion, void** -> ffi_raw*. */ | |
257 | ||
258 | void (*translate_args)(ffi_cif*,void*,void**,void*); | |
259 | void *this_closure; | |
260 | ||
261 | #endif | |
262 | ||
263 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*); | |
264 | void *user_data; | |
265 | ||
266 | } ffi_raw_closure; | |
267 | ||
268 | ffi_status | |
269 | ffi_prep_raw_closure (ffi_raw_closure*, | |
270 | ffi_cif *cif, | |
271 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), | |
272 | void *user_data); | |
273 | ||
18fa3240 AO |
274 | ffi_status |
275 | ffi_prep_raw_closure_loc (ffi_raw_closure*, | |
276 | ffi_cif *cif, | |
277 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), | |
278 | void *user_data, | |
279 | void *codeloc); | |
280 | ||
dc5de370 HB |
281 | ffi_status |
282 | ffi_prep_java_raw_closure (ffi_raw_closure*, | |
283 | ffi_cif *cif, | |
284 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), | |
285 | void *user_data); | |
dc5de370 | 286 | |
18fa3240 AO |
287 | ffi_status |
288 | ffi_prep_java_raw_closure_loc (ffi_raw_closure*, | |
289 | ffi_cif *cif, | |
290 | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), | |
291 | void *user_data, | |
292 | void *codeloc); | |
293 | ||
248c0b69 AG |
294 | #endif /* FFI_CLOSURES */ |
295 | ||
296 | /* ---- Public interface definition -------------------------------------- */ | |
297 | ||
8e5fde87 | 298 | ffi_status ffi_prep_cif(ffi_cif *cif, |
248c0b69 | 299 | ffi_abi abi, |
8e5fde87 AT |
300 | unsigned int nargs, |
301 | ffi_type *rtype, | |
302 | ffi_type **atypes); | |
303 | ||
304 | void ffi_call(ffi_cif *cif, | |
305 | void (*fn)(), | |
306 | void *rvalue, | |
307 | void **avalue); | |
248c0b69 AG |
308 | |
309 | /* Useful for eliminating compiler warnings */ | |
8d25f608 | 310 | #define FFI_FN(f) ((void (*)())f) |
248c0b69 AG |
311 | |
312 | /* ---- Definitions shared with assembly code ---------------------------- */ | |
313 | ||
314 | #endif | |
315 | ||
1450eb7a | 316 | /* If these change, update src/mips/ffitarget.h. */ |
248c0b69 AG |
317 | #define FFI_TYPE_VOID 0 |
318 | #define FFI_TYPE_INT 1 | |
319 | #define FFI_TYPE_FLOAT 2 | |
320 | #define FFI_TYPE_DOUBLE 3 | |
1450eb7a | 321 | #if @HAVE_LONG_DOUBLE@ |
248c0b69 | 322 | #define FFI_TYPE_LONGDOUBLE 4 |
1450eb7a AT |
323 | #else |
324 | #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE | |
248c0b69 | 325 | #endif |
1450eb7a AT |
326 | #define FFI_TYPE_UINT8 5 |
327 | #define FFI_TYPE_SINT8 6 | |
248c0b69 AG |
328 | #define FFI_TYPE_UINT16 7 |
329 | #define FFI_TYPE_SINT16 8 | |
330 | #define FFI_TYPE_UINT32 9 | |
331 | #define FFI_TYPE_SINT32 10 | |
332 | #define FFI_TYPE_UINT64 11 | |
333 | #define FFI_TYPE_SINT64 12 | |
1450eb7a | 334 | #define FFI_TYPE_STRUCT 13 |
248c0b69 AG |
335 | #define FFI_TYPE_POINTER 14 |
336 | ||
337 | /* This should always refer to the last type code (for sanity checks) */ | |
338 | #define FFI_TYPE_LAST FFI_TYPE_POINTER | |
339 | ||
340 | #ifdef __cplusplus | |
341 | } | |
342 | #endif | |
343 | ||
344 | #endif | |
345 |