OSDN Git Service

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