Merge pull request #238 from KubaKaszycki/master
[libffi.git] / include / ffi.h.in
1 /* -----------------------------------------------------------------*-C-*-
2 libffi @VERSION@ - Copyright (c) 2011, 2014 Anthony Green
3 - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc.
4
5 Permission is hereby granted, free of charge, to any person
6 obtaining a copy of this software and associated documentation
7 files (the ``Software''), to deal in the Software without
8 restriction, including without limitation the rights to use, copy,
9 modify, merge, publish, distribute, sublicense, and/or sell copies
10 of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
24
25 ----------------------------------------------------------------------- */
26
27 /* -------------------------------------------------------------------
28 Most of the API is documented in doc/libffi.texi.
29
30 The raw API is designed to bypass some of the argument packing and
31 unpacking on architectures for which it can be avoided. Routines
32 are provided to emulate the raw API if the underlying platform
33 doesn't allow faster implementation.
34
35 More details on the raw API can be found in:
36
37 http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
38
39 and
40
41 http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
42 -------------------------------------------------------------------- */
43
44 #ifndef LIBFFI_H
45 #define LIBFFI_H
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /* Specify which architecture libffi is configured for. */
52 #ifndef @TARGET@
53 #define @TARGET@
54 #endif
55
56 /* ---- System configuration information --------------------------------- */
57
58 #include <ffitarget.h>
59
60 #ifndef LIBFFI_ASM
61
62 #if defined(_MSC_VER) && !defined(__clang__)
63 #define __attribute__(X)
64 #endif
65
66 #include <stddef.h>
67 #include <limits.h>
68
69 /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
70 But we can find it either under the correct ANSI name, or under GNU
71 C's internal name. */
72
73 #define FFI_64_BIT_MAX 9223372036854775807
74
75 #ifdef LONG_LONG_MAX
76 # define FFI_LONG_LONG_MAX LONG_LONG_MAX
77 #else
78 # ifdef LLONG_MAX
79 # define FFI_LONG_LONG_MAX LLONG_MAX
80 # ifdef _AIX52 /* or newer has C99 LLONG_MAX */
81 # undef FFI_64_BIT_MAX
82 # define FFI_64_BIT_MAX 9223372036854775807LL
83 # endif /* _AIX52 or newer */
84 # else
85 # ifdef __GNUC__
86 # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
87 # endif
88 # ifdef _AIX /* AIX 5.1 and earlier have LONGLONG_MAX */
89 # ifndef __PPC64__
90 # if defined (__IBMC__) || defined (__IBMCPP__)
91 # define FFI_LONG_LONG_MAX LONGLONG_MAX
92 # endif
93 # endif /* __PPC64__ */
94 # undef FFI_64_BIT_MAX
95 # define FFI_64_BIT_MAX 9223372036854775807LL
96 # endif
97 # endif
98 #endif
99
100 /* The closure code assumes that this works on pointers, i.e. a size_t
101 can hold a pointer. */
102
103 typedef struct _ffi_type
104 {
105 size_t size;
106 unsigned short alignment;
107 unsigned short type;
108 struct _ffi_type **elements;
109 } ffi_type;
110
111 #ifndef LIBFFI_HIDE_BASIC_TYPES
112 #if SCHAR_MAX == 127
113 # define ffi_type_uchar ffi_type_uint8
114 # define ffi_type_schar ffi_type_sint8
115 #else
116 #error "char size not supported"
117 #endif
118
119 #if SHRT_MAX == 32767
120 # define ffi_type_ushort ffi_type_uint16
121 # define ffi_type_sshort ffi_type_sint16
122 #elif SHRT_MAX == 2147483647
123 # define ffi_type_ushort ffi_type_uint32
124 # define ffi_type_sshort ffi_type_sint32
125 #else
126 #error "short size not supported"
127 #endif
128
129 #if INT_MAX == 32767
130 # define ffi_type_uint ffi_type_uint16
131 # define ffi_type_sint ffi_type_sint16
132 #elif INT_MAX == 2147483647
133 # define ffi_type_uint ffi_type_uint32
134 # define ffi_type_sint ffi_type_sint32
135 #elif INT_MAX == 9223372036854775807
136 # define ffi_type_uint ffi_type_uint64
137 # define ffi_type_sint ffi_type_sint64
138 #else
139 #error "int size not supported"
140 #endif
141
142 #if LONG_MAX == 2147483647
143 # if FFI_LONG_LONG_MAX != FFI_64_BIT_MAX
144 #error "no 64-bit data type supported"
145 # endif
146 #elif LONG_MAX != FFI_64_BIT_MAX
147 #error "long size not supported"
148 #endif
149
150 #if LONG_MAX == 2147483647
151 # define ffi_type_ulong ffi_type_uint32
152 # define ffi_type_slong ffi_type_sint32
153 #elif LONG_MAX == FFI_64_BIT_MAX
154 # define ffi_type_ulong ffi_type_uint64
155 # define ffi_type_slong ffi_type_sint64
156 #else
157 #error "long size not supported"
158 #endif
159
160 /* Need minimal decorations for DLLs to works on Windows. GCC has
161 autoimport and autoexport. Rely on Libtool to help MSVC export
162 from a DLL, but always declare data to be imported for MSVC
163 clients. This costs an extra indirection for MSVC clients using
164 the static version of the library, but don't worry about that.
165 Besides, as a workaround, they can define FFI_BUILDING if they
166 *know* they are going to link with the static library. */
167 #if defined _MSC_VER && !defined FFI_BUILDING
168 #define FFI_EXTERN extern __declspec(dllimport)
169 #else
170 #define FFI_EXTERN extern
171 #endif
172
173 /* These are defined in types.c. */
174 FFI_EXTERN ffi_type ffi_type_void;
175 FFI_EXTERN ffi_type ffi_type_uint8;
176 FFI_EXTERN ffi_type ffi_type_sint8;
177 FFI_EXTERN ffi_type ffi_type_uint16;
178 FFI_EXTERN ffi_type ffi_type_sint16;
179 FFI_EXTERN ffi_type ffi_type_uint32;
180 FFI_EXTERN ffi_type ffi_type_sint32;
181 FFI_EXTERN ffi_type ffi_type_uint64;
182 FFI_EXTERN ffi_type ffi_type_sint64;
183 FFI_EXTERN ffi_type ffi_type_float;
184 FFI_EXTERN ffi_type ffi_type_double;
185 FFI_EXTERN ffi_type ffi_type_pointer;
186
187 #if @HAVE_LONG_DOUBLE@
188 FFI_EXTERN ffi_type ffi_type_longdouble;
189 #else
190 #define ffi_type_longdouble ffi_type_double
191 #endif
192
193 #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
194 FFI_EXTERN ffi_type ffi_type_complex_float;
195 FFI_EXTERN ffi_type ffi_type_complex_double;
196 #if @HAVE_LONG_DOUBLE@
197 FFI_EXTERN ffi_type ffi_type_complex_longdouble;
198 #else
199 #define ffi_type_complex_longdouble ffi_type_complex_double
200 #endif
201 #endif
202 #endif /* LIBFFI_HIDE_BASIC_TYPES */
203
204 typedef enum {
205 FFI_OK = 0,
206 FFI_BAD_TYPEDEF,
207 FFI_BAD_ABI
208 } ffi_status;
209
210 typedef struct {
211 ffi_abi abi;
212 unsigned nargs;
213 ffi_type **arg_types;
214 ffi_type *rtype;
215 unsigned bytes;
216 unsigned flags;
217 #ifdef FFI_EXTRA_CIF_FIELDS
218 FFI_EXTRA_CIF_FIELDS;
219 #endif
220 } ffi_cif;
221
222 /* ---- Definitions for the raw API -------------------------------------- */
223
224 #ifndef FFI_SIZEOF_ARG
225 # if LONG_MAX == 2147483647
226 # define FFI_SIZEOF_ARG 4
227 # elif LONG_MAX == FFI_64_BIT_MAX
228 # define FFI_SIZEOF_ARG 8
229 # endif
230 #endif
231
232 #ifndef FFI_SIZEOF_JAVA_RAW
233 # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
234 #endif
235
236 typedef union {
237 ffi_sarg sint;
238 ffi_arg uint;
239 float flt;
240 char data[FFI_SIZEOF_ARG];
241 void* ptr;
242 } ffi_raw;
243
244 #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
245 /* This is a special case for mips64/n32 ABI (and perhaps others) where
246 sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */
247 typedef union {
248 signed int sint;
249 unsigned int uint;
250 float flt;
251 char data[FFI_SIZEOF_JAVA_RAW];
252 void* ptr;
253 } ffi_java_raw;
254 #else
255 typedef ffi_raw ffi_java_raw;
256 #endif
257
258
259 void ffi_raw_call (ffi_cif *cif,
260 void (*fn)(void),
261 void *rvalue,
262 ffi_raw *avalue);
263
264 void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
265 void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
266 size_t ffi_raw_size (ffi_cif *cif);
267
268 /* This is analogous to the raw API, except it uses Java parameter
269 packing, even on 64-bit machines. I.e. on 64-bit machines longs
270 and doubles are followed by an empty 64-bit word. */
271
272 void ffi_java_raw_call (ffi_cif *cif,
273 void (*fn)(void),
274 void *rvalue,
275 ffi_java_raw *avalue);
276
277 void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw);
278 void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args);
279 size_t ffi_java_raw_size (ffi_cif *cif);
280
281 /* ---- Definitions for closures ----------------------------------------- */
282
283 #if FFI_CLOSURES
284
285 #ifdef _MSC_VER
286 __declspec(align(8))
287 #endif
288 typedef struct {
289 #if @FFI_EXEC_TRAMPOLINE_TABLE@
290 void *trampoline_table;
291 void *trampoline_table_entry;
292 #else
293 char tramp[FFI_TRAMPOLINE_SIZE];
294 #endif
295 ffi_cif *cif;
296 void (*fun)(ffi_cif*,void*,void**,void*);
297 void *user_data;
298 } ffi_closure
299 #ifdef __GNUC__
300 __attribute__((aligned (8)))
301 #endif
302 ;
303
304 #ifndef __GNUC__
305 # ifdef __sgi
306 # pragma pack 0
307 # endif
308 #endif
309
310 void *ffi_closure_alloc (size_t size, void **code);
311 void ffi_closure_free (void *);
312
313 ffi_status
314 ffi_prep_closure (ffi_closure*,
315 ffi_cif *,
316 void (*fun)(ffi_cif*,void*,void**,void*),
317 void *user_data)
318 __attribute__((deprecated ("use ffi_prep_closure_loc instead")));
319
320 ffi_status
321 ffi_prep_closure_loc (ffi_closure*,
322 ffi_cif *,
323 void (*fun)(ffi_cif*,void*,void**,void*),
324 void *user_data,
325 void*codeloc);
326
327 #ifdef __sgi
328 # pragma pack 8
329 #endif
330 typedef struct {
331 #if @FFI_EXEC_TRAMPOLINE_TABLE@
332 void *trampoline_table;
333 void *trampoline_table_entry;
334 #else
335 char tramp[FFI_TRAMPOLINE_SIZE];
336 #endif
337 ffi_cif *cif;
338
339 #if !FFI_NATIVE_RAW_API
340
341 /* If this is enabled, then a raw closure has the same layout
342 as a regular closure. We use this to install an intermediate
343 handler to do the transaltion, void** -> ffi_raw*. */
344
345 void (*translate_args)(ffi_cif*,void*,void**,void*);
346 void *this_closure;
347
348 #endif
349
350 void (*fun)(ffi_cif*,void*,ffi_raw*,void*);
351 void *user_data;
352
353 } ffi_raw_closure;
354
355 typedef struct {
356 #if @FFI_EXEC_TRAMPOLINE_TABLE@
357 void *trampoline_table;
358 void *trampoline_table_entry;
359 #else
360 char tramp[FFI_TRAMPOLINE_SIZE];
361 #endif
362
363 ffi_cif *cif;
364
365 #if !FFI_NATIVE_RAW_API
366
367 /* If this is enabled, then a raw closure has the same layout
368 as a regular closure. We use this to install an intermediate
369 handler to do the translation, void** -> ffi_raw*. */
370
371 void (*translate_args)(ffi_cif*,void*,void**,void*);
372 void *this_closure;
373
374 #endif
375
376 void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
377 void *user_data;
378
379 } ffi_java_raw_closure;
380
381 ffi_status
382 ffi_prep_raw_closure (ffi_raw_closure*,
383 ffi_cif *cif,
384 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
385 void *user_data);
386
387 ffi_status
388 ffi_prep_raw_closure_loc (ffi_raw_closure*,
389 ffi_cif *cif,
390 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
391 void *user_data,
392 void *codeloc);
393
394 ffi_status
395 ffi_prep_java_raw_closure (ffi_java_raw_closure*,
396 ffi_cif *cif,
397 void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
398 void *user_data);
399
400 ffi_status
401 ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
402 ffi_cif *cif,
403 void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
404 void *user_data,
405 void *codeloc);
406
407 #endif /* FFI_CLOSURES */
408
409 #if FFI_GO_CLOSURES
410
411 typedef struct {
412 void *tramp;
413 ffi_cif *cif;
414 void (*fun)(ffi_cif*,void*,void**,void*);
415 } ffi_go_closure;
416
417 ffi_status ffi_prep_go_closure (ffi_go_closure*, ffi_cif *,
418 void (*fun)(ffi_cif*,void*,void**,void*));
419
420 void ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
421 void **avalue, void *closure);
422
423 #endif /* FFI_GO_CLOSURES */
424
425 /* ---- Public interface definition -------------------------------------- */
426
427 ffi_status ffi_prep_cif(ffi_cif *cif,
428 ffi_abi abi,
429 unsigned int nargs,
430 ffi_type *rtype,
431 ffi_type **atypes);
432
433 ffi_status ffi_prep_cif_var(ffi_cif *cif,
434 ffi_abi abi,
435 unsigned int nfixedargs,
436 unsigned int ntotalargs,
437 ffi_type *rtype,
438 ffi_type **atypes);
439
440 void ffi_call(ffi_cif *cif,
441 void (*fn)(void),
442 void *rvalue,
443 void **avalue);
444
445 ffi_status ffi_get_struct_offsets (ffi_abi abi, ffi_type *struct_type,
446 size_t *offsets);
447
448 /* Useful for eliminating compiler warnings. */
449 #define FFI_FN(f) ((void (*)(void))f)
450
451 /* ---- Definitions shared with assembly code ---------------------------- */
452
453 #endif
454
455 /* If these change, update src/mips/ffitarget.h. */
456 #define FFI_TYPE_VOID 0
457 #define FFI_TYPE_INT 1
458 #define FFI_TYPE_FLOAT 2
459 #define FFI_TYPE_DOUBLE 3
460 #if @HAVE_LONG_DOUBLE@
461 #define FFI_TYPE_LONGDOUBLE 4
462 #else
463 #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
464 #endif
465 #define FFI_TYPE_UINT8 5
466 #define FFI_TYPE_SINT8 6
467 #define FFI_TYPE_UINT16 7
468 #define FFI_TYPE_SINT16 8
469 #define FFI_TYPE_UINT32 9
470 #define FFI_TYPE_SINT32 10
471 #define FFI_TYPE_UINT64 11
472 #define FFI_TYPE_SINT64 12
473 #define FFI_TYPE_STRUCT 13
474 #define FFI_TYPE_POINTER 14
475 #define FFI_TYPE_COMPLEX 15
476
477 /* This should always refer to the last type code (for sanity checks). */
478 #define FFI_TYPE_LAST FFI_TYPE_COMPLEX
479
480 #ifdef __cplusplus
481 }
482 #endif
483
484 #endif