OSDN Git Service

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