1 /* -----------------------------------------------------------------------
4 Copyright (C) 1998 Geoffrey Keating
5 Copyright (C) 2001 John Hornkvist
6 Copyright (C) 2002, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
8 FFI support for Darwin and AIX.
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 ``Software''), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice shall be included
19 in all copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 OTHER DEALINGS IN THE SOFTWARE.
28 ----------------------------------------------------------------------- */
31 #include <ffi_common.h>
35 extern void ffi_closure_ASM (void);
37 #if defined (FFI_GO_CLOSURES)
38 extern void ffi_go_closure_ASM (void);
42 /* The assembly depends on these exact flags.
43 For Darwin64 (when FLAG_RETURNS_STRUCT is set):
44 FLAG_RETURNS_FP indicates that the structure embeds FP data.
45 FLAG_RETURNS_128BITS signals a special struct size that is not
46 expanded for float content. */
47 FLAG_RETURNS_128BITS
= 1 << (31-31), /* These go in cr7 */
48 FLAG_RETURNS_NOTHING
= 1 << (31-30),
49 FLAG_RETURNS_FP
= 1 << (31-29),
50 FLAG_RETURNS_64BITS
= 1 << (31-28),
52 FLAG_RETURNS_STRUCT
= 1 << (31-27), /* This goes in cr6 */
54 FLAG_ARG_NEEDS_COPY
= 1 << (31- 7),
55 FLAG_FP_ARGUMENTS
= 1 << (31- 6), /* cr1.eq; specified by ABI */
56 FLAG_4_GPR_ARGUMENTS
= 1 << (31- 5),
57 FLAG_RETVAL_REFERENCE
= 1 << (31- 4)
60 /* About the DARWIN ABI. */
62 NUM_GPR_ARG_REGISTERS
= 8,
63 NUM_FPR_ARG_REGISTERS
= 13,
67 enum { ASM_NEEDS_REGISTERS
= 4 }; /* r28-r31 */
69 /* ffi_prep_args is called by the assembly routine once stack space
70 has been allocated for the function's arguments.
74 The stack layout we want looks like this:
76 | Return address from ffi_call_DARWIN | higher addresses
77 |--------------------------------------------|
78 | Previous backchain pointer 4/8 | stack pointer here
79 |--------------------------------------------|<+ <<< on entry to
80 | ASM_NEEDS_REGISTERS=r28-r31 4*(4/8) | | ffi_call_DARWIN
81 |--------------------------------------------| |
82 | When we have any FP activity... the | |
83 | FPRs occupy NUM_FPR_ARG_REGISTERS slots | |
84 | here fp13 .. fp1 from high to low addr. | |
86 | Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS
87 |--------------------------------------------| |
88 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
89 |--------------------------------------------| | stack |
90 | Reserved 2*4/8 | | grows |
91 |--------------------------------------------| | down V
92 | Space for callee's LR 4/8 | |
93 |--------------------------------------------| | lower addresses
94 | Saved CR [low word for m64] 4/8 | |
95 |--------------------------------------------| | stack pointer here
96 | Current backchain pointer 4/8 |-/ during
97 |--------------------------------------------| <<< ffi_call_DARWIN
101 #if defined(POWERPC_DARWIN64)
103 darwin64_pass_struct_by_value
104 (ffi_type
*, char *, unsigned, unsigned *, double **, unsigned long **);
107 /* This depends on GPR_SIZE = sizeof (unsigned long) */
110 ffi_prep_args (extended_cif
*ecif
, unsigned long *const stack
)
112 const unsigned bytes
= ecif
->cif
->bytes
;
113 const unsigned flags
= ecif
->cif
->flags
;
114 const unsigned nargs
= ecif
->cif
->nargs
;
115 #if !defined(POWERPC_DARWIN64)
116 const ffi_abi abi
= ecif
->cif
->abi
;
119 /* 'stacktop' points at the previous backchain pointer. */
120 unsigned long *const stacktop
= stack
+ (bytes
/ sizeof(unsigned long));
122 /* 'fpr_base' points at the space for fpr1, and grows upwards as
123 we use FPR registers. */
124 double *fpr_base
= (double *) (stacktop
- ASM_NEEDS_REGISTERS
) - NUM_FPR_ARG_REGISTERS
;
125 int gp_count
= 0, fparg_count
= 0;
127 /* 'next_arg' grows up as we put parameters in it. */
128 unsigned long *next_arg
= stack
+ LINKAGE_AREA_GPRS
; /* 6 reserved positions. */
132 void **p_argv
= ecif
->avalue
;
133 unsigned long gprvalue
;
134 ffi_type
** ptr
= ecif
->cif
->arg_types
;
135 #if !defined(POWERPC_DARWIN64)
138 unsigned size_al
= 0;
140 /* Check that everything starts aligned properly. */
141 FFI_ASSERT(((unsigned) (char *) stack
& 0xF) == 0);
142 FFI_ASSERT(((unsigned) (char *) stacktop
& 0xF) == 0);
143 FFI_ASSERT((bytes
& 0xF) == 0);
145 /* Deal with return values that are actually pass-by-reference.
147 Return values are referenced by r3, so r4 is the first parameter. */
149 if (flags
& FLAG_RETVAL_REFERENCE
)
150 *next_arg
++ = (unsigned long) (char *) ecif
->rvalue
;
152 /* Now for the arguments. */
153 for (i
= nargs
; i
> 0; i
--, ptr
++, p_argv
++)
155 switch ((*ptr
)->type
)
157 /* If a floating-point parameter appears before all of the general-
158 purpose registers are filled, the corresponding GPRs that match
159 the size of the floating-point parameter are skipped. */
161 double_tmp
= *(float *) *p_argv
;
162 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
163 *fpr_base
++ = double_tmp
;
164 #if defined(POWERPC_DARWIN)
165 *(float *)next_arg
= *(float *) *p_argv
;
167 *(double *)next_arg
= double_tmp
;
172 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
175 case FFI_TYPE_DOUBLE
:
176 double_tmp
= *(double *) *p_argv
;
177 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
178 *fpr_base
++ = double_tmp
;
179 *(double *)next_arg
= double_tmp
;
188 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
191 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
193 case FFI_TYPE_LONGDOUBLE
:
194 # if defined(POWERPC64) && !defined(POWERPC_DARWIN64)
195 /* ??? This will exceed the regs count when the value starts at fp13
196 and it will not put the extra bit on the stack. */
197 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
198 *(long double *) fpr_base
++ = *(long double *) *p_argv
;
200 *(long double *) next_arg
= *(long double *) *p_argv
;
204 double_tmp
= ((double *) *p_argv
)[0];
205 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
206 *fpr_base
++ = double_tmp
;
207 *(double *) next_arg
= double_tmp
;
208 # if defined(POWERPC_DARWIN64)
216 double_tmp
= ((double *) *p_argv
)[1];
217 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
218 *fpr_base
++ = double_tmp
;
219 *(double *) next_arg
= double_tmp
;
220 # if defined(POWERPC_DARWIN64)
229 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
232 case FFI_TYPE_UINT64
:
233 case FFI_TYPE_SINT64
:
235 gprvalue
= *(long long *) *p_argv
;
238 *(long long *) next_arg
= *(long long *) *p_argv
;
243 case FFI_TYPE_POINTER
:
244 gprvalue
= *(unsigned long *) *p_argv
;
247 gprvalue
= *(unsigned char *) *p_argv
;
250 gprvalue
= *(signed char *) *p_argv
;
252 case FFI_TYPE_UINT16
:
253 gprvalue
= *(unsigned short *) *p_argv
;
255 case FFI_TYPE_SINT16
:
256 gprvalue
= *(signed short *) *p_argv
;
259 case FFI_TYPE_STRUCT
:
260 size_al
= (*ptr
)->size
;
261 #if defined(POWERPC_DARWIN64)
262 next_arg
= (unsigned long *)FFI_ALIGN((char *)next_arg
, (*ptr
)->alignment
);
263 darwin64_pass_struct_by_value (*ptr
, (char *) *p_argv
,
265 (unsigned int *) &fparg_count
,
266 &fpr_base
, &next_arg
);
268 dest_cpy
= (char *) next_arg
;
270 /* If the first member of the struct is a double, then include enough
271 padding in the struct size to align it to double-word. */
272 if ((*ptr
)->elements
[0]->type
== FFI_TYPE_DOUBLE
)
273 size_al
= FFI_ALIGN((*ptr
)->size
, 8);
275 # if defined(POWERPC64)
276 FFI_ASSERT (abi
!= FFI_DARWIN
);
277 memcpy ((char *) dest_cpy
, (char *) *p_argv
, size_al
);
278 next_arg
+= (size_al
+ 7) / 8;
280 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
281 SI 4 bytes) are aligned as if they were those modes.
282 Structures with 3 byte in size are padded upwards. */
283 if (size_al
< 3 && abi
== FFI_DARWIN
)
284 dest_cpy
+= 4 - size_al
;
286 memcpy((char *) dest_cpy
, (char *) *p_argv
, size_al
);
287 next_arg
+= (size_al
+ 3) / 4;
293 case FFI_TYPE_SINT32
:
294 gprvalue
= *(signed int *) *p_argv
;
297 case FFI_TYPE_UINT32
:
298 gprvalue
= *(unsigned int *) *p_argv
;
300 *next_arg
++ = gprvalue
;
308 /* Check that we didn't overrun the stack... */
309 /* FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
310 FFI_ASSERT((unsigned *)fpr_base
311 <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
312 FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4); */
315 #if defined(POWERPC_DARWIN64)
317 /* See if we can put some of the struct into fprs.
318 This should not be called for structures of size 16 bytes, since these are not
319 broken out this way. */
321 darwin64_scan_struct_for_floats (ffi_type
*s
, unsigned *nfpr
)
325 FFI_ASSERT (s
->type
== FFI_TYPE_STRUCT
)
327 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
329 ffi_type
*p
= s
->elements
[i
];
332 case FFI_TYPE_STRUCT
:
333 darwin64_scan_struct_for_floats (p
, nfpr
);
335 case FFI_TYPE_LONGDOUBLE
:
338 case FFI_TYPE_DOUBLE
:
349 darwin64_struct_size_exceeds_gprs_p (ffi_type
*s
, char *src
, unsigned *nfpr
)
351 unsigned struct_offset
=0, i
;
353 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
356 ffi_type
*p
= s
->elements
[i
];
357 /* Find the start of this item (0 for the first one). */
359 struct_offset
= FFI_ALIGN(struct_offset
, p
->alignment
);
361 item_base
= src
+ struct_offset
;
365 case FFI_TYPE_STRUCT
:
366 if (darwin64_struct_size_exceeds_gprs_p (p
, item_base
, nfpr
))
369 case FFI_TYPE_LONGDOUBLE
:
370 if (*nfpr
>= NUM_FPR_ARG_REGISTERS
)
375 case FFI_TYPE_DOUBLE
:
376 if (*nfpr
>= NUM_FPR_ARG_REGISTERS
)
381 if (*nfpr
>= NUM_FPR_ARG_REGISTERS
)
386 /* If we try and place any item, that is non-float, once we've
387 exceeded the 8 GPR mark, then we can't fit the struct. */
388 if ((unsigned long)item_base
>= 8*8)
392 /* now count the size of what we just used. */
393 struct_offset
+= p
->size
;
398 /* Can this struct be returned by value? */
400 darwin64_struct_ret_by_value_p (ffi_type
*s
)
404 FFI_ASSERT (s
&& s
->type
== FFI_TYPE_STRUCT
);
406 /* The largest structure we can return is 8long + 13 doubles. */
410 /* We can't pass more than 13 floats. */
411 darwin64_scan_struct_for_floats (s
, &nfp
);
415 /* If there are not too many floats, and the struct is
416 small enough to accommodate in the GPRs, then it must be OK. */
420 /* Well, we have to look harder. */
422 if (darwin64_struct_size_exceeds_gprs_p (s
, NULL
, &nfp
))
429 darwin64_pass_struct_floats (ffi_type
*s
, char *src
,
430 unsigned *nfpr
, double **fprs
)
433 double *fpr_base
= *fprs
;
434 unsigned struct_offset
= 0;
436 /* We don't assume anything about the alignment of the source. */
437 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
440 ffi_type
*p
= s
->elements
[i
];
441 /* Find the start of this item (0 for the first one). */
443 struct_offset
= FFI_ALIGN(struct_offset
, p
->alignment
);
444 item_base
= src
+ struct_offset
;
448 case FFI_TYPE_STRUCT
:
449 darwin64_pass_struct_floats (p
, item_base
, nfpr
,
452 case FFI_TYPE_LONGDOUBLE
:
453 if (*nfpr
< NUM_FPR_ARG_REGISTERS
)
454 *fpr_base
++ = *(double *)item_base
;
458 case FFI_TYPE_DOUBLE
:
459 if (*nfpr
< NUM_FPR_ARG_REGISTERS
)
460 *fpr_base
++ = *(double *)item_base
;
464 if (*nfpr
< NUM_FPR_ARG_REGISTERS
)
465 *fpr_base
++ = (double) *(float *)item_base
;
471 /* now count the size of what we just used. */
472 struct_offset
+= p
->size
;
474 /* Update the scores. */
478 /* Darwin64 special rules.
479 Break out a struct into params and float registers. */
481 darwin64_pass_struct_by_value (ffi_type
*s
, char *src
, unsigned size
,
482 unsigned *nfpr
, double **fprs
, unsigned long **arg
)
484 unsigned long *next_arg
= *arg
;
485 char *dest_cpy
= (char *)next_arg
;
487 FFI_ASSERT (s
->type
== FFI_TYPE_STRUCT
)
492 /* First... special cases. */
496 && s
->elements
[0]->type
!= FFI_TYPE_FLOAT
))
498 /* Must be at least one GPR, padding is unspecified in value,
499 let's make it zero. */
501 dest_cpy
+= 8 - size
;
502 memcpy ((char *) dest_cpy
, src
, size
);
507 memcpy ((char *) dest_cpy
, src
, size
);
512 /* now the general case, we consider embedded floats. */
513 memcpy ((char *) dest_cpy
, src
, size
);
514 darwin64_pass_struct_floats (s
, src
, nfpr
, fprs
);
515 next_arg
+= (size
+7)/8;
522 darwin64_struct_floats_to_mem (ffi_type
*s
, char *dest
, double *fprs
, unsigned *nf
)
525 unsigned struct_offset
= 0;
527 /* We don't assume anything about the alignment of the source. */
528 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
531 ffi_type
*p
= s
->elements
[i
];
532 /* Find the start of this item (0 for the first one). */
534 struct_offset
= FFI_ALIGN(struct_offset
, p
->alignment
);
535 item_base
= dest
+ struct_offset
;
539 case FFI_TYPE_STRUCT
:
540 fprs
= darwin64_struct_floats_to_mem (p
, item_base
, fprs
, nf
);
542 case FFI_TYPE_LONGDOUBLE
:
543 if (*nf
< NUM_FPR_ARG_REGISTERS
)
545 *(double *)item_base
= *fprs
++ ;
550 case FFI_TYPE_DOUBLE
:
551 if (*nf
< NUM_FPR_ARG_REGISTERS
)
553 *(double *)item_base
= *fprs
++ ;
558 if (*nf
< NUM_FPR_ARG_REGISTERS
)
560 *(float *)item_base
= (float) *fprs
++ ;
567 /* now count the size of what we just used. */
568 struct_offset
+= p
->size
;
575 /* Adjust the size of S to be correct for Darwin.
576 On Darwin m32, the first field of a structure has natural alignment.
577 On Darwin m64, all fields have natural alignment. */
580 darwin_adjust_aggregate_sizes (ffi_type
*s
)
584 if (s
->type
!= FFI_TYPE_STRUCT
)
588 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
594 if (p
->type
== FFI_TYPE_STRUCT
)
595 darwin_adjust_aggregate_sizes (p
);
596 #if defined(POWERPC_DARWIN64)
597 /* Natural alignment for all items. */
598 align
= p
->alignment
;
600 /* Natural alignment for the first item... */
602 align
= p
->alignment
;
603 else if (p
->alignment
== 16 || p
->alignment
< 4)
604 /* .. subsequent items with vector or align < 4 have natural align. */
605 align
= p
->alignment
;
607 /* .. or align is 4. */
610 /* Pad, if necessary, before adding the current item. */
611 s
->size
= FFI_ALIGN(s
->size
, align
) + p
->size
;
614 s
->size
= FFI_ALIGN(s
->size
, s
->alignment
);
616 /* This should not be necessary on m64, but harmless. */
617 if (s
->elements
[0]->type
== FFI_TYPE_UINT64
618 || s
->elements
[0]->type
== FFI_TYPE_SINT64
619 || s
->elements
[0]->type
== FFI_TYPE_DOUBLE
620 || s
->elements
[0]->alignment
== 8)
621 s
->alignment
= s
->alignment
> 8 ? s
->alignment
: 8;
622 /* Do not add additional tail padding. */
625 /* Adjust the size of S to be correct for AIX.
626 Word-align double unless it is the first member of a structure. */
629 aix_adjust_aggregate_sizes (ffi_type
*s
)
633 if (s
->type
!= FFI_TYPE_STRUCT
)
637 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
643 aix_adjust_aggregate_sizes (p
);
644 align
= p
->alignment
;
645 if (i
!= 0 && p
->type
== FFI_TYPE_DOUBLE
)
647 s
->size
= FFI_ALIGN(s
->size
, align
) + p
->size
;
650 s
->size
= FFI_ALIGN(s
->size
, s
->alignment
);
652 if (s
->elements
[0]->type
== FFI_TYPE_UINT64
653 || s
->elements
[0]->type
== FFI_TYPE_SINT64
654 || s
->elements
[0]->type
== FFI_TYPE_DOUBLE
655 || s
->elements
[0]->alignment
== 8)
656 s
->alignment
= s
->alignment
> 8 ? s
->alignment
: 8;
657 /* Do not add additional tail padding. */
660 /* Perform machine dependent cif processing. */
662 ffi_prep_cif_machdep (ffi_cif
*cif
)
664 /* All this is for the DARWIN ABI. */
668 unsigned fparg_count
= 0, intarg_count
= 0;
670 unsigned size_al
= 0;
672 /* All the machine-independent calculation of cif->bytes will be wrong.
673 All the calculation of structure sizes will also be wrong.
674 Redo the calculation for DARWIN. */
676 if (cif
->abi
== FFI_DARWIN
)
678 darwin_adjust_aggregate_sizes (cif
->rtype
);
679 for (i
= 0; i
< cif
->nargs
; i
++)
680 darwin_adjust_aggregate_sizes (cif
->arg_types
[i
]);
683 if (cif
->abi
== FFI_AIX
)
685 aix_adjust_aggregate_sizes (cif
->rtype
);
686 for (i
= 0; i
< cif
->nargs
; i
++)
687 aix_adjust_aggregate_sizes (cif
->arg_types
[i
]);
690 /* Space for the frame pointer, callee's LR, CR, etc, and for
691 the asm's temp regs. */
693 bytes
= (LINKAGE_AREA_GPRS
+ ASM_NEEDS_REGISTERS
) * sizeof(unsigned long);
695 /* Return value handling.
696 The rules m32 are as follows:
697 - 32-bit (or less) integer values are returned in gpr3;
698 - structures of size <= 4 bytes also returned in gpr3;
699 - 64-bit integer values [??? and structures between 5 and 8 bytes] are
700 returned in gpr3 and gpr4;
701 - Single/double FP values are returned in fpr1;
702 - Long double FP (if not equivalent to double) values are returned in
705 - 64-bit or smaller integral values are returned in GPR3
706 - Single/double FP values are returned in fpr1;
707 - Long double FP values are returned in fpr1 and fpr2;
709 - If the structure could be accommodated in registers were it to be the
710 first argument to a routine, then it is returned in those registers.
711 m32/m64 structures otherwise:
712 - Larger structures values are allocated space and a pointer is passed
713 as the first argument. */
714 switch (cif
->rtype
->type
)
717 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
718 case FFI_TYPE_LONGDOUBLE
:
719 flags
|= FLAG_RETURNS_128BITS
;
720 flags
|= FLAG_RETURNS_FP
;
724 case FFI_TYPE_DOUBLE
:
725 flags
|= FLAG_RETURNS_64BITS
;
728 flags
|= FLAG_RETURNS_FP
;
731 case FFI_TYPE_UINT64
:
732 case FFI_TYPE_SINT64
:
734 case FFI_TYPE_POINTER
:
736 flags
|= FLAG_RETURNS_64BITS
;
739 case FFI_TYPE_STRUCT
:
740 #if defined(POWERPC_DARWIN64)
742 /* Can we fit the struct into regs? */
743 if (darwin64_struct_ret_by_value_p (cif
->rtype
))
746 flags
|= FLAG_RETURNS_STRUCT
;
747 if (cif
->rtype
->size
!= 16)
748 darwin64_scan_struct_for_floats (cif
->rtype
, &nfpr
) ;
750 flags
|= FLAG_RETURNS_128BITS
;
751 /* Will be 0 for 16byte struct. */
753 flags
|= FLAG_RETURNS_FP
;
757 flags
|= FLAG_RETVAL_REFERENCE
;
758 flags
|= FLAG_RETURNS_NOTHING
;
762 #elif defined(DARWIN_PPC)
763 if (cif
->rtype
->size
<= 4)
764 flags
|= FLAG_RETURNS_STRUCT
;
765 else /* else by reference. */
767 flags
|= FLAG_RETVAL_REFERENCE
;
768 flags
|= FLAG_RETURNS_NOTHING
;
771 #else /* assume we pass by ref. */
772 flags
|= FLAG_RETVAL_REFERENCE
;
773 flags
|= FLAG_RETURNS_NOTHING
;
778 flags
|= FLAG_RETURNS_NOTHING
;
782 /* Returns 32-bit integer, or similar. Nothing to do here. */
786 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
787 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
789 ??? Structures are passed as a pointer to a copy of the structure.
790 Stuff on the stack needs to keep proper alignment.
791 For m64 the count is effectively of half-GPRs. */
792 for (ptr
= cif
->arg_types
, i
= cif
->nargs
; i
> 0; i
--, ptr
++)
794 unsigned align_words
;
795 switch ((*ptr
)->type
)
798 case FFI_TYPE_DOUBLE
:
800 #if !defined(POWERPC_DARWIN64)
801 /* If this FP arg is going on the stack, it must be
803 if (fparg_count
> NUM_FPR_ARG_REGISTERS
804 && (intarg_count
& 0x01) != 0)
809 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
810 case FFI_TYPE_LONGDOUBLE
:
812 /* If this FP arg is going on the stack, it must be
814 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
815 #if defined (POWERPC64)
816 intarg_count
= FFI_ALIGN(intarg_count
, 2);
818 intarg_count
= FFI_ALIGN(intarg_count
, 4);
823 case FFI_TYPE_UINT64
:
824 case FFI_TYPE_SINT64
:
825 #if defined(POWERPC64)
828 /* 'long long' arguments are passed as two words, but
829 either both words must fit in registers or both go
830 on the stack. If they go on the stack, they must
831 be 8-byte-aligned. */
832 if (intarg_count
== NUM_GPR_ARG_REGISTERS
-1
833 || (intarg_count
>= NUM_GPR_ARG_REGISTERS
834 && (intarg_count
& 0x01) != 0))
840 case FFI_TYPE_STRUCT
:
841 size_al
= (*ptr
)->size
;
842 #if defined(POWERPC_DARWIN64)
843 align_words
= (*ptr
)->alignment
>> 3;
845 intarg_count
= FFI_ALIGN(intarg_count
, align_words
);
846 /* Base size of the struct. */
847 intarg_count
+= (size_al
+ 7) / 8;
848 /* If 16 bytes then don't worry about floats. */
850 /* Scan through for floats to be placed in regs. */
851 darwin64_scan_struct_for_floats (*ptr
, &fparg_count
) ;
853 align_words
= (*ptr
)->alignment
>> 2;
855 intarg_count
= FFI_ALIGN(intarg_count
, align_words
);
856 /* If the first member of the struct is a double, then align
857 the struct to double-word.
858 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
859 size_al = FFI_ALIGN((*ptr)->size, 8); */
861 intarg_count
+= (size_al
+ 7) / 8;
863 intarg_count
+= (size_al
+ 3) / 4;
869 /* Everything else is passed as a 4-byte word in a GPR, either
870 the object itself or a pointer to it. */
876 if (fparg_count
!= 0)
877 flags
|= FLAG_FP_ARGUMENTS
;
879 #if defined(POWERPC_DARWIN64)
880 /* Space to image the FPR registers, if needed - which includes when they might be
881 used in a struct return. */
883 || ((flags
& FLAG_RETURNS_STRUCT
)
884 && (flags
& FLAG_RETURNS_FP
)))
885 bytes
+= NUM_FPR_ARG_REGISTERS
* sizeof(double);
887 /* Space for the FPR registers, if needed. */
888 if (fparg_count
!= 0)
889 bytes
+= NUM_FPR_ARG_REGISTERS
* sizeof(double);
894 if ((intarg_count
+ fparg_count
) > NUM_GPR_ARG_REGISTERS
)
895 bytes
+= (intarg_count
+ fparg_count
) * sizeof(long);
897 if ((intarg_count
+ 2 * fparg_count
) > NUM_GPR_ARG_REGISTERS
)
898 bytes
+= (intarg_count
+ 2 * fparg_count
) * sizeof(long);
901 bytes
+= NUM_GPR_ARG_REGISTERS
* sizeof(long);
903 /* The stack space allocated needs to be a multiple of 16 bytes. */
904 bytes
= FFI_ALIGN(bytes
, 16) ;
912 extern void ffi_call_AIX(extended_cif
*, long, unsigned, unsigned *,
913 void (*fn
)(void), void (*fn2
)(void));
915 #if defined (FFI_GO_CLOSURES)
916 extern void ffi_call_go_AIX(extended_cif
*, long, unsigned, unsigned *,
917 void (*fn
)(void), void (*fn2
)(void), void *closure
);
920 extern void ffi_call_DARWIN(extended_cif
*, long, unsigned, unsigned *,
921 void (*fn
)(void), void (*fn2
)(void), ffi_type
*);
924 ffi_call (ffi_cif
*cif
, void (*fn
)(void), void *rvalue
, void **avalue
)
929 ecif
.avalue
= avalue
;
931 /* If the return value is a struct and we don't have a return
932 value address then we need to make one. */
934 if ((rvalue
== NULL
) &&
935 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
937 ecif
.rvalue
= alloca (cif
->rtype
->size
);
940 ecif
.rvalue
= rvalue
;
945 ffi_call_AIX(&ecif
, -(long)cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
,
946 FFI_FN(ffi_prep_args
));
949 ffi_call_DARWIN(&ecif
, -(long)cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
,
950 FFI_FN(ffi_prep_args
), cif
->rtype
);
958 #if defined (FFI_GO_CLOSURES)
960 ffi_call_go (ffi_cif
*cif
, void (*fn
) (void), void *rvalue
, void **avalue
,
966 ecif
.avalue
= avalue
;
968 /* If the return value is a struct and we don't have a return
969 value address then we need to make one. */
971 if ((rvalue
== NULL
) &&
972 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
974 ecif
.rvalue
= alloca (cif
->rtype
->size
);
977 ecif
.rvalue
= rvalue
;
982 ffi_call_go_AIX(&ecif
, -(long)cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
,
983 FFI_FN(ffi_prep_args
), closure
);
992 static void flush_icache(char *);
993 static void flush_range(char *, int);
995 /* The layout of a function descriptor. A C function pointer really
996 points to one of these. */
998 typedef struct aix_fd_struct
{
1003 /* here I'd like to add the stack frame layout we use in darwin_closure.S
1008 The stack layout looks like this:
1010 | Additional params... | | Higher address
1012 | Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS
1013 |--------------------------------------------| |
1014 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
1015 |--------------------------------------------| |
1016 | Reserved 2*4/8 | |
1017 |--------------------------------------------| |
1018 | Space for callee's LR 4/8 | |
1019 |--------------------------------------------| |
1020 | Saved CR [low word for m64] 4/8 | |
1021 |--------------------------------------------| |
1022 | Current backchain pointer 4/8 |-/ Parent's frame.
1023 |--------------------------------------------| <+ <<< on entry to ffi_closure_ASM
1024 | Result Bytes 16 | |
1025 |--------------------------------------------| |
1026 ~ padding to 16-byte alignment ~ ~
1027 |--------------------------------------------| |
1028 | NUM_FPR_ARG_REGISTERS slots | |
1029 | here fp13 .. fp1 13*8 | |
1030 |--------------------------------------------| |
1031 | R3..R10 8*4/8=32/64 | | NUM_GPR_ARG_REGISTERS
1032 |--------------------------------------------| |
1033 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
1034 |--------------------------------------------| | stack |
1035 | Reserved [compiler,binder] 2*4/8 | | grows |
1036 |--------------------------------------------| | down V
1037 | Space for callee's LR 4/8 | |
1038 |--------------------------------------------| | lower addresses
1039 | Saved CR [low word for m64] 4/8 | |
1040 |--------------------------------------------| | stack pointer here
1041 | Current backchain pointer 4/8 |-/ during
1042 |--------------------------------------------| <<< ffi_closure_ASM.
1047 ffi_prep_closure_loc (ffi_closure
* closure
,
1049 void (*fun
)(ffi_cif
*, void*, void**, void*),
1053 unsigned int *tramp
;
1054 struct ffi_aix_trampoline_struct
*tramp_aix
;
1061 FFI_ASSERT (cif
->abi
== FFI_DARWIN
);
1063 tramp
= (unsigned int *) &closure
->tramp
[0];
1064 #if defined(POWERPC_DARWIN64)
1065 tramp
[0] = 0x7c0802a6; /* mflr r0 */
1066 tramp
[1] = 0x429f0015; /* bcl- 20,4*cr7+so, +0x18 (L1) */
1067 /* We put the addresses here. */
1068 tramp
[6] = 0x7d6802a6; /*L1: mflr r11 */
1069 tramp
[7] = 0xe98b0000; /* ld r12,0(r11) function address */
1070 tramp
[8] = 0x7c0803a6; /* mtlr r0 */
1071 tramp
[9] = 0x7d8903a6; /* mtctr r12 */
1072 tramp
[10] = 0xe96b0008; /* lwz r11,8(r11) static chain */
1073 tramp
[11] = 0x4e800420; /* bctr */
1075 *((unsigned long *)&tramp
[2]) = (unsigned long) ffi_closure_ASM
; /* function */
1076 *((unsigned long *)&tramp
[4]) = (unsigned long) codeloc
; /* context */
1078 tramp
[0] = 0x7c0802a6; /* mflr r0 */
1079 tramp
[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
1080 tramp
[4] = 0x7d6802a6; /* mflr r11 */
1081 tramp
[5] = 0x818b0000; /* lwz r12,0(r11) function address */
1082 tramp
[6] = 0x7c0803a6; /* mtlr r0 */
1083 tramp
[7] = 0x7d8903a6; /* mtctr r12 */
1084 tramp
[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
1085 tramp
[9] = 0x4e800420; /* bctr */
1086 tramp
[2] = (unsigned long) ffi_closure_ASM
; /* function */
1087 tramp
[3] = (unsigned long) codeloc
; /* context */
1091 closure
->user_data
= user_data
;
1093 /* Flush the icache. Only necessary on Darwin. */
1094 flush_range(codeloc
, FFI_TRAMPOLINE_SIZE
);
1100 tramp_aix
= (struct ffi_aix_trampoline_struct
*) (closure
->tramp
);
1101 fd
= (aix_fd
*)(void *)ffi_closure_ASM
;
1103 FFI_ASSERT (cif
->abi
== FFI_AIX
);
1105 tramp_aix
->code_pointer
= fd
->code_pointer
;
1106 tramp_aix
->toc
= fd
->toc
;
1107 tramp_aix
->static_chain
= codeloc
;
1110 closure
->user_data
= user_data
;
1120 #if defined (FFI_GO_CLOSURES)
1122 ffi_prep_go_closure (ffi_go_closure
* closure
,
1124 void (*fun
)(ffi_cif
*, void*, void**, void*))
1130 FFI_ASSERT (cif
->abi
== FFI_AIX
);
1132 closure
->tramp
= (void *)ffi_go_closure_ASM
;
1137 // For now, ffi_prep_go_closure is only implemented for AIX, not for Darwin
1147 flush_icache(char *addr
)
1156 : : "r"(addr
) : "memory");
1161 flush_range(char * addr1
, int size
)
1163 #define MIN_LINE_SIZE 32
1165 for (i
= 0; i
< size
; i
+= MIN_LINE_SIZE
)
1166 flush_icache(addr1
+i
);
1167 flush_icache(addr1
+size
-1);
1177 ffi_closure_helper_DARWIN (ffi_closure
*, void *,
1178 unsigned long *, ffi_dblfl
*);
1180 #if defined (FFI_GO_CLOSURES)
1182 ffi_go_closure_helper_DARWIN (ffi_go_closure
*, void *,
1183 unsigned long *, ffi_dblfl
*);
1186 /* Basically the trampoline invokes ffi_closure_ASM, and on
1187 entry, r11 holds the address of the closure.
1188 After storing the registers that could possibly contain
1189 parameters to be passed into the stack frame and setting
1190 up space for a return value, ffi_closure_ASM invokes the
1191 following helper function to do most of the work. */
1194 ffi_closure_helper_common (ffi_cif
* cif
,
1195 void (*fun
)(ffi_cif
*, void*, void**, void*),
1196 void *user_data
, void *rvalue
,
1197 unsigned long *pgr
, ffi_dblfl
*pfr
)
1199 /* rvalue is the pointer to space for return value in closure assembly
1200 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
1201 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
1203 typedef double ldbits
[2];
1212 ffi_type
** arg_types
;
1214 ffi_dblfl
* end_pfr
= pfr
+ NUM_FPR_ARG_REGISTERS
;
1216 #if defined(POWERPC_DARWIN64)
1217 unsigned fpsused
= 0;
1220 avalue
= alloca (cif
->nargs
* sizeof(void *));
1222 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
1224 #if defined(POWERPC_DARWIN64)
1225 if (!darwin64_struct_ret_by_value_p (cif
->rtype
))
1227 /* Won't fit into the regs - return by ref. */
1228 rvalue
= (void *) *pgr
;
1231 #elif defined(DARWIN_PPC)
1232 if (cif
->rtype
->size
> 4)
1234 rvalue
= (void *) *pgr
;
1237 #else /* assume we return by ref. */
1238 rvalue
= (void *) *pgr
;
1245 arg_types
= cif
->arg_types
;
1247 /* Grab the addresses of the arguments from the stack frame. */
1250 switch (arg_types
[i
]->type
)
1252 case FFI_TYPE_SINT8
:
1253 case FFI_TYPE_UINT8
:
1254 #if defined(POWERPC64)
1255 avalue
[i
] = (char *) pgr
+ 7;
1257 avalue
[i
] = (char *) pgr
+ 3;
1262 case FFI_TYPE_SINT16
:
1263 case FFI_TYPE_UINT16
:
1264 #if defined(POWERPC64)
1265 avalue
[i
] = (char *) pgr
+ 6;
1267 avalue
[i
] = (char *) pgr
+ 2;
1272 case FFI_TYPE_SINT32
:
1273 case FFI_TYPE_UINT32
:
1274 #if defined(POWERPC64)
1275 avalue
[i
] = (char *) pgr
+ 4;
1277 case FFI_TYPE_POINTER
:
1283 case FFI_TYPE_STRUCT
:
1284 size_al
= arg_types
[i
]->size
;
1285 #if defined(POWERPC_DARWIN64)
1286 pgr
= (unsigned long *)FFI_ALIGN((char *)pgr
, arg_types
[i
]->alignment
);
1287 if (size_al
< 3 || size_al
== 4)
1289 avalue
[i
] = ((char *)pgr
)+8-size_al
;
1290 if (arg_types
[i
]->elements
[0]->type
== FFI_TYPE_FLOAT
1291 && fpsused
< NUM_FPR_ARG_REGISTERS
)
1293 *(float *)pgr
= (float) *(double *)pfr
;
1302 darwin64_struct_floats_to_mem (arg_types
[i
], (char *)pgr
,
1303 (double *)pfr
, &fpsused
);
1306 pgr
+= (size_al
+ 7) / 8;
1308 /* If the first member of the struct is a double, then align
1309 the struct to double-word. */
1310 if (arg_types
[i
]->elements
[0]->type
== FFI_TYPE_DOUBLE
)
1311 size_al
= FFI_ALIGN(arg_types
[i
]->size
, 8);
1312 # if defined(POWERPC64)
1313 FFI_ASSERT (cif
->abi
!= FFI_DARWIN
);
1315 pgr
+= (size_al
+ 7) / 8;
1317 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
1318 SI 4 bytes) are aligned as if they were those modes. */
1319 if (size_al
< 3 && cif
->abi
== FFI_DARWIN
)
1320 avalue
[i
] = (char*) pgr
+ 4 - size_al
;
1323 pgr
+= (size_al
+ 3) / 4;
1328 case FFI_TYPE_SINT64
:
1329 case FFI_TYPE_UINT64
:
1330 #if defined(POWERPC64)
1331 case FFI_TYPE_POINTER
:
1336 /* Long long ints are passed in two gpr's. */
1342 case FFI_TYPE_FLOAT
:
1343 /* A float value consumes a GPR.
1344 There are 13 64bit floating point registers. */
1347 double temp
= pfr
->d
;
1348 pfr
->f
= (float) temp
;
1359 case FFI_TYPE_DOUBLE
:
1360 /* A double value consumes two GPRs.
1361 There are 13 64bit floating point registers. */
1378 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1380 case FFI_TYPE_LONGDOUBLE
:
1382 if (pfr
+ 1 < end_pfr
)
1391 *pgr
= *(unsigned long *) pfr
;
1397 #else /* POWERPC64 */
1398 /* A long double value consumes four GPRs and two FPRs.
1399 There are 13 64bit floating point registers. */
1400 if (pfr
+ 1 < end_pfr
)
1405 /* Here we have the situation where one part of the long double
1406 is stored in fpr13 and the other part is already on the stack.
1407 We use a union to pass the long double to avalue[i]. */
1408 else if (pfr
+ 1 == end_pfr
)
1411 memcpy (&temp_ld
.lb
[0], pfr
, sizeof(ldbits
));
1412 memcpy (&temp_ld
.lb
[1], pgr
+ 2, sizeof(ldbits
));
1413 avalue
[i
] = &temp_ld
.ld
;
1421 #endif /* POWERPC64 */
1430 (fun
) (cif
, rvalue
, avalue
, user_data
);
1432 /* Tell ffi_closure_ASM to perform return type promotions. */
1437 ffi_closure_helper_DARWIN (ffi_closure
*closure
, void *rvalue
,
1438 unsigned long *pgr
, ffi_dblfl
*pfr
)
1440 return ffi_closure_helper_common (closure
->cif
, closure
->fun
,
1441 closure
->user_data
, rvalue
, pgr
, pfr
);
1444 #if defined (FFI_GO_CLOSURES)
1446 ffi_go_closure_helper_DARWIN (ffi_go_closure
*closure
, void *rvalue
,
1447 unsigned long *pgr
, ffi_dblfl
*pfr
)
1449 return ffi_closure_helper_common (closure
->cif
, closure
->fun
,
1450 closure
, rvalue
, pgr
, pfr
);