1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2002 Kaz Kojima
4 SuperH 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>
36 #if defined(__HITACHI__)
37 #define STRUCT_VALUE_ADDRESS_WITH_ARG 1
39 #define STRUCT_VALUE_ADDRESS_WITH_ARG 0
42 /* If the structure has essentialy an unique element, return its type. */
44 simple_type (ffi_type *arg)
46 if (arg->type != FFI_TYPE_STRUCT)
48 else if (arg->elements[1])
49 return FFI_TYPE_STRUCT;
51 return simple_type (arg->elements[0]);
55 return_type (ffi_type *arg)
59 if (arg->type != FFI_TYPE_STRUCT)
62 type = simple_type (arg->elements[0]);
63 if (! arg->elements[1])
80 /* gcc uses r0/r1 pair for some kind of structures. */
81 if (arg->size <= 2 * sizeof (int))
86 while ((e = arg->elements[i++]))
88 type = simple_type (e);
95 return FFI_TYPE_UINT64;
103 return FFI_TYPE_STRUCT;
106 /* ffi_prep_args is called by the assembly routine once stack space
107 has been allocated for the function's arguments */
110 void ffi_prep_args(char *stack, extended_cif *ecif)
113 register unsigned int i;
115 register unsigned int avn;
116 register void **p_argv;
118 register ffi_type **p_arg;
127 if (return_type (ecif->cif->rtype) == FFI_TYPE_STRUCT)
129 *(void **) argp = ecif->rvalue;
131 ireg = STRUCT_VALUE_ADDRESS_WITH_ARG ? 1 : 0;
136 /* Set arguments for registers. */
138 avn = ecif->cif->nargs;
139 p_argv = ecif->avalue;
141 for (i = 0, p_arg = ecif->cif->arg_types; i < avn; i++, p_arg++, p_argv++)
148 if (greg++ >= NGREGARG)
152 switch ((*p_arg)->type)
155 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
159 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
162 case FFI_TYPE_SINT16:
163 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
166 case FFI_TYPE_UINT16:
167 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
170 case FFI_TYPE_STRUCT:
171 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
179 else if (z == sizeof(int))
182 if ((*p_arg)->type == FFI_TYPE_FLOAT)
184 if (freg++ >= NFREGARG)
190 if (greg++ >= NGREGARG)
193 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
197 else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
199 if (freg + 1 >= NFREGARG)
201 freg = (freg + 1) & ~1;
203 memcpy (argp, *p_argv, z);
209 int n = (z + sizeof (int) - 1) / sizeof (int);
211 if (greg + n - 1 >= NGREGARG)
215 if (greg >= NGREGARG)
217 else if (greg + n - 1 >= NGREGARG)
222 memcpy (argp, *p_argv, z);
227 /* Set arguments on stack. */
232 p_argv = ecif->avalue;
234 for (i = 0, p_arg = ecif->cif->arg_types; i < avn; i++, p_arg++, p_argv++)
241 if (greg++ < NGREGARG)
245 switch ((*p_arg)->type)
248 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
252 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
255 case FFI_TYPE_SINT16:
256 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
259 case FFI_TYPE_UINT16:
260 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
263 case FFI_TYPE_STRUCT:
264 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
272 else if (z == sizeof(int))
275 if ((*p_arg)->type == FFI_TYPE_FLOAT)
277 if (freg++ < NFREGARG)
283 if (greg++ < NGREGARG)
286 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
290 else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
292 if (freg + 1 < NFREGARG)
294 freg = (freg + 1) & ~1;
298 memcpy (argp, *p_argv, z);
304 int n = (z + sizeof (int) - 1) / sizeof (int);
305 if (greg + n - 1 < NGREGARG)
310 #if (! defined(__SH4__))
311 else if (greg < NGREGARG)
317 memcpy (argp, *p_argv, z);
325 /* Perform machine dependent cif processing */
326 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
338 greg = ((return_type (cif->rtype) == FFI_TYPE_STRUCT) &&
339 STRUCT_VALUE_ADDRESS_WITH_ARG) ? 1 : 0;
342 for (i = j = 0; i < cif->nargs && j < 12; i++)
344 type = (cif->arg_types)[i]->type;
348 if (freg >= NFREGARG)
351 cif->flags += ((cif->arg_types)[i]->type) << (2 * j);
355 case FFI_TYPE_DOUBLE:
356 if ((freg + 1) >= NFREGARG)
358 freg = (freg + 1) & ~1;
360 cif->flags += ((cif->arg_types)[i]->type) << (2 * j);
365 size = (cif->arg_types)[i]->size;
366 n = (size + sizeof (int) - 1) / sizeof (int);
367 if (greg + n - 1 >= NGREGARG)
370 for (m = 0; m < n; m++)
371 cif->flags += FFI_TYPE_INT << (2 * j++);
376 for (i = j = 0; i < cif->nargs && j < 4; i++)
378 size = (cif->arg_types)[i]->size;
379 n = (size + sizeof (int) - 1) / sizeof (int);
380 if (greg >= NGREGARG)
382 else if (greg + n - 1 >= NGREGARG)
386 for (m = 0; m < n; m++)
387 cif->flags += FFI_TYPE_INT << (2 * j++);
391 /* Set the return type flag */
392 switch (cif->rtype->type)
394 case FFI_TYPE_STRUCT:
395 cif->flags += (unsigned) (return_type (cif->rtype)) << 24;
400 case FFI_TYPE_DOUBLE:
401 case FFI_TYPE_SINT64:
402 case FFI_TYPE_UINT64:
403 cif->flags += (unsigned) cif->rtype->type << 24;
407 cif->flags += FFI_TYPE_INT << 24;
416 extern void ffi_call_SYSV(void (*)(char *, extended_cif *),
417 /*@out@*/ extended_cif *,
419 /*@out@*/ unsigned *,
424 void ffi_call(/*@dependent@*/ ffi_cif *cif,
426 /*@out@*/ void *rvalue,
427 /*@dependent@*/ void **avalue)
432 ecif.avalue = avalue;
434 /* If the return value is a struct and we don't have a return */
435 /* value address then we need to make one */
437 if ((rvalue == NULL) &&
438 (cif->rtype->type == FFI_TYPE_STRUCT))
441 ecif.rvalue = alloca(cif->rtype->size);
445 ecif.rvalue = rvalue;
452 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes,
453 cif->flags, ecif.rvalue, fn);
462 extern void ffi_closure_SYSV (void);
464 extern void __ic_invalidate (void *line);
468 ffi_prep_closure (ffi_closure* closure,
470 void (*fun)(ffi_cif*, void*, void**, void*),
475 FFI_ASSERT (cif->abi == FFI_GCC_SYSV);
477 tramp = (unsigned int *) &closure->tramp[0];
478 #ifdef __LITTLE_ENDIAN__
479 tramp[0] = 0xd301d202;
480 tramp[1] = 0x0009422b;
482 tramp[0] = 0xd202d301;
483 tramp[1] = 0x422b0009;
485 *(void **) &tramp[2] = (void *)closure; /* ctx */
486 *(void **) &tramp[3] = (void *)ffi_closure_SYSV; /* funaddr */
490 closure->user_data = user_data;
493 /* Flush the icache. */
494 __ic_invalidate(&closure->tramp[0]);
500 /* Basically the trampoline invokes ffi_closure_SYSV, and on
501 * entry, r3 holds the address of the closure.
502 * After storing the registers that could possibly contain
503 * parameters to be passed into the stack frame and setting
504 * up space for a return value, ffi_closure_SYSV invokes the
505 * following helper function to do most of the work.
508 #ifdef __LITTLE_ENDIAN__
517 ffi_closure_helper_SYSV (ffi_closure *closure, void *rvalue,
518 unsigned long *pgr, unsigned long *pfr,
532 avalue = alloca(cif->nargs * sizeof(void *));
534 /* Copy the caller's structure return value address so that the closure
535 returns the data directly to the caller. */
536 if (cif->rtype->type == FFI_TYPE_STRUCT)
539 ireg = STRUCT_VALUE_ADDRESS_WITH_ARG ? 1 : 0;
548 /* Grab the addresses of the arguments from the stack frame. */
549 for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
556 if (greg++ >= NGREGARG)
560 switch ((*p_arg)->type)
564 avalue[i] = (((char *)pgr) + OFS_INT8);
567 case FFI_TYPE_SINT16:
568 case FFI_TYPE_UINT16:
569 avalue[i] = (((char *)pgr) + OFS_INT16);
572 case FFI_TYPE_STRUCT:
581 else if (z == sizeof(int))
584 if ((*p_arg)->type == FFI_TYPE_FLOAT)
586 if (freg++ >= NFREGARG)
594 if (greg++ >= NGREGARG)
601 else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
603 if (freg + 1 >= NFREGARG)
605 freg = (freg + 1) & ~1;
613 int n = (z + sizeof (int) - 1) / sizeof (int);
615 if (greg + n - 1 >= NGREGARG)
619 if (greg >= NGREGARG)
621 else if (greg + n - 1 >= NGREGARG)
636 for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
643 if (greg++ < NGREGARG)
647 switch ((*p_arg)->type)
651 avalue[i] = (((char *)pst) + OFS_INT8);
654 case FFI_TYPE_SINT16:
655 case FFI_TYPE_UINT16:
656 avalue[i] = (((char *)pst) + OFS_INT16);
659 case FFI_TYPE_STRUCT:
668 else if (z == sizeof(int))
671 if ((*p_arg)->type == FFI_TYPE_FLOAT)
673 if (freg++ < NFREGARG)
679 if (greg++ < NGREGARG)
686 else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
688 if (freg + 1 < NFREGARG)
690 freg = (freg + 1) & ~1;
700 int n = (z + sizeof (int) - 1) / sizeof (int);
701 if (greg + n - 1 < NGREGARG)
706 #if (! defined(__SH4__))
707 else if (greg < NGREGARG)
718 (closure->fun) (cif, rvalue, avalue, closure->user_data);
720 /* Tell ffi_closure_osf how to perform return type promotions. */
721 return cif->rtype->type;