1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998, 2008 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,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
28 #include <ffi_common.h>
32 /* Forward declares. */
33 static int vfp_type_p (ffi_type *);
34 static void layout_vfp_args (ffi_cif *);
36 /* ffi_prep_args is called by the assembly routine once stack space
37 has been allocated for the function's arguments
39 The vfp_space parameter is the load area for VFP regs, the return
40 value is cif->vfp_used (word bitset of VFP regs used for passing
41 arguments). These are only used for the VFP hard-float ABI.
43 int ffi_prep_args(char *stack, extended_cif *ecif, float *vfp_space)
45 register unsigned int i, vi = 0;
46 register void **p_argv;
48 register ffi_type **p_arg;
52 if ( ecif->cif->flags == FFI_TYPE_STRUCT ) {
53 *(void **) argp = ecif->rvalue;
57 p_argv = ecif->avalue;
59 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
65 /* Allocated in VFP registers. */
66 if (ecif->cif->abi == FFI_VFP
67 && vi < ecif->cif->vfp_nargs && vfp_type_p (*p_arg))
69 float* vfp_slot = vfp_space + ecif->cif->vfp_args[vi++];
70 if ((*p_arg)->type == FFI_TYPE_FLOAT)
71 *((float*)vfp_slot) = *((float*)*p_argv);
72 else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
73 *((double*)vfp_slot) = *((double*)*p_argv);
75 memcpy(vfp_slot, *p_argv, (*p_arg)->size);
80 /* Align if necessary */
81 if (((*p_arg)->alignment - 1) & (unsigned) argp) {
82 argp = (char *) ALIGN(argp, (*p_arg)->alignment);
85 if ((*p_arg)->type == FFI_TYPE_STRUCT)
86 argp = (char *) ALIGN(argp, 4);
92 switch ((*p_arg)->type)
95 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
99 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
102 case FFI_TYPE_SINT16:
103 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
106 case FFI_TYPE_UINT16:
107 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
110 case FFI_TYPE_STRUCT:
111 memcpy(argp, *p_argv, (*p_arg)->size);
118 else if (z == sizeof(int))
120 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
124 memcpy(argp, *p_argv, z);
130 /* Indicate the VFP registers used. */
131 return ecif->cif->vfp_used;
134 /* Perform machine dependent cif processing */
135 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
138 /* Round the stack up to a multiple of 8 bytes. This isn't needed
139 everywhere, but it is on some platforms, and it doesn't harm anything
140 when it isn't needed. */
141 cif->bytes = (cif->bytes + 7) & ~7;
143 /* Set the return type flag */
144 switch (cif->rtype->type)
148 case FFI_TYPE_DOUBLE:
149 cif->flags = (unsigned) cif->rtype->type;
152 case FFI_TYPE_SINT64:
153 case FFI_TYPE_UINT64:
154 cif->flags = (unsigned) FFI_TYPE_SINT64;
157 case FFI_TYPE_STRUCT:
158 if (cif->abi == FFI_VFP
159 && (type_code = vfp_type_p (cif->rtype)) != 0)
161 /* A Composite Type passed in VFP registers, either
162 FFI_TYPE_STRUCT_VFP_FLOAT or FFI_TYPE_STRUCT_VFP_DOUBLE. */
163 cif->flags = (unsigned) type_code;
165 else if (cif->rtype->size <= 4)
166 /* A Composite Type not larger than 4 bytes is returned in r0. */
167 cif->flags = (unsigned)FFI_TYPE_INT;
169 /* A Composite Type larger than 4 bytes, or whose size cannot
170 be determined statically ... is stored in memory at an
171 address passed [in r0]. */
172 cif->flags = (unsigned)FFI_TYPE_STRUCT;
176 cif->flags = FFI_TYPE_INT;
180 /* Map out the register placements of VFP register args.
181 The VFP hard-float calling conventions are slightly more sophisticated than
182 the base calling conventions, so we do it here instead of in ffi_prep_args(). */
183 if (cif->abi == FFI_VFP)
184 layout_vfp_args (cif);
189 /* Prototypes for assembly functions, in sysv.S */
190 extern void ffi_call_SYSV (void (*fn)(void), extended_cif *, unsigned, unsigned, unsigned *);
191 extern void ffi_call_VFP (void (*fn)(void), extended_cif *, unsigned, unsigned, unsigned *);
193 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
197 int small_struct = (cif->flags == FFI_TYPE_INT
198 && cif->rtype->type == FFI_TYPE_STRUCT);
199 int vfp_struct = (cif->flags == FFI_TYPE_STRUCT_VFP_FLOAT
200 || cif->flags == FFI_TYPE_STRUCT_VFP_DOUBLE);
203 ecif.avalue = avalue;
207 /* If the return value is a struct and we don't have a return */
208 /* value address then we need to make one */
210 if ((rvalue == NULL) &&
211 (cif->flags == FFI_TYPE_STRUCT))
213 ecif.rvalue = alloca(cif->rtype->size);
215 else if (small_struct)
219 /* Largest case is double x 4. */
220 ecif.rvalue = alloca(32);
223 ecif.rvalue = rvalue;
228 ffi_call_SYSV (fn, &ecif, cif->bytes, cif->flags, ecif.rvalue);
232 ffi_call_VFP (fn, &ecif, cif->bytes, cif->flags, ecif.rvalue);
240 memcpy (rvalue, &temp, cif->rtype->size);
242 memcpy (rvalue, ecif.rvalue, cif->rtype->size);
245 /** private members **/
247 static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
248 void** args, ffi_cif* cif, float *vfp_stack);
250 void ffi_closure_SYSV (ffi_closure *);
252 void ffi_closure_VFP (ffi_closure *);
254 /* This function is jumped to by the trampoline */
257 ffi_closure_SYSV_inner (closure, respp, args, vfp_args)
258 ffi_closure *closure;
263 // our various things...
268 arg_area = (void**) alloca (cif->nargs * sizeof (void*));
270 /* this call will initialize ARG_AREA, such that each
271 * element in that array points to the corresponding
272 * value on the stack; and if the function returns
273 * a structure, it will re-set RESP to point to the
274 * structure return address. */
276 ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif, vfp_args);
278 (closure->fun) (cif, *respp, arg_area, closure->user_data);
285 ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
286 void **avalue, ffi_cif *cif,
287 /* Used only under VFP hard-float ABI. */
291 register unsigned int i, vi = 0;
292 register void **p_argv;
294 register ffi_type **p_arg;
298 if ( cif->flags == FFI_TYPE_STRUCT ) {
299 *rvalue = *(void **) argp;
305 for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
310 if (cif->abi == FFI_VFP
311 && vi < cif->vfp_nargs && vfp_type_p (*p_arg))
313 *p_argv++ = (void*)(vfp_stack + cif->vfp_args[vi++]);
317 alignment = (*p_arg)->alignment;
320 /* Align if necessary */
321 if ((alignment - 1) & (unsigned) argp) {
322 argp = (char *) ALIGN(argp, alignment);
327 /* because we're little endian, this is what it turns into. */
329 *p_argv = (void*) argp;
338 /* How to make a trampoline. */
340 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
341 ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
342 unsigned int __fun = (unsigned int)(FUN); \
343 unsigned int __ctx = (unsigned int)(CTX); \
344 *(unsigned int*) &__tramp[0] = 0xe92d000f; /* stmfd sp!, {r0-r3} */ \
345 *(unsigned int*) &__tramp[4] = 0xe59f0000; /* ldr r0, [pc] */ \
346 *(unsigned int*) &__tramp[8] = 0xe59ff000; /* ldr pc, [pc] */ \
347 *(unsigned int*) &__tramp[12] = __ctx; \
348 *(unsigned int*) &__tramp[16] = __fun; \
349 __clear_cache((&__tramp[0]), (&__tramp[19])); \
353 /* the cif must already be prep'ed */
356 ffi_prep_closure_loc (ffi_closure* closure,
358 void (*fun)(ffi_cif*,void*,void**,void*),
362 void (*closure_func)(ffi_closure*) = NULL;
364 if (cif->abi == FFI_SYSV)
365 closure_func = &ffi_closure_SYSV;
366 else if (cif->abi == FFI_VFP)
367 closure_func = &ffi_closure_VFP;
371 FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
376 closure->user_data = user_data;
382 /* Below are routines for VFP hard-float support. */
384 static int rec_vfp_type_p (ffi_type *t, int *elt, int *elnum)
389 case FFI_TYPE_DOUBLE:
390 *elt = (int) t->type;
394 case FFI_TYPE_STRUCT_VFP_FLOAT:
395 *elt = FFI_TYPE_FLOAT;
396 *elnum = t->size / sizeof (float);
399 case FFI_TYPE_STRUCT_VFP_DOUBLE:
400 *elt = FFI_TYPE_DOUBLE;
401 *elnum = t->size / sizeof (double);
404 case FFI_TYPE_STRUCT:;
406 int base_elt = 0, total_elnum = 0;
407 ffi_type **el = t->elements;
410 int el_elt = 0, el_elnum = 0;
411 if (! rec_vfp_type_p (*el, &el_elt, &el_elnum)
412 || (base_elt && base_elt != el_elt)
413 || total_elnum + el_elnum > 4)
416 total_elnum += el_elnum;
419 *elnum = total_elnum;
428 static int vfp_type_p (ffi_type *t)
431 if (rec_vfp_type_p (t, &elt, &elnum))
433 if (t->type == FFI_TYPE_STRUCT)
438 t->type = (elt == FFI_TYPE_FLOAT
439 ? FFI_TYPE_STRUCT_VFP_FLOAT
440 : FFI_TYPE_STRUCT_VFP_DOUBLE);
442 return (int) t->type;
447 static void place_vfp_arg (ffi_cif *cif, ffi_type *t)
449 int reg = cif->vfp_reg_free;
450 int nregs = t->size / sizeof (float);
451 int align = ((t->type == FFI_TYPE_STRUCT_VFP_FLOAT
452 || t->type == FFI_TYPE_FLOAT) ? 1 : 2);
453 /* Align register number. */
454 if ((reg & 1) && align == 2)
456 while (reg + nregs <= 16)
459 for (s = reg; s < reg + nregs; s++)
461 new_used |= (1 << s);
462 if (cif->vfp_used & (1 << s))
468 /* Found regs to allocate. */
469 cif->vfp_used |= new_used;
470 cif->vfp_args[cif->vfp_nargs++] = reg;
472 /* Update vfp_reg_free. */
473 if (cif->vfp_used & (1 << cif->vfp_reg_free))
476 while (cif->vfp_used & (1 << reg))
478 cif->vfp_reg_free = reg;
485 static void layout_vfp_args (ffi_cif *cif)
488 /* Init VFP fields */
491 cif->vfp_reg_free = 0;
492 memset (cif->vfp_args, -1, 16); /* Init to -1. */
494 for (i = 0; i < cif->nargs; i++)
496 ffi_type *t = cif->arg_types[i];
498 place_vfp_arg (cif, t);