1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 1998, 1999, 2001, 2007 Red Hat, Inc.
3 Copyright (c) 2002 Ranjit Mathew
4 Copyright (c) 2002 Bo Thorsen
5 Copyright (c) 2002 Roger Sayle
7 x86 Foreign Function Interface
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 ``Software''), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
17 The above copyright notice and this permission notice shall be included
18 in all copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 OTHER DEALINGS IN THE SOFTWARE.
27 ----------------------------------------------------------------------- */
32 #include <ffi_common.h>
36 /* ffi_prep_args is called by the assembly routine once stack space
37 has been allocated for the function's arguments */
39 void ffi_prep_args(char *stack, extended_cif *ecif)
41 register unsigned int i;
42 register void **p_argv;
44 register ffi_type **p_arg;
48 if (ecif->cif->flags == FFI_TYPE_STRUCT)
50 *(void **) argp = ecif->rvalue;
54 p_argv = ecif->avalue;
56 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
62 /* Align if necessary */
63 if ((sizeof(int) - 1) & (unsigned) argp)
64 argp = (char *) ALIGN(argp, sizeof(int));
70 switch ((*p_arg)->type)
73 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
77 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
81 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
85 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
89 *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
93 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
97 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
106 memcpy(argp, *p_argv, z);
115 /* Perform machine dependent cif processing */
116 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
118 /* Set the return type flag */
119 switch (cif->rtype->type)
123 case FFI_TYPE_STRUCT:
125 case FFI_TYPE_UINT16:
127 case FFI_TYPE_SINT16:
130 case FFI_TYPE_SINT64:
132 case FFI_TYPE_DOUBLE:
133 case FFI_TYPE_LONGDOUBLE:
134 cif->flags = (unsigned) cif->rtype->type;
137 case FFI_TYPE_UINT64:
138 cif->flags = FFI_TYPE_SINT64;
142 case FFI_TYPE_STRUCT:
143 if (cif->rtype->size == 1)
145 cif->flags = FFI_TYPE_SINT8; /* same as char size */
147 else if (cif->rtype->size == 2)
149 cif->flags = FFI_TYPE_SINT16; /* same as short size */
151 else if (cif->rtype->size == 4)
153 cif->flags = FFI_TYPE_INT; /* same as int type */
155 else if (cif->rtype->size == 8)
157 cif->flags = FFI_TYPE_SINT64; /* same as int64 type */
161 cif->flags = FFI_TYPE_STRUCT;
167 cif->flags = FFI_TYPE_INT;
172 cif->bytes = (cif->bytes + 15) & ~0xF;
178 extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
179 unsigned, unsigned, unsigned *, void (*fn)());
182 extern void ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *,
183 unsigned, unsigned, unsigned *, void (*fn)());
185 #endif /* X86_WIN32 */
187 void ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
192 ecif.avalue = avalue;
194 /* If the return value is a struct and we don't have a return */
195 /* value address then we need to make one */
197 if ((rvalue == NULL) &&
198 (cif->flags == FFI_TYPE_STRUCT))
200 ecif.rvalue = alloca(cif->rtype->size);
203 ecif.rvalue = rvalue;
209 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
214 ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes, cif->flags,
217 #endif /* X86_WIN32 */
225 /** private members **/
227 static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
228 void** args, ffi_cif* cif);
229 void FFI_HIDDEN ffi_closure_SYSV (ffi_closure *)
230 __attribute__ ((regparm(1)));
231 unsigned int FFI_HIDDEN ffi_closure_SYSV_inner (ffi_closure *, void **, void *)
232 __attribute__ ((regparm(1)));
233 void FFI_HIDDEN ffi_closure_raw_SYSV (ffi_raw_closure *)
234 __attribute__ ((regparm(1)));
236 /* This function is jumped to by the trampoline */
238 unsigned int FFI_HIDDEN
239 ffi_closure_SYSV_inner (closure, respp, args)
240 ffi_closure *closure;
244 // our various things...
249 arg_area = (void**) alloca (cif->nargs * sizeof (void*));
251 /* this call will initialize ARG_AREA, such that each
252 * element in that array points to the corresponding
253 * value on the stack; and if the function returns
254 * a structure, it will re-set RESP to point to the
255 * structure return address. */
257 ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
259 (closure->fun) (cif, *respp, arg_area, closure->user_data);
265 ffi_prep_incoming_args_SYSV(char *stack, void **rvalue, void **avalue,
268 register unsigned int i;
269 register void **p_argv;
271 register ffi_type **p_arg;
275 if ( cif->flags == FFI_TYPE_STRUCT ) {
276 *rvalue = *(void **) argp;
282 for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
286 /* Align if necessary */
287 if ((sizeof(int) - 1) & (unsigned) argp) {
288 argp = (char *) ALIGN(argp, sizeof(int));
293 /* because we're little endian, this is what it turns into. */
295 *p_argv = (void*) argp;
304 /* How to make a trampoline. Derived from gcc/config/i386/i386.c. */
306 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
307 ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
308 unsigned int __fun = (unsigned int)(FUN); \
309 unsigned int __ctx = (unsigned int)(CTX); \
310 unsigned int __dis = __fun - (__ctx + FFI_TRAMPOLINE_SIZE); \
311 *(unsigned char*) &__tramp[0] = 0xb8; \
312 *(unsigned int*) &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
313 *(unsigned char *) &__tramp[5] = 0xe9; \
314 *(unsigned int*) &__tramp[6] = __dis; /* jmp __fun */ \
318 /* the cif must already be prep'ed */
321 ffi_prep_closure_loc (ffi_closure* closure,
323 void (*fun)(ffi_cif*,void*,void**,void*),
327 FFI_ASSERT (cif->abi == FFI_SYSV);
329 FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
334 closure->user_data = user_data;
340 /* ------- Native raw API support -------------------------------- */
345 ffi_prep_raw_closure_loc (ffi_raw_closure* closure,
347 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
353 FFI_ASSERT (cif->abi == FFI_SYSV);
355 // we currently don't support certain kinds of arguments for raw
356 // closures. This should be implemented by a separate assembly language
357 // routine, since it would require argument processing, something we
358 // don't do now for performance.
360 for (i = cif->nargs-1; i >= 0; i--)
362 FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT);
363 FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE);
367 FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV,
371 closure->user_data = user_data;
378 ffi_prep_args_raw(char *stack, extended_cif *ecif)
380 memcpy (stack, ecif->avalue, ecif->cif->bytes);
383 /* we borrow this routine from libffi (it must be changed, though, to
384 * actually call the function passed in the first argument. as of
385 * libffi-1.20, this is not the case.)
389 ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *, unsigned,
390 unsigned, unsigned *, void (*fn)());
394 ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *, unsigned,
395 unsigned, unsigned *, void (*fn)());
396 #endif /* X86_WIN32 */
399 ffi_raw_call(ffi_cif *cif, void (*fn)(), void *rvalue, ffi_raw *fake_avalue)
402 void **avalue = (void **)fake_avalue;
405 ecif.avalue = avalue;
407 /* If the return value is a struct and we don't have a return */
408 /* value address then we need to make one */
410 if ((rvalue == NULL) &&
411 (cif->rtype->type == FFI_TYPE_STRUCT))
413 ecif.rvalue = alloca(cif->rtype->size);
416 ecif.rvalue = rvalue;
422 ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags,
427 ffi_call_STDCALL(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags,
430 #endif /* X86_WIN32 */
439 #endif /* __x86_64__ */