1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 2003 Red Hat, Inc.
4 Sparc 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>
32 extern void ffi_closure_v9(void);
34 extern void ffi_closure_v8(void);
37 /* ffi_prep_args is called by the assembly routine once stack space
38 has been allocated for the function's arguments */
40 void ffi_prep_args_v8(char *stack
, extended_cif
*ecif
)
47 /* Skip 16 words for the window save area */
48 argp
= stack
+ 16*sizeof(int);
50 /* This should only really be done when we are returning a structure,
51 however, it's faster just to do it all the time...
53 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) */
54 *(int *) argp
= (long)ecif
->rvalue
;
56 /* And 1 word for the structure return value. */
60 /* Purify will probably complain in our assembly routine, unless we
61 zero out this memory. */
71 p_argv
= ecif
->avalue
;
73 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
; i
; i
--, p_arg
++)
77 if ((*p_arg
)->type
== FFI_TYPE_STRUCT
78 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
79 || (*p_arg
)->type
== FFI_TYPE_LONGDOUBLE
83 *(unsigned int *) argp
= (unsigned long)(* p_argv
);
92 switch ((*p_arg
)->type
)
95 *(signed int *) argp
= *(SINT8
*)(* p_argv
);
99 *(unsigned int *) argp
= *(UINT8
*)(* p_argv
);
102 case FFI_TYPE_SINT16
:
103 *(signed int *) argp
= *(SINT16
*)(* p_argv
);
106 case FFI_TYPE_UINT16
:
107 *(unsigned int *) argp
= *(UINT16
*)(* p_argv
);
116 memcpy(argp
, *p_argv
, z
);
126 int ffi_prep_args_v9(char *stack
, extended_cif
*ecif
)
136 /* Skip 16 words for the window save area */
137 argp
= stack
+ 16*sizeof(long long);
140 /* Purify will probably complain in our assembly routine, unless we
141 zero out this memory. */
143 ((long long*)argp
)[0] = 0;
144 ((long long*)argp
)[1] = 0;
145 ((long long*)argp
)[2] = 0;
146 ((long long*)argp
)[3] = 0;
147 ((long long*)argp
)[4] = 0;
148 ((long long*)argp
)[5] = 0;
151 p_argv
= ecif
->avalue
;
153 if (ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
&&
154 ecif
->cif
->rtype
->size
> 32)
156 *(unsigned long long *) argp
= (unsigned long)ecif
->rvalue
;
160 for (i
= 0, p_arg
= ecif
->cif
->arg_types
; i
< ecif
->cif
->nargs
;
166 switch ((*p_arg
)->type
)
168 case FFI_TYPE_STRUCT
:
171 /* For structures larger than 16 bytes we pass reference. */
172 *(unsigned long long *) argp
= (unsigned long)* p_argv
;
173 argp
+= sizeof(long long);
180 case FFI_TYPE_DOUBLE
:
181 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
182 case FFI_TYPE_LONGDOUBLE
:
184 ret
= 1; /* We should promote into FP regs as well as integer. */
187 if (z
< sizeof(long long))
189 switch ((*p_arg
)->type
)
192 *(signed long long *) argp
= *(SINT8
*)(* p_argv
);
196 *(unsigned long long *) argp
= *(UINT8
*)(* p_argv
);
199 case FFI_TYPE_SINT16
:
200 *(signed long long *) argp
= *(SINT16
*)(* p_argv
);
203 case FFI_TYPE_UINT16
:
204 *(unsigned long long *) argp
= *(UINT16
*)(* p_argv
);
207 case FFI_TYPE_SINT32
:
208 *(signed long long *) argp
= *(SINT32
*)(* p_argv
);
211 case FFI_TYPE_UINT32
:
212 *(unsigned long long *) argp
= *(UINT32
*)(* p_argv
);
216 *(float *) (argp
+ 4) = *(FLOAT32
*)(* p_argv
); /* Right justify */
219 case FFI_TYPE_STRUCT
:
220 memcpy(argp
, *p_argv
, z
);
226 z
= sizeof(long long);
229 else if (z
== sizeof(long long))
231 memcpy(argp
, *p_argv
, z
);
232 z
= sizeof(long long);
237 if ((tmp
& 1) && (*p_arg
)->alignment
> 8)
240 argp
+= sizeof(long long);
242 memcpy(argp
, *p_argv
, z
);
243 z
= 2 * sizeof(long long);
253 /* Perform machine dependent cif processing */
254 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
258 if (cif
->abi
!= FFI_V9
)
262 /* If we are returning a struct, this will already have been added.
263 Otherwise we need to add it because it's always got to be there! */
265 if (cif
->rtype
->type
!= FFI_TYPE_STRUCT
)
266 cif
->bytes
+= wordsize
;
268 /* sparc call frames require that space is allocated for 6 args,
269 even if they aren't used. Make that space if necessary. */
271 if (cif
->bytes
< 4*6+4)
278 /* sparc call frames require that space is allocated for 6 args,
279 even if they aren't used. Make that space if necessary. */
281 if (cif
->bytes
< 8*6)
285 /* Adjust cif->bytes. to include 16 words for the window save area,
286 and maybe the struct/union return pointer area, */
288 cif
->bytes
+= 16 * wordsize
;
290 /* The stack must be 2 word aligned, so round bytes up
293 cif
->bytes
= ALIGN(cif
->bytes
, 2 * wordsize
);
295 /* Set the return type flag */
296 switch (cif
->rtype
->type
)
300 case FFI_TYPE_DOUBLE
:
301 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
302 case FFI_TYPE_LONGDOUBLE
:
304 cif
->flags
= cif
->rtype
->type
;
307 case FFI_TYPE_STRUCT
:
308 if (cif
->abi
== FFI_V9
&& cif
->rtype
->size
> 32)
309 cif
->flags
= FFI_TYPE_VOID
;
311 cif
->flags
= FFI_TYPE_STRUCT
;
314 case FFI_TYPE_SINT64
:
315 case FFI_TYPE_UINT64
:
316 if (cif
->abi
!= FFI_V9
)
318 cif
->flags
= FFI_TYPE_SINT64
;
323 cif
->flags
= FFI_TYPE_INT
;
329 int ffi_V9_return_struct(ffi_type
*arg
, int off
, char *ret
, char *intg
, char *flt
)
331 ffi_type
**ptr
= &arg
->elements
[0];
335 if (off
& ((*ptr
)->alignment
- 1))
336 off
= ALIGN(off
, (*ptr
)->alignment
);
338 switch ((*ptr
)->type
)
340 case FFI_TYPE_STRUCT
:
341 off
= ffi_V9_return_struct(*ptr
, off
, ret
, intg
, flt
);
344 case FFI_TYPE_DOUBLE
:
345 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
346 case FFI_TYPE_LONGDOUBLE
:
348 memcpy(ret
+ off
, flt
+ off
, (*ptr
)->size
);
352 memcpy(ret
+ off
, intg
+ off
, (*ptr
)->size
);
361 extern int ffi_call_V8(void *, extended_cif
*, unsigned,
362 unsigned, unsigned *, void (*fn
)());
363 extern int ffi_call_V9(void *, extended_cif
*, unsigned,
364 unsigned, unsigned *, void (*fn
)());
366 void ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
372 ecif
.avalue
= avalue
;
374 /* If the return value is a struct and we don't have a return */
375 /* value address then we need to make one */
377 ecif
.rvalue
= rvalue
;
378 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
380 if (cif
->rtype
->size
<= 32)
386 ecif
.rvalue
= alloca(cif
->rtype
->size
);
394 /* We don't yet support calling 32bit code from 64bit */
397 ffi_call_V8(ffi_prep_args_v8
, &ecif
, cif
->bytes
,
398 cif
->flags
, rvalue
, fn
);
403 ffi_call_V9(ffi_prep_args_v9
, &ecif
, cif
->bytes
,
404 cif
->flags
, rval
, fn
);
405 if (rvalue
&& rval
&& cif
->rtype
->type
== FFI_TYPE_STRUCT
)
406 ffi_V9_return_struct(cif
->rtype
, 0, (char *)rvalue
, (char *)rval
, ((char *)rval
)+32);
420 ffi_prep_closure (ffi_closure
* closure
,
422 void (*fun
)(ffi_cif
*, void*, void**, void*),
425 unsigned int *tramp
= (unsigned int *) &closure
->tramp
[0];
427 unsigned long ctx
= (unsigned long) closure
;
430 /* Trampoline address is equal to the closure address. We take advantage
431 of that to reduce the trampoline size by 8 bytes. */
432 FFI_ASSERT (cif
->abi
== FFI_V9
);
433 fn
= (unsigned long) ffi_closure_v9
;
434 tramp
[0] = 0x83414000; /* rd %pc, %g1 */
435 tramp
[1] = 0xca586010; /* ldx [%g1+16], %g5 */
436 tramp
[2] = 0x81c14000; /* jmp %g5 */
437 tramp
[3] = 0x01000000; /* nop */
438 *((unsigned long *) &tramp
[4]) = fn
;
440 FFI_ASSERT (cif
->abi
== FFI_V8
);
441 fn
= (unsigned long) ffi_closure_v8
;
442 tramp
[0] = 0x03000000 | fn
>> 10; /* sethi %hi(fn), %g1 */
443 tramp
[1] = 0x05000000 | ctx
>> 10; /* sethi %hi(ctx), %g2 */
444 tramp
[2] = 0x81c06000 | (fn
& 0x3ff); /* jmp %g1+%lo(fn) */
445 tramp
[3] = 0x8410a000 | (ctx
& 0x3ff);/* or %g2, %lo(ctx) */
450 closure
->user_data
= user_data
;
452 /* Flush the Icache. FIXME: alignment isn't certain, assume 8 bytes */
454 asm volatile ("flush %0" : : "r" (closure
) : "memory");
455 asm volatile ("flush %0" : : "r" (((char *) closure
) + 8) : "memory");
457 asm volatile ("iflush %0" : : "r" (closure
) : "memory");
458 asm volatile ("iflush %0" : : "r" (((char *) closure
) + 8) : "memory");
465 ffi_closure_sparc_inner(ffi_closure
*closure
,
466 void *rvalue
, unsigned long *gpr
, double *fpr
)
470 ffi_type
**arg_types
;
474 avalue
= alloca(cif
->nargs
* sizeof(void *));
478 /* Copy the caller's structure return address to that the closure
479 returns the data directly to the caller. */
480 if (cif
->flags
== FFI_TYPE_STRUCT
)
482 rvalue
= (void *) gpr
[0];
488 arg_types
= cif
->arg_types
;
490 /* Grab the addresses of the arguments from the stack frame. */
493 /* Assume big-endian. FIXME */
494 argn
+= ALIGN(arg_types
[i
]->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
497 if (i
< 16 && (arg_types
[i
]->type
== FFI_TYPE_FLOAT
498 || arg_types
[i
]->type
== FFI_TYPE_DOUBLE
499 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
500 || arg_types
[i
]->type
== FFI_TYPE_LONGDOUBLE
503 avalue
[i
] = ((char *) &fpr
[argn
]) - arg_types
[i
]->size
;
506 avalue
[i
] = ((char *) &gpr
[argn
]) - arg_types
[i
]->size
;
510 /* Invoke the closure. */
511 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
513 /* Tell ffi_closure_sparc how to perform return type promotions. */
514 return cif
->rtype
->type
;