1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 1998, 1999 Cygnus Solutions
4 x86 Foreign Function Interface
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 ----------------------------------------------------------------------- */
27 #include <ffi_common.h>
31 /* ffi_prep_args is called by the assembly routine once stack space
32 has been allocated for the function's arguments */
35 void ffi_prep_args(char *stack
, extended_cif
*ecif
)
38 register unsigned int i
;
40 register unsigned int avn
;
41 register void **p_argv
;
43 register ffi_type
**p_arg
;
48 if ( ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
) {
49 *(void **) argp
= ecif
->rvalue
;
53 avn
= ecif
->cif
->nargs
;
54 p_argv
= ecif
->avalue
;
56 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
57 (i
!= 0) && (avn
!= 0);
62 /* Align if necessary */
63 if (((*p_arg
)->alignment
- 1) & (unsigned) argp
) {
64 argp
= (char *) ALIGN(argp
, (*p_arg
)->alignment
);
74 switch ((*p_arg
)->type
)
77 *(signed int *) argp
= (signed int)*(SINT8
*)(* p_argv
);
81 *(unsigned int *) argp
= (unsigned int)*(UINT8
*)(* p_argv
);
85 *(signed int *) argp
= (signed int)*(SINT16
*)(* p_argv
);
89 *(unsigned int *) argp
= (unsigned int)*(UINT16
*)(* p_argv
);
93 *(signed int *) argp
= (signed int)*(SINT32
*)(* p_argv
);
97 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
100 case FFI_TYPE_STRUCT
:
101 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
110 memcpy(argp
, *p_argv
, z
);
120 /* Perform machine dependent cif processing */
121 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
123 /* Set the return type flag */
124 switch (cif
->rtype
->type
)
127 case FFI_TYPE_STRUCT
:
128 case FFI_TYPE_SINT64
:
130 case FFI_TYPE_DOUBLE
:
131 case FFI_TYPE_LONGDOUBLE
:
132 cif
->flags
= (unsigned) cif
->rtype
->type
;
135 case FFI_TYPE_UINT64
:
136 cif
->flags
= FFI_TYPE_SINT64
;
140 cif
->flags
= FFI_TYPE_INT
;
149 extern void ffi_call_SYSV(void (*)(char *, extended_cif
*),
150 /*@out@*/ extended_cif
*,
152 /*@out@*/ unsigned *,
157 void ffi_call(/*@dependent@*/ ffi_cif
*cif
,
159 /*@out@*/ void *rvalue
,
160 /*@dependent@*/ void **avalue
)
165 ecif
.avalue
= avalue
;
167 /* If the return value is a struct and we don't have a return */
168 /* value address then we need to make one */
170 if ((rvalue
== NULL
) &&
171 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
174 ecif
.rvalue
= alloca(cif
->rtype
->size
);
178 ecif
.rvalue
= rvalue
;
185 ffi_call_SYSV(ffi_prep_args
, &ecif
, cif
->bytes
,
186 cif
->flags
, ecif
.rvalue
, fn
);
196 /** private members **/
198 static void ffi_prep_incoming_args_SYSV (char *stack
, void **ret
,
199 void** args
, ffi_cif
* cif
);
200 static void ffi_closure_SYSV ();
201 static void ffi_closure_raw_SYSV ();
203 /* This function is jumped to by the trampoline, on entry, %ecx (a
204 * caller-save register) holds the address of the closure.
205 * Clearly, this requires __GNUC__, so perhaps we should translate this
206 * into an assembly file if this is to be distributed with ffi.
212 // this is our return value storage
215 // our various things...
219 ffi_closure
*closure
;
220 unsigned short rtype
;
221 void *resp
= (void*)&res
;
223 /* grab the trampoline context pointer */
224 asm ("movl %%ecx,%0" : "=r" (closure
));
227 arg_area
= (void**) alloca (cif
->nargs
* sizeof (void*));
228 asm ("leal 8(%%ebp),%0" : "=q" (args
));
230 /* this call will initialize ARG_AREA, such that each
231 * element in that array points to the corresponding
232 * value on the stack; and if the function returns
233 * a structure, it will re-set RESP to point to the
234 * structure return address. */
236 ffi_prep_incoming_args_SYSV(args
, (void**)&resp
, arg_area
, cif
);
238 (closure
->fun
) (cif
, resp
, arg_area
, closure
->user_data
);
242 /* now, do a generic return based on the value of rtype */
243 if (rtype
== FFI_TYPE_INT
)
245 asm ("movl (%0),%%eax" : : "r" (resp
) : "eax");
247 else if (rtype
== FFI_TYPE_FLOAT
)
249 asm ("flds (%0)" : : "r" (resp
) : "st" );
251 else if (rtype
== FFI_TYPE_DOUBLE
)
253 asm ("fldl (%0)" : : "r" (resp
) : "st", "st(1)" );
255 else if (rtype
== FFI_TYPE_LONGDOUBLE
)
257 asm ("fldt (%0)" : : "r" (resp
) : "st", "st(1)" );
259 else if (rtype
== FFI_TYPE_SINT64
)
261 asm ("movl 0(%0),%%eax;"
270 ffi_prep_incoming_args_SYSV(char *stack
, void **rvalue
,
271 void **avalue
, ffi_cif
*cif
)
274 register unsigned int i
;
276 register unsigned int avn
;
277 register void **p_argv
;
279 register ffi_type
**p_arg
;
284 if ( cif
->rtype
->type
== FFI_TYPE_STRUCT
) {
285 *rvalue
= *(void **) argp
;
292 for (i
= cif
->nargs
, p_arg
= cif
->arg_types
;
293 (i
!= 0) && (avn
!= 0);
298 /* Align if necessary */
299 if (((*p_arg
)->alignment
- 1) & (unsigned) argp
) {
300 argp
= (char *) ALIGN(argp
, (*p_arg
)->alignment
);
308 /* because we're little endian, this is
309 what it turns into. */
311 *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 + 10); \
328 *(unsigned char*) &__tramp[0] = 0xb9; \
329 *(unsigned int*) &__tramp[1] = __ctx; \
330 *(unsigned char*) &__tramp[5] = 0xe9; \
331 *(unsigned int*) &__tramp[6] = __dis; \
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 ()
363 // this is our return value storage
366 // our various things...
370 ffi_raw_closure
*closure
;
371 unsigned short rtype
;
372 void *resp
= (void*)&res
;
374 /* grab the trampoline context pointer */
375 asm ("movl %%ecx,%0" : "=r" (closure
));
377 /* take the argument pointer */
378 asm ("leal 8(%%ebp),%0" : "=q" (args
));
383 /* the SYSV/X86 abi matches the RAW API exactly, well.. almost */
384 raw_args
= (ffi_raw
*) args
;
386 (closure
->fun
) (cif
, resp
, raw_args
, closure
->user_data
);
390 /* now, do a generic return based on the value of rtype */
391 if (rtype
== FFI_TYPE_INT
)
393 asm ("movl (%0),%%eax" : : "r" (resp
) : "eax");
395 else if (rtype
== FFI_TYPE_FLOAT
)
397 asm ("flds (%0)" : : "r" (resp
) : "st" );
399 else if (rtype
== FFI_TYPE_DOUBLE
)
401 asm ("fldl (%0)" : : "r" (resp
) : "st", "st(1)" );
403 else if (rtype
== FFI_TYPE_LONGDOUBLE
)
405 asm ("fldt (%0)" : : "r" (resp
) : "st", "st(1)" );
407 else if (rtype
== FFI_TYPE_SINT64
)
409 asm ("movl 0(%0),%%eax; movl 4(%0),%%edx"
419 ffi_prep_raw_closure (ffi_raw_closure
* closure
,
421 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
426 FFI_ASSERT (cif
->abi
== FFI_SYSV
);
428 // we currently don't support certain kinds of arguments for raw
429 // closures. This should be implemented by a separate assembly language
430 // routine, since it would require argument processing, something we
431 // don't do now for performance.
433 for (i
= cif
->nargs
-1; i
>= 0; i
--)
435 FFI_ASSERT (cif
->arg_types
[i
]->type
!= FFI_TYPE_STRUCT
);
436 FFI_ASSERT (cif
->arg_types
[i
]->type
!= FFI_TYPE_LONGDOUBLE
);
440 FFI_INIT_TRAMPOLINE (&closure
->tramp
[0], &ffi_closure_raw_SYSV
,
444 closure
->user_data
= user_data
;
451 ffi_prep_args_raw(char *stack
, extended_cif
*ecif
)
453 memcpy (stack
, ecif
->avalue
, ecif
->cif
->bytes
);
456 /* we borrow this routine from libffi (it must be changed, though, to
457 * actually call the function passed in the first argument. as of
458 * libffi-1.20, this is not the case.)
462 ffi_call_SYSV(void (*)(char *, extended_cif
*),
463 /*@out@*/ extended_cif
*,
465 /*@out@*/ unsigned *,
469 ffi_raw_call(/*@dependent@*/ ffi_cif
*cif
,
471 /*@out@*/ void *rvalue
,
472 /*@dependent@*/ ffi_raw
*fake_avalue
)
475 void **avalue
= (void **)fake_avalue
;
478 ecif
.avalue
= avalue
;
480 /* If the return value is a struct and we don't have a return */
481 /* value address then we need to make one */
483 if ((rvalue
== NULL
) &&
484 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
487 ecif
.rvalue
= alloca(cif
->rtype
->size
);
491 ecif
.rvalue
= rvalue
;
498 ffi_call_SYSV(ffi_prep_args_raw
, &ecif
, cif
->bytes
,
499 cif
->flags
, ecif
.rvalue
, fn
);