OSDN Git Service

2003-10-19 Andreas Tobler <a.tobler@schweiz.ch>
[pf3gnuchains/gcc-fork.git] / libffi / src / powerpc / ffi_darwin.c
1 /* -----------------------------------------------------------------------
2    ffi.c - Copyright (c) 1998 Geoffrey Keating
3
4    PowerPC Foreign Function Interface
5
6    Darwin ABI support (c) 2001 John Hornkvist
7    AIX ABI support (c) 2002 Free Software Foundation, Inc.
8
9    Permission is hereby granted, free of charge, to any person obtaining
10    a copy of this software and associated documentation files (the
11    ``Software''), to deal in the Software without restriction, including
12    without limitation the rights to use, copy, modify, merge, publish,
13    distribute, sublicense, and/or sell copies of the Software, and to
14    permit persons to whom the Software is furnished to do so, subject to
15    the following conditions:
16
17    The above copyright notice and this permission notice shall be included
18    in all copies or substantial portions of the Software.
19
20    THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
21    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
24    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25    ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26    OTHER DEALINGS IN THE SOFTWARE.
27    ----------------------------------------------------------------------- */
28 #include <ffi.h>
29 #include <ffi_common.h>
30
31 #include <stdlib.h>
32
33 extern void ffi_closure_ASM(void);
34
35 enum {
36   /* The assembly depends on these exact flags.  */
37   FLAG_RETURNS_NOTHING  = 1 << (31-30), /* These go in cr7  */
38   FLAG_RETURNS_FP       = 1 << (31-29),
39   FLAG_RETURNS_64BITS   = 1 << (31-28),
40
41   FLAG_ARG_NEEDS_COPY   = 1 << (31- 7),
42   FLAG_FP_ARGUMENTS     = 1 << (31- 6), /* cr1.eq; specified by ABI  */
43   FLAG_4_GPR_ARGUMENTS  = 1 << (31- 5),
44   FLAG_RETVAL_REFERENCE = 1 << (31- 4)
45 };
46
47 /* About the DARWIN ABI.  */
48 enum {
49   NUM_GPR_ARG_REGISTERS = 8,
50   NUM_FPR_ARG_REGISTERS = 13
51 };
52 enum { ASM_NEEDS_REGISTERS = 4 };
53
54 /* ffi_prep_args is called by the assembly routine once stack space
55    has been allocated for the function's arguments.
56
57    The stack layout we want looks like this:
58
59    |   Return address from ffi_call_DARWIN      |       higher addresses
60    |--------------------------------------------|
61    |   Previous backchain pointer       4       |       stack pointer here
62    |--------------------------------------------|<+ <<< on entry to
63    |   Saved r28-r31                    4*4     | |     ffi_call_DARWIN
64    |--------------------------------------------| |
65    |   Parameters             (at least 8*4=32) | |
66    |--------------------------------------------| |
67    |   Space for GPR2                   4       | |
68    |--------------------------------------------| |     stack   |
69    |   Reserved                       2*4       | |     grows   |
70    |--------------------------------------------| |     down    V
71    |   Space for callee's LR            4       | |
72    |--------------------------------------------| |     lower addresses
73    |   Saved CR                         4       | |
74    |--------------------------------------------| |     stack pointer here
75    |   Current backchain pointer        4       |-/     during
76    |--------------------------------------------|   <<< ffi_call_DARWIN
77
78    */
79
80 /*@-exportheader@*/
81 void ffi_prep_args(extended_cif *ecif, unsigned *const stack)
82 /*@=exportheader@*/
83 {
84   const unsigned bytes = ecif->cif->bytes;
85   const unsigned flags = ecif->cif->flags;
86
87   /* 'stacktop' points at the previous backchain pointer.  */
88   unsigned *const stacktop = stack + (ecif->cif->bytes / sizeof(unsigned));
89
90   /* 'fpr_base' points at the space for fpr1, and grows upwards as
91      we use FPR registers.  */
92   double *fpr_base = (double*) (stacktop - ASM_NEEDS_REGISTERS) - NUM_FPR_ARG_REGISTERS;
93   int fparg_count = 0;
94
95
96   /* 'next_arg' grows up as we put parameters in it.  */
97   unsigned *next_arg = stack + 6; /* 6 reserved posistions.  */
98
99   int i = ecif->cif->nargs;
100   double double_tmp;
101   void **p_argv = ecif->avalue;
102   unsigned gprvalue;
103   ffi_type** ptr = ecif->cif->arg_types;
104   char *dest_cpy;
105   unsigned size_al = 0;
106
107   /* Check that everything starts aligned properly.  */
108   FFI_ASSERT(((unsigned)(char *)stack & 0xF) == 0);
109   FFI_ASSERT(((unsigned)(char *)stacktop & 0xF) == 0);
110   FFI_ASSERT((bytes & 0xF) == 0);
111
112   /* Deal with return values that are actually pass-by-reference.
113      Rule:
114      Return values are referenced by r3, so r4 is the first parameter.  */
115
116   if (flags & FLAG_RETVAL_REFERENCE)
117     *next_arg++ = (unsigned)(char *)ecif->rvalue;
118
119   /* Now for the arguments.  */
120   for (;
121        i > 0;
122        i--, ptr++, p_argv++)
123     {
124       switch ((*ptr)->type)
125         {
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;
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           next_arg += 2;
146           fparg_count++;
147           FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
148           break;
149
150         case FFI_TYPE_UINT64:
151         case FFI_TYPE_SINT64:
152           *(long long *)next_arg = *(long long *)*p_argv;
153           next_arg+=2;
154           break;
155         case FFI_TYPE_UINT8:
156           gprvalue = *(unsigned char *)*p_argv;
157           goto putgpr;
158         case FFI_TYPE_SINT8:
159           gprvalue = *(signed char *)*p_argv;
160           goto putgpr;
161         case FFI_TYPE_UINT16:
162           gprvalue = *(unsigned short *)*p_argv;
163           goto putgpr;
164         case FFI_TYPE_SINT16:
165           gprvalue = *(signed short *)*p_argv;
166           goto putgpr;
167
168         case FFI_TYPE_STRUCT:
169
170 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
171         case FFI_TYPE_LONGDOUBLE:
172 #endif
173           dest_cpy = (char *) next_arg;
174
175           /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
176              SI 4 bytes) are aligned as if they were those modes.
177              Structures with 3 byte in size are padded upwards.  */
178           size_al = (*ptr)->size;
179           /* If the first member of the struct is a double, then align
180              the struct to double-word.
181              Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3.  */
182           if ((*ptr)->elements[0]->type == 3)
183             size_al = ALIGN((*ptr)->size, 8);
184           if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
185             dest_cpy += 4 - size_al;
186
187           memcpy((char *)dest_cpy, (char *)*p_argv, size_al);
188           next_arg += (size_al + 3) / 4;
189           break;
190
191         case FFI_TYPE_INT:
192         case FFI_TYPE_UINT32:
193         case FFI_TYPE_SINT32:
194         case FFI_TYPE_POINTER:
195           gprvalue = *(unsigned *)*p_argv;
196         putgpr:
197           *next_arg++ = gprvalue;
198           break;
199         default:
200           break;
201         }
202     }
203
204   /* Check that we didn't overrun the stack...  */
205   //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
206   //FFI_ASSERT((unsigned *)fpr_base
207   //         <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
208   //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
209 }
210
211 /* Perform machine dependent cif processing.  */
212 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
213 {
214   /* All this is for the DARWIN ABI.  */
215   int i;
216   ffi_type **ptr;
217   unsigned bytes;
218   int fparg_count = 0, intarg_count = 0;
219   unsigned flags = 0;
220   unsigned size_al = 0;
221
222   /* All the machine-independent calculation of cif->bytes will be wrong.
223      Redo the calculation for DARWIN.  */
224
225   /* Space for the frame pointer, callee's LR, CR, etc, and for
226      the asm's temp regs.  */
227
228   bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
229
230   /* Return value handling.  The rules are as follows:
231      - 32-bit (or less) integer values are returned in gpr3;
232      - Structures of size <= 4 bytes also returned in gpr3;
233      - 64-bit integer values and structures between 5 and 8 bytes are returned
234        in gpr3 and gpr4;
235      - Single/double FP values are returned in fpr1;
236      - Long double FP (if not equivalent to double) values are returned in
237        fpr1 and fpr2;
238      - Larger structures values are allocated space and a pointer is passed
239        as the first argument.  */
240   switch (cif->rtype->type)
241     {
242 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
243     case FFI_TYPE_LONGDOUBLE:
244 #endif
245       /* Fall through.  */
246     case FFI_TYPE_DOUBLE:
247       flags |= FLAG_RETURNS_64BITS;
248       /* Fall through.  */
249     case FFI_TYPE_FLOAT:
250       flags |= FLAG_RETURNS_FP;
251       break;
252
253     case FFI_TYPE_UINT64:
254     case FFI_TYPE_SINT64:
255       flags |= FLAG_RETURNS_64BITS;
256       break;
257
258     case FFI_TYPE_STRUCT:
259       flags |= FLAG_RETVAL_REFERENCE;
260       flags |= FLAG_RETURNS_NOTHING;
261       intarg_count++;
262       break;
263     case FFI_TYPE_VOID:
264       flags |= FLAG_RETURNS_NOTHING;
265       break;
266
267     default:
268       /* Returns 32-bit integer, or similar.  Nothing to do here.  */
269       break;
270     }
271
272   /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
273      first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
274      goes on the stack.  Structures and long doubles (if not equivalent
275      to double) are passed as a pointer to a copy of the structure.
276      Stuff on the stack needs to keep proper alignment.  */
277   for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
278     {
279       switch ((*ptr)->type)
280         {
281         case FFI_TYPE_FLOAT:
282         case FFI_TYPE_DOUBLE:
283           fparg_count++;
284           /* If this FP arg is going on the stack, it must be
285              8-byte-aligned.  */
286           if (fparg_count > NUM_FPR_ARG_REGISTERS
287               && intarg_count%2 != 0)
288             intarg_count++;
289           break;
290
291         case FFI_TYPE_UINT64:
292         case FFI_TYPE_SINT64:
293           /* 'long long' arguments are passed as two words, but
294              either both words must fit in registers or both go
295              on the stack.  If they go on the stack, they must
296              be 8-byte-aligned.  */
297           if (intarg_count == NUM_GPR_ARG_REGISTERS-1
298               || (intarg_count >= NUM_GPR_ARG_REGISTERS && intarg_count%2 != 0))
299             intarg_count++;
300           intarg_count += 2;
301           break;
302
303         case FFI_TYPE_STRUCT:
304 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
305         case FFI_TYPE_LONGDOUBLE:
306 #endif
307           size_al = (*ptr)->size;
308           /* If the first member of the struct is a double, then align
309              the struct to double-word.
310              Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3.  */
311           if ((*ptr)->elements[0]->type == 3)
312             size_al = ALIGN((*ptr)->size, 8);
313           intarg_count += (size_al + 3) / 4;
314           break;
315
316         default:
317           /* Everything else is passed as a 4-byte word in a GPR, either
318              the object itself or a pointer to it.  */
319           intarg_count++;
320           break;
321         }
322     }
323
324   if (fparg_count != 0)
325     flags |= FLAG_FP_ARGUMENTS;
326
327   /* Space for the FPR registers, if needed.  */
328   if (fparg_count != 0)
329     bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
330
331   /* Stack space.  */
332   if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
333     bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
334   else
335     bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
336
337   /* The stack space allocated needs to be a multiple of 16 bytes.  */
338   bytes = (bytes + 15) & ~0xF;
339
340   cif->flags = flags;
341   cif->bytes = bytes;
342
343   return FFI_OK;
344 }
345
346 /*@-declundef@*/
347 /*@-exportheader@*/
348 extern void ffi_call_AIX(/*@out@*/ extended_cif *,
349                          unsigned, unsigned,
350                          /*@out@*/ unsigned *,
351                          void (*fn)(),
352                          void (*fn2)());
353 extern void ffi_call_DARWIN(/*@out@*/ extended_cif *,
354                             unsigned, unsigned,
355                             /*@out@*/ unsigned *,
356                             void (*fn)(),
357                             void (*fn2)());
358 /*@=declundef@*/
359 /*@=exportheader@*/
360
361 void ffi_call(/*@dependent@*/ ffi_cif *cif,
362               void (*fn)(),
363               /*@out@*/ void *rvalue,
364               /*@dependent@*/ void **avalue)
365 {
366   extended_cif ecif;
367
368   ecif.cif = cif;
369   ecif.avalue = avalue;
370
371   /* If the return value is a struct and we don't have a return
372      value address then we need to make one.  */
373
374   if ((rvalue == NULL) &&
375       (cif->rtype->type == FFI_TYPE_STRUCT))
376     {
377       /*@-sysunrecog@*/
378       ecif.rvalue = alloca(cif->rtype->size);
379       /*@=sysunrecog@*/
380     }
381   else
382     ecif.rvalue = rvalue;
383
384   switch (cif->abi)
385     {
386     case FFI_AIX:
387       /*@-usedef@*/
388       ffi_call_AIX(&ecif, -cif->bytes,
389                    cif->flags, ecif.rvalue, fn, ffi_prep_args);
390       /*@=usedef@*/
391       break;
392     case FFI_DARWIN:
393       /*@-usedef@*/
394       ffi_call_DARWIN(&ecif, -cif->bytes,
395                       cif->flags, ecif.rvalue, fn, ffi_prep_args);
396       /*@=usedef@*/
397       break;
398     default:
399       FFI_ASSERT(0);
400       break;
401     }
402 }
403
404 static void flush_icache(char *);
405 static void flush_range(char *, int);
406
407 /* The layout of a function descriptor.  A C function pointer really
408    points to one of these.  */
409
410 typedef struct aix_fd_struct {
411     void *code_pointer;
412     void *toc;
413 } aix_fd;
414
415 /* here I'd like to add the stack frame layout we use in darwin_closure.S
416    and aix_clsoure.S
417
418    SP previous -> +---------------------------------------+ <--- child frame
419                   | back chain to caller 4                |
420                   +---------------------------------------+ 4
421                   | saved CR 4                            |
422                   +---------------------------------------+ 8
423                   | saved LR 4                            |
424                   +---------------------------------------+ 12
425                   | reserved for compilers 4              |
426                   +---------------------------------------+ 16
427                   | reserved for binders 4                |
428                   +---------------------------------------+ 20
429                   | saved TOC pointer 4                   |
430                   +---------------------------------------+ 24
431                   | always reserved 8*4=32 (previous GPRs)|
432                   | according to the linkage convention   |
433                   | from AIX                              |
434                   +---------------------------------------+ 56
435                   | our FPR area 13*8=104                 |
436                   | f1                                    |
437                   | .                                     |
438                   | f13                                   |
439                   +---------------------------------------+ 160
440                   | result area 8                         |
441                   +---------------------------------------+ 168
442                   | alignement to the next multiple of 16 |
443 SP current -->    +---------------------------------------+ 176 <- parent frame
444                   | back chain to caller 4                |
445                   +---------------------------------------+ 180
446                   | saved CR 4                            |
447                   +---------------------------------------+ 184
448                   | saved LR 4                            |
449                   +---------------------------------------+ 188
450                   | reserved for compilers 4              |
451                   +---------------------------------------+ 192
452                   | reserved for binders 4                |
453                   +---------------------------------------+ 196
454                   | saved TOC pointer 4                   |
455                   +---------------------------------------+ 200
456                   | always reserved 8*4=32  we store our  |
457                   | GPRs here                             |
458                   | r3                                    |
459                   | .                                     |
460                   | r10                                   |
461                   +---------------------------------------+ 232
462                   | overflow part                         |
463                   +---------------------------------------+ xxx
464                   | ????                                  |
465                   +---------------------------------------+ xxx
466
467 */
468 ffi_status
469 ffi_prep_closure (ffi_closure* closure,
470                   ffi_cif* cif,
471                   void (*fun)(ffi_cif*, void*, void**, void*),
472                   void *user_data)
473 {
474   unsigned int *tramp;
475   struct ffi_aix_trampoline_struct *tramp_aix;
476   aix_fd *fd;
477
478   switch (cif->abi)
479     {
480     case FFI_DARWIN:
481
482       FFI_ASSERT (cif->abi == FFI_DARWIN);
483
484       tramp = (unsigned int *) &closure->tramp[0];
485       tramp[0] = 0x7c0802a6;  /*   mflr    r0  */
486       tramp[1] = 0x429f000d;  /*   bcl-    20,4*cr7+so,0x10  */
487       tramp[4] = 0x7d6802a6;  /*   mflr    r11  */
488       tramp[5] = 0x818b0000;  /*   lwz     r12,0(r11) function address  */
489       tramp[6] = 0x7c0803a6;  /*   mtlr    r0   */
490       tramp[7] = 0x7d8903a6;  /*   mtctr   r12  */
491       tramp[8] = 0x816b0004;  /*   lwz     r11,4(r11) static chain  */
492       tramp[9] = 0x4e800420;  /*   bctr  */
493       tramp[2] = (unsigned long) ffi_closure_ASM; /* function  */
494       tramp[3] = (unsigned long) closure; /* context  */
495
496       closure->cif = cif;
497       closure->fun = fun;
498       closure->user_data = user_data;
499
500       /* Flush the icache. Only necessary on Darwin.  */
501       flush_range(&closure->tramp[0],FFI_TRAMPOLINE_SIZE);
502
503       break;
504
505     case FFI_AIX:
506
507       tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
508       fd = (aix_fd *)(void *)ffi_closure_ASM;
509
510       FFI_ASSERT (cif->abi == FFI_AIX);
511
512       tramp_aix->code_pointer = fd->code_pointer;
513       tramp_aix->toc = fd->toc;
514       tramp_aix->static_chain = closure;
515       closure->cif = cif;
516       closure->fun = fun;
517       closure->user_data = user_data;
518
519     default:
520
521       FFI_ASSERT(0);
522       break;
523     }
524   return FFI_OK;
525 }
526
527 static void
528 flush_icache(char *addr)
529 {
530 #ifndef _AIX
531   __asm__ volatile (
532                 "dcbf 0,%0;"
533                 "sync;"
534                 "icbi 0,%0;"
535                 "sync;"
536                 "isync;"
537                 : : "r"(addr) : "memory");
538 #endif
539 }
540
541 static void
542 flush_range(char * addr1, int size)
543 {
544 #define MIN_LINE_SIZE 32
545   int i;
546   for (i = 0; i < size; i += MIN_LINE_SIZE)
547     flush_icache(addr1+i);
548   flush_icache(addr1+size-1);
549 }
550
551 typedef union
552 {
553   float f;
554   double d;
555 } ffi_dblfl;
556
557 int ffi_closure_helper_DARWIN (ffi_closure*, void*,
558                                unsigned long*, ffi_dblfl*);
559
560 /* Basically the trampoline invokes ffi_closure_ASM, and on
561    entry, r11 holds the address of the closure.
562    After storing the registers that could possibly contain
563    parameters to be passed into the stack frame and setting
564    up space for a return value, ffi_closure_ASM invokes the
565    following helper function to do most of the work.  */
566
567 int ffi_closure_helper_DARWIN (ffi_closure* closure, void * rvalue,
568                                unsigned long * pgr, ffi_dblfl * pfr)
569 {
570   /* rvalue is the pointer to space for return value in closure assembly
571      pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
572      pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM.  */
573
574
575   void **          avalue;
576   ffi_type **      arg_types;
577   long             i, avn;
578   long             nf;   /* number of floating registers already used.  */
579   long             ng;   /* number of general registers already used.  */
580   ffi_cif *        cif;
581   double           temp;
582   unsigned         size_al;
583
584   cif = closure->cif;
585   avalue = alloca(cif->nargs * sizeof(void *));
586
587   nf = 0;
588   ng = 0;
589
590   /* Copy the caller's structure return value address so that the closure
591      returns the data directly to the caller.  */
592   if (cif->rtype->type == FFI_TYPE_STRUCT)
593     {
594       rvalue = (void *) *pgr;
595       pgr++;
596       ng++;
597     }
598
599   i = 0;
600   avn = cif->nargs;
601   arg_types = cif->arg_types;
602
603   /* Grab the addresses of the arguments from the stack frame.  */
604   while (i < avn)
605     {
606       switch (arg_types[i]->type)
607         {
608         case FFI_TYPE_SINT8:
609         case FFI_TYPE_UINT8:
610           avalue[i] = (char *) pgr + 3;
611           ng++;
612           pgr++;
613           break;
614
615         case FFI_TYPE_SINT16:
616         case FFI_TYPE_UINT16:
617           avalue[i] = (char *) pgr + 2;
618           ng++;
619           pgr++;
620           break;
621
622         case FFI_TYPE_SINT32:
623         case FFI_TYPE_UINT32:
624         case FFI_TYPE_POINTER:
625           avalue[i] = pgr;
626           ng++;
627           pgr++;
628           break;
629
630         case FFI_TYPE_STRUCT:
631           /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
632              SI 4 bytes) are aligned as if they were those modes.  */
633           size_al = arg_types[i]->size;
634           /* If the first member of the struct is a double, then align
635              the struct to double-word.
636              Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3.  */
637           if (arg_types[i]->elements[0]->type == 3)
638             size_al = ALIGN(arg_types[i]->size, 8);
639           if (size_al < 3 && cif->abi == FFI_DARWIN)
640             avalue[i] = (void*) pgr + 4 - size_al;
641           else
642             avalue[i] = (void*) pgr;
643           ng += (size_al + 3) / 4;
644           pgr += (size_al + 3) / 4;
645           break;
646
647         case FFI_TYPE_SINT64:
648         case FFI_TYPE_UINT64:
649           /* Long long ints are passed in two gpr's.  */
650           avalue[i] = pgr;
651           ng += 2;
652           pgr += 2;
653           break;
654
655         case FFI_TYPE_FLOAT:
656           /* A float value consumes a GPR.
657              There are 13 64bit floating point registers.  */
658           if (nf < NUM_FPR_ARG_REGISTERS)
659             {
660               temp = pfr->d;
661               pfr->f = (float)temp;
662               avalue[i] = pfr;
663               pfr++;
664             }
665           else
666             {
667               avalue[i] = pgr;
668             }
669           nf++;
670           ng++;
671           pgr++;
672           break;
673
674         case FFI_TYPE_DOUBLE:
675           /* A double value consumes two GPRs.
676              There are 13 64bit floating point registers.  */
677           if (nf < NUM_FPR_ARG_REGISTERS)
678             {
679               avalue[i] = pfr;
680               pfr++;
681             }
682           else
683             {
684               avalue[i] = pgr;
685             }
686           nf++;
687           ng += 2;
688           pgr += 2;
689           break;
690
691         default:
692           FFI_ASSERT(0);
693         }
694       i++;
695     }
696
697   (closure->fun) (cif, rvalue, avalue, closure->user_data);
698
699   /* Tell ffi_closure_ASM to perform return type promotions.  */
700   return cif->rtype->type;
701 }