Update missing changes for 3.0.9r4.
[libffi.git] / src / powerpc / ffi_darwin.c
1 /* -----------------------------------------------------------------------
2 ffi_darwin.c
3
4 Copyright (C) 1998 Geoffrey Keating
5 Copyright (C) 2001 John Hornkvist
6 Copyright (C) 2002, 2006, 2007, 2009 Free Software Foundation, Inc.
7
8 FFI support for Darwin and AIX.
9
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:
17
18 The above copyright notice and this permission notice shall be included
19 in all copies or substantial portions of the Software.
20
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 ----------------------------------------------------------------------- */
29
30 #include <ffi.h>
31 #include <ffi_common.h>
32
33 #include <stdlib.h>
34
35 extern void ffi_closure_ASM(void);
36
37 enum {
38 /* The assembly depends on these exact flags. */
39 FLAG_RETURNS_NOTHING = 1 << (31-30), /* These go in cr7 */
40 FLAG_RETURNS_FP = 1 << (31-29),
41 FLAG_RETURNS_64BITS = 1 << (31-28),
42 FLAG_RETURNS_128BITS = 1 << (31-31),
43
44 FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
45 FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
46 FLAG_4_GPR_ARGUMENTS = 1 << (31- 5),
47 FLAG_RETVAL_REFERENCE = 1 << (31- 4)
48 };
49
50 /* About the DARWIN ABI. */
51 enum {
52 NUM_GPR_ARG_REGISTERS = 8,
53 NUM_FPR_ARG_REGISTERS = 13
54 };
55 enum { ASM_NEEDS_REGISTERS = 4 };
56
57 /* ffi_prep_args is called by the assembly routine once stack space
58 has been allocated for the function's arguments.
59
60 The stack layout we want looks like this:
61
62 | Return address from ffi_call_DARWIN | higher addresses
63 |--------------------------------------------|
64 | Previous backchain pointer 4 | stack pointer here
65 |--------------------------------------------|<+ <<< on entry to
66 | Saved r28-r31 4*4 | | ffi_call_DARWIN
67 |--------------------------------------------| |
68 | Parameters (at least 8*4=32) | |
69 |--------------------------------------------| |
70 | Space for GPR2 4 | |
71 |--------------------------------------------| | stack |
72 | Reserved 2*4 | | grows |
73 |--------------------------------------------| | down V
74 | Space for callee's LR 4 | |
75 |--------------------------------------------| | lower addresses
76 | Saved CR 4 | |
77 |--------------------------------------------| | stack pointer here
78 | Current backchain pointer 4 |-/ during
79 |--------------------------------------------| <<< ffi_call_DARWIN
80
81 */
82
83 void ffi_prep_args(extended_cif *ecif, unsigned long *const stack)
84 {
85 const unsigned bytes = ecif->cif->bytes;
86 const unsigned flags = ecif->cif->flags;
87
88 /* 'stacktop' points at the previous backchain pointer. */
89 unsigned long *const stacktop = stack + (bytes / sizeof(unsigned long));
90
91 /* 'fpr_base' points at the space for fpr1, and grows upwards as
92 we use FPR registers. */
93 double *fpr_base = (double *) (stacktop - ASM_NEEDS_REGISTERS) - NUM_FPR_ARG_REGISTERS;
94 int fparg_count = 0;
95
96
97 /* 'next_arg' grows up as we put parameters in it. */
98 unsigned long *next_arg = stack + 6; /* 6 reserved positions. */
99
100 int i;
101 double double_tmp;
102 void **p_argv = ecif->avalue;
103 unsigned long gprvalue;
104 ffi_type** ptr = ecif->cif->arg_types;
105 char *dest_cpy;
106 unsigned size_al = 0;
107
108 /* Check that everything starts aligned properly. */
109 FFI_ASSERT(((unsigned) (char *) stack & 0xF) == 0);
110 FFI_ASSERT(((unsigned) (char *) stacktop & 0xF) == 0);
111 FFI_ASSERT((bytes & 0xF) == 0);
112
113 /* Deal with return values that are actually pass-by-reference.
114 Rule:
115 Return values are referenced by r3, so r4 is the first parameter. */
116
117 if (flags & FLAG_RETVAL_REFERENCE)
118 *next_arg++ = (unsigned long) (char *) ecif->rvalue;
119
120 /* Now for the arguments. */
121 for (i = ecif->cif->nargs; i > 0; i--, ptr++, p_argv++)
122 {
123 switch ((*ptr)->type)
124 {
125 /* If a floating-point parameter appears before all of the general-
126 purpose registers are filled, the corresponding GPRs that match
127 the size of the floating-point parameter are skipped. */
128 case FFI_TYPE_FLOAT:
129 double_tmp = *(float *) *p_argv;
130 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
131 *(double *)next_arg = double_tmp;
132 else
133 *fpr_base++ = double_tmp;
134 next_arg++;
135 fparg_count++;
136 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
137 break;
138
139 case FFI_TYPE_DOUBLE:
140 double_tmp = *(double *) *p_argv;
141 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
142 *(double *)next_arg = double_tmp;
143 else
144 *fpr_base++ = double_tmp;
145 #ifdef POWERPC64
146 next_arg++;
147 #else
148 next_arg += 2;
149 #endif
150 fparg_count++;
151 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
152 break;
153
154 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
155
156 case FFI_TYPE_LONGDOUBLE:
157 #ifdef POWERPC64
158 if (fparg_count < NUM_FPR_ARG_REGISTERS)
159 *(long double *) fpr_base++ = *(long double *) *p_argv;
160 else
161 *(long double *) next_arg = *(long double *) *p_argv;
162 next_arg += 2;
163 fparg_count += 2;
164 #else
165 double_tmp = ((double *) *p_argv)[0];
166 if (fparg_count < NUM_FPR_ARG_REGISTERS)
167 *fpr_base++ = double_tmp;
168 else
169 *(double *) next_arg = double_tmp;
170 next_arg += 2;
171 fparg_count++;
172
173 double_tmp = ((double *) *p_argv)[1];
174 if (fparg_count < NUM_FPR_ARG_REGISTERS)
175 *fpr_base++ = double_tmp;
176 else
177 *(double *) next_arg = double_tmp;
178 next_arg += 2;
179 fparg_count++;
180 #endif
181 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
182 break;
183 #endif
184 case FFI_TYPE_UINT64:
185 case FFI_TYPE_SINT64:
186 #ifdef POWERPC64
187 gprvalue = *(long long *) *p_argv;
188 goto putgpr;
189 #else
190 *(long long *) next_arg = *(long long *) *p_argv;
191 next_arg += 2;
192 #endif
193 break;
194 case FFI_TYPE_POINTER:
195 gprvalue = *(unsigned long *) *p_argv;
196 goto putgpr;
197 case FFI_TYPE_UINT8:
198 gprvalue = *(unsigned char *) *p_argv;
199 goto putgpr;
200 case FFI_TYPE_SINT8:
201 gprvalue = *(signed char *) *p_argv;
202 goto putgpr;
203 case FFI_TYPE_UINT16:
204 gprvalue = *(unsigned short *) *p_argv;
205 goto putgpr;
206 case FFI_TYPE_SINT16:
207 gprvalue = *(signed short *) *p_argv;
208 goto putgpr;
209
210 case FFI_TYPE_STRUCT:
211 #ifdef POWERPC64
212 dest_cpy = (char *) next_arg;
213 size_al = (*ptr)->size;
214 if ((*ptr)->elements[0]->type == 3)
215 size_al = ALIGN((*ptr)->size, 8);
216 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
217 dest_cpy += 4 - size_al;
218
219 memcpy ((char *) dest_cpy, (char *) *p_argv, size_al);
220 next_arg += (size_al + 7) / 8;
221 #else
222 dest_cpy = (char *) next_arg;
223
224 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
225 SI 4 bytes) are aligned as if they were those modes.
226 Structures with 3 byte in size are padded upwards. */
227 size_al = (*ptr)->size;
228 /* If the first member of the struct is a double, then align
229 the struct to double-word. */
230 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
231 size_al = ALIGN((*ptr)->size, 8);
232 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
233 dest_cpy += 4 - size_al;
234
235 memcpy((char *) dest_cpy, (char *) *p_argv, size_al);
236 next_arg += (size_al + 3) / 4;
237 #endif
238 break;
239
240 case FFI_TYPE_INT:
241 case FFI_TYPE_SINT32:
242 gprvalue = *(signed int *) *p_argv;
243 goto putgpr;
244
245 case FFI_TYPE_UINT32:
246 gprvalue = *(unsigned int *) *p_argv;
247 putgpr:
248 *next_arg++ = gprvalue;
249 break;
250 default:
251 break;
252 }
253 }
254
255 /* Check that we didn't overrun the stack... */
256 //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
257 //FFI_ASSERT((unsigned *)fpr_base
258 // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
259 //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
260 }
261
262 /* Adjust the size of S to be correct for Darwin.
263 On Darwin, the first field of a structure has natural alignment. */
264
265 static void
266 darwin_adjust_aggregate_sizes (ffi_type *s)
267 {
268 int i;
269
270 if (s->type != FFI_TYPE_STRUCT)
271 return;
272
273 s->size = 0;
274 for (i = 0; s->elements[i] != NULL; i++)
275 {
276 ffi_type *p;
277 int align;
278
279 p = s->elements[i];
280 darwin_adjust_aggregate_sizes (p);
281 if (i == 0
282 && (p->type == FFI_TYPE_UINT64
283 || p->type == FFI_TYPE_SINT64
284 || p->type == FFI_TYPE_DOUBLE
285 || p->alignment == 8))
286 align = 8;
287 else if (p->alignment == 16 || p->alignment < 4)
288 align = p->alignment;
289 else
290 align = 4;
291 s->size = ALIGN(s->size, align) + p->size;
292 }
293
294 s->size = ALIGN(s->size, s->alignment);
295
296 if (s->elements[0]->type == FFI_TYPE_UINT64
297 || s->elements[0]->type == FFI_TYPE_SINT64
298 || s->elements[0]->type == FFI_TYPE_DOUBLE
299 || s->elements[0]->alignment == 8)
300 s->alignment = s->alignment > 8 ? s->alignment : 8;
301 /* Do not add additional tail padding. */
302 }
303
304 /* Perform machine dependent cif processing. */
305 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
306 {
307 /* All this is for the DARWIN ABI. */
308 int i;
309 ffi_type **ptr;
310 unsigned bytes;
311 int fparg_count = 0, intarg_count = 0;
312 unsigned flags = 0;
313 unsigned size_al = 0;
314
315 /* All the machine-independent calculation of cif->bytes will be wrong.
316 All the calculation of structure sizes will also be wrong.
317 Redo the calculation for DARWIN. */
318
319 if (cif->abi == FFI_DARWIN)
320 {
321 darwin_adjust_aggregate_sizes (cif->rtype);
322 for (i = 0; i < cif->nargs; i++)
323 darwin_adjust_aggregate_sizes (cif->arg_types[i]);
324 }
325
326 /* Space for the frame pointer, callee's LR, CR, etc, and for
327 the asm's temp regs. */
328
329 bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
330
331 /* Return value handling. The rules are as follows:
332 - 32-bit (or less) integer values are returned in gpr3;
333 - Structures of size <= 4 bytes also returned in gpr3;
334 - 64-bit integer values and structures between 5 and 8 bytes are returned
335 in gpr3 and gpr4;
336 - Single/double FP values are returned in fpr1;
337 - Long double FP (if not equivalent to double) values are returned in
338 fpr1 and fpr2;
339 - Larger structures values are allocated space and a pointer is passed
340 as the first argument. */
341 switch (cif->rtype->type)
342 {
343
344 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
345 case FFI_TYPE_LONGDOUBLE:
346 flags |= FLAG_RETURNS_128BITS;
347 flags |= FLAG_RETURNS_FP;
348 break;
349 #endif
350
351 case FFI_TYPE_DOUBLE:
352 flags |= FLAG_RETURNS_64BITS;
353 /* Fall through. */
354 case FFI_TYPE_FLOAT:
355 flags |= FLAG_RETURNS_FP;
356 break;
357
358 case FFI_TYPE_UINT64:
359 case FFI_TYPE_SINT64:
360 #ifdef POWERPC64
361 case FFI_TYPE_POINTER:
362 #endif
363 flags |= FLAG_RETURNS_64BITS;
364 break;
365
366 case FFI_TYPE_STRUCT:
367 flags |= FLAG_RETVAL_REFERENCE;
368 flags |= FLAG_RETURNS_NOTHING;
369 intarg_count++;
370 break;
371 case FFI_TYPE_VOID:
372 flags |= FLAG_RETURNS_NOTHING;
373 break;
374
375 default:
376 /* Returns 32-bit integer, or similar. Nothing to do here. */
377 break;
378 }
379
380 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
381 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
382 goes on the stack. Structures are passed as a pointer to a copy of
383 the structure. Stuff on the stack needs to keep proper alignment. */
384 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
385 {
386 switch ((*ptr)->type)
387 {
388 case FFI_TYPE_FLOAT:
389 case FFI_TYPE_DOUBLE:
390 fparg_count++;
391 /* If this FP arg is going on the stack, it must be
392 8-byte-aligned. */
393 if (fparg_count > NUM_FPR_ARG_REGISTERS
394 && intarg_count%2 != 0)
395 intarg_count++;
396 break;
397
398 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
399
400 case FFI_TYPE_LONGDOUBLE:
401 fparg_count += 2;
402 /* If this FP arg is going on the stack, it must be
403 8-byte-aligned. */
404 if (fparg_count > NUM_FPR_ARG_REGISTERS
405 && intarg_count%2 != 0)
406 intarg_count++;
407 intarg_count +=2;
408 break;
409 #endif
410
411 case FFI_TYPE_UINT64:
412 case FFI_TYPE_SINT64:
413 /* 'long long' arguments are passed as two words, but
414 either both words must fit in registers or both go
415 on the stack. If they go on the stack, they must
416 be 8-byte-aligned. */
417 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
418 || (intarg_count >= NUM_GPR_ARG_REGISTERS && intarg_count%2 != 0))
419 intarg_count++;
420 intarg_count += 2;
421 break;
422
423 case FFI_TYPE_STRUCT:
424 size_al = (*ptr)->size;
425 /* If the first member of the struct is a double, then align
426 the struct to double-word. */
427 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
428 size_al = ALIGN((*ptr)->size, 8);
429 #ifdef POWERPC64
430 intarg_count += (size_al + 7) / 8;
431 #else
432 intarg_count += (size_al + 3) / 4;
433 #endif
434 break;
435
436 default:
437 /* Everything else is passed as a 4-byte word in a GPR, either
438 the object itself or a pointer to it. */
439 intarg_count++;
440 break;
441 }
442 }
443
444 if (fparg_count != 0)
445 flags |= FLAG_FP_ARGUMENTS;
446
447 /* Space for the FPR registers, if needed. */
448 if (fparg_count != 0)
449 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
450
451 /* Stack space. */
452 #ifdef POWERPC64
453 if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS)
454 bytes += (intarg_count + fparg_count) * sizeof(long);
455 #else
456 if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
457 bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
458 #endif
459 else
460 bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
461
462 /* The stack space allocated needs to be a multiple of 16 bytes. */
463 bytes = (bytes + 15) & ~0xF;
464
465 cif->flags = flags;
466 cif->bytes = bytes;
467
468 return FFI_OK;
469 }
470
471 extern void ffi_call_AIX(extended_cif *, long, unsigned, unsigned *,
472 void (*fn)(void), void (*fn2)(void));
473 extern void ffi_call_DARWIN(extended_cif *, long, unsigned, unsigned *,
474 void (*fn)(void), void (*fn2)(void));
475
476 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
477 {
478 extended_cif ecif;
479
480 ecif.cif = cif;
481 ecif.avalue = avalue;
482
483 /* If the return value is a struct and we don't have a return
484 value address then we need to make one. */
485
486 if ((rvalue == NULL) &&
487 (cif->rtype->type == FFI_TYPE_STRUCT))
488 {
489 ecif.rvalue = alloca(cif->rtype->size);
490 }
491 else
492 ecif.rvalue = rvalue;
493
494 switch (cif->abi)
495 {
496 case FFI_AIX:
497 ffi_call_AIX(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
498 ffi_prep_args);
499 break;
500 case FFI_DARWIN:
501 ffi_call_DARWIN(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
502 ffi_prep_args);
503 break;
504 default:
505 FFI_ASSERT(0);
506 break;
507 }
508 }
509
510 static void flush_icache(char *);
511 static void flush_range(char *, int);
512
513 /* The layout of a function descriptor. A C function pointer really
514 points to one of these. */
515
516 typedef struct aix_fd_struct {
517 void *code_pointer;
518 void *toc;
519 } aix_fd;
520
521 /* here I'd like to add the stack frame layout we use in darwin_closure.S
522 and aix_clsoure.S
523
524 SP previous -> +---------------------------------------+ <--- child frame
525 | back chain to caller 4 |
526 +---------------------------------------+ 4
527 | saved CR 4 |
528 +---------------------------------------+ 8
529 | saved LR 4 |
530 +---------------------------------------+ 12
531 | reserved for compilers 4 |
532 +---------------------------------------+ 16
533 | reserved for binders 4 |
534 +---------------------------------------+ 20
535 | saved TOC pointer 4 |
536 +---------------------------------------+ 24
537 | always reserved 8*4=32 (previous GPRs)|
538 | according to the linkage convention |
539 | from AIX |
540 +---------------------------------------+ 56
541 | our FPR area 13*8=104 |
542 | f1 |
543 | . |
544 | f13 |
545 +---------------------------------------+ 160
546 | result area 8 |
547 +---------------------------------------+ 168
548 | alignement to the next multiple of 16 |
549 SP current --> +---------------------------------------+ 176 <- parent frame
550 | back chain to caller 4 |
551 +---------------------------------------+ 180
552 | saved CR 4 |
553 +---------------------------------------+ 184
554 | saved LR 4 |
555 +---------------------------------------+ 188
556 | reserved for compilers 4 |
557 +---------------------------------------+ 192
558 | reserved for binders 4 |
559 +---------------------------------------+ 196
560 | saved TOC pointer 4 |
561 +---------------------------------------+ 200
562 | always reserved 8*4=32 we store our |
563 | GPRs here |
564 | r3 |
565 | . |
566 | r10 |
567 +---------------------------------------+ 232
568 | overflow part |
569 +---------------------------------------+ xxx
570 | ???? |
571 +---------------------------------------+ xxx
572
573 */
574 ffi_status
575 ffi_prep_closure_loc (ffi_closure* closure,
576 ffi_cif* cif,
577 void (*fun)(ffi_cif*, void*, void**, void*),
578 void *user_data,
579 void *codeloc)
580 {
581 unsigned int *tramp;
582 struct ffi_aix_trampoline_struct *tramp_aix;
583 aix_fd *fd;
584
585 switch (cif->abi)
586 {
587 case FFI_DARWIN:
588
589 FFI_ASSERT (cif->abi == FFI_DARWIN);
590
591 tramp = (unsigned int *) &closure->tramp[0];
592 tramp[0] = 0x7c0802a6; /* mflr r0 */
593 tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
594 tramp[4] = 0x7d6802a6; /* mflr r11 */
595 tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */
596 tramp[6] = 0x7c0803a6; /* mtlr r0 */
597 tramp[7] = 0x7d8903a6; /* mtctr r12 */
598 tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
599 tramp[9] = 0x4e800420; /* bctr */
600 tramp[2] = (unsigned long) ffi_closure_ASM; /* function */
601 tramp[3] = (unsigned long) codeloc; /* context */
602
603 closure->cif = cif;
604 closure->fun = fun;
605 closure->user_data = user_data;
606
607 /* Flush the icache. Only necessary on Darwin. */
608 flush_range(codeloc, FFI_TRAMPOLINE_SIZE);
609
610 break;
611
612 case FFI_AIX:
613
614 tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
615 fd = (aix_fd *)(void *)ffi_closure_ASM;
616
617 FFI_ASSERT (cif->abi == FFI_AIX);
618
619 tramp_aix->code_pointer = fd->code_pointer;
620 tramp_aix->toc = fd->toc;
621 tramp_aix->static_chain = codeloc;
622 closure->cif = cif;
623 closure->fun = fun;
624 closure->user_data = user_data;
625
626 default:
627
628 FFI_ASSERT(0);
629 break;
630 }
631 return FFI_OK;
632 }
633
634 static void
635 flush_icache(char *addr)
636 {
637 #ifndef _AIX
638 __asm__ volatile (
639 "dcbf 0,%0\n"
640 "\tsync\n"
641 "\ticbi 0,%0\n"
642 "\tsync\n"
643 "\tisync"
644 : : "r"(addr) : "memory");
645 #endif
646 }
647
648 static void
649 flush_range(char * addr1, int size)
650 {
651 #define MIN_LINE_SIZE 32
652 int i;
653 for (i = 0; i < size; i += MIN_LINE_SIZE)
654 flush_icache(addr1+i);
655 flush_icache(addr1+size-1);
656 }
657
658 typedef union
659 {
660 float f;
661 double d;
662 } ffi_dblfl;
663
664 int ffi_closure_helper_DARWIN (ffi_closure*, void*,
665 unsigned long*, ffi_dblfl*);
666
667 /* Basically the trampoline invokes ffi_closure_ASM, and on
668 entry, r11 holds the address of the closure.
669 After storing the registers that could possibly contain
670 parameters to be passed into the stack frame and setting
671 up space for a return value, ffi_closure_ASM invokes the
672 following helper function to do most of the work. */
673
674 int ffi_closure_helper_DARWIN (ffi_closure* closure, void * rvalue,
675 unsigned long * pgr, ffi_dblfl * pfr)
676 {
677 /* rvalue is the pointer to space for return value in closure assembly
678 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
679 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
680
681 typedef double ldbits[2];
682
683 union ldu
684 {
685 ldbits lb;
686 long double ld;
687 };
688
689 void ** avalue;
690 ffi_type ** arg_types;
691 long i, avn;
692 ffi_cif * cif;
693 ffi_dblfl * end_pfr = pfr + NUM_FPR_ARG_REGISTERS;
694 unsigned size_al;
695
696 cif = closure->cif;
697 avalue = alloca(cif->nargs * sizeof(void *));
698
699 /* Copy the caller's structure return value address so that the closure
700 returns the data directly to the caller. */
701 if (cif->rtype->type == FFI_TYPE_STRUCT)
702 {
703 rvalue = (void *) *pgr;
704 pgr++;
705 }
706
707 i = 0;
708 avn = cif->nargs;
709 arg_types = cif->arg_types;
710
711 /* Grab the addresses of the arguments from the stack frame. */
712 while (i < avn)
713 {
714 switch (arg_types[i]->type)
715 {
716 case FFI_TYPE_SINT8:
717 case FFI_TYPE_UINT8:
718 #ifdef POWERPC64
719 avalue[i] = (char *) pgr + 7;
720 #else
721 avalue[i] = (char *) pgr + 3;
722 #endif
723 pgr++;
724 break;
725
726 case FFI_TYPE_SINT16:
727 case FFI_TYPE_UINT16:
728 #ifdef POWERPC64
729 avalue[i] = (char *) pgr + 6;
730 #else
731 avalue[i] = (char *) pgr + 2;
732 #endif
733 pgr++;
734 break;
735
736 case FFI_TYPE_SINT32:
737 case FFI_TYPE_UINT32:
738 #ifdef POWERPC64
739 avalue[i] = (char *) pgr + 4;
740 #else
741 case FFI_TYPE_POINTER:
742 avalue[i] = pgr;
743 #endif
744 pgr++;
745 break;
746
747 case FFI_TYPE_STRUCT:
748 #ifdef POWERPC64
749 size_al = arg_types[i]->size;
750 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
751 size_al = ALIGN (arg_types[i]->size, 8);
752 if (size_al < 3 && cif->abi == FFI_DARWIN)
753 avalue[i] = (void *) pgr + 8 - size_al;
754 else
755 avalue[i] = (void *) pgr;
756 pgr += (size_al + 7) / 8;
757 #else
758 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
759 SI 4 bytes) are aligned as if they were those modes. */
760 size_al = arg_types[i]->size;
761 /* If the first member of the struct is a double, then align
762 the struct to double-word. */
763 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
764 size_al = ALIGN(arg_types[i]->size, 8);
765 if (size_al < 3 && cif->abi == FFI_DARWIN)
766 avalue[i] = (void*) pgr + 4 - size_al;
767 else
768 avalue[i] = (void*) pgr;
769 pgr += (size_al + 3) / 4;
770 #endif
771 break;
772
773 case FFI_TYPE_SINT64:
774 case FFI_TYPE_UINT64:
775 #ifdef POWERPC64
776 case FFI_TYPE_POINTER:
777 avalue[i] = pgr;
778 pgr++;
779 break;
780 #else
781 /* Long long ints are passed in two gpr's. */
782 avalue[i] = pgr;
783 pgr += 2;
784 break;
785 #endif
786
787 case FFI_TYPE_FLOAT:
788 /* A float value consumes a GPR.
789 There are 13 64bit floating point registers. */
790 if (pfr < end_pfr)
791 {
792 double temp = pfr->d;
793 pfr->f = (float) temp;
794 avalue[i] = pfr;
795 pfr++;
796 }
797 else
798 {
799 avalue[i] = pgr;
800 }
801 pgr++;
802 break;
803
804 case FFI_TYPE_DOUBLE:
805 /* A double value consumes two GPRs.
806 There are 13 64bit floating point registers. */
807 if (pfr < end_pfr)
808 {
809 avalue[i] = pfr;
810 pfr++;
811 }
812 else
813 {
814 avalue[i] = pgr;
815 }
816 #ifdef POWERPC64
817 pgr++;
818 #else
819 pgr += 2;
820 #endif
821 break;
822
823 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
824
825 case FFI_TYPE_LONGDOUBLE:
826 #ifdef POWERPC64
827 if (pfr + 1 < end_pfr)
828 {
829 avalue[i] = pfr;
830 pfr += 2;
831 }
832 else
833 {
834 if (pfr < end_pfr)
835 {
836 *pgr = *(unsigned long *) pfr;
837 pfr++;
838 }
839 avalue[i] = pgr;
840 }
841 pgr += 2;
842 #else /* POWERPC64 */
843 /* A long double value consumes four GPRs and two FPRs.
844 There are 13 64bit floating point registers. */
845 if (pfr + 1 < end_pfr)
846 {
847 avalue[i] = pfr;
848 pfr += 2;
849 }
850 /* Here we have the situation where one part of the long double
851 is stored in fpr13 and the other part is already on the stack.
852 We use a union to pass the long double to avalue[i]. */
853 else if (pfr + 1 == end_pfr)
854 {
855 union ldu temp_ld;
856 memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits));
857 memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits));
858 avalue[i] = &temp_ld.ld;
859 pfr++;
860 }
861 else
862 {
863 avalue[i] = pgr;
864 }
865 pgr += 4;
866 #endif /* POWERPC64 */
867 break;
868 #endif
869 default:
870 FFI_ASSERT(0);
871 }
872 i++;
873 }
874
875 (closure->fun) (cif, rvalue, avalue, closure->user_data);
876
877 /* Tell ffi_closure_ASM to perform return type promotions. */
878 return cif->rtype->type;
879 }