1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 1998, 1999, 2001 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 */
40 void ffi_prep_args(char *stack
, extended_cif
*ecif
)
43 register unsigned int i
;
44 register void **p_argv
;
46 register ffi_type
**p_arg
;
50 if (ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
)
52 *(void **) argp
= ecif
->rvalue
;
56 p_argv
= ecif
->avalue
;
58 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
64 /* Align if necessary */
65 if ((sizeof(int) - 1) & (unsigned) argp
)
66 argp
= (char *) ALIGN(argp
, sizeof(int));
72 switch ((*p_arg
)->type
)
75 *(signed int *) argp
= (signed int)*(SINT8
*)(* p_argv
);
79 *(unsigned int *) argp
= (unsigned int)*(UINT8
*)(* p_argv
);
83 *(signed int *) argp
= (signed int)*(SINT16
*)(* p_argv
);
87 *(unsigned int *) argp
= (unsigned int)*(UINT16
*)(* p_argv
);
91 *(signed int *) argp
= (signed int)*(SINT32
*)(* p_argv
);
95 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
99 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
108 memcpy(argp
, *p_argv
, z
);
117 /* Perform machine dependent cif processing */
118 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
120 /* Set the return type flag */
121 switch (cif
->rtype
->type
)
124 case FFI_TYPE_STRUCT
:
125 case FFI_TYPE_SINT64
:
127 case FFI_TYPE_DOUBLE
:
128 case FFI_TYPE_LONGDOUBLE
:
129 cif
->flags
= (unsigned) cif
->rtype
->type
;
132 case FFI_TYPE_UINT64
:
133 cif
->flags
= FFI_TYPE_SINT64
;
137 cif
->flags
= FFI_TYPE_INT
;
146 extern void ffi_call_SYSV(void (*)(char *, extended_cif
*),
147 /*@out@*/ extended_cif
*,
149 /*@out@*/ unsigned *,
157 extern void ffi_call_STDCALL(void (*)(char *, extended_cif
*),
158 /*@out@*/ extended_cif
*,
160 /*@out@*/ unsigned *,
164 #endif /* X86_WIN32 */
166 void ffi_call(/*@dependent@*/ ffi_cif
*cif
,
168 /*@out@*/ void *rvalue
,
169 /*@dependent@*/ void **avalue
)
174 ecif
.avalue
= avalue
;
176 /* If the return value is a struct and we don't have a return */
177 /* value address then we need to make one */
179 if ((rvalue
== NULL
) &&
180 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
183 ecif
.rvalue
= alloca(cif
->rtype
->size
);
187 ecif
.rvalue
= rvalue
;
194 ffi_call_SYSV(ffi_prep_args
, &ecif
, cif
->bytes
,
195 cif
->flags
, ecif
.rvalue
, fn
);
201 ffi_call_STDCALL(ffi_prep_args
, &ecif
, cif
->bytes
,
202 cif
->flags
, ecif
.rvalue
, fn
);
205 #endif /* X86_WIN32 */
213 /** private members **/
215 static void ffi_prep_incoming_args_SYSV (char *stack
, void **ret
,
216 void** args
, ffi_cif
* cif
);
217 static void ffi_closure_SYSV (ffi_closure
*)
218 __attribute__ ((regparm(1)));
219 static void ffi_closure_raw_SYSV (ffi_raw_closure
*)
220 __attribute__ ((regparm(1)));
222 /* This function is jumped to by the trampoline */
225 ffi_closure_SYSV (closure
)
226 ffi_closure
*closure
;
228 // this is our return value storage
231 // our various things...
234 unsigned short rtype
;
235 void *resp
= (void*)&res
;
236 void *args
= __builtin_dwarf_cfa ();
239 arg_area
= (void**) alloca (cif
->nargs
* sizeof (void*));
241 /* this call will initialize ARG_AREA, such that each
242 * element in that array points to the corresponding
243 * value on the stack; and if the function returns
244 * a structure, it will re-set RESP to point to the
245 * structure return address. */
247 ffi_prep_incoming_args_SYSV(args
, (void**)&resp
, arg_area
, cif
);
249 (closure
->fun
) (cif
, resp
, arg_area
, closure
->user_data
);
253 /* now, do a generic return based on the value of rtype */
254 if (rtype
== FFI_TYPE_INT
)
256 asm ("movl (%0),%%eax" : : "r" (resp
) : "eax");
258 else if (rtype
== FFI_TYPE_FLOAT
)
260 asm ("flds (%0)" : : "r" (resp
) : "st" );
262 else if (rtype
== FFI_TYPE_DOUBLE
)
264 asm ("fldl (%0)" : : "r" (resp
) : "st", "st(1)" );
266 else if (rtype
== FFI_TYPE_LONGDOUBLE
)
268 asm ("fldt (%0)" : : "r" (resp
) : "st", "st(1)" );
270 else if (rtype
== FFI_TYPE_SINT64
)
272 asm ("movl 0(%0),%%eax;"
281 ffi_prep_incoming_args_SYSV(char *stack
, void **rvalue
,
282 void **avalue
, ffi_cif
*cif
)
285 register unsigned int i
;
286 register void **p_argv
;
288 register ffi_type
**p_arg
;
292 if ( cif
->rtype
->type
== FFI_TYPE_STRUCT
) {
293 *rvalue
= *(void **) argp
;
299 for (i
= cif
->nargs
, p_arg
= cif
->arg_types
; (i
!= 0); i
--, p_arg
++)
303 /* Align if necessary */
304 if ((sizeof(int) - 1) & (unsigned) argp
) {
305 argp
= (char *) ALIGN(argp
, sizeof(int));
310 /* because we're little endian, this is what it turns into. */
312 *p_argv
= (void*) argp
;
321 /* How to make a trampoline. Derived from gcc/config/i386/i386.c. */
323 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
324 ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
325 unsigned int __fun = (unsigned int)(FUN); \
326 unsigned int __ctx = (unsigned int)(CTX); \
327 unsigned int __dis = __fun - ((unsigned int) __tramp + FFI_TRAMPOLINE_SIZE); \
328 *(unsigned char*) &__tramp[0] = 0xb8; \
329 *(unsigned int*) &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
330 *(unsigned char *) &__tramp[5] = 0xe9; \
331 *(unsigned int*) &__tramp[6] = __dis; /* jmp __fun */ \
335 /* the cif must already be prep'ed */
338 ffi_prep_closure (ffi_closure
* closure
,
340 void (*fun
)(ffi_cif
*,void*,void**,void*),
343 FFI_ASSERT (cif
->abi
== FFI_SYSV
);
345 FFI_INIT_TRAMPOLINE (&closure
->tramp
[0], \
350 closure
->user_data
= user_data
;
356 /* ------- Native raw API support -------------------------------- */
361 ffi_closure_raw_SYSV (closure
)
362 ffi_raw_closure
*closure
;
364 // this is our return value storage
367 // our various things...
370 unsigned short rtype
;
371 void *resp
= (void*)&res
;
376 /* the SYSV/X86 abi matches the RAW API exactly, well.. almost */
377 raw_args
= (ffi_raw
*) __builtin_dwarf_cfa ();
379 (closure
->fun
) (cif
, resp
, raw_args
, closure
->user_data
);
383 /* now, do a generic return based on the value of rtype */
384 if (rtype
== FFI_TYPE_INT
)
386 asm ("movl (%0),%%eax" : : "r" (resp
) : "eax");
388 else if (rtype
== FFI_TYPE_FLOAT
)
390 asm ("flds (%0)" : : "r" (resp
) : "st" );
392 else if (rtype
== FFI_TYPE_DOUBLE
)
394 asm ("fldl (%0)" : : "r" (resp
) : "st", "st(1)" );
396 else if (rtype
== FFI_TYPE_LONGDOUBLE
)
398 asm ("fldt (%0)" : : "r" (resp
) : "st", "st(1)" );
400 else if (rtype
== FFI_TYPE_SINT64
)
402 asm ("movl 0(%0),%%eax; movl 4(%0),%%edx"
412 ffi_prep_raw_closure (ffi_raw_closure
* closure
,
414 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
419 FFI_ASSERT (cif
->abi
== FFI_SYSV
);
421 // we currently don't support certain kinds of arguments for raw
422 // closures. This should be implemented by a separate assembly language
423 // routine, since it would require argument processing, something we
424 // don't do now for performance.
426 for (i
= cif
->nargs
-1; i
>= 0; i
--)
428 FFI_ASSERT (cif
->arg_types
[i
]->type
!= FFI_TYPE_STRUCT
);
429 FFI_ASSERT (cif
->arg_types
[i
]->type
!= FFI_TYPE_LONGDOUBLE
);
433 FFI_INIT_TRAMPOLINE (&closure
->tramp
[0], &ffi_closure_raw_SYSV
,
437 closure
->user_data
= user_data
;
444 ffi_prep_args_raw(char *stack
, extended_cif
*ecif
)
446 memcpy (stack
, ecif
->avalue
, ecif
->cif
->bytes
);
449 /* we borrow this routine from libffi (it must be changed, though, to
450 * actually call the function passed in the first argument. as of
451 * libffi-1.20, this is not the case.)
455 ffi_call_SYSV(void (*)(char *, extended_cif
*),
456 /*@out@*/ extended_cif
*,
458 /*@out@*/ unsigned *,
463 ffi_call_STDCALL(void (*)(char *, extended_cif
*),
464 /*@out@*/ extended_cif
*,
466 /*@out@*/ unsigned *,
468 #endif /* X86_WIN32 */
471 ffi_raw_call(/*@dependent@*/ ffi_cif
*cif
,
473 /*@out@*/ void *rvalue
,
474 /*@dependent@*/ ffi_raw
*fake_avalue
)
477 void **avalue
= (void **)fake_avalue
;
480 ecif
.avalue
= avalue
;
482 /* If the return value is a struct and we don't have a return */
483 /* value address then we need to make one */
485 if ((rvalue
== NULL
) &&
486 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
489 ecif
.rvalue
= alloca(cif
->rtype
->size
);
493 ecif
.rvalue
= rvalue
;
500 ffi_call_SYSV(ffi_prep_args_raw
, &ecif
, cif
->bytes
,
501 cif
->flags
, ecif
.rvalue
, fn
);
507 ffi_call_STDCALL(ffi_prep_args_raw
, &ecif
, cif
->bytes
,
508 cif
->flags
, ecif
.rvalue
, fn
);
511 #endif /* X86_WIN32 */
520 #endif /* __x86_64__ */