The powerpc64 support opted to pass floating point values both in the
[platform/upstream/libffi.git] / src / powerpc / ffi.c
1 /* -----------------------------------------------------------------------
2    ffi.c - Copyright (C) 2011 Anthony Green
3            Copyright (C) 2011 Kyle Moffett
4            Copyright (C) 2008 Red Hat, Inc
5            Copyright (C) 2007, 2008 Free Software Foundation, Inc
6            Copyright (c) 1998 Geoffrey Keating
7
8    PowerPC Foreign Function Interface
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 #include <stdio.h>
35
36
37 extern void ffi_closure_SYSV (void);
38 extern void FFI_HIDDEN ffi_closure_LINUX64 (void);
39
40 enum {
41   /* The assembly depends on these exact flags.  */
42   FLAG_RETURNS_SMST     = 1 << (31-31), /* Used for FFI_SYSV small structs.  */
43   FLAG_RETURNS_NOTHING  = 1 << (31-30), /* These go in cr7 */
44 #ifndef __NO_FPRS__
45   FLAG_RETURNS_FP       = 1 << (31-29),
46 #endif
47   FLAG_RETURNS_64BITS   = 1 << (31-28),
48
49   FLAG_RETURNS_128BITS  = 1 << (31-27), /* cr6  */
50
51   FLAG_SYSV_SMST_R4     = 1 << (31-26), /* use r4 for FFI_SYSV 8 byte
52                                            structs.  */
53   FLAG_SYSV_SMST_R3     = 1 << (31-25), /* use r3 for FFI_SYSV 4 byte
54                                            structs.  */
55
56   FLAG_ARG_NEEDS_COPY   = 1 << (31- 7),
57 #ifndef __NO_FPRS__
58   FLAG_FP_ARGUMENTS     = 1 << (31- 6), /* cr1.eq; specified by ABI */
59 #endif
60   FLAG_4_GPR_ARGUMENTS  = 1 << (31- 5),
61   FLAG_RETVAL_REFERENCE = 1 << (31- 4)
62 };
63
64 /* About the SYSV ABI.  */
65 #define ASM_NEEDS_REGISTERS 4
66 #define NUM_GPR_ARG_REGISTERS 8
67 #ifndef __NO_FPRS__
68 # define NUM_FPR_ARG_REGISTERS 8
69 #endif
70
71 /* ffi_prep_args_SYSV is called by the assembly routine once stack space
72    has been allocated for the function's arguments.
73
74    The stack layout we want looks like this:
75
76    |   Return address from ffi_call_SYSV 4bytes |       higher addresses
77    |--------------------------------------------|
78    |   Previous backchain pointer       4       |       stack pointer here
79    |--------------------------------------------|<+ <<< on entry to
80    |   Saved r28-r31                    4*4     | |     ffi_call_SYSV
81    |--------------------------------------------| |
82    |   GPR registers r3-r10             8*4     | |     ffi_call_SYSV
83    |--------------------------------------------| |
84    |   FPR registers f1-f8 (optional)   8*8     | |
85    |--------------------------------------------| |     stack   |
86    |   Space for copied structures              | |     grows   |
87    |--------------------------------------------| |     down    V
88    |   Parameters that didn't fit in registers  | |
89    |--------------------------------------------| |     lower addresses
90    |   Space for callee's LR            4       | |
91    |--------------------------------------------| |     stack pointer here
92    |   Current backchain pointer        4       |-/     during
93    |--------------------------------------------|   <<< ffi_call_SYSV
94
95 */
96
97 void
98 ffi_prep_args_SYSV (extended_cif *ecif, unsigned *const stack)
99 {
100   const unsigned bytes = ecif->cif->bytes;
101   const unsigned flags = ecif->cif->flags;
102
103   typedef union {
104     char *c;
105     unsigned *u;
106     long long *ll;
107     float *f;
108     double *d;
109   } valp;
110
111   /* 'stacktop' points at the previous backchain pointer.  */
112   valp stacktop;
113
114   /* 'gpr_base' points at the space for gpr3, and grows upwards as
115      we use GPR registers.  */
116   valp gpr_base;
117   int intarg_count;
118
119 #ifndef __NO_FPRS__
120   /* 'fpr_base' points at the space for fpr1, and grows upwards as
121      we use FPR registers.  */
122   valp fpr_base;
123   int fparg_count;
124 #endif
125
126   /* 'copy_space' grows down as we put structures in it.  It should
127      stay 16-byte aligned.  */
128   valp copy_space;
129
130   /* 'next_arg' grows up as we put parameters in it.  */
131   valp next_arg;
132
133   int i;
134   ffi_type **ptr;
135 #ifndef __NO_FPRS__
136   double double_tmp;
137 #endif
138   union {
139     void **v;
140     char **c;
141     signed char **sc;
142     unsigned char **uc;
143     signed short **ss;
144     unsigned short **us;
145     unsigned int **ui;
146     long long **ll;
147     float **f;
148     double **d;
149   } p_argv;
150   size_t struct_copy_size;
151   unsigned gprvalue;
152
153   stacktop.c = (char *) stack + bytes;
154   gpr_base.u = stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS;
155   intarg_count = 0;
156 #ifndef __NO_FPRS__
157   fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS;
158   fparg_count = 0;
159   copy_space.c = ((flags & FLAG_FP_ARGUMENTS) ? fpr_base.c : gpr_base.c);
160 #else
161   copy_space.c = gpr_base.c;
162 #endif
163   next_arg.u = stack + 2;
164
165   /* Check that everything starts aligned properly.  */
166   FFI_ASSERT (((unsigned long) (char *) stack & 0xF) == 0);
167   FFI_ASSERT (((unsigned long) copy_space.c & 0xF) == 0);
168   FFI_ASSERT (((unsigned long) stacktop.c & 0xF) == 0);
169   FFI_ASSERT ((bytes & 0xF) == 0);
170   FFI_ASSERT (copy_space.c >= next_arg.c);
171
172   /* Deal with return values that are actually pass-by-reference.  */
173   if (flags & FLAG_RETVAL_REFERENCE)
174     {
175       *gpr_base.u++ = (unsigned long) (char *) ecif->rvalue;
176       intarg_count++;
177     }
178
179   /* Now for the arguments.  */
180   p_argv.v = ecif->avalue;
181   for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
182        i > 0;
183        i--, ptr++, p_argv.v++)
184     {
185       unsigned short typenum = (*ptr)->type;
186
187       /* We may need to handle some values depending on ABI */
188       if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT) {
189                 if (typenum == FFI_TYPE_FLOAT)
190                         typenum = FFI_TYPE_UINT32;
191                 if (typenum == FFI_TYPE_DOUBLE)
192                         typenum = FFI_TYPE_UINT64;
193                 if (typenum == FFI_TYPE_LONGDOUBLE)
194                         typenum = FFI_TYPE_UINT128;
195       } else if (ecif->cif->abi != FFI_LINUX) {
196 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
197                 if (typenum == FFI_TYPE_LONGDOUBLE)
198                         typenum = FFI_TYPE_STRUCT;
199 #endif
200       }
201
202       /* Now test the translated value */
203       switch (typenum) {
204 #ifndef __NO_FPRS__
205         case FFI_TYPE_FLOAT:
206           /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32.  */
207           double_tmp = **p_argv.f;
208           if (fparg_count >= NUM_FPR_ARG_REGISTERS)
209             {
210               *next_arg.f = (float) double_tmp;
211               next_arg.u += 1;
212               intarg_count++;
213             }
214           else
215             *fpr_base.d++ = double_tmp;
216           fparg_count++;
217           FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
218           break;
219
220         case FFI_TYPE_DOUBLE:
221           /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64.  */
222           double_tmp = **p_argv.d;
223
224           if (fparg_count >= NUM_FPR_ARG_REGISTERS)
225             {
226               if (intarg_count >= NUM_GPR_ARG_REGISTERS
227                   && intarg_count % 2 != 0)
228                 {
229                   intarg_count++;
230                   next_arg.u++;
231                 }
232               *next_arg.d = double_tmp;
233               next_arg.u += 2;
234             }
235           else
236             *fpr_base.d++ = double_tmp;
237           fparg_count++;
238           FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
239           break;
240
241 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
242         case FFI_TYPE_LONGDOUBLE:
243               double_tmp = (*p_argv.d)[0];
244
245               if (fparg_count >= NUM_FPR_ARG_REGISTERS - 1)
246                 {
247                   if (intarg_count >= NUM_GPR_ARG_REGISTERS
248                       && intarg_count % 2 != 0)
249                     {
250                       intarg_count++;
251                       next_arg.u++;
252                     }
253                   *next_arg.d = double_tmp;
254                   next_arg.u += 2;
255                   double_tmp = (*p_argv.d)[1];
256                   *next_arg.d = double_tmp;
257                   next_arg.u += 2;
258                 }
259               else
260                 {
261                   *fpr_base.d++ = double_tmp;
262                   double_tmp = (*p_argv.d)[1];
263                   *fpr_base.d++ = double_tmp;
264                 }
265
266               fparg_count += 2;
267               FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
268           break;
269 #endif
270 #endif /* have FPRs */
271
272         /*
273          * The soft float ABI for long doubles works like this, a long double
274          * is passed in four consecutive GPRs if available.  A maximum of 2
275          * long doubles can be passed in gprs.  If we do not have 4 GPRs
276          * left, the long double is passed on the stack, 4-byte aligned.
277          */
278         case FFI_TYPE_UINT128: {
279                 unsigned int int_tmp = (*p_argv.ui)[0];
280                 unsigned int ii;
281                 if (intarg_count >= NUM_GPR_ARG_REGISTERS - 3) {
282                         if (intarg_count < NUM_GPR_ARG_REGISTERS)
283                                 intarg_count += NUM_GPR_ARG_REGISTERS - intarg_count;
284                         *(next_arg.u++) = int_tmp;
285                         for (ii = 1; ii < 4; ii++) {
286                                 int_tmp = (*p_argv.ui)[ii];
287                                 *(next_arg.u++) = int_tmp;
288                         }
289                 } else {
290                         *(gpr_base.u++) = int_tmp;
291                         for (ii = 1; ii < 4; ii++) {
292                                 int_tmp = (*p_argv.ui)[ii];
293                                 *(gpr_base.u++) = int_tmp;
294                         }
295                 }
296                 intarg_count += 4;
297                 break;
298         }
299
300         case FFI_TYPE_UINT64:
301         case FFI_TYPE_SINT64:
302           if (intarg_count == NUM_GPR_ARG_REGISTERS-1)
303             intarg_count++;
304           if (intarg_count >= NUM_GPR_ARG_REGISTERS)
305             {
306               if (intarg_count % 2 != 0)
307                 {
308                   intarg_count++;
309                   next_arg.u++;
310                 }
311               *next_arg.ll = **p_argv.ll;
312               next_arg.u += 2;
313             }
314           else
315             {
316               /* whoops: abi states only certain register pairs
317                * can be used for passing long long int
318                * specifically (r3,r4), (r5,r6), (r7,r8),
319                * (r9,r10) and if next arg is long long but
320                * not correct starting register of pair then skip
321                * until the proper starting register
322                */
323               if (intarg_count % 2 != 0)
324                 {
325                   intarg_count ++;
326                   gpr_base.u++;
327                 }
328               *gpr_base.ll++ = **p_argv.ll;
329             }
330           intarg_count += 2;
331           break;
332
333         case FFI_TYPE_STRUCT:
334           struct_copy_size = ((*ptr)->size + 15) & ~0xF;
335           copy_space.c -= struct_copy_size;
336           memcpy (copy_space.c, *p_argv.c, (*ptr)->size);
337
338           gprvalue = (unsigned long) copy_space.c;
339
340           FFI_ASSERT (copy_space.c > next_arg.c);
341           FFI_ASSERT (flags & FLAG_ARG_NEEDS_COPY);
342           goto putgpr;
343
344         case FFI_TYPE_UINT8:
345           gprvalue = **p_argv.uc;
346           goto putgpr;
347         case FFI_TYPE_SINT8:
348           gprvalue = **p_argv.sc;
349           goto putgpr;
350         case FFI_TYPE_UINT16:
351           gprvalue = **p_argv.us;
352           goto putgpr;
353         case FFI_TYPE_SINT16:
354           gprvalue = **p_argv.ss;
355           goto putgpr;
356
357         case FFI_TYPE_INT:
358         case FFI_TYPE_UINT32:
359         case FFI_TYPE_SINT32:
360         case FFI_TYPE_POINTER:
361
362           gprvalue = **p_argv.ui;
363
364         putgpr:
365           if (intarg_count >= NUM_GPR_ARG_REGISTERS)
366             *next_arg.u++ = gprvalue;
367           else
368             *gpr_base.u++ = gprvalue;
369           intarg_count++;
370           break;
371         }
372     }
373
374   /* Check that we didn't overrun the stack...  */
375   FFI_ASSERT (copy_space.c >= next_arg.c);
376   FFI_ASSERT (gpr_base.u <= stacktop.u - ASM_NEEDS_REGISTERS);
377   /* The assert below is testing that the number of integer arguments agrees
378      with the number found in ffi_prep_cif_machdep().  However, intarg_count
379      is incremented whenever we place an FP arg on the stack, so account for
380      that before our assert test.  */
381 #ifndef __NO_FPRS__
382   if (fparg_count > NUM_FPR_ARG_REGISTERS)
383     intarg_count -= fparg_count - NUM_FPR_ARG_REGISTERS;
384   FFI_ASSERT (fpr_base.u
385               <= stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
386 #endif
387   FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
388 }
389
390 /* About the LINUX64 ABI.  */
391 enum {
392   NUM_GPR_ARG_REGISTERS64 = 8,
393   NUM_FPR_ARG_REGISTERS64 = 13
394 };
395 enum { ASM_NEEDS_REGISTERS64 = 4 };
396
397 /* ffi_prep_args64 is called by the assembly routine once stack space
398    has been allocated for the function's arguments.
399
400    The stack layout we want looks like this:
401
402    |   Ret addr from ffi_call_LINUX64   8bytes  |       higher addresses
403    |--------------------------------------------|
404    |   CR save area                     8bytes  |
405    |--------------------------------------------|
406    |   Previous backchain pointer       8       |       stack pointer here
407    |--------------------------------------------|<+ <<< on entry to
408    |   Saved r28-r31                    4*8     | |     ffi_call_LINUX64
409    |--------------------------------------------| |
410    |   GPR registers r3-r10             8*8     | |
411    |--------------------------------------------| |
412    |   FPR registers f1-f13 (optional)  13*8    | |
413    |--------------------------------------------| |
414    |   Parameter save area                      | |
415    |--------------------------------------------| |
416    |   TOC save area                    8       | |
417    |--------------------------------------------| |     stack   |
418    |   Linker doubleword                8       | |     grows   |
419    |--------------------------------------------| |     down    V
420    |   Compiler doubleword              8       | |
421    |--------------------------------------------| |     lower addresses
422    |   Space for callee's LR            8       | |
423    |--------------------------------------------| |
424    |   CR save area                     8       | |
425    |--------------------------------------------| |     stack pointer here
426    |   Current backchain pointer        8       |-/     during
427    |--------------------------------------------|   <<< ffi_call_LINUX64
428
429 */
430
431 void FFI_HIDDEN
432 ffi_prep_args64 (extended_cif *ecif, unsigned long *const stack)
433 {
434   const unsigned long bytes = ecif->cif->bytes;
435   const unsigned long flags = ecif->cif->flags;
436
437   typedef union {
438     char *c;
439     unsigned long *ul;
440     float *f;
441     double *d;
442   } valp;
443
444   /* 'stacktop' points at the previous backchain pointer.  */
445   valp stacktop;
446
447   /* 'next_arg' points at the space for gpr3, and grows upwards as
448      we use GPR registers, then continues at rest.  */
449   valp gpr_base;
450   valp gpr_end;
451   valp rest;
452   valp next_arg;
453
454   /* 'fpr_base' points at the space for fpr3, and grows upwards as
455      we use FPR registers.  */
456   valp fpr_base;
457   unsigned int fparg_count;
458
459   unsigned int i, words, nargs, nfixedargs;
460   ffi_type **ptr;
461   double double_tmp;
462   union {
463     void **v;
464     char **c;
465     signed char **sc;
466     unsigned char **uc;
467     signed short **ss;
468     unsigned short **us;
469     signed int **si;
470     unsigned int **ui;
471     unsigned long **ul;
472     float **f;
473     double **d;
474   } p_argv;
475   unsigned long gprvalue;
476
477   stacktop.c = (char *) stack + bytes;
478   gpr_base.ul = stacktop.ul - ASM_NEEDS_REGISTERS64 - NUM_GPR_ARG_REGISTERS64;
479   gpr_end.ul = gpr_base.ul + NUM_GPR_ARG_REGISTERS64;
480   rest.ul = stack + 6 + NUM_GPR_ARG_REGISTERS64;
481   fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS64;
482   fparg_count = 0;
483   next_arg.ul = gpr_base.ul;
484
485   /* Check that everything starts aligned properly.  */
486   FFI_ASSERT (((unsigned long) (char *) stack & 0xF) == 0);
487   FFI_ASSERT (((unsigned long) stacktop.c & 0xF) == 0);
488   FFI_ASSERT ((bytes & 0xF) == 0);
489
490   /* Deal with return values that are actually pass-by-reference.  */
491   if (flags & FLAG_RETVAL_REFERENCE)
492     *next_arg.ul++ = (unsigned long) (char *) ecif->rvalue;
493
494   /* Now for the arguments.  */
495   p_argv.v = ecif->avalue;
496   nargs = ecif->cif->nargs;
497   nfixedargs = ecif->cif->nfixedargs;
498   for (ptr = ecif->cif->arg_types, i = 0;
499        i < nargs;
500        i++, ptr++, p_argv.v++)
501     {
502       switch ((*ptr)->type)
503         {
504         case FFI_TYPE_FLOAT:
505           double_tmp = **p_argv.f;
506           if (fparg_count < NUM_FPR_ARG_REGISTERS64 && i < nfixedargs)
507             *fpr_base.d++ = double_tmp;
508           else
509             *next_arg.f = (float) double_tmp;
510           if (++next_arg.ul == gpr_end.ul)
511             next_arg.ul = rest.ul;
512           fparg_count++;
513           FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
514           break;
515
516         case FFI_TYPE_DOUBLE:
517           double_tmp = **p_argv.d;
518           if (fparg_count < NUM_FPR_ARG_REGISTERS64 && i < nfixedargs)
519             *fpr_base.d++ = double_tmp;
520           else
521             *next_arg.d = double_tmp;
522           if (++next_arg.ul == gpr_end.ul)
523             next_arg.ul = rest.ul;
524           fparg_count++;
525           FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
526           break;
527
528 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
529         case FFI_TYPE_LONGDOUBLE:
530           double_tmp = (*p_argv.d)[0];
531           if (fparg_count < NUM_FPR_ARG_REGISTERS64 && i < nfixedargs)
532             *fpr_base.d++ = double_tmp;
533           else
534             *next_arg.d = double_tmp;
535           if (++next_arg.ul == gpr_end.ul)
536             next_arg.ul = rest.ul;
537           fparg_count++;
538           double_tmp = (*p_argv.d)[1];
539           if (fparg_count < NUM_FPR_ARG_REGISTERS64 && i < nfixedargs)
540             *fpr_base.d++ = double_tmp;
541           else
542             *next_arg.d = double_tmp;
543           if (++next_arg.ul == gpr_end.ul)
544             next_arg.ul = rest.ul;
545           fparg_count++;
546           FFI_ASSERT (__LDBL_MANT_DIG__ == 106);
547           FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
548           break;
549 #endif
550
551         case FFI_TYPE_STRUCT:
552           words = ((*ptr)->size + 7) / 8;
553           if (next_arg.ul >= gpr_base.ul && next_arg.ul + words > gpr_end.ul)
554             {
555               size_t first = gpr_end.c - next_arg.c;
556               memcpy (next_arg.c, *p_argv.c, first);
557               memcpy (rest.c, *p_argv.c + first, (*ptr)->size - first);
558               next_arg.c = rest.c + words * 8 - first;
559             }
560           else
561             {
562               char *where = next_arg.c;
563
564 #ifndef __LITTLE_ENDIAN__
565               /* Structures with size less than eight bytes are passed
566                  left-padded.  */
567               if ((*ptr)->size < 8)
568                 where += 8 - (*ptr)->size;
569 #endif
570               memcpy (where, *p_argv.c, (*ptr)->size);
571               next_arg.ul += words;
572               if (next_arg.ul == gpr_end.ul)
573                 next_arg.ul = rest.ul;
574             }
575           break;
576
577         case FFI_TYPE_UINT8:
578           gprvalue = **p_argv.uc;
579           goto putgpr;
580         case FFI_TYPE_SINT8:
581           gprvalue = **p_argv.sc;
582           goto putgpr;
583         case FFI_TYPE_UINT16:
584           gprvalue = **p_argv.us;
585           goto putgpr;
586         case FFI_TYPE_SINT16:
587           gprvalue = **p_argv.ss;
588           goto putgpr;
589         case FFI_TYPE_UINT32:
590           gprvalue = **p_argv.ui;
591           goto putgpr;
592         case FFI_TYPE_INT:
593         case FFI_TYPE_SINT32:
594           gprvalue = **p_argv.si;
595           goto putgpr;
596
597         case FFI_TYPE_UINT64:
598         case FFI_TYPE_SINT64:
599         case FFI_TYPE_POINTER:
600           gprvalue = **p_argv.ul;
601         putgpr:
602           *next_arg.ul++ = gprvalue;
603           if (next_arg.ul == gpr_end.ul)
604             next_arg.ul = rest.ul;
605           break;
606         }
607     }
608
609   FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS
610               || (next_arg.ul >= gpr_base.ul
611                   && next_arg.ul <= gpr_base.ul + 4));
612 }
613
614
615
616 /* Perform machine dependent cif processing */
617 static ffi_status
618 ffi_prep_cif_machdep_core (ffi_cif *cif)
619 {
620   /* All this is for the SYSV and LINUX64 ABI.  */
621   ffi_type **ptr;
622   unsigned bytes;
623   unsigned i, fparg_count = 0, intarg_count = 0;
624   unsigned flags = cif->flags;
625   unsigned struct_copy_size = 0;
626   unsigned type = cif->rtype->type;
627   unsigned size = cif->rtype->size;
628
629   if (cif->abi != FFI_LINUX64)
630     {
631       /* All the machine-independent calculation of cif->bytes will be wrong.
632          Redo the calculation for SYSV.  */
633
634       /* Space for the frame pointer, callee's LR, and the asm's temp regs.  */
635       bytes = (2 + ASM_NEEDS_REGISTERS) * sizeof (int);
636
637       /* Space for the GPR registers.  */
638       bytes += NUM_GPR_ARG_REGISTERS * sizeof (int);
639     }
640   else
641     {
642       /* 64-bit ABI.  */
643
644       /* Space for backchain, CR, LR, cc/ld doubleword, TOC and the asm's temp
645          regs.  */
646       bytes = (6 + ASM_NEEDS_REGISTERS64) * sizeof (long);
647
648       /* Space for the mandatory parm save area and general registers.  */
649       bytes += 2 * NUM_GPR_ARG_REGISTERS64 * sizeof (long);
650     }
651
652   /* Return value handling.  The rules for SYSV are as follows:
653      - 32-bit (or less) integer values are returned in gpr3;
654      - Structures of size <= 4 bytes also returned in gpr3;
655      - 64-bit integer values and structures between 5 and 8 bytes are returned
656      in gpr3 and gpr4;
657      - Single/double FP values are returned in fpr1;
658      - Larger structures are allocated space and a pointer is passed as
659      the first argument.
660      - long doubles (if not equivalent to double) are returned in
661      fpr1,fpr2 for Linux and as for large structs for SysV.
662      For LINUX64:
663      - integer values in gpr3;
664      - Structures/Unions by reference;
665      - Single/double FP values in fpr1, long double in fpr1,fpr2.
666      - soft-float float/doubles are treated as UINT32/UINT64 respectivley.
667      - soft-float long doubles are returned in gpr3-gpr6.  */
668   /* First translate for softfloat/nonlinux */
669   if (cif->abi == FFI_LINUX_SOFT_FLOAT)
670     {
671       if (type == FFI_TYPE_FLOAT)
672         type = FFI_TYPE_UINT32;
673       if (type == FFI_TYPE_DOUBLE)
674         type = FFI_TYPE_UINT64;
675       if (type == FFI_TYPE_LONGDOUBLE)
676         type = FFI_TYPE_UINT128;
677     }
678   else if (cif->abi != FFI_LINUX
679            && cif->abi != FFI_LINUX64)
680     {
681 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
682       if (type == FFI_TYPE_LONGDOUBLE)
683         type = FFI_TYPE_STRUCT;
684 #endif
685     }
686
687   switch (type)
688     {
689 #ifndef __NO_FPRS__
690 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
691     case FFI_TYPE_LONGDOUBLE:
692       flags |= FLAG_RETURNS_128BITS;
693       /* Fall through.  */
694 #endif
695     case FFI_TYPE_DOUBLE:
696       flags |= FLAG_RETURNS_64BITS;
697       /* Fall through.  */
698     case FFI_TYPE_FLOAT:
699       flags |= FLAG_RETURNS_FP;
700       break;
701 #endif
702
703     case FFI_TYPE_UINT128:
704       flags |= FLAG_RETURNS_128BITS;
705       /* Fall through.  */
706     case FFI_TYPE_UINT64:
707     case FFI_TYPE_SINT64:
708       flags |= FLAG_RETURNS_64BITS;
709       break;
710
711     case FFI_TYPE_STRUCT:
712       if (cif->abi == FFI_SYSV)
713         {
714           /* The final SYSV ABI says that structures smaller or equal 8 bytes
715              are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
716              in memory.  */
717
718           /* Treat structs with size <= 8 bytes.  */
719           if (size <= 8)
720             {
721               flags |= FLAG_RETURNS_SMST;
722               /* These structs are returned in r3. We pack the type and the
723                  precalculated shift value (needed in the sysv.S) into flags.
724                  The same applies for the structs returned in r3/r4.  */
725               if (size <= 4)
726                 {
727                   flags |= FLAG_SYSV_SMST_R3;
728                   flags |= 8 * (4 - size) << 8;
729                   break;
730                 }
731               /* These structs are returned in r3 and r4. See above.   */
732               if  (size <= 8)
733                 {
734                   flags |= FLAG_SYSV_SMST_R3 | FLAG_SYSV_SMST_R4;
735                   flags |= 8 * (8 - size) << 8;
736                   break;
737                 }
738             }
739         }
740
741       intarg_count++;
742       flags |= FLAG_RETVAL_REFERENCE;
743       /* Fall through.  */
744     case FFI_TYPE_VOID:
745       flags |= FLAG_RETURNS_NOTHING;
746       break;
747
748     default:
749       /* Returns 32-bit integer, or similar.  Nothing to do here.  */
750       break;
751     }
752
753   if (cif->abi != FFI_LINUX64)
754     /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
755        first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
756        goes on the stack.  Structures and long doubles (if not equivalent
757        to double) are passed as a pointer to a copy of the structure.
758        Stuff on the stack needs to keep proper alignment.  */
759     for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
760       {
761         unsigned short typenum = (*ptr)->type;
762
763         /* We may need to handle some values depending on ABI */
764         if (cif->abi == FFI_LINUX_SOFT_FLOAT) {
765                 if (typenum == FFI_TYPE_FLOAT)
766                         typenum = FFI_TYPE_UINT32;
767                 if (typenum == FFI_TYPE_DOUBLE)
768                         typenum = FFI_TYPE_UINT64;
769                 if (typenum == FFI_TYPE_LONGDOUBLE)
770                         typenum = FFI_TYPE_UINT128;
771         } else if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX64) {
772 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
773                 if (typenum == FFI_TYPE_LONGDOUBLE)
774                         typenum = FFI_TYPE_STRUCT;
775 #endif
776         }
777
778         switch (typenum) {
779 #ifndef __NO_FPRS__
780           case FFI_TYPE_FLOAT:
781             fparg_count++;
782             /* floating singles are not 8-aligned on stack */
783             break;
784
785 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
786           case FFI_TYPE_LONGDOUBLE:
787             fparg_count++;
788             /* Fall thru */
789 #endif
790           case FFI_TYPE_DOUBLE:
791             fparg_count++;
792             /* If this FP arg is going on the stack, it must be
793                8-byte-aligned.  */
794             if (fparg_count > NUM_FPR_ARG_REGISTERS
795                 && intarg_count >= NUM_GPR_ARG_REGISTERS
796                 && intarg_count % 2 != 0)
797               intarg_count++;
798             break;
799 #endif
800           case FFI_TYPE_UINT128:
801                 /*
802                  * A long double in FFI_LINUX_SOFT_FLOAT can use only a set
803                  * of four consecutive gprs. If we do not have enough, we
804                  * have to adjust the intarg_count value.
805                  */
806                 if (intarg_count >= NUM_GPR_ARG_REGISTERS - 3
807                                 && intarg_count < NUM_GPR_ARG_REGISTERS)
808                         intarg_count = NUM_GPR_ARG_REGISTERS;
809                 intarg_count += 4;
810                 break;
811
812           case FFI_TYPE_UINT64:
813           case FFI_TYPE_SINT64:
814             /* 'long long' arguments are passed as two words, but
815                either both words must fit in registers or both go
816                on the stack.  If they go on the stack, they must
817                be 8-byte-aligned.
818
819                Also, only certain register pairs can be used for
820                passing long long int -- specifically (r3,r4), (r5,r6),
821                (r7,r8), (r9,r10).
822             */
823             if (intarg_count == NUM_GPR_ARG_REGISTERS-1
824                 || intarg_count % 2 != 0)
825               intarg_count++;
826             intarg_count += 2;
827             break;
828
829           case FFI_TYPE_STRUCT:
830             /* We must allocate space for a copy of these to enforce
831                pass-by-value.  Pad the space up to a multiple of 16
832                bytes (the maximum alignment required for anything under
833                the SYSV ABI).  */
834             struct_copy_size += ((*ptr)->size + 15) & ~0xF;
835             /* Fall through (allocate space for the pointer).  */
836
837           case FFI_TYPE_POINTER:
838           case FFI_TYPE_INT:
839           case FFI_TYPE_UINT32:
840           case FFI_TYPE_SINT32:
841           case FFI_TYPE_UINT16:
842           case FFI_TYPE_SINT16:
843           case FFI_TYPE_UINT8:
844           case FFI_TYPE_SINT8:
845             /* Everything else is passed as a 4-byte word in a GPR, either
846                the object itself or a pointer to it.  */
847             intarg_count++;
848             break;
849           default:
850                 FFI_ASSERT (0);
851           }
852       }
853   else
854     for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
855       {
856         switch ((*ptr)->type)
857           {
858 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
859           case FFI_TYPE_LONGDOUBLE:
860             fparg_count += 2;
861             intarg_count += 2;
862             break;
863 #endif
864           case FFI_TYPE_FLOAT:
865           case FFI_TYPE_DOUBLE:
866             fparg_count++;
867             intarg_count++;
868             break;
869
870           case FFI_TYPE_STRUCT:
871             intarg_count += ((*ptr)->size + 7) / 8;
872             break;
873
874           case FFI_TYPE_POINTER:
875           case FFI_TYPE_UINT64:
876           case FFI_TYPE_SINT64:
877           case FFI_TYPE_INT:
878           case FFI_TYPE_UINT32:
879           case FFI_TYPE_SINT32:
880           case FFI_TYPE_UINT16:
881           case FFI_TYPE_SINT16:
882           case FFI_TYPE_UINT8:
883           case FFI_TYPE_SINT8:
884             /* Everything else is passed as a 8-byte word in a GPR, either
885                the object itself or a pointer to it.  */
886             intarg_count++;
887             break;
888           default:
889                 FFI_ASSERT (0);
890           }
891       }
892
893 #ifndef __NO_FPRS__
894   if (fparg_count != 0)
895     flags |= FLAG_FP_ARGUMENTS;
896 #endif
897   if (intarg_count > 4)
898     flags |= FLAG_4_GPR_ARGUMENTS;
899   if (struct_copy_size != 0)
900     flags |= FLAG_ARG_NEEDS_COPY;
901
902   if (cif->abi != FFI_LINUX64)
903     {
904 #ifndef __NO_FPRS__
905       /* Space for the FPR registers, if needed.  */
906       if (fparg_count != 0)
907         bytes += NUM_FPR_ARG_REGISTERS * sizeof (double);
908 #endif
909
910       /* Stack space.  */
911       if (intarg_count > NUM_GPR_ARG_REGISTERS)
912         bytes += (intarg_count - NUM_GPR_ARG_REGISTERS) * sizeof (int);
913 #ifndef __NO_FPRS__
914       if (fparg_count > NUM_FPR_ARG_REGISTERS)
915         bytes += (fparg_count - NUM_FPR_ARG_REGISTERS) * sizeof (double);
916 #endif
917     }
918   else
919     {
920 #ifndef __NO_FPRS__
921       /* Space for the FPR registers, if needed.  */
922       if (fparg_count != 0)
923         bytes += NUM_FPR_ARG_REGISTERS64 * sizeof (double);
924 #endif
925
926       /* Stack space.  */
927       if (intarg_count > NUM_GPR_ARG_REGISTERS64)
928         bytes += (intarg_count - NUM_GPR_ARG_REGISTERS64) * sizeof (long);
929     }
930
931   /* The stack space allocated needs to be a multiple of 16 bytes.  */
932   bytes = (bytes + 15) & ~0xF;
933
934   /* Add in the space for the copied structures.  */
935   bytes += struct_copy_size;
936
937   cif->flags = flags;
938   cif->bytes = bytes;
939
940   return FFI_OK;
941 }
942
943 ffi_status
944 ffi_prep_cif_machdep (ffi_cif *cif)
945 {
946   cif->nfixedargs = cif->nargs;
947   return ffi_prep_cif_machdep_core (cif);
948 }
949
950 ffi_status
951 ffi_prep_cif_machdep_var (ffi_cif *cif,
952                           unsigned int nfixedargs,
953                           unsigned int ntotalargs MAYBE_UNUSED)
954 {
955   cif->nfixedargs = nfixedargs;
956   return ffi_prep_cif_machdep_core (cif);
957 }
958
959 extern void ffi_call_SYSV(extended_cif *, unsigned, unsigned, unsigned *,
960                           void (*fn)(void));
961 extern void FFI_HIDDEN ffi_call_LINUX64(extended_cif *, unsigned long,
962                                         unsigned long, unsigned long *,
963                                         void (*fn)(void));
964
965 void
966 ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
967 {
968   /*
969    * The final SYSV ABI says that structures smaller or equal 8 bytes
970    * are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
971    * in memory.
972    *
973    * Just to keep things simple for the assembly code, we will always
974    * bounce-buffer struct return values less than or equal to 8 bytes.
975    * This allows the ASM to handle SYSV small structures by directly
976    * writing r3 and r4 to memory without worrying about struct size.
977    */
978   unsigned int smst_buffer[2];
979   extended_cif ecif;
980   unsigned int rsize = 0;
981
982   ecif.cif = cif;
983   ecif.avalue = avalue;
984
985   /* Ensure that we have a valid struct return value */
986   ecif.rvalue = rvalue;
987   if (cif->rtype->type == FFI_TYPE_STRUCT) {
988     rsize = cif->rtype->size;
989     if (rsize <= 8)
990       ecif.rvalue = smst_buffer;
991     else if (!rvalue)
992       ecif.rvalue = alloca(rsize);
993   }
994
995   switch (cif->abi)
996     {
997 #ifndef POWERPC64
998 # ifndef __NO_FPRS__
999     case FFI_SYSV:
1000     case FFI_GCC_SYSV:
1001     case FFI_LINUX:
1002 # endif
1003     case FFI_LINUX_SOFT_FLOAT:
1004       ffi_call_SYSV (&ecif, -cif->bytes, cif->flags, ecif.rvalue, fn);
1005       break;
1006 #else
1007     case FFI_LINUX64:
1008       ffi_call_LINUX64 (&ecif, -(long) cif->bytes, cif->flags, ecif.rvalue, fn);
1009       break;
1010 #endif
1011     default:
1012       FFI_ASSERT (0);
1013       break;
1014     }
1015
1016   /* Check for a bounce-buffered return value */
1017   if (rvalue && ecif.rvalue == smst_buffer)
1018     memcpy(rvalue, smst_buffer, rsize);
1019 }
1020
1021
1022 #ifndef POWERPC64
1023 #define MIN_CACHE_LINE_SIZE 8
1024
1025 static void
1026 flush_icache (char *wraddr, char *xaddr, int size)
1027 {
1028   int i;
1029   for (i = 0; i < size; i += MIN_CACHE_LINE_SIZE)
1030     __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;"
1031                       : : "r" (xaddr + i), "r" (wraddr + i) : "memory");
1032   __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;" "sync;" "isync;"
1033                     : : "r"(xaddr + size - 1), "r"(wraddr + size - 1)
1034                     : "memory");
1035 }
1036 #endif
1037
1038 ffi_status
1039 ffi_prep_closure_loc (ffi_closure *closure,
1040                       ffi_cif *cif,
1041                       void (*fun) (ffi_cif *, void *, void **, void *),
1042                       void *user_data,
1043                       void *codeloc)
1044 {
1045 #ifdef POWERPC64
1046   void **tramp = (void **) &closure->tramp[0];
1047
1048   if (cif->abi != FFI_LINUX64)
1049     return FFI_BAD_ABI;
1050   /* Copy function address and TOC from ffi_closure_LINUX64.  */
1051   memcpy (tramp, (char *) ffi_closure_LINUX64, 16);
1052   tramp[2] = codeloc;
1053 #else
1054   unsigned int *tramp;
1055
1056   if (! (cif->abi == FFI_GCC_SYSV 
1057          || cif->abi == FFI_SYSV
1058          || cif->abi == FFI_LINUX
1059          || cif->abi == FFI_LINUX_SOFT_FLOAT))
1060     return FFI_BAD_ABI;
1061
1062   tramp = (unsigned int *) &closure->tramp[0];
1063   tramp[0] = 0x7c0802a6;  /*   mflr    r0 */
1064   tramp[1] = 0x4800000d;  /*   bl      10 <trampoline_initial+0x10> */
1065   tramp[4] = 0x7d6802a6;  /*   mflr    r11 */
1066   tramp[5] = 0x7c0803a6;  /*   mtlr    r0 */
1067   tramp[6] = 0x800b0000;  /*   lwz     r0,0(r11) */
1068   tramp[7] = 0x816b0004;  /*   lwz     r11,4(r11) */
1069   tramp[8] = 0x7c0903a6;  /*   mtctr   r0 */
1070   tramp[9] = 0x4e800420;  /*   bctr */
1071   *(void **) &tramp[2] = (void *) ffi_closure_SYSV; /* function */
1072   *(void **) &tramp[3] = codeloc;                   /* context */
1073
1074   /* Flush the icache.  */
1075   flush_icache ((char *)tramp, (char *)codeloc, FFI_TRAMPOLINE_SIZE);
1076 #endif
1077
1078   closure->cif = cif;
1079   closure->fun = fun;
1080   closure->user_data = user_data;
1081
1082   return FFI_OK;
1083 }
1084
1085 typedef union
1086 {
1087   float f;
1088   double d;
1089 } ffi_dblfl;
1090
1091 int ffi_closure_helper_SYSV (ffi_closure *, void *, unsigned long *,
1092                              ffi_dblfl *, unsigned long *);
1093
1094 /* Basically the trampoline invokes ffi_closure_SYSV, and on
1095  * entry, r11 holds the address of the closure.
1096  * After storing the registers that could possibly contain
1097  * parameters to be passed into the stack frame and setting
1098  * up space for a return value, ffi_closure_SYSV invokes the
1099  * following helper function to do most of the work
1100  */
1101
1102 int
1103 ffi_closure_helper_SYSV (ffi_closure *closure, void *rvalue,
1104                          unsigned long *pgr, ffi_dblfl *pfr,
1105                          unsigned long *pst)
1106 {
1107   /* rvalue is the pointer to space for return value in closure assembly */
1108   /* pgr is the pointer to where r3-r10 are stored in ffi_closure_SYSV */
1109   /* pfr is the pointer to where f1-f8 are stored in ffi_closure_SYSV  */
1110   /* pst is the pointer to outgoing parameter stack in original caller */
1111
1112   void **          avalue;
1113   ffi_type **      arg_types;
1114   long             i, avn;
1115 #ifndef __NO_FPRS__
1116   long             nf = 0;   /* number of floating registers already used */
1117 #endif
1118   long             ng = 0;   /* number of general registers already used */
1119
1120   ffi_cif *cif = closure->cif;
1121   unsigned       size     = cif->rtype->size;
1122   unsigned short rtypenum = cif->rtype->type;
1123
1124   avalue = alloca (cif->nargs * sizeof (void *));
1125
1126   /* First translate for softfloat/nonlinux */
1127   if (cif->abi == FFI_LINUX_SOFT_FLOAT) {
1128         if (rtypenum == FFI_TYPE_FLOAT)
1129                 rtypenum = FFI_TYPE_UINT32;
1130         if (rtypenum == FFI_TYPE_DOUBLE)
1131                 rtypenum = FFI_TYPE_UINT64;
1132         if (rtypenum == FFI_TYPE_LONGDOUBLE)
1133                 rtypenum = FFI_TYPE_UINT128;
1134   } else if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX64) {
1135 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1136         if (rtypenum == FFI_TYPE_LONGDOUBLE)
1137                 rtypenum = FFI_TYPE_STRUCT;
1138 #endif
1139   }
1140
1141
1142   /* Copy the caller's structure return value address so that the closure
1143      returns the data directly to the caller.
1144      For FFI_SYSV the result is passed in r3/r4 if the struct size is less
1145      or equal 8 bytes.  */
1146   if (rtypenum == FFI_TYPE_STRUCT && ((cif->abi != FFI_SYSV) || (size > 8))) {
1147       rvalue = (void *) *pgr;
1148       ng++;
1149       pgr++;
1150     }
1151
1152   i = 0;
1153   avn = cif->nargs;
1154   arg_types = cif->arg_types;
1155
1156   /* Grab the addresses of the arguments from the stack frame.  */
1157   while (i < avn) {
1158       unsigned short typenum = arg_types[i]->type;
1159
1160       /* We may need to handle some values depending on ABI */
1161       if (cif->abi == FFI_LINUX_SOFT_FLOAT) {
1162                 if (typenum == FFI_TYPE_FLOAT)
1163                         typenum = FFI_TYPE_UINT32;
1164                 if (typenum == FFI_TYPE_DOUBLE)
1165                         typenum = FFI_TYPE_UINT64;
1166                 if (typenum == FFI_TYPE_LONGDOUBLE)
1167                         typenum = FFI_TYPE_UINT128;
1168       } else if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX64) {
1169 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1170                 if (typenum == FFI_TYPE_LONGDOUBLE)
1171                         typenum = FFI_TYPE_STRUCT;
1172 #endif
1173       }
1174
1175       switch (typenum) {
1176 #ifndef __NO_FPRS__
1177         case FFI_TYPE_FLOAT:
1178           /* unfortunately float values are stored as doubles
1179            * in the ffi_closure_SYSV code (since we don't check
1180            * the type in that routine).
1181            */
1182
1183           /* there are 8 64bit floating point registers */
1184
1185           if (nf < 8)
1186             {
1187               double temp = pfr->d;
1188               pfr->f = (float) temp;
1189               avalue[i] = pfr;
1190               nf++;
1191               pfr++;
1192             }
1193           else
1194             {
1195               /* FIXME? here we are really changing the values
1196                * stored in the original calling routines outgoing
1197                * parameter stack.  This is probably a really
1198                * naughty thing to do but...
1199                */
1200               avalue[i] = pst;
1201               pst += 1;
1202             }
1203           break;
1204
1205         case FFI_TYPE_DOUBLE:
1206           /* On the outgoing stack all values are aligned to 8 */
1207           /* there are 8 64bit floating point registers */
1208
1209           if (nf < 8)
1210             {
1211               avalue[i] = pfr;
1212               nf++;
1213               pfr++;
1214             }
1215           else
1216             {
1217               if (((long) pst) & 4)
1218                 pst++;
1219               avalue[i] = pst;
1220               pst += 2;
1221             }
1222           break;
1223
1224 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1225         case FFI_TYPE_LONGDOUBLE:
1226           if (nf < 7)
1227             {
1228               avalue[i] = pfr;
1229               pfr += 2;
1230               nf += 2;
1231             }
1232           else
1233             {
1234               if (((long) pst) & 4)
1235                 pst++;
1236               avalue[i] = pst;
1237               pst += 4;
1238               nf = 8;
1239             }
1240           break;
1241 #endif
1242 #endif /* have FPRS */
1243
1244         case FFI_TYPE_UINT128:
1245                 /*
1246                  * Test if for the whole long double, 4 gprs are available.
1247                  * otherwise the stuff ends up on the stack.
1248                  */
1249                 if (ng < 5) {
1250                         avalue[i] = pgr;
1251                         pgr += 4;
1252                         ng += 4;
1253                 } else {
1254                         avalue[i] = pst;
1255                         pst += 4;
1256                         ng = 8+4;
1257                 }
1258                 break;
1259
1260         case FFI_TYPE_SINT8:
1261         case FFI_TYPE_UINT8:
1262 #ifndef __LITTLE_ENDIAN__
1263           /* there are 8 gpr registers used to pass values */
1264           if (ng < 8)
1265             {
1266               avalue[i] = (char *) pgr + 3;
1267               ng++;
1268               pgr++;
1269             }
1270           else
1271             {
1272               avalue[i] = (char *) pst + 3;
1273               pst++;
1274             }
1275           break;
1276 #endif
1277
1278         case FFI_TYPE_SINT16:
1279         case FFI_TYPE_UINT16:
1280 #ifndef __LITTLE_ENDIAN__
1281           /* there are 8 gpr registers used to pass values */
1282           if (ng < 8)
1283             {
1284               avalue[i] = (char *) pgr + 2;
1285               ng++;
1286               pgr++;
1287             }
1288           else
1289             {
1290               avalue[i] = (char *) pst + 2;
1291               pst++;
1292             }
1293           break;
1294 #endif
1295
1296         case FFI_TYPE_SINT32:
1297         case FFI_TYPE_UINT32:
1298         case FFI_TYPE_POINTER:
1299           /* there are 8 gpr registers used to pass values */
1300           if (ng < 8)
1301             {
1302               avalue[i] = pgr;
1303               ng++;
1304               pgr++;
1305             }
1306           else
1307             {
1308               avalue[i] = pst;
1309               pst++;
1310             }
1311           break;
1312
1313         case FFI_TYPE_STRUCT:
1314           /* Structs are passed by reference. The address will appear in a
1315              gpr if it is one of the first 8 arguments.  */
1316           if (ng < 8)
1317             {
1318               avalue[i] = (void *) *pgr;
1319               ng++;
1320               pgr++;
1321             }
1322           else
1323             {
1324               avalue[i] = (void *) *pst;
1325               pst++;
1326             }
1327           break;
1328
1329         case FFI_TYPE_SINT64:
1330         case FFI_TYPE_UINT64:
1331           /* passing long long ints are complex, they must
1332            * be passed in suitable register pairs such as
1333            * (r3,r4) or (r5,r6) or (r6,r7), or (r7,r8) or (r9,r10)
1334            * and if the entire pair aren't available then the outgoing
1335            * parameter stack is used for both but an alignment of 8
1336            * must will be kept.  So we must either look in pgr
1337            * or pst to find the correct address for this type
1338            * of parameter.
1339            */
1340           if (ng < 7)
1341             {
1342               if (ng & 0x01)
1343                 {
1344                   /* skip r4, r6, r8 as starting points */
1345                   ng++;
1346                   pgr++;
1347                 }
1348               avalue[i] = pgr;
1349               ng += 2;
1350               pgr += 2;
1351             }
1352           else
1353             {
1354               if (((long) pst) & 4)
1355                 pst++;
1356               avalue[i] = pst;
1357               pst += 2;
1358               ng = 8;
1359             }
1360           break;
1361
1362         default:
1363                 FFI_ASSERT (0);
1364         }
1365
1366       i++;
1367     }
1368
1369
1370   (closure->fun) (cif, rvalue, avalue, closure->user_data);
1371
1372   /* Tell ffi_closure_SYSV how to perform return type promotions.
1373      Because the FFI_SYSV ABI returns the structures <= 8 bytes in r3/r4
1374      we have to tell ffi_closure_SYSV how to treat them. We combine the base
1375      type FFI_SYSV_TYPE_SMALL_STRUCT - 1  with the size of the struct.
1376      So a one byte struct gets the return type 16. Return type 1 to 15 are
1377      already used and we never have a struct with size zero. That is the reason
1378      for the subtraction of 1. See the comment in ffitarget.h about ordering.
1379   */
1380   if (cif->abi == FFI_SYSV && rtypenum == FFI_TYPE_STRUCT && size <= 8)
1381     return (FFI_SYSV_TYPE_SMALL_STRUCT - 1) + size;
1382   return rtypenum;
1383 }
1384
1385 int FFI_HIDDEN ffi_closure_helper_LINUX64 (ffi_closure *, void *,
1386                                            unsigned long *, ffi_dblfl *);
1387
1388 int FFI_HIDDEN
1389 ffi_closure_helper_LINUX64 (ffi_closure *closure, void *rvalue,
1390                             unsigned long *pst, ffi_dblfl *pfr)
1391 {
1392   /* rvalue is the pointer to space for return value in closure assembly */
1393   /* pst is the pointer to parameter save area
1394      (r3-r10 are stored into its first 8 slots by ffi_closure_LINUX64) */
1395   /* pfr is the pointer to where f1-f13 are stored in ffi_closure_LINUX64 */
1396
1397   void **avalue;
1398   ffi_type **arg_types;
1399   unsigned long i, avn, nfixedargs;
1400   ffi_cif *cif;
1401   ffi_dblfl *end_pfr = pfr + NUM_FPR_ARG_REGISTERS64;
1402
1403   cif = closure->cif;
1404   avalue = alloca (cif->nargs * sizeof (void *));
1405
1406   /* Copy the caller's structure return value address so that the closure
1407      returns the data directly to the caller.  */
1408   if (cif->rtype->type == FFI_TYPE_STRUCT)
1409     {
1410       rvalue = (void *) *pst;
1411       pst++;
1412     }
1413
1414   i = 0;
1415   avn = cif->nargs;
1416   nfixedargs = cif->nfixedargs;
1417   arg_types = cif->arg_types;
1418
1419   /* Grab the addresses of the arguments from the stack frame.  */
1420   while (i < avn)
1421     {
1422       switch (arg_types[i]->type)
1423         {
1424         case FFI_TYPE_SINT8:
1425         case FFI_TYPE_UINT8:
1426 #ifndef __LITTLE_ENDIAN__
1427           avalue[i] = (char *) pst + 7;
1428           pst++;
1429           break;
1430 #endif
1431
1432         case FFI_TYPE_SINT16:
1433         case FFI_TYPE_UINT16:
1434 #ifndef __LITTLE_ENDIAN__
1435           avalue[i] = (char *) pst + 6;
1436           pst++;
1437           break;
1438 #endif
1439
1440         case FFI_TYPE_SINT32:
1441         case FFI_TYPE_UINT32:
1442 #ifndef __LITTLE_ENDIAN__
1443           avalue[i] = (char *) pst + 4;
1444           pst++;
1445           break;
1446 #endif
1447
1448         case FFI_TYPE_SINT64:
1449         case FFI_TYPE_UINT64:
1450         case FFI_TYPE_POINTER:
1451           avalue[i] = pst;
1452           pst++;
1453           break;
1454
1455         case FFI_TYPE_STRUCT:
1456 #ifndef __LITTLE_ENDIAN__
1457           /* Structures with size less than eight bytes are passed
1458              left-padded.  */
1459           if (arg_types[i]->size < 8)
1460             avalue[i] = (char *) pst + 8 - arg_types[i]->size;
1461           else
1462 #endif
1463             avalue[i] = pst;
1464           pst += (arg_types[i]->size + 7) / 8;
1465           break;
1466
1467         case FFI_TYPE_FLOAT:
1468           /* unfortunately float values are stored as doubles
1469            * in the ffi_closure_LINUX64 code (since we don't check
1470            * the type in that routine).
1471            */
1472
1473           /* there are 13 64bit floating point registers */
1474
1475           if (pfr < end_pfr && i < nfixedargs)
1476             {
1477               double temp = pfr->d;
1478               pfr->f = (float) temp;
1479               avalue[i] = pfr;
1480               pfr++;
1481             }
1482           else
1483             avalue[i] = pst;
1484           pst++;
1485           break;
1486
1487         case FFI_TYPE_DOUBLE:
1488           /* On the outgoing stack all values are aligned to 8 */
1489           /* there are 13 64bit floating point registers */
1490
1491           if (pfr < end_pfr && i < nfixedargs)
1492             {
1493               avalue[i] = pfr;
1494               pfr++;
1495             }
1496           else
1497             avalue[i] = pst;
1498           pst++;
1499           break;
1500
1501 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1502         case FFI_TYPE_LONGDOUBLE:
1503           if (pfr + 1 < end_pfr && i + 1 < nfixedargs)
1504             {
1505               avalue[i] = pfr;
1506               pfr += 2;
1507             }
1508           else
1509             {
1510               if (pfr < end_pfr && i < nfixedargs)
1511                 {
1512                   /* Passed partly in f13 and partly on the stack.
1513                      Move it all to the stack.  */
1514                   *pst = *(unsigned long *) pfr;
1515                   pfr++;
1516                 }
1517               avalue[i] = pst;
1518             }
1519           pst += 2;
1520           break;
1521 #endif
1522
1523         default:
1524           FFI_ASSERT (0);
1525         }
1526
1527       i++;
1528     }
1529
1530
1531   (closure->fun) (cif, rvalue, avalue, closure->user_data);
1532
1533   /* Tell ffi_closure_LINUX64 how to perform return type promotions.  */
1534   return cif->rtype->type;
1535 }