* src/powerpc/ffi_darwin.c (ffi_prep_args): Fix typo in INT64
[official-gcc.git] / libffi / src / powerpc / ffi_darwin.c
blob01550a396d890e0fea1d1a4229914a8e05fc7aca
1 /* -----------------------------------------------------------------------
2 ffi_darwin.c
4 Copyright (C) 1998 Geoffrey Keating
5 Copyright (C) 2001 John Hornkvist
6 Copyright (C) 2002, 2006, 2007, 2009 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 ----------------------------------------------------------------------- */
30 #include <ffi.h>
31 #include <ffi_common.h>
33 #include <stdlib.h>
35 extern void ffi_closure_ASM(void);
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),
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)
50 /* About the DARWIN ABI. */
51 enum {
52 NUM_GPR_ARG_REGISTERS = 8,
53 NUM_FPR_ARG_REGISTERS = 13
55 enum { ASM_NEEDS_REGISTERS = 4 };
57 /* ffi_prep_args is called by the assembly routine once stack space
58 has been allocated for the function's arguments.
60 The stack layout we want looks like this:
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
83 void ffi_prep_args(extended_cif *ecif, unsigned long *const stack)
85 const unsigned bytes = ecif->cif->bytes;
86 const unsigned flags = ecif->cif->flags;
88 /* 'stacktop' points at the previous backchain pointer. */
89 unsigned long *const stacktop = stack + (bytes / sizeof(unsigned long));
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)
94 - NUM_FPR_ARG_REGISTERS);
95 int fparg_count = 0;
98 /* 'next_arg' grows up as we put parameters in it. */
99 unsigned long *next_arg = (unsigned long *) stack + 6; /* 6 reserved positions. */
101 int i;
102 double double_tmp;
103 void **p_argv = ecif->avalue;
104 unsigned long gprvalue;
105 ffi_type** ptr = ecif->cif->arg_types;
106 char *dest_cpy;
107 unsigned size_al = 0;
109 /* Check that everything starts aligned properly. */
110 FFI_ASSERT(((unsigned)(char *)stack & 0xF) == 0);
111 FFI_ASSERT(((unsigned)(char *)stacktop & 0xF) == 0);
112 FFI_ASSERT((bytes & 0xF) == 0);
114 /* Deal with return values that are actually pass-by-reference.
115 Rule:
116 Return values are referenced by r3, so r4 is the first parameter. */
118 if (flags & FLAG_RETVAL_REFERENCE)
119 *next_arg++ = (unsigned long)(char *)ecif->rvalue;
121 /* Now for the arguments. */
122 for (i = ecif->cif->nargs; i > 0; i--, ptr++, p_argv++)
124 switch ((*ptr)->type)
126 /* If a floating-point parameter appears before all of the general-
127 purpose registers are filled, the corresponding GPRs that match
128 the size of the floating-point parameter are skipped. */
129 case FFI_TYPE_FLOAT:
130 double_tmp = *(float *)*p_argv;
131 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
132 *(double *)next_arg = double_tmp;
133 else
134 *fpr_base++ = double_tmp;
135 next_arg++;
136 fparg_count++;
137 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
138 break;
140 case FFI_TYPE_DOUBLE:
141 double_tmp = *(double *)*p_argv;
142 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
143 *(double *)next_arg = double_tmp;
144 else
145 *fpr_base++ = double_tmp;
146 #ifdef POWERPC64
147 next_arg++;
148 #else
149 next_arg += 2;
150 #endif
151 fparg_count++;
152 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
153 break;
155 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
157 case FFI_TYPE_LONGDOUBLE:
158 #ifdef POWERPC64
159 if (fparg_count < NUM_FPR_ARG_REGISTERS)
160 *(long double *) fpr_base++ = *(long double *) *p_argv;
161 else
162 *(long double *) next_arg = *(long double *) *p_argv;
163 next_arg += 2;
164 fparg_count += 2;
165 #else
166 double_tmp = *((double *) *p_argv);
167 if (fparg_count < NUM_FPR_ARG_REGISTERS)
168 *fpr_base++ = double_tmp;
169 else
170 *(double *) next_arg = double_tmp;
172 double_tmp = ((double *) *p_argv)[1];
173 if (fparg_count < NUM_FPR_ARG_REGISTERS)
174 *fpr_base++ = double_tmp;
175 else
176 *(double *) next_arg = double_tmp;
177 next_arg += 2;
178 fparg_count++;
179 #endif
180 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
181 break;
182 #endif
183 case FFI_TYPE_UINT64:
184 case FFI_TYPE_SINT64:
185 #ifdef POWERPC64
186 gprvalue = *(long long *) *p_argv;
187 goto putgpr;
188 #else
189 *(long long *) next_arg = *(long long *) *p_argv;
190 next_arg+=2;
191 #endif
192 break;
193 case FFI_TYPE_POINTER:
194 gprvalue = *(unsigned long *) *p_argv;
195 goto putgpr;
196 case FFI_TYPE_UINT8:
197 gprvalue = *(unsigned char *) *p_argv;
198 goto putgpr;
199 case FFI_TYPE_SINT8:
200 gprvalue = *(signed char *) *p_argv;
201 goto putgpr;
202 case FFI_TYPE_UINT16:
203 gprvalue = *(unsigned short *) *p_argv;
204 goto putgpr;
205 case FFI_TYPE_SINT16:
206 gprvalue = *(signed short *) *p_argv;
207 goto putgpr;
209 case FFI_TYPE_STRUCT:
210 #ifdef POWERPC64
211 dest_cpy = (char *) next_arg;
212 size_al = (*ptr)->size;
213 if ((*ptr)->elements[0]->type == 3)
214 size_al = ALIGN((*ptr)->size, 8);
215 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
216 dest_cpy += 4 - size_al;
218 memcpy ((char *) dest_cpy, (char *) *p_argv, size_al);
219 next_arg += (size_al + 7) / 8;
220 #else
221 dest_cpy = (char *) next_arg;
223 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
224 SI 4 bytes) are aligned as if they were those modes.
225 Structures with 3 byte in size are padded upwards. */
226 size_al = (*ptr)->size;
227 /* If the first member of the struct is a double, then align
228 the struct to double-word.
229 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
230 if ((*ptr)->elements[0]->type == 3)
231 size_al = ALIGN((*ptr)->size, 8);
232 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
233 dest_cpy += 4 - size_al;
235 memcpy((char *)dest_cpy, (char *)*p_argv, size_al);
236 next_arg += (size_al + 3) / 4;
237 #endif
238 break;
240 case FFI_TYPE_INT:
241 case FFI_TYPE_SINT32:
242 gprvalue = *(signed int *) *p_argv;
243 goto putgpr;
245 case FFI_TYPE_UINT32:
246 gprvalue = *(unsigned int *) *p_argv;
247 putgpr:
248 *next_arg++ = gprvalue;
249 break;
250 default:
251 break;
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);
262 /* Adjust the size of S to be correct for Darwin.
263 On Darwin, the first field of a structure has natural alignment. */
265 static void
266 darwin_adjust_aggregate_sizes (ffi_type *s)
268 int i;
270 if (s->type != FFI_TYPE_STRUCT)
271 return;
273 s->size = 0;
274 for (i = 0; s->elements[i] != NULL; i++)
276 ffi_type *p;
277 int align;
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;
294 s->size = ALIGN(s->size, s->alignment);
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. */
304 /* Perform machine dependent cif processing. */
305 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
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;
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. */
319 if (cif->abi == FFI_DARWIN)
321 darwin_adjust_aggregate_sizes (cif->rtype);
322 for (i = 0; i < cif->nargs; i++)
323 darwin_adjust_aggregate_sizes (cif->arg_types[i]);
326 /* Space for the frame pointer, callee's LR, CR, etc, and for
327 the asm's temp regs. */
329 bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
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)
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
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;
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;
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;
375 default:
376 /* Returns 32-bit integer, or similar. Nothing to do here. */
377 break;
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++)
386 switch ((*ptr)->type)
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;
398 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
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
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;
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;
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;
444 if (fparg_count != 0)
445 flags |= FLAG_FP_ARGUMENTS;
447 /* Space for the FPR registers, if needed. */
448 if (fparg_count != 0)
449 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
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);
462 /* The stack space allocated needs to be a multiple of 16 bytes. */
463 bytes = (bytes + 15) & ~0xF;
465 cif->flags = flags;
466 cif->bytes = bytes;
468 return FFI_OK;
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));
476 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
478 extended_cif ecif;
480 ecif.cif = cif;
481 ecif.avalue = avalue;
483 /* If the return value is a struct and we don't have a return
484 value address then we need to make one. */
486 if ((rvalue == NULL) &&
487 (cif->rtype->type == FFI_TYPE_STRUCT))
489 ecif.rvalue = alloca(cif->rtype->size);
491 else
492 ecif.rvalue = rvalue;
494 switch (cif->abi)
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;
510 static void flush_icache(char *);
511 static void flush_range(char *, int);
513 /* The layout of a function descriptor. A C function pointer really
514 points to one of these. */
516 typedef struct aix_fd_struct {
517 void *code_pointer;
518 void *toc;
519 } aix_fd;
521 /* here I'd like to add the stack frame layout we use in darwin_closure.S
522 and aix_clsoure.S
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
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)
581 unsigned int *tramp;
582 struct ffi_aix_trampoline_struct *tramp_aix;
583 aix_fd *fd;
585 switch (cif->abi)
587 case FFI_DARWIN:
589 FFI_ASSERT (cif->abi == FFI_DARWIN);
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 */
603 closure->cif = cif;
604 closure->fun = fun;
605 closure->user_data = user_data;
607 /* Flush the icache. Only necessary on Darwin. */
608 flush_range(codeloc, FFI_TRAMPOLINE_SIZE);
610 break;
612 case FFI_AIX:
614 tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
615 fd = (aix_fd *)(void *)ffi_closure_ASM;
617 FFI_ASSERT (cif->abi == FFI_AIX);
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;
626 default:
628 FFI_ASSERT(0);
629 break;
631 return FFI_OK;
634 static void
635 flush_icache(char *addr)
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
648 static void
649 flush_range(char * addr1, int size)
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);
658 typedef union
660 float f;
661 double d;
662 } ffi_dblfl;
664 int ffi_closure_helper_DARWIN (ffi_closure*, void*,
665 unsigned long*, ffi_dblfl*);
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. */
674 int ffi_closure_helper_DARWIN (ffi_closure* closure, void * rvalue,
675 unsigned long * pgr, ffi_dblfl * pfr)
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. */
681 typedef double ldbits[2];
683 union ldu
685 ldbits lb;
686 long double ld;
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;
696 cif = closure->cif;
697 avalue = alloca(cif->nargs * sizeof(void *));
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)
703 rvalue = (void *) *pgr;
704 pgr++;
707 i = 0;
708 avn = cif->nargs;
709 arg_types = cif->arg_types;
711 /* Grab the addresses of the arguments from the stack frame. */
712 while (i < avn)
714 switch (arg_types[i]->type)
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;
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;
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;
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;
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
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)
792 double temp = pfr->d;
793 pfr->f = (float) temp;
794 avalue[i] = pfr;
795 pfr++;
797 else
799 avalue[i] = pgr;
801 pgr++;
802 break;
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)
809 avalue[i] = pfr;
810 pfr++;
812 else
814 avalue[i] = pgr;
816 #ifdef POWERPC64
817 pgr++;
818 #else
819 pgr += 2;
820 #endif
821 break;
823 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
825 case FFI_TYPE_LONGDOUBLE:
826 #ifdef POWERPC64
827 if (pfr + 1 < end_pfr)
829 avalue[i] = pfr;
830 pfr += 2;
832 else
834 if (pfr < end_pfr)
836 *pgr = *(unsigned long *) pfr;
837 pfr++;
839 avalue[i] = pgr;
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)
847 avalue[i] = pfr;
848 pfr += 2;
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)
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;
860 else
862 avalue[i] = pgr;
864 pgr += 4;
865 #endif /* POWERPC64 */
866 break;
867 #endif
868 default:
869 FFI_ASSERT(0);
871 i++;
874 (closure->fun) (cif, rvalue, avalue, closure->user_data);
876 /* Tell ffi_closure_ASM to perform return type promotions. */
877 return cif->rtype->type;