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