1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 2003, 2004, 2007 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 /* ffi_prep_args is called by the assembly routine once stack space
33 has been allocated for the function's arguments */
35 void ffi_prep_args_v8(char *stack
, extended_cif
*ecif
)
42 /* Skip 16 words for the window save area */
43 argp
= stack
+ 16*sizeof(int);
45 /* This should only really be done when we are returning a structure,
46 however, it's faster just to do it all the time...
48 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) */
49 *(int *) argp
= (long)ecif
->rvalue
;
51 /* And 1 word for the structure return value. */
55 /* Purify will probably complain in our assembly routine, unless we
56 zero out this memory. */
66 p_argv
= ecif
->avalue
;
68 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
; i
; i
--, p_arg
++)
72 if ((*p_arg
)->type
== FFI_TYPE_STRUCT
73 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
74 || (*p_arg
)->type
== FFI_TYPE_LONGDOUBLE
78 *(unsigned int *) argp
= (unsigned long)(* p_argv
);
87 switch ((*p_arg
)->type
)
90 *(signed int *) argp
= *(SINT8
*)(* p_argv
);
94 *(unsigned int *) argp
= *(UINT8
*)(* p_argv
);
98 *(signed int *) argp
= *(SINT16
*)(* p_argv
);
101 case FFI_TYPE_UINT16
:
102 *(unsigned int *) argp
= *(UINT16
*)(* p_argv
);
111 memcpy(argp
, *p_argv
, z
);
121 int ffi_prep_args_v9(char *stack
, extended_cif
*ecif
)
131 /* Skip 16 words for the window save area */
132 argp
= stack
+ 16*sizeof(long long);
135 /* Purify will probably complain in our assembly routine, unless we
136 zero out this memory. */
138 ((long long*)argp
)[0] = 0;
139 ((long long*)argp
)[1] = 0;
140 ((long long*)argp
)[2] = 0;
141 ((long long*)argp
)[3] = 0;
142 ((long long*)argp
)[4] = 0;
143 ((long long*)argp
)[5] = 0;
146 p_argv
= ecif
->avalue
;
148 if (ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
&&
149 ecif
->cif
->rtype
->size
> 32)
151 *(unsigned long long *) argp
= (unsigned long)ecif
->rvalue
;
152 argp
+= sizeof(long long);
156 for (i
= 0, p_arg
= ecif
->cif
->arg_types
; i
< ecif
->cif
->nargs
;
162 switch ((*p_arg
)->type
)
164 case FFI_TYPE_STRUCT
:
167 /* For structures larger than 16 bytes we pass reference. */
168 *(unsigned long long *) argp
= (unsigned long)* p_argv
;
169 argp
+= sizeof(long long);
176 case FFI_TYPE_DOUBLE
:
177 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
178 case FFI_TYPE_LONGDOUBLE
:
180 ret
= 1; /* We should promote into FP regs as well as integer. */
183 if (z
< sizeof(long long))
185 switch ((*p_arg
)->type
)
188 *(signed long long *) argp
= *(SINT8
*)(* p_argv
);
192 *(unsigned long long *) argp
= *(UINT8
*)(* p_argv
);
195 case FFI_TYPE_SINT16
:
196 *(signed long long *) argp
= *(SINT16
*)(* p_argv
);
199 case FFI_TYPE_UINT16
:
200 *(unsigned long long *) argp
= *(UINT16
*)(* p_argv
);
203 case FFI_TYPE_SINT32
:
204 *(signed long long *) argp
= *(SINT32
*)(* p_argv
);
207 case FFI_TYPE_UINT32
:
208 *(unsigned long long *) argp
= *(UINT32
*)(* p_argv
);
212 *(float *) (argp
+ 4) = *(FLOAT32
*)(* p_argv
); /* Right justify */
215 case FFI_TYPE_STRUCT
:
216 memcpy(argp
, *p_argv
, z
);
222 z
= sizeof(long long);
225 else if (z
== sizeof(long long))
227 memcpy(argp
, *p_argv
, z
);
228 z
= sizeof(long long);
233 if ((tmp
& 1) && (*p_arg
)->alignment
> 8)
236 argp
+= sizeof(long long);
238 memcpy(argp
, *p_argv
, z
);
239 z
= 2 * sizeof(long long);
249 /* Perform machine dependent cif processing */
250 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
254 if (cif
->abi
!= FFI_V9
)
258 /* If we are returning a struct, this will already have been added.
259 Otherwise we need to add it because it's always got to be there! */
261 if (cif
->rtype
->type
!= FFI_TYPE_STRUCT
)
262 cif
->bytes
+= wordsize
;
264 /* sparc call frames require that space is allocated for 6 args,
265 even if they aren't used. Make that space if necessary. */
267 if (cif
->bytes
< 4*6+4)
274 /* sparc call frames require that space is allocated for 6 args,
275 even if they aren't used. Make that space if necessary. */
277 if (cif
->bytes
< 8*6)
281 /* Adjust cif->bytes. to include 16 words for the window save area,
282 and maybe the struct/union return pointer area, */
284 cif
->bytes
+= 16 * wordsize
;
286 /* The stack must be 2 word aligned, so round bytes up
289 cif
->bytes
= ALIGN(cif
->bytes
, 2 * wordsize
);
291 /* Set the return type flag */
292 switch (cif
->rtype
->type
)
296 case FFI_TYPE_DOUBLE
:
297 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
298 case FFI_TYPE_LONGDOUBLE
:
300 cif
->flags
= cif
->rtype
->type
;
303 case FFI_TYPE_STRUCT
:
304 if (cif
->abi
== FFI_V9
&& cif
->rtype
->size
> 32)
305 cif
->flags
= FFI_TYPE_VOID
;
307 cif
->flags
= FFI_TYPE_STRUCT
;
310 case FFI_TYPE_SINT64
:
311 case FFI_TYPE_UINT64
:
312 if (cif
->abi
!= FFI_V9
)
314 cif
->flags
= FFI_TYPE_SINT64
;
319 cif
->flags
= FFI_TYPE_INT
;
325 int ffi_v9_layout_struct(ffi_type
*arg
, int off
, char *ret
, char *intg
, char *flt
)
327 ffi_type
**ptr
= &arg
->elements
[0];
331 if (off
& ((*ptr
)->alignment
- 1))
332 off
= ALIGN(off
, (*ptr
)->alignment
);
334 switch ((*ptr
)->type
)
336 case FFI_TYPE_STRUCT
:
337 off
= ffi_v9_layout_struct(*ptr
, off
, ret
, intg
, flt
);
338 off
= ALIGN(off
, FFI_SIZEOF_ARG
);
341 case FFI_TYPE_DOUBLE
:
342 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
343 case FFI_TYPE_LONGDOUBLE
:
345 memmove(ret
+ off
, flt
+ off
, (*ptr
)->size
);
349 memmove(ret
+ off
, intg
+ off
, (*ptr
)->size
);
360 extern int ffi_call_v9(void *, extended_cif
*, unsigned,
361 unsigned, unsigned *, void (*fn
)());
363 extern int ffi_call_v8(void *, extended_cif
*, unsigned,
364 unsigned, unsigned *, void (*fn
)());
367 void ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
373 ecif
.avalue
= avalue
;
375 /* If the return value is a struct and we don't have a return */
376 /* value address then we need to make one */
378 ecif
.rvalue
= rvalue
;
379 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
381 if (cif
->rtype
->size
<= 32)
387 ecif
.rvalue
= alloca(cif
->rtype
->size
);
395 /* We don't yet support calling 32bit code from 64bit */
398 ffi_call_v8(ffi_prep_args_v8
, &ecif
, cif
->bytes
,
399 cif
->flags
, rvalue
, fn
);
404 ffi_call_v9(ffi_prep_args_v9
, &ecif
, cif
->bytes
,
405 cif
->flags
, rval
, fn
);
406 if (rvalue
&& rval
&& cif
->rtype
->type
== FFI_TYPE_STRUCT
)
407 ffi_v9_layout_struct(cif
->rtype
, 0, (char *)rvalue
, (char *)rval
, ((char *)rval
)+32);
422 extern void ffi_closure_v9(void);
424 extern void ffi_closure_v8(void);
428 ffi_prep_closure_loc (ffi_closure
* closure
,
430 void (*fun
)(ffi_cif
*, void*, void**, void*),
434 unsigned int *tramp
= (unsigned int *) &closure
->tramp
[0];
437 /* Trampoline address is equal to the closure address. We take advantage
438 of that to reduce the trampoline size by 8 bytes. */
439 FFI_ASSERT (cif
->abi
== FFI_V9
);
440 fn
= (unsigned long) ffi_closure_v9
;
441 tramp
[0] = 0x83414000; /* rd %pc, %g1 */
442 tramp
[1] = 0xca586010; /* ldx [%g1+16], %g5 */
443 tramp
[2] = 0x81c14000; /* jmp %g5 */
444 tramp
[3] = 0x01000000; /* nop */
445 *((unsigned long *) &tramp
[4]) = fn
;
447 unsigned long ctx
= (unsigned long) codeloc
;
448 FFI_ASSERT (cif
->abi
== FFI_V8
);
449 fn
= (unsigned long) ffi_closure_v8
;
450 tramp
[0] = 0x03000000 | fn
>> 10; /* sethi %hi(fn), %g1 */
451 tramp
[1] = 0x05000000 | ctx
>> 10; /* sethi %hi(ctx), %g2 */
452 tramp
[2] = 0x81c06000 | (fn
& 0x3ff); /* jmp %g1+%lo(fn) */
453 tramp
[3] = 0x8410a000 | (ctx
& 0x3ff);/* or %g2, %lo(ctx) */
458 closure
->user_data
= user_data
;
460 /* Flush the Icache. FIXME: alignment isn't certain, assume 8 bytes */
462 asm volatile ("flush %0" : : "r" (closure
) : "memory");
463 asm volatile ("flush %0" : : "r" (((char *) closure
) + 8) : "memory");
465 asm volatile ("iflush %0" : : "r" (closure
) : "memory");
466 asm volatile ("iflush %0" : : "r" (((char *) closure
) + 8) : "memory");
473 ffi_closure_sparc_inner_v8(ffi_closure
*closure
,
474 void *rvalue
, unsigned long *gpr
, unsigned long *scratch
)
477 ffi_type
**arg_types
;
482 arg_types
= cif
->arg_types
;
483 avalue
= alloca(cif
->nargs
* sizeof(void *));
485 /* Copy the caller's structure return address so that the closure
486 returns the data directly to the caller. */
487 if (cif
->flags
== FFI_TYPE_STRUCT
488 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
489 || cif
->flags
== FFI_TYPE_LONGDOUBLE
492 rvalue
= (void *) gpr
[0];
494 /* Always skip the structure return address. */
497 /* Grab the addresses of the arguments from the stack frame. */
498 for (i
= 0; i
< cif
->nargs
; i
++)
500 if (arg_types
[i
]->type
== FFI_TYPE_STRUCT
501 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
502 || arg_types
[i
]->type
== FFI_TYPE_LONGDOUBLE
506 /* Straight copy of invisible reference. */
507 avalue
[i
] = (void *)gpr
[argn
++];
509 else if ((arg_types
[i
]->type
== FFI_TYPE_DOUBLE
510 || arg_types
[i
]->type
== FFI_TYPE_SINT64
511 || arg_types
[i
]->type
== FFI_TYPE_UINT64
)
512 /* gpr is 8-byte aligned. */
515 /* Align on a 8-byte boundary. */
516 scratch
[0] = gpr
[argn
];
517 scratch
[1] = gpr
[argn
+1];
524 /* Always right-justify. */
525 argn
+= ALIGN(arg_types
[i
]->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
526 avalue
[i
] = ((char *) &gpr
[argn
]) - arg_types
[i
]->size
;
530 /* Invoke the closure. */
531 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
533 /* Tell ffi_closure_sparc how to perform return type promotions. */
534 return cif
->rtype
->type
;
538 ffi_closure_sparc_inner_v9(ffi_closure
*closure
,
539 void *rvalue
, unsigned long *gpr
, double *fpr
)
542 ffi_type
**arg_types
;
544 int i
, argn
, fp_slot_max
;
547 arg_types
= cif
->arg_types
;
548 avalue
= alloca(cif
->nargs
* sizeof(void *));
550 /* Copy the caller's structure return address so that the closure
551 returns the data directly to the caller. */
552 if (cif
->flags
== FFI_TYPE_VOID
553 && cif
->rtype
->type
== FFI_TYPE_STRUCT
)
555 rvalue
= (void *) gpr
[0];
556 /* Skip the structure return address. */
562 fp_slot_max
= 16 - argn
;
564 /* Grab the addresses of the arguments from the stack frame. */
565 for (i
= 0; i
< cif
->nargs
; i
++)
567 if (arg_types
[i
]->type
== FFI_TYPE_STRUCT
)
569 if (arg_types
[i
]->size
> 16)
571 /* Straight copy of invisible reference. */
572 avalue
[i
] = (void *)gpr
[argn
++];
577 ffi_v9_layout_struct(arg_types
[i
],
581 (char *) &fpr
[argn
]);
582 avalue
[i
] = &gpr
[argn
];
583 argn
+= ALIGN(arg_types
[i
]->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
589 argn
+= ALIGN(arg_types
[i
]->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
592 && (arg_types
[i
]->type
== FFI_TYPE_FLOAT
593 || arg_types
[i
]->type
== FFI_TYPE_DOUBLE
594 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
595 || arg_types
[i
]->type
== FFI_TYPE_LONGDOUBLE
598 avalue
[i
] = ((char *) &fpr
[argn
]) - arg_types
[i
]->size
;
600 avalue
[i
] = ((char *) &gpr
[argn
]) - arg_types
[i
]->size
;
604 /* Invoke the closure. */
605 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
607 /* Tell ffi_closure_sparc how to perform return type promotions. */
608 return cif
->rtype
->type
;