* src/powerpc/ffi_darwin.c (ffi_prep_args): Copy abi and nargs to
[official-gcc.git] / libffi / src / powerpc / ffi_darwin.c
blobd84f1c393a8d5b2c6f489cf799ce74fa27b58330
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
84 ffi_prep_args (extended_cif *ecif, unsigned long *const stack)
86 const unsigned bytes = ecif->cif->bytes;
87 const unsigned flags = ecif->cif->flags;
88 const unsigned nargs = ecif->cif->nargs;
89 const ffi_abi abi = ecif->cif->abi;
91 /* 'stacktop' points at the previous backchain pointer. */
92 unsigned long *const stacktop = stack + (bytes / sizeof(unsigned long));
94 /* 'fpr_base' points at the space for fpr1, and grows upwards as
95 we use FPR registers. */
96 double *fpr_base = (double *) (stacktop - ASM_NEEDS_REGISTERS) - NUM_FPR_ARG_REGISTERS;
97 int fparg_count = 0;
100 /* 'next_arg' grows up as we put parameters in it. */
101 unsigned long *next_arg = stack + 6; /* 6 reserved positions. */
103 int i;
104 double double_tmp;
105 void **p_argv = ecif->avalue;
106 unsigned long gprvalue;
107 ffi_type** ptr = ecif->cif->arg_types;
108 char *dest_cpy;
109 unsigned size_al = 0;
111 /* Check that everything starts aligned properly. */
112 FFI_ASSERT(((unsigned) (char *) stack & 0xF) == 0);
113 FFI_ASSERT(((unsigned) (char *) stacktop & 0xF) == 0);
114 FFI_ASSERT((bytes & 0xF) == 0);
116 /* Deal with return values that are actually pass-by-reference.
117 Rule:
118 Return values are referenced by r3, so r4 is the first parameter. */
120 if (flags & FLAG_RETVAL_REFERENCE)
121 *next_arg++ = (unsigned long) (char *) ecif->rvalue;
123 /* Now for the arguments. */
124 for (i = nargs; i > 0; i--, ptr++, p_argv++)
126 switch ((*ptr)->type)
128 /* If a floating-point parameter appears before all of the general-
129 purpose registers are filled, the corresponding GPRs that match
130 the size of the floating-point parameter are skipped. */
131 case FFI_TYPE_FLOAT:
132 double_tmp = *(float *) *p_argv;
133 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
134 *(double *)next_arg = double_tmp;
135 else
136 *fpr_base++ = double_tmp;
137 next_arg++;
138 fparg_count++;
139 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
140 break;
142 case FFI_TYPE_DOUBLE:
143 double_tmp = *(double *) *p_argv;
144 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
145 *(double *)next_arg = double_tmp;
146 else
147 *fpr_base++ = double_tmp;
148 #ifdef POWERPC64
149 next_arg++;
150 #else
151 next_arg += 2;
152 #endif
153 fparg_count++;
154 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
155 break;
157 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
159 case FFI_TYPE_LONGDOUBLE:
160 #ifdef POWERPC64
161 if (fparg_count < NUM_FPR_ARG_REGISTERS)
162 *(long double *) fpr_base++ = *(long double *) *p_argv;
163 else
164 *(long double *) next_arg = *(long double *) *p_argv;
165 next_arg += 2;
166 fparg_count += 2;
167 #else
168 double_tmp = ((double *) *p_argv)[0];
169 if (fparg_count < NUM_FPR_ARG_REGISTERS)
170 *fpr_base++ = double_tmp;
171 else
172 *(double *) next_arg = double_tmp;
173 next_arg += 2;
174 fparg_count++;
176 double_tmp = ((double *) *p_argv)[1];
177 if (fparg_count < NUM_FPR_ARG_REGISTERS)
178 *fpr_base++ = double_tmp;
179 else
180 *(double *) next_arg = double_tmp;
181 next_arg += 2;
182 fparg_count++;
183 #endif
184 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
185 break;
186 #endif
187 case FFI_TYPE_UINT64:
188 case FFI_TYPE_SINT64:
189 #ifdef POWERPC64
190 gprvalue = *(long long *) *p_argv;
191 goto putgpr;
192 #else
193 *(long long *) next_arg = *(long long *) *p_argv;
194 next_arg += 2;
195 #endif
196 break;
197 case FFI_TYPE_POINTER:
198 gprvalue = *(unsigned long *) *p_argv;
199 goto putgpr;
200 case FFI_TYPE_UINT8:
201 gprvalue = *(unsigned char *) *p_argv;
202 goto putgpr;
203 case FFI_TYPE_SINT8:
204 gprvalue = *(signed char *) *p_argv;
205 goto putgpr;
206 case FFI_TYPE_UINT16:
207 gprvalue = *(unsigned short *) *p_argv;
208 goto putgpr;
209 case FFI_TYPE_SINT16:
210 gprvalue = *(signed short *) *p_argv;
211 goto putgpr;
213 case FFI_TYPE_STRUCT:
214 #ifdef POWERPC64
215 dest_cpy = (char *) next_arg;
216 size_al = (*ptr)->size;
217 if ((*ptr)->elements[0]->type == 3)
218 size_al = ALIGN((*ptr)->size, 8);
219 if (size_al < 3 && abi == FFI_DARWIN)
220 dest_cpy += 4 - size_al;
222 memcpy ((char *) dest_cpy, (char *) *p_argv, size_al);
223 next_arg += (size_al + 7) / 8;
224 #else
225 dest_cpy = (char *) next_arg;
227 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
228 SI 4 bytes) are aligned as if they were those modes.
229 Structures with 3 byte in size are padded upwards. */
230 size_al = (*ptr)->size;
231 /* If the first member of the struct is a double, then align
232 the struct to double-word. */
233 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
234 size_al = ALIGN((*ptr)->size, 8);
235 if (size_al < 3 && abi == FFI_DARWIN)
236 dest_cpy += 4 - size_al;
238 memcpy((char *) dest_cpy, (char *) *p_argv, size_al);
239 next_arg += (size_al + 3) / 4;
240 #endif
241 break;
243 case FFI_TYPE_INT:
244 case FFI_TYPE_SINT32:
245 gprvalue = *(signed int *) *p_argv;
246 goto putgpr;
248 case FFI_TYPE_UINT32:
249 gprvalue = *(unsigned int *) *p_argv;
250 putgpr:
251 *next_arg++ = gprvalue;
252 break;
253 default:
254 break;
258 /* Check that we didn't overrun the stack... */
259 //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
260 //FFI_ASSERT((unsigned *)fpr_base
261 // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
262 //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
265 /* Adjust the size of S to be correct for Darwin.
266 On Darwin, the first field of a structure has natural alignment. */
268 static void
269 darwin_adjust_aggregate_sizes (ffi_type *s)
271 int i;
273 if (s->type != FFI_TYPE_STRUCT)
274 return;
276 s->size = 0;
277 for (i = 0; s->elements[i] != NULL; i++)
279 ffi_type *p;
280 int align;
282 p = s->elements[i];
283 darwin_adjust_aggregate_sizes (p);
284 if (i == 0
285 && (p->type == FFI_TYPE_UINT64
286 || p->type == FFI_TYPE_SINT64
287 || p->type == FFI_TYPE_DOUBLE
288 || p->alignment == 8))
289 align = 8;
290 else if (p->alignment == 16 || p->alignment < 4)
291 align = p->alignment;
292 else
293 align = 4;
294 s->size = ALIGN(s->size, align) + p->size;
297 s->size = ALIGN(s->size, s->alignment);
299 if (s->elements[0]->type == FFI_TYPE_UINT64
300 || s->elements[0]->type == FFI_TYPE_SINT64
301 || s->elements[0]->type == FFI_TYPE_DOUBLE
302 || s->elements[0]->alignment == 8)
303 s->alignment = s->alignment > 8 ? s->alignment : 8;
304 /* Do not add additional tail padding. */
307 /* Adjust the size of S to be correct for AIX.
308 Word-align double unless it is the first member of a structure. */
310 static void
311 aix_adjust_aggregate_sizes (ffi_type *s)
313 int i;
315 if (s->type != FFI_TYPE_STRUCT)
316 return;
318 s->size = 0;
319 for (i = 0; s->elements[i] != NULL; i++)
321 ffi_type *p;
322 int align;
324 p = s->elements[i];
325 aix_adjust_aggregate_sizes (p);
326 align = p->alignment;
327 if (i != 0 && p->type == FFI_TYPE_DOUBLE)
328 align = 4;
329 s->size = ALIGN(s->size, align) + p->size;
332 s->size = ALIGN(s->size, s->alignment);
334 if (s->elements[0]->type == FFI_TYPE_UINT64
335 || s->elements[0]->type == FFI_TYPE_SINT64
336 || s->elements[0]->type == FFI_TYPE_DOUBLE
337 || s->elements[0]->alignment == 8)
338 s->alignment = s->alignment > 8 ? s->alignment : 8;
339 /* Do not add additional tail padding. */
342 /* Perform machine dependent cif processing. */
343 ffi_status
344 ffi_prep_cif_machdep (ffi_cif *cif)
346 /* All this is for the DARWIN ABI. */
347 int i;
348 ffi_type **ptr;
349 unsigned bytes;
350 int fparg_count = 0, intarg_count = 0;
351 unsigned flags = 0;
352 unsigned size_al = 0;
354 /* All the machine-independent calculation of cif->bytes will be wrong.
355 All the calculation of structure sizes will also be wrong.
356 Redo the calculation for DARWIN. */
358 if (cif->abi == FFI_DARWIN)
360 darwin_adjust_aggregate_sizes (cif->rtype);
361 for (i = 0; i < cif->nargs; i++)
362 darwin_adjust_aggregate_sizes (cif->arg_types[i]);
365 if (cif->abi == FFI_AIX)
367 aix_adjust_aggregate_sizes (cif->rtype);
368 for (i = 0; i < cif->nargs; i++)
369 aix_adjust_aggregate_sizes (cif->arg_types[i]);
372 /* Space for the frame pointer, callee's LR, CR, etc, and for
373 the asm's temp regs. */
375 bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
377 /* Return value handling. The rules are as follows:
378 - 32-bit (or less) integer values are returned in gpr3;
379 - Structures of size <= 4 bytes also returned in gpr3;
380 - 64-bit integer values and structures between 5 and 8 bytes are returned
381 in gpr3 and gpr4;
382 - Single/double FP values are returned in fpr1;
383 - Long double FP (if not equivalent to double) values are returned in
384 fpr1 and fpr2;
385 - Larger structures values are allocated space and a pointer is passed
386 as the first argument. */
387 switch (cif->rtype->type)
390 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
391 case FFI_TYPE_LONGDOUBLE:
392 flags |= FLAG_RETURNS_128BITS;
393 flags |= FLAG_RETURNS_FP;
394 break;
395 #endif
397 case FFI_TYPE_DOUBLE:
398 flags |= FLAG_RETURNS_64BITS;
399 /* Fall through. */
400 case FFI_TYPE_FLOAT:
401 flags |= FLAG_RETURNS_FP;
402 break;
404 case FFI_TYPE_UINT64:
405 case FFI_TYPE_SINT64:
406 #ifdef POWERPC64
407 case FFI_TYPE_POINTER:
408 #endif
409 flags |= FLAG_RETURNS_64BITS;
410 break;
412 case FFI_TYPE_STRUCT:
413 flags |= FLAG_RETVAL_REFERENCE;
414 flags |= FLAG_RETURNS_NOTHING;
415 intarg_count++;
416 break;
417 case FFI_TYPE_VOID:
418 flags |= FLAG_RETURNS_NOTHING;
419 break;
421 default:
422 /* Returns 32-bit integer, or similar. Nothing to do here. */
423 break;
426 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
427 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
428 goes on the stack. Structures are passed as a pointer to a copy of
429 the structure. Stuff on the stack needs to keep proper alignment. */
430 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
432 switch ((*ptr)->type)
434 case FFI_TYPE_FLOAT:
435 case FFI_TYPE_DOUBLE:
436 fparg_count++;
437 /* If this FP arg is going on the stack, it must be
438 8-byte-aligned. */
439 if (fparg_count > NUM_FPR_ARG_REGISTERS
440 && intarg_count%2 != 0)
441 intarg_count++;
442 break;
444 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
446 case FFI_TYPE_LONGDOUBLE:
447 fparg_count += 2;
448 /* If this FP arg is going on the stack, it must be
449 8-byte-aligned. */
450 if (fparg_count > NUM_FPR_ARG_REGISTERS
451 && intarg_count%2 != 0)
452 intarg_count++;
453 intarg_count +=2;
454 break;
455 #endif
457 case FFI_TYPE_UINT64:
458 case FFI_TYPE_SINT64:
459 /* 'long long' arguments are passed as two words, but
460 either both words must fit in registers or both go
461 on the stack. If they go on the stack, they must
462 be 8-byte-aligned. */
463 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
464 || (intarg_count >= NUM_GPR_ARG_REGISTERS && intarg_count%2 != 0))
465 intarg_count++;
466 intarg_count += 2;
467 break;
469 case FFI_TYPE_STRUCT:
470 size_al = (*ptr)->size;
471 /* If the first member of the struct is a double, then align
472 the struct to double-word. */
473 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
474 size_al = ALIGN((*ptr)->size, 8);
475 #ifdef POWERPC64
476 intarg_count += (size_al + 7) / 8;
477 #else
478 intarg_count += (size_al + 3) / 4;
479 #endif
480 break;
482 default:
483 /* Everything else is passed as a 4-byte word in a GPR, either
484 the object itself or a pointer to it. */
485 intarg_count++;
486 break;
490 if (fparg_count != 0)
491 flags |= FLAG_FP_ARGUMENTS;
493 /* Space for the FPR registers, if needed. */
494 if (fparg_count != 0)
495 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
497 /* Stack space. */
498 #ifdef POWERPC64
499 if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS)
500 bytes += (intarg_count + fparg_count) * sizeof(long);
501 #else
502 if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
503 bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
504 #endif
505 else
506 bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
508 /* The stack space allocated needs to be a multiple of 16 bytes. */
509 bytes = (bytes + 15) & ~0xF;
511 cif->flags = flags;
512 cif->bytes = bytes;
514 return FFI_OK;
517 extern void ffi_call_AIX(extended_cif *, long, unsigned, unsigned *,
518 void (*fn)(void), void (*fn2)(void));
519 extern void ffi_call_DARWIN(extended_cif *, long, unsigned, unsigned *,
520 void (*fn)(void), void (*fn2)(void));
522 void
523 ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
525 extended_cif ecif;
527 ecif.cif = cif;
528 ecif.avalue = avalue;
530 /* If the return value is a struct and we don't have a return
531 value address then we need to make one. */
533 if ((rvalue == NULL) &&
534 (cif->rtype->type == FFI_TYPE_STRUCT))
536 ecif.rvalue = alloca (cif->rtype->size);
538 else
539 ecif.rvalue = rvalue;
541 switch (cif->abi)
543 case FFI_AIX:
544 ffi_call_AIX(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
545 ffi_prep_args);
546 break;
547 case FFI_DARWIN:
548 ffi_call_DARWIN(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
549 ffi_prep_args);
550 break;
551 default:
552 FFI_ASSERT(0);
553 break;
557 static void flush_icache(char *);
558 static void flush_range(char *, int);
560 /* The layout of a function descriptor. A C function pointer really
561 points to one of these. */
563 typedef struct aix_fd_struct {
564 void *code_pointer;
565 void *toc;
566 } aix_fd;
568 /* here I'd like to add the stack frame layout we use in darwin_closure.S
569 and aix_clsoure.S
571 SP previous -> +---------------------------------------+ <--- child frame
572 | back chain to caller 4 |
573 +---------------------------------------+ 4
574 | saved CR 4 |
575 +---------------------------------------+ 8
576 | saved LR 4 |
577 +---------------------------------------+ 12
578 | reserved for compilers 4 |
579 +---------------------------------------+ 16
580 | reserved for binders 4 |
581 +---------------------------------------+ 20
582 | saved TOC pointer 4 |
583 +---------------------------------------+ 24
584 | always reserved 8*4=32 (previous GPRs)|
585 | according to the linkage convention |
586 | from AIX |
587 +---------------------------------------+ 56
588 | our FPR area 13*8=104 |
589 | f1 |
590 | . |
591 | f13 |
592 +---------------------------------------+ 160
593 | result area 8 |
594 +---------------------------------------+ 168
595 | alignement to the next multiple of 16 |
596 SP current --> +---------------------------------------+ 176 <- parent frame
597 | back chain to caller 4 |
598 +---------------------------------------+ 180
599 | saved CR 4 |
600 +---------------------------------------+ 184
601 | saved LR 4 |
602 +---------------------------------------+ 188
603 | reserved for compilers 4 |
604 +---------------------------------------+ 192
605 | reserved for binders 4 |
606 +---------------------------------------+ 196
607 | saved TOC pointer 4 |
608 +---------------------------------------+ 200
609 | always reserved 8*4=32 we store our |
610 | GPRs here |
611 | r3 |
612 | . |
613 | r10 |
614 +---------------------------------------+ 232
615 | overflow part |
616 +---------------------------------------+ xxx
617 | ???? |
618 +---------------------------------------+ xxx
621 ffi_status
622 ffi_prep_closure_loc (ffi_closure* closure,
623 ffi_cif* cif,
624 void (*fun)(ffi_cif*, void*, void**, void*),
625 void *user_data,
626 void *codeloc)
628 unsigned int *tramp;
629 struct ffi_aix_trampoline_struct *tramp_aix;
630 aix_fd *fd;
632 switch (cif->abi)
634 case FFI_DARWIN:
636 FFI_ASSERT (cif->abi == FFI_DARWIN);
638 tramp = (unsigned int *) &closure->tramp[0];
639 tramp[0] = 0x7c0802a6; /* mflr r0 */
640 tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
641 tramp[4] = 0x7d6802a6; /* mflr r11 */
642 tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */
643 tramp[6] = 0x7c0803a6; /* mtlr r0 */
644 tramp[7] = 0x7d8903a6; /* mtctr r12 */
645 tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
646 tramp[9] = 0x4e800420; /* bctr */
647 tramp[2] = (unsigned long) ffi_closure_ASM; /* function */
648 tramp[3] = (unsigned long) codeloc; /* context */
650 closure->cif = cif;
651 closure->fun = fun;
652 closure->user_data = user_data;
654 /* Flush the icache. Only necessary on Darwin. */
655 flush_range(codeloc, FFI_TRAMPOLINE_SIZE);
657 break;
659 case FFI_AIX:
661 tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
662 fd = (aix_fd *)(void *)ffi_closure_ASM;
664 FFI_ASSERT (cif->abi == FFI_AIX);
666 tramp_aix->code_pointer = fd->code_pointer;
667 tramp_aix->toc = fd->toc;
668 tramp_aix->static_chain = codeloc;
669 closure->cif = cif;
670 closure->fun = fun;
671 closure->user_data = user_data;
673 default:
675 FFI_ASSERT(0);
676 break;
678 return FFI_OK;
681 static void
682 flush_icache(char *addr)
684 #ifndef _AIX
685 __asm__ volatile (
686 "dcbf 0,%0\n"
687 "\tsync\n"
688 "\ticbi 0,%0\n"
689 "\tsync\n"
690 "\tisync"
691 : : "r"(addr) : "memory");
692 #endif
695 static void
696 flush_range(char * addr1, int size)
698 #define MIN_LINE_SIZE 32
699 int i;
700 for (i = 0; i < size; i += MIN_LINE_SIZE)
701 flush_icache(addr1+i);
702 flush_icache(addr1+size-1);
705 typedef union
707 float f;
708 double d;
709 } ffi_dblfl;
712 ffi_closure_helper_DARWIN (ffi_closure *, void *,
713 unsigned long *, ffi_dblfl *);
715 /* Basically the trampoline invokes ffi_closure_ASM, and on
716 entry, r11 holds the address of the closure.
717 After storing the registers that could possibly contain
718 parameters to be passed into the stack frame and setting
719 up space for a return value, ffi_closure_ASM invokes the
720 following helper function to do most of the work. */
723 ffi_closure_helper_DARWIN (ffi_closure *closure, void *rvalue,
724 unsigned long *pgr, ffi_dblfl *pfr)
726 /* rvalue is the pointer to space for return value in closure assembly
727 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
728 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
730 typedef double ldbits[2];
732 union ldu
734 ldbits lb;
735 long double ld;
738 void ** avalue;
739 ffi_type ** arg_types;
740 long i, avn;
741 ffi_cif * cif;
742 ffi_dblfl * end_pfr = pfr + NUM_FPR_ARG_REGISTERS;
743 unsigned size_al;
745 cif = closure->cif;
746 avalue = alloca (cif->nargs * sizeof(void *));
748 /* Copy the caller's structure return value address so that the closure
749 returns the data directly to the caller. */
750 if (cif->rtype->type == FFI_TYPE_STRUCT)
752 rvalue = (void *) *pgr;
753 pgr++;
756 i = 0;
757 avn = cif->nargs;
758 arg_types = cif->arg_types;
760 /* Grab the addresses of the arguments from the stack frame. */
761 while (i < avn)
763 switch (arg_types[i]->type)
765 case FFI_TYPE_SINT8:
766 case FFI_TYPE_UINT8:
767 #ifdef POWERPC64
768 avalue[i] = (char *) pgr + 7;
769 #else
770 avalue[i] = (char *) pgr + 3;
771 #endif
772 pgr++;
773 break;
775 case FFI_TYPE_SINT16:
776 case FFI_TYPE_UINT16:
777 #ifdef POWERPC64
778 avalue[i] = (char *) pgr + 6;
779 #else
780 avalue[i] = (char *) pgr + 2;
781 #endif
782 pgr++;
783 break;
785 case FFI_TYPE_SINT32:
786 case FFI_TYPE_UINT32:
787 #ifdef POWERPC64
788 avalue[i] = (char *) pgr + 4;
789 #else
790 case FFI_TYPE_POINTER:
791 avalue[i] = pgr;
792 #endif
793 pgr++;
794 break;
796 case FFI_TYPE_STRUCT:
797 #ifdef POWERPC64
798 size_al = arg_types[i]->size;
799 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
800 size_al = ALIGN (arg_types[i]->size, 8);
801 if (size_al < 3 && cif->abi == FFI_DARWIN)
802 avalue[i] = (void *) pgr + 8 - size_al;
803 else
804 avalue[i] = (void *) pgr;
805 pgr += (size_al + 7) / 8;
806 #else
807 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
808 SI 4 bytes) are aligned as if they were those modes. */
809 size_al = arg_types[i]->size;
810 /* If the first member of the struct is a double, then align
811 the struct to double-word. */
812 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
813 size_al = ALIGN(arg_types[i]->size, 8);
814 if (size_al < 3 && cif->abi == FFI_DARWIN)
815 avalue[i] = (void*) pgr + 4 - size_al;
816 else
817 avalue[i] = (void*) pgr;
818 pgr += (size_al + 3) / 4;
819 #endif
820 break;
822 case FFI_TYPE_SINT64:
823 case FFI_TYPE_UINT64:
824 #ifdef POWERPC64
825 case FFI_TYPE_POINTER:
826 avalue[i] = pgr;
827 pgr++;
828 break;
829 #else
830 /* Long long ints are passed in two gpr's. */
831 avalue[i] = pgr;
832 pgr += 2;
833 break;
834 #endif
836 case FFI_TYPE_FLOAT:
837 /* A float value consumes a GPR.
838 There are 13 64bit floating point registers. */
839 if (pfr < end_pfr)
841 double temp = pfr->d;
842 pfr->f = (float) temp;
843 avalue[i] = pfr;
844 pfr++;
846 else
848 avalue[i] = pgr;
850 pgr++;
851 break;
853 case FFI_TYPE_DOUBLE:
854 /* A double value consumes two GPRs.
855 There are 13 64bit floating point registers. */
856 if (pfr < end_pfr)
858 avalue[i] = pfr;
859 pfr++;
861 else
863 avalue[i] = pgr;
865 #ifdef POWERPC64
866 pgr++;
867 #else
868 pgr += 2;
869 #endif
870 break;
872 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
874 case FFI_TYPE_LONGDOUBLE:
875 #ifdef POWERPC64
876 if (pfr + 1 < end_pfr)
878 avalue[i] = pfr;
879 pfr += 2;
881 else
883 if (pfr < end_pfr)
885 *pgr = *(unsigned long *) pfr;
886 pfr++;
888 avalue[i] = pgr;
890 pgr += 2;
891 #else /* POWERPC64 */
892 /* A long double value consumes four GPRs and two FPRs.
893 There are 13 64bit floating point registers. */
894 if (pfr + 1 < end_pfr)
896 avalue[i] = pfr;
897 pfr += 2;
899 /* Here we have the situation where one part of the long double
900 is stored in fpr13 and the other part is already on the stack.
901 We use a union to pass the long double to avalue[i]. */
902 else if (pfr + 1 == end_pfr)
904 union ldu temp_ld;
905 memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits));
906 memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits));
907 avalue[i] = &temp_ld.ld;
908 pfr++;
910 else
912 avalue[i] = pgr;
914 pgr += 4;
915 #endif /* POWERPC64 */
916 break;
917 #endif
918 default:
919 FFI_ASSERT(0);
921 i++;
924 (closure->fun) (cif, rvalue, avalue, closure->user_data);
926 /* Tell ffi_closure_ASM to perform return type promotions. */
927 return cif->rtype->type;