1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2003, 2004, 2006, 2007 Kaz Kojima
3 Copyright (c) 2008 Anthony Green
5 SuperH SHmedia Foreign Function Interface
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 ``Software''), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 ----------------------------------------------------------------------- */
29 #include <ffi_common.h>
37 return_type (ffi_type *arg)
40 if (arg->type != FFI_TYPE_STRUCT)
43 /* gcc uses r2 if the result can be packed in on register. */
44 if (arg->size <= sizeof (UINT8))
45 return FFI_TYPE_UINT8;
46 else if (arg->size <= sizeof (UINT16))
47 return FFI_TYPE_UINT16;
48 else if (arg->size <= sizeof (UINT32))
49 return FFI_TYPE_UINT32;
50 else if (arg->size <= sizeof (UINT64))
51 return FFI_TYPE_UINT64;
53 return FFI_TYPE_STRUCT;
56 /* ffi_prep_args is called by the assembly routine once stack space
57 has been allocated for the function's arguments */
59 void ffi_prep_args(char *stack, extended_cif *ecif)
61 register unsigned int i;
62 register unsigned int avn;
63 register void **p_argv;
65 register ffi_type **p_arg;
69 if (return_type (ecif->cif->rtype) == FFI_TYPE_STRUCT)
71 *(void **) argp = ecif->rvalue;
72 argp += sizeof (UINT64);
75 avn = ecif->cif->nargs;
76 p_argv = ecif->avalue;
78 for (i = 0, p_arg = ecif->cif->arg_types; i < avn; i++, p_arg++, p_argv++)
84 align = (*p_arg)->alignment;
85 if (z < sizeof (UINT32))
87 switch ((*p_arg)->type)
90 *(SINT64 *) argp = (SINT64) *(SINT8 *)(*p_argv);
94 *(UINT64 *) argp = (UINT64) *(UINT8 *)(*p_argv);
98 *(SINT64 *) argp = (SINT64) *(SINT16 *)(*p_argv);
101 case FFI_TYPE_UINT16:
102 *(UINT64 *) argp = (UINT64) *(UINT16 *)(*p_argv);
105 case FFI_TYPE_STRUCT:
106 memcpy (argp, *p_argv, z);
112 argp += sizeof (UINT64);
114 else if (z == sizeof (UINT32) && align == sizeof (UINT32))
116 switch ((*p_arg)->type)
119 case FFI_TYPE_SINT32:
120 *(SINT64 *) argp = (SINT64) *(SINT32 *) (*p_argv);
124 case FFI_TYPE_POINTER:
125 case FFI_TYPE_UINT32:
126 case FFI_TYPE_STRUCT:
127 *(UINT64 *) argp = (UINT64) *(UINT32 *) (*p_argv);
134 argp += sizeof (UINT64);
136 else if (z == sizeof (UINT64)
137 && align == sizeof (UINT64)
138 && ((int) *p_argv & (sizeof (UINT64) - 1)) == 0)
140 *(UINT64 *) argp = *(UINT64 *) (*p_argv);
141 argp += sizeof (UINT64);
145 int n = (z + sizeof (UINT64) - 1) / sizeof (UINT64);
147 memcpy (argp, *p_argv, z);
148 argp += n * sizeof (UINT64);
155 /* Perform machine dependent cif processing */
156 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
165 greg = (return_type (cif->rtype) == FFI_TYPE_STRUCT ? 1 : 0);
169 for (i = j = 0; i < cif->nargs; i++)
171 type = (cif->arg_types)[i]->type;
176 cif->bytes += sizeof (UINT64) - sizeof (float);
177 if (freg >= NFREGARG - 1)
186 cif->flags2 += ((cif->arg_types)[i]->type) << (2 * j++);
189 case FFI_TYPE_DOUBLE:
190 if (greg++ >= NGREGARG && (freg + 1) >= NFREGARG)
192 if ((freg + 1) < NFREGARG)
195 cif->flags2 += ((cif->arg_types)[i]->type) << (2 * j++);
198 cif->flags2 += FFI_TYPE_INT << (2 * j++);
202 size = (cif->arg_types)[i]->size;
203 if (size < sizeof (UINT64))
204 cif->bytes += sizeof (UINT64) - size;
205 n = (size + sizeof (UINT64) - 1) / sizeof (UINT64);
206 if (greg >= NGREGARG)
208 else if (greg + n - 1 >= NGREGARG)
212 for (m = 0; m < n; m++)
213 cif->flags2 += FFI_TYPE_INT << (2 * j++);
218 /* Set the return type flag */
219 switch (cif->rtype->type)
221 case FFI_TYPE_STRUCT:
222 cif->flags = return_type (cif->rtype);
227 case FFI_TYPE_DOUBLE:
228 case FFI_TYPE_SINT64:
229 case FFI_TYPE_UINT64:
230 cif->flags = cif->rtype->type;
234 cif->flags = FFI_TYPE_INT;
243 extern void ffi_call_SYSV(void (*)(char *, extended_cif *),
244 /*@out@*/ extended_cif *,
245 unsigned, unsigned, long long,
246 /*@out@*/ unsigned *,
251 void ffi_call(/*@dependent@*/ ffi_cif *cif,
253 /*@out@*/ void *rvalue,
254 /*@dependent@*/ void **avalue)
260 ecif.avalue = avalue;
262 /* If the return value is a struct and we don't have a return */
263 /* value address then we need to make one */
265 if (cif->rtype->type == FFI_TYPE_STRUCT
266 && return_type (cif->rtype) != FFI_TYPE_STRUCT)
267 ecif.rvalue = &trvalue;
268 else if ((rvalue == NULL) &&
269 (cif->rtype->type == FFI_TYPE_STRUCT))
271 ecif.rvalue = alloca(cif->rtype->size);
274 ecif.rvalue = rvalue;
279 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, cif->flags2,
288 && cif->rtype->type == FFI_TYPE_STRUCT
289 && return_type (cif->rtype) != FFI_TYPE_STRUCT)
290 memcpy (rvalue, &trvalue, cif->rtype->size);
293 extern void ffi_closure_SYSV (void);
294 extern void __ic_invalidate (void *line);
297 ffi_prep_closure_loc (ffi_closure *closure,
299 void (*fun)(ffi_cif*, void*, void**, void*),
305 FFI_ASSERT (cif->abi == FFI_GCC_SYSV);
307 tramp = (unsigned int *) &closure->tramp[0];
308 /* Since ffi_closure is an aligned object, the ffi trampoline is
309 called as an SHcompact code. Sigh.
311 mova @(1,pc),r0; add #1,r0; jmp @r0; nop;
313 movi fnaddr >> 16,r1; shori fnaddr,r1; ptabs/l r1,tr0
314 movi cxt >> 16,r1; shori cxt,r1; blink tr0,r63 */
315 #ifdef __LITTLE_ENDIAN__
316 tramp[0] = 0x7001c701;
317 tramp[1] = 0x0009402b;
319 tramp[0] = 0xc7017001;
320 tramp[1] = 0x402b0009;
322 tramp[2] = 0xcc000010 | (((UINT32) ffi_closure_SYSV) >> 16) << 10;
323 tramp[3] = 0xc8000010 | (((UINT32) ffi_closure_SYSV) & 0xffff) << 10;
324 tramp[4] = 0x6bf10600;
325 tramp[5] = 0xcc000010 | (((UINT32) codeloc) >> 16) << 10;
326 tramp[6] = 0xc8000010 | (((UINT32) codeloc) & 0xffff) << 10;
327 tramp[7] = 0x4401fff0;
331 closure->user_data = user_data;
333 /* Flush the icache. */
334 asm volatile ("ocbwb %0,0; synco; icbi %1,0; synci" : : "r" (tramp),
340 /* Basically the trampoline invokes ffi_closure_SYSV, and on
341 * entry, r3 holds the address of the closure.
342 * After storing the registers that could possibly contain
343 * parameters to be passed into the stack frame and setting
344 * up space for a return value, ffi_closure_SYSV invokes the
345 * following helper function to do most of the work.
349 ffi_closure_helper_SYSV (ffi_closure *closure, UINT64 *rvalue,
350 UINT64 *pgr, UINT64 *pfr, UINT64 *pst)
360 avalue = alloca (cif->nargs * sizeof (void *));
362 /* Copy the caller's structure return value address so that the closure
363 returns the data directly to the caller. */
364 if (return_type (cif->rtype) == FFI_TYPE_STRUCT)
366 rvalue = (UINT64 *) *pgr;
376 /* Grab the addresses of the arguments from the stack frame. */
377 for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
383 if (z < sizeof (UINT32))
387 switch ((*p_arg)->type)
391 case FFI_TYPE_SINT16:
392 case FFI_TYPE_UINT16:
393 case FFI_TYPE_STRUCT:
394 #ifdef __LITTLE_ENDIAN__
397 avalue[i] = ((char *) p) + sizeof (UINT32) - z;
405 else if (z == sizeof (UINT32))
407 if ((*p_arg)->type == FFI_TYPE_FLOAT)
409 if (freg < NFREGARG - 1)
413 avalue[i] = (UINT32 *) pfr + fpair;
418 #ifdef __LITTLE_ENDIAN__
420 avalue[i] = (UINT32 *) pfr + (1 ^ freg);
423 avalue[i] = (UINT32 *) pfr + freg;
429 #ifdef __LITTLE_ENDIAN__
430 avalue[i] = pgr + greg;
432 avalue[i] = (UINT32 *) (pgr + greg) + 1;
436 #ifdef __LITTLE_ENDIAN__
437 avalue[i] = pgr + greg;
439 avalue[i] = (UINT32 *) (pgr + greg) + 1;
443 else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
445 if (freg + 1 >= NFREGARG)
446 avalue[i] = pgr + greg;
449 avalue[i] = pfr + (freg >> 1);
456 int n = (z + sizeof (UINT64) - 1) / sizeof (UINT64);
458 avalue[i] = pgr + greg;
463 (closure->fun) (cif, rvalue, avalue, closure->user_data);
465 /* Tell ffi_closure_SYSV how to perform return type promotions. */
466 return return_type (cif->rtype);