1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2003, 2004 Kaz Kojima
4 SuperH SHmedia 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>
35 return_type (ffi_type
*arg
)
38 if (arg
->type
!= FFI_TYPE_STRUCT
)
41 /* gcc uses r2 if the result can be packed in on register. */
42 if (arg
->size
<= sizeof (UINT8
))
43 return FFI_TYPE_UINT8
;
44 else if (arg
->size
<= sizeof (UINT16
))
45 return FFI_TYPE_UINT16
;
46 else if (arg
->size
<= sizeof (UINT32
))
47 return FFI_TYPE_UINT32
;
48 else if (arg
->size
<= sizeof (UINT64
))
49 return FFI_TYPE_UINT64
;
51 return FFI_TYPE_STRUCT
;
54 /* ffi_prep_args is called by the assembly routine once stack space
55 has been allocated for the function's arguments */
58 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
)
164 greg
= (return_type (cif
->rtype
) == FFI_TYPE_STRUCT
? 1 : 0);
168 for (i
= j
= 0; i
< cif
->nargs
; i
++)
170 type
= (cif
->arg_types
)[i
]->type
;
175 cif
->bytes
+= sizeof (UINT64
) - sizeof (float);
176 if (freg
>= NFREGARG
- 1)
179 cif
->flags2
+= ((cif
->arg_types
)[i
]->type
) << (2 * j
++);
182 case FFI_TYPE_DOUBLE
:
183 if (greg
++ >= NGREGARG
&& (freg
+ 1) >= NFREGARG
)
185 if ((freg
+ 1) < NFREGARG
)
187 freg
= (freg
+ 1) & ~1;
189 cif
->flags2
+= ((cif
->arg_types
)[i
]->type
) << (2 * j
++);
192 cif
->flags2
+= FFI_TYPE_INT
<< (2 * j
++);
196 size
= (cif
->arg_types
)[i
]->size
;
197 if (size
< sizeof (UINT64
))
198 cif
->bytes
+= sizeof (UINT64
) - size
;
199 n
= (size
+ sizeof (UINT64
) - 1) / sizeof (UINT64
);
200 if (greg
>= NGREGARG
)
202 else if (greg
+ n
- 1 >= NGREGARG
)
206 for (m
= 0; m
< n
; m
++)
207 cif
->flags2
+= FFI_TYPE_INT
<< (2 * j
++);
212 /* Set the return type flag */
213 switch (cif
->rtype
->type
)
215 case FFI_TYPE_STRUCT
:
216 cif
->flags
= return_type (cif
->rtype
);
221 case FFI_TYPE_DOUBLE
:
222 case FFI_TYPE_SINT64
:
223 case FFI_TYPE_UINT64
:
224 cif
->flags
= cif
->rtype
->type
;
228 cif
->flags
= FFI_TYPE_INT
;
237 extern void ffi_call_SYSV(void (*)(char *, extended_cif
*),
238 /*@out@*/ extended_cif
*,
239 unsigned, unsigned, long long,
240 /*@out@*/ unsigned *,
245 void ffi_call(/*@dependent@*/ ffi_cif
*cif
,
247 /*@out@*/ void *rvalue
,
248 /*@dependent@*/ void **avalue
)
254 ecif
.avalue
= avalue
;
256 /* If the return value is a struct and we don't have a return */
257 /* value address then we need to make one */
259 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
260 && return_type (cif
->rtype
) != FFI_TYPE_STRUCT
)
261 ecif
.rvalue
= &trvalue
;
262 else if ((rvalue
== NULL
) &&
263 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
266 ecif
.rvalue
= alloca(cif
->rtype
->size
);
270 ecif
.rvalue
= rvalue
;
276 ffi_call_SYSV(ffi_prep_args
, &ecif
, cif
->bytes
,
277 cif
->flags
, cif
->flags2
, ecif
.rvalue
, fn
);
286 && cif
->rtype
->type
== FFI_TYPE_STRUCT
287 && return_type (cif
->rtype
) != FFI_TYPE_STRUCT
)
288 memcpy (rvalue
, &trvalue
, cif
->rtype
->size
);
291 extern void ffi_closure_SYSV (void);
292 extern void __ic_invalidate (void *line
);
295 ffi_prep_closure (ffi_closure
*closure
,
297 void (*fun
)(ffi_cif
*, void*, void**, void*),
302 FFI_ASSERT (cif
->abi
== FFI_GCC_SYSV
);
304 tramp
= (unsigned int *) &closure
->tramp
[0];
305 /* Since ffi_closure is an aligned object, the ffi trampoline is
306 called as an SHcompact code. Sigh.
308 mova @(1,pc),r0; add #1,r0; jmp @r0; nop;
310 movi fnaddr >> 16,r1; shori fnaddr,r1; ptabs/l r1,tr0
311 movi cxt >> 16,r1; shori cxt,r1; blink tr0,r63 */
312 #ifdef __LITTLE_ENDIAN__
313 tramp
[0] = 0x7001c701;
314 tramp
[1] = 0x0009402b;
316 tramp
[0] = 0xc7017001;
317 tramp
[1] = 0x402b0009;
319 tramp
[2] = 0xcc000010 | (((UINT32
) ffi_closure_SYSV
) >> 16) << 10;
320 tramp
[3] = 0xc8000010 | (((UINT32
) ffi_closure_SYSV
) & 0xffff) << 10;
321 tramp
[4] = 0x6bf10600;
322 tramp
[5] = 0xcc000010 | (((UINT32
) closure
) >> 16) << 10;
323 tramp
[6] = 0xc8000010 | (((UINT32
) closure
) & 0xffff) << 10;
324 tramp
[7] = 0x4401fff0;
328 closure
->user_data
= user_data
;
330 /* Flush the icache. */
331 asm volatile ("ocbwb %0,0; synco; icbi %0,0; synci" : : "r" (tramp
));
336 /* Basically the trampoline invokes ffi_closure_SYSV, and on
337 * entry, r3 holds the address of the closure.
338 * After storing the registers that could possibly contain
339 * parameters to be passed into the stack frame and setting
340 * up space for a return value, ffi_closure_SYSV invokes the
341 * following helper function to do most of the work.
345 ffi_closure_helper_SYSV (ffi_closure
*closure
, UINT64
*rvalue
,
346 UINT64
*pgr
, UINT64
*pfr
, UINT64
*pst
)
355 avalue
= alloca (cif
->nargs
* sizeof (void *));
357 /* Copy the caller's structure return value address so that the closure
358 returns the data directly to the caller. */
359 if (return_type (cif
->rtype
) == FFI_TYPE_STRUCT
)
371 /* Grab the addresses of the arguments from the stack frame. */
372 for (i
= 0, p_arg
= cif
->arg_types
; i
< avn
; i
++, p_arg
++)
378 if (z
< sizeof (UINT32
))
382 switch ((*p_arg
)->type
)
386 case FFI_TYPE_SINT16
:
387 case FFI_TYPE_UINT16
:
388 case FFI_TYPE_STRUCT
:
389 #ifdef __LITTLE_ENDIAN__
392 avalue
[i
] = ((char *) p
) + sizeof (UINT32
) - z
;
400 else if (z
== sizeof (UINT32
))
402 if ((*p_arg
)->type
== FFI_TYPE_FLOAT
)
404 if (freg
< NFREGARG
- 1)
405 #ifdef __LITTLE_ENDIAN__
406 avalue
[i
] = (UINT32
*) pfr
+ (1 ^ freg
++);
408 avalue
[i
] = (UINT32
*) pfr
+ freg
++;
411 #ifdef __LITTLE_ENDIAN__
412 avalue
[i
] = pgr
+ greg
;
414 avalue
[i
] = (UINT32
*) (pgr
+ greg
) + 1;
418 #ifdef __LITTLE_ENDIAN__
419 avalue
[i
] = pgr
+ greg
;
421 avalue
[i
] = (UINT32
*) (pgr
+ greg
) + 1;
425 else if ((*p_arg
)->type
== FFI_TYPE_DOUBLE
)
427 if (freg
+ 1 >= NFREGARG
)
428 avalue
[i
] = pgr
+ greg
;
431 freg
= (freg
+ 1) & ~1;
432 avalue
[i
] = pfr
+ (freg
>> 1);
439 int n
= (z
+ sizeof (UINT64
) - 1) / sizeof (UINT64
);
441 avalue
[i
] = pgr
+ greg
;
446 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
448 /* Tell ffi_closure_SYSV how to perform return type promotions. */
449 return return_type (cif
->rtype
);