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