1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Red Hat, Inc.
4 ARM 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 */
34 void ffi_prep_args(char *stack
, extended_cif
*ecif
)
36 register unsigned int i
;
37 register void **p_argv
;
39 register ffi_type
**p_arg
;
43 if ( ecif
->cif
->flags
== FFI_TYPE_STRUCT
) {
44 *(void **) argp
= ecif
->rvalue
;
48 p_argv
= ecif
->avalue
;
50 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
56 /* Align if necessary */
57 if (((*p_arg
)->alignment
- 1) & (unsigned) argp
) {
58 argp
= (char *) ALIGN(argp
, (*p_arg
)->alignment
);
61 if ((*p_arg
)->type
== FFI_TYPE_STRUCT
)
62 argp
= (char *) ALIGN(argp
, 4);
68 switch ((*p_arg
)->type
)
71 *(signed int *) argp
= (signed int)*(SINT8
*)(* p_argv
);
75 *(unsigned int *) argp
= (unsigned int)*(UINT8
*)(* p_argv
);
79 *(signed int *) argp
= (signed int)*(SINT16
*)(* p_argv
);
83 *(unsigned int *) argp
= (unsigned int)*(UINT16
*)(* p_argv
);
87 memcpy(argp
, *p_argv
, (*p_arg
)->size
);
94 else if (z
== sizeof(int))
96 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
100 memcpy(argp
, *p_argv
, z
);
109 /* Perform machine dependent cif processing */
110 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
112 /* Round the stack up to a multiple of 8 bytes. This isn't needed
113 everywhere, but it is on some platforms, and it doesn't harm anything
114 when it isn't needed. */
115 cif
->bytes
= (cif
->bytes
+ 7) & ~7;
117 /* Set the return type flag */
118 switch (cif
->rtype
->type
)
122 case FFI_TYPE_DOUBLE
:
123 cif
->flags
= (unsigned) cif
->rtype
->type
;
126 case FFI_TYPE_SINT64
:
127 case FFI_TYPE_UINT64
:
128 cif
->flags
= (unsigned) FFI_TYPE_SINT64
;
131 case FFI_TYPE_STRUCT
:
132 if (cif
->rtype
->size
<= 4)
133 /* A Composite Type not larger than 4 bytes is returned in r0. */
134 cif
->flags
= (unsigned)FFI_TYPE_INT
;
136 /* A Composite Type larger than 4 bytes, or whose size cannot
137 be determined statically ... is stored in memory at an
138 address passed [in r0]. */
139 cif
->flags
= (unsigned)FFI_TYPE_STRUCT
;
143 cif
->flags
= FFI_TYPE_INT
;
150 extern void ffi_call_SYSV(void (*)(char *, extended_cif
*), extended_cif
*,
151 unsigned, unsigned, unsigned *, void (*fn
)());
153 void ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
157 int small_struct
= (cif
->flags
== FFI_TYPE_INT
158 && cif
->rtype
->type
== FFI_TYPE_STRUCT
);
161 ecif
.avalue
= avalue
;
165 /* If the return value is a struct and we don't have a return */
166 /* value address then we need to make one */
168 if ((rvalue
== NULL
) &&
169 (cif
->flags
== FFI_TYPE_STRUCT
))
171 ecif
.rvalue
= alloca(cif
->rtype
->size
);
173 else if (small_struct
)
176 ecif
.rvalue
= rvalue
;
181 ffi_call_SYSV(ffi_prep_args
, &ecif
, cif
->bytes
, cif
->flags
, ecif
.rvalue
,
190 memcpy (rvalue
, &temp
, cif
->rtype
->size
);
193 /** private members **/
195 static void ffi_prep_incoming_args_SYSV (char *stack
, void **ret
,
196 void** args
, ffi_cif
* cif
);
198 void ffi_closure_SYSV (ffi_closure
*);
200 /* This function is jumped to by the trampoline */
203 ffi_closure_SYSV_inner (closure
, respp
, args
)
204 ffi_closure
*closure
;
208 // our various things...
213 arg_area
= (void**) alloca (cif
->nargs
* sizeof (void*));
215 /* this call will initialize ARG_AREA, such that each
216 * element in that array points to the corresponding
217 * value on the stack; and if the function returns
218 * a structure, it will re-set RESP to point to the
219 * structure return address. */
221 ffi_prep_incoming_args_SYSV(args
, respp
, arg_area
, cif
);
223 (closure
->fun
) (cif
, *respp
, arg_area
, closure
->user_data
);
230 ffi_prep_incoming_args_SYSV(char *stack
, void **rvalue
,
231 void **avalue
, ffi_cif
*cif
)
234 register unsigned int i
;
235 register void **p_argv
;
237 register ffi_type
**p_arg
;
241 if ( cif
->flags
== FFI_TYPE_STRUCT
) {
242 *rvalue
= *(void **) argp
;
248 for (i
= cif
->nargs
, p_arg
= cif
->arg_types
; (i
!= 0); i
--, p_arg
++)
252 size_t alignment
= (*p_arg
)->alignment
;
255 /* Align if necessary */
256 if ((alignment
- 1) & (unsigned) argp
) {
257 argp
= (char *) ALIGN(argp
, alignment
);
262 /* because we're little endian, this is what it turns into. */
264 *p_argv
= (void*) argp
;
273 /* How to make a trampoline. */
275 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
276 ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
277 unsigned int __fun = (unsigned int)(FUN); \
278 unsigned int __ctx = (unsigned int)(CTX); \
279 *(unsigned int*) &__tramp[0] = 0xe92d000f; /* stmfd sp!, {r0-r3} */ \
280 *(unsigned int*) &__tramp[4] = 0xe59f0000; /* ldr r0, [pc] */ \
281 *(unsigned int*) &__tramp[8] = 0xe59ff000; /* ldr pc, [pc] */ \
282 *(unsigned int*) &__tramp[12] = __ctx; \
283 *(unsigned int*) &__tramp[16] = __fun; \
284 __clear_cache((&__tramp[0]), (&__tramp[19])); \
288 /* the cif must already be prep'ed */
291 ffi_prep_closure_loc (ffi_closure
* closure
,
293 void (*fun
)(ffi_cif
*,void*,void**,void*),
297 FFI_ASSERT (cif
->abi
== FFI_SYSV
);
299 FFI_INIT_TRAMPOLINE (&closure
->tramp
[0], \
304 closure
->user_data
= user_data
;