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);
38 /* The assembly depends on these exact flags.
39 For Darwin64 (when FLAG_RETURNS_STRUCT is set):
40 FLAG_RETURNS_FP indicates that the structure embeds FP data.
41 FLAG_RETURNS_128BITS signals a special struct size that is not
42 expanded for float content. */
43 FLAG_RETURNS_128BITS
= 1 << (31-31), /* These go in cr7 */
44 FLAG_RETURNS_NOTHING
= 1 << (31-30),
45 FLAG_RETURNS_FP
= 1 << (31-29),
46 FLAG_RETURNS_64BITS
= 1 << (31-28),
48 FLAG_RETURNS_STRUCT
= 1 << (31-27), /* This goes in cr6 */
50 FLAG_ARG_NEEDS_COPY
= 1 << (31- 7),
51 FLAG_FP_ARGUMENTS
= 1 << (31- 6), /* cr1.eq; specified by ABI */
52 FLAG_4_GPR_ARGUMENTS
= 1 << (31- 5),
53 FLAG_RETVAL_REFERENCE
= 1 << (31- 4)
56 /* About the DARWIN ABI. */
58 NUM_GPR_ARG_REGISTERS
= 8,
59 NUM_FPR_ARG_REGISTERS
= 13,
63 enum { ASM_NEEDS_REGISTERS
= 4 }; /* r28-r31 */
65 /* ffi_prep_args is called by the assembly routine once stack space
66 has been allocated for the function's arguments.
70 The stack layout we want looks like this:
72 | Return address from ffi_call_DARWIN | higher addresses
73 |--------------------------------------------|
74 | Previous backchain pointer 4/8 | stack pointer here
75 |--------------------------------------------|<+ <<< on entry to
76 | ASM_NEEDS_REGISTERS=r28-r31 4*(4/8) | | ffi_call_DARWIN
77 |--------------------------------------------| |
78 | When we have any FP activity... the | |
79 | FPRs occupy NUM_FPR_ARG_REGISTERS slots | |
80 | here fp13 .. fp1 from high to low addr. | |
82 | Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS
83 |--------------------------------------------| |
84 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
85 |--------------------------------------------| | stack |
86 | Reserved 2*4/8 | | grows |
87 |--------------------------------------------| | down V
88 | Space for callee's LR 4/8 | |
89 |--------------------------------------------| | lower addresses
90 | Saved CR [low word for m64] 4/8 | |
91 |--------------------------------------------| | stack pointer here
92 | Current backchain pointer 4/8 |-/ during
93 |--------------------------------------------| <<< ffi_call_DARWIN
97 #if defined(POWERPC_DARWIN64)
99 darwin64_pass_struct_by_value
100 (ffi_type
*, char *, unsigned, unsigned *, double **, unsigned long **);
103 /* This depends on GPR_SIZE = sizeof (unsigned long) */
106 ffi_prep_args (extended_cif
*ecif
, unsigned long *const stack
)
108 const unsigned bytes
= ecif
->cif
->bytes
;
109 const unsigned flags
= ecif
->cif
->flags
;
110 const unsigned nargs
= ecif
->cif
->nargs
;
111 #if !defined(POWERPC_DARWIN64)
112 const ffi_abi abi
= ecif
->cif
->abi
;
115 /* 'stacktop' points at the previous backchain pointer. */
116 unsigned long *const stacktop
= stack
+ (bytes
/ sizeof(unsigned long));
118 /* 'fpr_base' points at the space for fpr1, and grows upwards as
119 we use FPR registers. */
120 double *fpr_base
= (double *) (stacktop
- ASM_NEEDS_REGISTERS
) - NUM_FPR_ARG_REGISTERS
;
121 int gp_count
= 0, fparg_count
= 0;
123 /* 'next_arg' grows up as we put parameters in it. */
124 unsigned long *next_arg
= stack
+ LINKAGE_AREA_GPRS
; /* 6 reserved positions. */
128 void **p_argv
= ecif
->avalue
;
129 unsigned long gprvalue
;
130 ffi_type
** ptr
= ecif
->cif
->arg_types
;
131 #if !defined(POWERPC_DARWIN64)
134 unsigned size_al
= 0;
136 /* Check that everything starts aligned properly. */
137 FFI_ASSERT(((unsigned) (char *) stack
& 0xF) == 0);
138 FFI_ASSERT(((unsigned) (char *) stacktop
& 0xF) == 0);
139 FFI_ASSERT((bytes
& 0xF) == 0);
141 /* Deal with return values that are actually pass-by-reference.
143 Return values are referenced by r3, so r4 is the first parameter. */
145 if (flags
& FLAG_RETVAL_REFERENCE
)
146 *next_arg
++ = (unsigned long) (char *) ecif
->rvalue
;
148 /* Now for the arguments. */
149 for (i
= nargs
; i
> 0; i
--, ptr
++, p_argv
++)
151 switch ((*ptr
)->type
)
153 /* If a floating-point parameter appears before all of the general-
154 purpose registers are filled, the corresponding GPRs that match
155 the size of the floating-point parameter are skipped. */
157 double_tmp
= *(float *) *p_argv
;
158 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
159 *fpr_base
++ = double_tmp
;
160 #if defined(POWERPC_DARWIN)
161 *(float *)next_arg
= *(float *) *p_argv
;
163 *(double *)next_arg
= double_tmp
;
168 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
171 case FFI_TYPE_DOUBLE
:
172 double_tmp
= *(double *) *p_argv
;
173 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
174 *fpr_base
++ = double_tmp
;
175 *(double *)next_arg
= double_tmp
;
184 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
187 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
189 case FFI_TYPE_LONGDOUBLE
:
190 # if defined(POWERPC64) && !defined(POWERPC_DARWIN64)
191 /* ??? This will exceed the regs count when the value starts at fp13
192 and it will not put the extra bit on the stack. */
193 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
194 *(long double *) fpr_base
++ = *(long double *) *p_argv
;
196 *(long double *) next_arg
= *(long double *) *p_argv
;
200 double_tmp
= ((double *) *p_argv
)[0];
201 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
202 *fpr_base
++ = double_tmp
;
203 *(double *) next_arg
= double_tmp
;
204 # if defined(POWERPC_DARWIN64)
212 double_tmp
= ((double *) *p_argv
)[1];
213 if (fparg_count
< NUM_FPR_ARG_REGISTERS
)
214 *fpr_base
++ = double_tmp
;
215 *(double *) next_arg
= double_tmp
;
216 # if defined(POWERPC_DARWIN64)
225 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
228 case FFI_TYPE_UINT64
:
229 case FFI_TYPE_SINT64
:
231 gprvalue
= *(long long *) *p_argv
;
234 *(long long *) next_arg
= *(long long *) *p_argv
;
239 case FFI_TYPE_POINTER
:
240 gprvalue
= *(unsigned long *) *p_argv
;
243 gprvalue
= *(unsigned char *) *p_argv
;
246 gprvalue
= *(signed char *) *p_argv
;
248 case FFI_TYPE_UINT16
:
249 gprvalue
= *(unsigned short *) *p_argv
;
251 case FFI_TYPE_SINT16
:
252 gprvalue
= *(signed short *) *p_argv
;
255 case FFI_TYPE_STRUCT
:
256 size_al
= (*ptr
)->size
;
257 #if defined(POWERPC_DARWIN64)
258 next_arg
= (unsigned long *)ALIGN((char *)next_arg
, (*ptr
)->alignment
);
259 darwin64_pass_struct_by_value (*ptr
, (char *) *p_argv
,
261 (unsigned int *) &fparg_count
,
262 &fpr_base
, &next_arg
);
264 dest_cpy
= (char *) next_arg
;
266 /* If the first member of the struct is a double, then include enough
267 padding in the struct size to align it to double-word. */
268 if ((*ptr
)->elements
[0]->type
== FFI_TYPE_DOUBLE
)
269 size_al
= ALIGN((*ptr
)->size
, 8);
271 # if defined(POWERPC64)
272 FFI_ASSERT (abi
!= FFI_DARWIN
);
273 memcpy ((char *) dest_cpy
, (char *) *p_argv
, size_al
);
274 next_arg
+= (size_al
+ 7) / 8;
276 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
277 SI 4 bytes) are aligned as if they were those modes.
278 Structures with 3 byte in size are padded upwards. */
279 if (size_al
< 3 && abi
== FFI_DARWIN
)
280 dest_cpy
+= 4 - size_al
;
282 memcpy((char *) dest_cpy
, (char *) *p_argv
, size_al
);
283 next_arg
+= (size_al
+ 3) / 4;
289 case FFI_TYPE_SINT32
:
290 gprvalue
= *(signed int *) *p_argv
;
293 case FFI_TYPE_UINT32
:
294 gprvalue
= *(unsigned int *) *p_argv
;
296 *next_arg
++ = gprvalue
;
304 /* Check that we didn't overrun the stack... */
305 /* FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
306 FFI_ASSERT((unsigned *)fpr_base
307 <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
308 FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4); */
311 #if defined(POWERPC_DARWIN64)
313 /* See if we can put some of the struct into fprs.
314 This should not be called for structures of size 16 bytes, since these are not
315 broken out this way. */
317 darwin64_scan_struct_for_floats (ffi_type
*s
, unsigned *nfpr
)
321 FFI_ASSERT (s
->type
== FFI_TYPE_STRUCT
)
323 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
325 ffi_type
*p
= s
->elements
[i
];
328 case FFI_TYPE_STRUCT
:
329 darwin64_scan_struct_for_floats (p
, nfpr
);
331 case FFI_TYPE_LONGDOUBLE
:
334 case FFI_TYPE_DOUBLE
:
345 darwin64_struct_size_exceeds_gprs_p (ffi_type
*s
, char *src
, unsigned *nfpr
)
347 unsigned struct_offset
=0, i
;
349 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
352 ffi_type
*p
= s
->elements
[i
];
353 /* Find the start of this item (0 for the first one). */
355 struct_offset
= ALIGN(struct_offset
, p
->alignment
);
357 item_base
= src
+ struct_offset
;
361 case FFI_TYPE_STRUCT
:
362 if (darwin64_struct_size_exceeds_gprs_p (p
, item_base
, nfpr
))
365 case FFI_TYPE_LONGDOUBLE
:
366 if (*nfpr
>= NUM_FPR_ARG_REGISTERS
)
371 case FFI_TYPE_DOUBLE
:
372 if (*nfpr
>= NUM_FPR_ARG_REGISTERS
)
377 if (*nfpr
>= NUM_FPR_ARG_REGISTERS
)
382 /* If we try and place any item, that is non-float, once we've
383 exceeded the 8 GPR mark, then we can't fit the struct. */
384 if ((unsigned long)item_base
>= 8*8)
388 /* now count the size of what we just used. */
389 struct_offset
+= p
->size
;
394 /* Can this struct be returned by value? */
396 darwin64_struct_ret_by_value_p (ffi_type
*s
)
400 FFI_ASSERT (s
&& s
->type
== FFI_TYPE_STRUCT
);
402 /* The largest structure we can return is 8long + 13 doubles. */
406 /* We can't pass more than 13 floats. */
407 darwin64_scan_struct_for_floats (s
, &nfp
);
411 /* If there are not too many floats, and the struct is
412 small enough to accommodate in the GPRs, then it must be OK. */
416 /* Well, we have to look harder. */
418 if (darwin64_struct_size_exceeds_gprs_p (s
, NULL
, &nfp
))
425 darwin64_pass_struct_floats (ffi_type
*s
, char *src
,
426 unsigned *nfpr
, double **fprs
)
429 double *fpr_base
= *fprs
;
430 unsigned struct_offset
= 0;
432 /* We don't assume anything about the alignment of the source. */
433 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
436 ffi_type
*p
= s
->elements
[i
];
437 /* Find the start of this item (0 for the first one). */
439 struct_offset
= ALIGN(struct_offset
, p
->alignment
);
440 item_base
= src
+ struct_offset
;
444 case FFI_TYPE_STRUCT
:
445 darwin64_pass_struct_floats (p
, item_base
, nfpr
,
448 case FFI_TYPE_LONGDOUBLE
:
449 if (*nfpr
< NUM_FPR_ARG_REGISTERS
)
450 *fpr_base
++ = *(double *)item_base
;
454 case FFI_TYPE_DOUBLE
:
455 if (*nfpr
< NUM_FPR_ARG_REGISTERS
)
456 *fpr_base
++ = *(double *)item_base
;
460 if (*nfpr
< NUM_FPR_ARG_REGISTERS
)
461 *fpr_base
++ = (double) *(float *)item_base
;
467 /* now count the size of what we just used. */
468 struct_offset
+= p
->size
;
470 /* Update the scores. */
474 /* Darwin64 special rules.
475 Break out a struct into params and float registers. */
477 darwin64_pass_struct_by_value (ffi_type
*s
, char *src
, unsigned size
,
478 unsigned *nfpr
, double **fprs
, unsigned long **arg
)
480 unsigned long *next_arg
= *arg
;
481 char *dest_cpy
= (char *)next_arg
;
483 FFI_ASSERT (s
->type
== FFI_TYPE_STRUCT
)
488 /* First... special cases. */
492 && s
->elements
[0]->type
!= FFI_TYPE_FLOAT
))
494 /* Must be at least one GPR, padding is unspecified in value,
495 let's make it zero. */
497 dest_cpy
+= 8 - size
;
498 memcpy ((char *) dest_cpy
, src
, size
);
503 memcpy ((char *) dest_cpy
, src
, size
);
508 /* now the general case, we consider embedded floats. */
509 memcpy ((char *) dest_cpy
, src
, size
);
510 darwin64_pass_struct_floats (s
, src
, nfpr
, fprs
);
511 next_arg
+= (size
+7)/8;
518 darwin64_struct_floats_to_mem (ffi_type
*s
, char *dest
, double *fprs
, unsigned *nf
)
521 unsigned struct_offset
= 0;
523 /* We don't assume anything about the alignment of the source. */
524 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
527 ffi_type
*p
= s
->elements
[i
];
528 /* Find the start of this item (0 for the first one). */
530 struct_offset
= ALIGN(struct_offset
, p
->alignment
);
531 item_base
= dest
+ struct_offset
;
535 case FFI_TYPE_STRUCT
:
536 fprs
= darwin64_struct_floats_to_mem (p
, item_base
, fprs
, nf
);
538 case FFI_TYPE_LONGDOUBLE
:
539 if (*nf
< NUM_FPR_ARG_REGISTERS
)
541 *(double *)item_base
= *fprs
++ ;
546 case FFI_TYPE_DOUBLE
:
547 if (*nf
< NUM_FPR_ARG_REGISTERS
)
549 *(double *)item_base
= *fprs
++ ;
554 if (*nf
< NUM_FPR_ARG_REGISTERS
)
556 *(float *)item_base
= (float) *fprs
++ ;
563 /* now count the size of what we just used. */
564 struct_offset
+= p
->size
;
571 /* Adjust the size of S to be correct for Darwin.
572 On Darwin m32, the first field of a structure has natural alignment.
573 On Darwin m64, all fields have natural alignment. */
576 darwin_adjust_aggregate_sizes (ffi_type
*s
)
580 if (s
->type
!= FFI_TYPE_STRUCT
)
584 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
590 if (p
->type
== FFI_TYPE_STRUCT
)
591 darwin_adjust_aggregate_sizes (p
);
592 #if defined(POWERPC_DARWIN64)
593 /* Natural alignment for all items. */
594 align
= p
->alignment
;
596 /* Natural alignment for the first item... */
598 align
= p
->alignment
;
599 else if (p
->alignment
== 16 || p
->alignment
< 4)
600 /* .. subsequent items with vector or align < 4 have natural align. */
601 align
= p
->alignment
;
603 /* .. or align is 4. */
606 /* Pad, if necessary, before adding the current item. */
607 s
->size
= ALIGN(s
->size
, align
) + p
->size
;
610 s
->size
= ALIGN(s
->size
, s
->alignment
);
612 /* This should not be necessary on m64, but harmless. */
613 if (s
->elements
[0]->type
== FFI_TYPE_UINT64
614 || s
->elements
[0]->type
== FFI_TYPE_SINT64
615 || s
->elements
[0]->type
== FFI_TYPE_DOUBLE
616 || s
->elements
[0]->alignment
== 8)
617 s
->alignment
= s
->alignment
> 8 ? s
->alignment
: 8;
618 /* Do not add additional tail padding. */
621 /* Adjust the size of S to be correct for AIX.
622 Word-align double unless it is the first member of a structure. */
625 aix_adjust_aggregate_sizes (ffi_type
*s
)
629 if (s
->type
!= FFI_TYPE_STRUCT
)
633 for (i
= 0; s
->elements
[i
] != NULL
; i
++)
639 aix_adjust_aggregate_sizes (p
);
640 align
= p
->alignment
;
641 if (i
!= 0 && p
->type
== FFI_TYPE_DOUBLE
)
643 s
->size
= ALIGN(s
->size
, align
) + p
->size
;
646 s
->size
= ALIGN(s
->size
, s
->alignment
);
648 if (s
->elements
[0]->type
== FFI_TYPE_UINT64
649 || s
->elements
[0]->type
== FFI_TYPE_SINT64
650 || s
->elements
[0]->type
== FFI_TYPE_DOUBLE
651 || s
->elements
[0]->alignment
== 8)
652 s
->alignment
= s
->alignment
> 8 ? s
->alignment
: 8;
653 /* Do not add additional tail padding. */
656 /* Perform machine dependent cif processing. */
658 ffi_prep_cif_machdep (ffi_cif
*cif
)
660 /* All this is for the DARWIN ABI. */
664 unsigned fparg_count
= 0, intarg_count
= 0;
666 unsigned size_al
= 0;
668 /* All the machine-independent calculation of cif->bytes will be wrong.
669 All the calculation of structure sizes will also be wrong.
670 Redo the calculation for DARWIN. */
672 if (cif
->abi
== FFI_DARWIN
)
674 darwin_adjust_aggregate_sizes (cif
->rtype
);
675 for (i
= 0; i
< cif
->nargs
; i
++)
676 darwin_adjust_aggregate_sizes (cif
->arg_types
[i
]);
679 if (cif
->abi
== FFI_AIX
)
681 aix_adjust_aggregate_sizes (cif
->rtype
);
682 for (i
= 0; i
< cif
->nargs
; i
++)
683 aix_adjust_aggregate_sizes (cif
->arg_types
[i
]);
686 /* Space for the frame pointer, callee's LR, CR, etc, and for
687 the asm's temp regs. */
689 bytes
= (LINKAGE_AREA_GPRS
+ ASM_NEEDS_REGISTERS
) * sizeof(unsigned long);
691 /* Return value handling.
692 The rules m32 are as follows:
693 - 32-bit (or less) integer values are returned in gpr3;
694 - structures of size <= 4 bytes also returned in gpr3;
695 - 64-bit integer values [??? and structures between 5 and 8 bytes] are
696 returned in gpr3 and gpr4;
697 - Single/double FP values are returned in fpr1;
698 - Long double FP (if not equivalent to double) values are returned in
701 - 64-bit or smaller integral values are returned in GPR3
702 - Single/double FP values are returned in fpr1;
703 - Long double FP values are returned in fpr1 and fpr2;
705 - If the structure could be accommodated in registers were it to be the
706 first argument to a routine, then it is returned in those registers.
707 m32/m64 structures otherwise:
708 - Larger structures values are allocated space and a pointer is passed
709 as the first argument. */
710 switch (cif
->rtype
->type
)
713 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
714 case FFI_TYPE_LONGDOUBLE
:
715 flags
|= FLAG_RETURNS_128BITS
;
716 flags
|= FLAG_RETURNS_FP
;
720 case FFI_TYPE_DOUBLE
:
721 flags
|= FLAG_RETURNS_64BITS
;
724 flags
|= FLAG_RETURNS_FP
;
727 case FFI_TYPE_UINT64
:
728 case FFI_TYPE_SINT64
:
730 case FFI_TYPE_POINTER
:
732 flags
|= FLAG_RETURNS_64BITS
;
735 case FFI_TYPE_STRUCT
:
736 #if defined(POWERPC_DARWIN64)
738 /* Can we fit the struct into regs? */
739 if (darwin64_struct_ret_by_value_p (cif
->rtype
))
742 flags
|= FLAG_RETURNS_STRUCT
;
743 if (cif
->rtype
->size
!= 16)
744 darwin64_scan_struct_for_floats (cif
->rtype
, &nfpr
) ;
746 flags
|= FLAG_RETURNS_128BITS
;
747 /* Will be 0 for 16byte struct. */
749 flags
|= FLAG_RETURNS_FP
;
753 flags
|= FLAG_RETVAL_REFERENCE
;
754 flags
|= FLAG_RETURNS_NOTHING
;
758 #elif defined(DARWIN_PPC)
759 if (cif
->rtype
->size
<= 4)
760 flags
|= FLAG_RETURNS_STRUCT
;
761 else /* else by reference. */
763 flags
|= FLAG_RETVAL_REFERENCE
;
764 flags
|= FLAG_RETURNS_NOTHING
;
767 #else /* assume we pass by ref. */
768 flags
|= FLAG_RETVAL_REFERENCE
;
769 flags
|= FLAG_RETURNS_NOTHING
;
774 flags
|= FLAG_RETURNS_NOTHING
;
778 /* Returns 32-bit integer, or similar. Nothing to do here. */
782 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
783 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
785 ??? Structures are passed as a pointer to a copy of the structure.
786 Stuff on the stack needs to keep proper alignment.
787 For m64 the count is effectively of half-GPRs. */
788 for (ptr
= cif
->arg_types
, i
= cif
->nargs
; i
> 0; i
--, ptr
++)
790 unsigned align_words
;
791 switch ((*ptr
)->type
)
794 case FFI_TYPE_DOUBLE
:
796 #if !defined(POWERPC_DARWIN64)
797 /* If this FP arg is going on the stack, it must be
799 if (fparg_count
> NUM_FPR_ARG_REGISTERS
800 && (intarg_count
& 0x01) != 0)
805 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
806 case FFI_TYPE_LONGDOUBLE
:
808 /* If this FP arg is going on the stack, it must be
810 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
811 #if defined (POWERPC64)
812 intarg_count
= ALIGN(intarg_count
, 2);
814 intarg_count
= ALIGN(intarg_count
, 4);
819 case FFI_TYPE_UINT64
:
820 case FFI_TYPE_SINT64
:
821 #if defined(POWERPC64)
824 /* 'long long' arguments are passed as two words, but
825 either both words must fit in registers or both go
826 on the stack. If they go on the stack, they must
827 be 8-byte-aligned. */
828 if (intarg_count
== NUM_GPR_ARG_REGISTERS
-1
829 || (intarg_count
>= NUM_GPR_ARG_REGISTERS
830 && (intarg_count
& 0x01) != 0))
836 case FFI_TYPE_STRUCT
:
837 size_al
= (*ptr
)->size
;
838 #if defined(POWERPC_DARWIN64)
839 align_words
= (*ptr
)->alignment
>> 3;
841 intarg_count
= ALIGN(intarg_count
, align_words
);
842 /* Base size of the struct. */
843 intarg_count
+= (size_al
+ 7) / 8;
844 /* If 16 bytes then don't worry about floats. */
846 /* Scan through for floats to be placed in regs. */
847 darwin64_scan_struct_for_floats (*ptr
, &fparg_count
) ;
849 align_words
= (*ptr
)->alignment
>> 2;
851 intarg_count
= ALIGN(intarg_count
, align_words
);
852 /* If the first member of the struct is a double, then align
853 the struct to double-word.
854 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
855 size_al = ALIGN((*ptr)->size, 8); */
857 intarg_count
+= (size_al
+ 7) / 8;
859 intarg_count
+= (size_al
+ 3) / 4;
865 /* Everything else is passed as a 4-byte word in a GPR, either
866 the object itself or a pointer to it. */
872 if (fparg_count
!= 0)
873 flags
|= FLAG_FP_ARGUMENTS
;
875 #if defined(POWERPC_DARWIN64)
876 /* Space to image the FPR registers, if needed - which includes when they might be
877 used in a struct return. */
879 || ((flags
& FLAG_RETURNS_STRUCT
)
880 && (flags
& FLAG_RETURNS_FP
)))
881 bytes
+= NUM_FPR_ARG_REGISTERS
* sizeof(double);
883 /* Space for the FPR registers, if needed. */
884 if (fparg_count
!= 0)
885 bytes
+= NUM_FPR_ARG_REGISTERS
* sizeof(double);
890 if ((intarg_count
+ fparg_count
) > NUM_GPR_ARG_REGISTERS
)
891 bytes
+= (intarg_count
+ fparg_count
) * sizeof(long);
893 if ((intarg_count
+ 2 * fparg_count
) > NUM_GPR_ARG_REGISTERS
)
894 bytes
+= (intarg_count
+ 2 * fparg_count
) * sizeof(long);
897 bytes
+= NUM_GPR_ARG_REGISTERS
* sizeof(long);
899 /* The stack space allocated needs to be a multiple of 16 bytes. */
900 bytes
= ALIGN(bytes
, 16) ;
908 extern void ffi_call_AIX(extended_cif
*, long, unsigned, unsigned *,
909 void (*fn
)(void), void (*fn2
)(void));
911 extern void ffi_call_DARWIN(extended_cif
*, long, unsigned, unsigned *,
912 void (*fn
)(void), void (*fn2
)(void), ffi_type
*);
915 ffi_call (ffi_cif
*cif
, void (*fn
)(void), void *rvalue
, void **avalue
)
920 ecif
.avalue
= avalue
;
922 /* If the return value is a struct and we don't have a return
923 value address then we need to make one. */
925 if ((rvalue
== NULL
) &&
926 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
928 ecif
.rvalue
= alloca (cif
->rtype
->size
);
931 ecif
.rvalue
= rvalue
;
936 ffi_call_AIX(&ecif
, -(long)cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
,
937 FFI_FN(ffi_prep_args
));
940 ffi_call_DARWIN(&ecif
, -(long)cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
,
941 FFI_FN(ffi_prep_args
), cif
->rtype
);
949 static void flush_icache(char *);
950 static void flush_range(char *, int);
952 /* The layout of a function descriptor. A C function pointer really
953 points to one of these. */
955 typedef struct aix_fd_struct
{
960 /* here I'd like to add the stack frame layout we use in darwin_closure.S
965 The stack layout looks like this:
967 | Additional params... | | Higher address
969 | Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS
970 |--------------------------------------------| |
971 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
972 |--------------------------------------------| |
974 |--------------------------------------------| |
975 | Space for callee's LR 4/8 | |
976 |--------------------------------------------| |
977 | Saved CR [low word for m64] 4/8 | |
978 |--------------------------------------------| |
979 | Current backchain pointer 4/8 |-/ Parent's frame.
980 |--------------------------------------------| <+ <<< on entry to ffi_closure_ASM
981 | Result Bytes 16 | |
982 |--------------------------------------------| |
983 ~ padding to 16-byte alignment ~ ~
984 |--------------------------------------------| |
985 | NUM_FPR_ARG_REGISTERS slots | |
986 | here fp13 .. fp1 13*8 | |
987 |--------------------------------------------| |
988 | R3..R10 8*4/8=32/64 | | NUM_GPR_ARG_REGISTERS
989 |--------------------------------------------| |
990 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
991 |--------------------------------------------| | stack |
992 | Reserved [compiler,binder] 2*4/8 | | grows |
993 |--------------------------------------------| | down V
994 | Space for callee's LR 4/8 | |
995 |--------------------------------------------| | lower addresses
996 | Saved CR [low word for m64] 4/8 | |
997 |--------------------------------------------| | stack pointer here
998 | Current backchain pointer 4/8 |-/ during
999 |--------------------------------------------| <<< ffi_closure_ASM.
1004 ffi_prep_closure_loc (ffi_closure
* closure
,
1006 void (*fun
)(ffi_cif
*, void*, void**, void*),
1010 unsigned int *tramp
;
1011 struct ffi_aix_trampoline_struct
*tramp_aix
;
1018 FFI_ASSERT (cif
->abi
== FFI_DARWIN
);
1020 tramp
= (unsigned int *) &closure
->tramp
[0];
1021 #if defined(POWERPC_DARWIN64)
1022 tramp
[0] = 0x7c0802a6; /* mflr r0 */
1023 tramp
[1] = 0x429f0015; /* bcl- 20,4*cr7+so, +0x18 (L1) */
1024 /* We put the addresses here. */
1025 tramp
[6] = 0x7d6802a6; /*L1: mflr r11 */
1026 tramp
[7] = 0xe98b0000; /* ld r12,0(r11) function address */
1027 tramp
[8] = 0x7c0803a6; /* mtlr r0 */
1028 tramp
[9] = 0x7d8903a6; /* mtctr r12 */
1029 tramp
[10] = 0xe96b0008; /* lwz r11,8(r11) static chain */
1030 tramp
[11] = 0x4e800420; /* bctr */
1032 *((unsigned long *)&tramp
[2]) = (unsigned long) ffi_closure_ASM
; /* function */
1033 *((unsigned long *)&tramp
[4]) = (unsigned long) codeloc
; /* context */
1035 tramp
[0] = 0x7c0802a6; /* mflr r0 */
1036 tramp
[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
1037 tramp
[4] = 0x7d6802a6; /* mflr r11 */
1038 tramp
[5] = 0x818b0000; /* lwz r12,0(r11) function address */
1039 tramp
[6] = 0x7c0803a6; /* mtlr r0 */
1040 tramp
[7] = 0x7d8903a6; /* mtctr r12 */
1041 tramp
[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
1042 tramp
[9] = 0x4e800420; /* bctr */
1043 tramp
[2] = (unsigned long) ffi_closure_ASM
; /* function */
1044 tramp
[3] = (unsigned long) codeloc
; /* context */
1048 closure
->user_data
= user_data
;
1050 /* Flush the icache. Only necessary on Darwin. */
1051 flush_range(codeloc
, FFI_TRAMPOLINE_SIZE
);
1057 tramp_aix
= (struct ffi_aix_trampoline_struct
*) (closure
->tramp
);
1058 fd
= (aix_fd
*)(void *)ffi_closure_ASM
;
1060 FFI_ASSERT (cif
->abi
== FFI_AIX
);
1062 tramp_aix
->code_pointer
= fd
->code_pointer
;
1063 tramp_aix
->toc
= fd
->toc
;
1064 tramp_aix
->static_chain
= codeloc
;
1067 closure
->user_data
= user_data
;
1078 flush_icache(char *addr
)
1087 : : "r"(addr
) : "memory");
1092 flush_range(char * addr1
, int size
)
1094 #define MIN_LINE_SIZE 32
1096 for (i
= 0; i
< size
; i
+= MIN_LINE_SIZE
)
1097 flush_icache(addr1
+i
);
1098 flush_icache(addr1
+size
-1);
1108 ffi_closure_helper_DARWIN (ffi_closure
*, void *,
1109 unsigned long *, ffi_dblfl
*);
1111 /* Basically the trampoline invokes ffi_closure_ASM, and on
1112 entry, r11 holds the address of the closure.
1113 After storing the registers that could possibly contain
1114 parameters to be passed into the stack frame and setting
1115 up space for a return value, ffi_closure_ASM invokes the
1116 following helper function to do most of the work. */
1119 ffi_closure_helper_DARWIN (ffi_closure
*closure
, void *rvalue
,
1120 unsigned long *pgr
, ffi_dblfl
*pfr
)
1122 /* rvalue is the pointer to space for return value in closure assembly
1123 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
1124 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
1126 typedef double ldbits
[2];
1135 ffi_type
** arg_types
;
1138 ffi_dblfl
* end_pfr
= pfr
+ NUM_FPR_ARG_REGISTERS
;
1140 #if defined(POWERPC_DARWIN64)
1141 unsigned fpsused
= 0;
1145 avalue
= alloca (cif
->nargs
* sizeof(void *));
1147 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
1149 #if defined(POWERPC_DARWIN64)
1150 if (!darwin64_struct_ret_by_value_p (cif
->rtype
))
1152 /* Won't fit into the regs - return by ref. */
1153 rvalue
= (void *) *pgr
;
1156 #elif defined(DARWIN_PPC)
1157 if (cif
->rtype
->size
> 4)
1159 rvalue
= (void *) *pgr
;
1162 #else /* assume we return by ref. */
1163 rvalue
= (void *) *pgr
;
1170 arg_types
= cif
->arg_types
;
1172 /* Grab the addresses of the arguments from the stack frame. */
1175 switch (arg_types
[i
]->type
)
1177 case FFI_TYPE_SINT8
:
1178 case FFI_TYPE_UINT8
:
1179 #if defined(POWERPC64)
1180 avalue
[i
] = (char *) pgr
+ 7;
1182 avalue
[i
] = (char *) pgr
+ 3;
1187 case FFI_TYPE_SINT16
:
1188 case FFI_TYPE_UINT16
:
1189 #if defined(POWERPC64)
1190 avalue
[i
] = (char *) pgr
+ 6;
1192 avalue
[i
] = (char *) pgr
+ 2;
1197 case FFI_TYPE_SINT32
:
1198 case FFI_TYPE_UINT32
:
1199 #if defined(POWERPC64)
1200 avalue
[i
] = (char *) pgr
+ 4;
1202 case FFI_TYPE_POINTER
:
1208 case FFI_TYPE_STRUCT
:
1209 size_al
= arg_types
[i
]->size
;
1210 #if defined(POWERPC_DARWIN64)
1211 pgr
= (unsigned long *)ALIGN((char *)pgr
, arg_types
[i
]->alignment
);
1212 if (size_al
< 3 || size_al
== 4)
1214 avalue
[i
] = ((char *)pgr
)+8-size_al
;
1215 if (arg_types
[i
]->elements
[0]->type
== FFI_TYPE_FLOAT
1216 && fpsused
< NUM_FPR_ARG_REGISTERS
)
1218 *(float *)pgr
= (float) *(double *)pfr
;
1227 darwin64_struct_floats_to_mem (arg_types
[i
], (char *)pgr
,
1228 (double *)pfr
, &fpsused
);
1231 pgr
+= (size_al
+ 7) / 8;
1233 /* If the first member of the struct is a double, then align
1234 the struct to double-word. */
1235 if (arg_types
[i
]->elements
[0]->type
== FFI_TYPE_DOUBLE
)
1236 size_al
= ALIGN(arg_types
[i
]->size
, 8);
1237 # if defined(POWERPC64)
1238 FFI_ASSERT (cif
->abi
!= FFI_DARWIN
);
1240 pgr
+= (size_al
+ 7) / 8;
1242 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
1243 SI 4 bytes) are aligned as if they were those modes. */
1244 if (size_al
< 3 && cif
->abi
== FFI_DARWIN
)
1245 avalue
[i
] = (char*) pgr
+ 4 - size_al
;
1248 pgr
+= (size_al
+ 3) / 4;
1253 case FFI_TYPE_SINT64
:
1254 case FFI_TYPE_UINT64
:
1255 #if defined(POWERPC64)
1256 case FFI_TYPE_POINTER
:
1261 /* Long long ints are passed in two gpr's. */
1267 case FFI_TYPE_FLOAT
:
1268 /* A float value consumes a GPR.
1269 There are 13 64bit floating point registers. */
1272 double temp
= pfr
->d
;
1273 pfr
->f
= (float) temp
;
1284 case FFI_TYPE_DOUBLE
:
1285 /* A double value consumes two GPRs.
1286 There are 13 64bit floating point registers. */
1303 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1305 case FFI_TYPE_LONGDOUBLE
:
1307 if (pfr
+ 1 < end_pfr
)
1316 *pgr
= *(unsigned long *) pfr
;
1322 #else /* POWERPC64 */
1323 /* A long double value consumes four GPRs and two FPRs.
1324 There are 13 64bit floating point registers. */
1325 if (pfr
+ 1 < end_pfr
)
1330 /* Here we have the situation where one part of the long double
1331 is stored in fpr13 and the other part is already on the stack.
1332 We use a union to pass the long double to avalue[i]. */
1333 else if (pfr
+ 1 == end_pfr
)
1336 memcpy (&temp_ld
.lb
[0], pfr
, sizeof(ldbits
));
1337 memcpy (&temp_ld
.lb
[1], pgr
+ 2, sizeof(ldbits
));
1338 avalue
[i
] = &temp_ld
.ld
;
1346 #endif /* POWERPC64 */
1355 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
1357 /* Tell ffi_closure_ASM to perform return type promotions. */