blob: dd897f4c631c4f3f0d7f6490d1ad5a096eeebf23 [file] [log] [blame]
Thomas Hellerd4c93202006-03-08 19:35:11 +00001/* -----------------------------------------------------------------------
Christian Heimes78644762008-03-04 23:39:23 +00002 ffi_darwin.c
Thomas Hellerd4c93202006-03-08 19:35:11 +00003
Christian Heimes78644762008-03-04 23:39:23 +00004 Copyright (C) 1998 Geoffrey Keating
5 Copyright (C) 2001 John Hornkvist
doko@ubuntu.com2a918762012-06-26 17:56:44 +02006 Copyright (C) 2002, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
Thomas Hellerd4c93202006-03-08 19:35:11 +00007
Christian Heimes78644762008-03-04 23:39:23 +00008 FFI support for Darwin and AIX.
9
Thomas Hellerd4c93202006-03-08 19:35:11 +000010 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
Matthias Klosea8349752010-03-15 13:25:28 +000035extern void ffi_closure_ASM (void);
Thomas Hellerd4c93202006-03-08 19:35:11 +000036
37enum {
doko@ubuntu.com2a918762012-06-26 17:56:44 +020038 /* The assembly depends on these exact flags.
39 For Darwin64 (when FLAG_RETURNS_STRUCT is set):
40 FLAG_RETURNS_FP indicates that the structure embeds FP data.
41 FLAG_RETURNS_128BITS signals a special struct size that is not
42 expanded for float content. */
43 FLAG_RETURNS_128BITS = 1 << (31-31), /* These go in cr7 */
44 FLAG_RETURNS_NOTHING = 1 << (31-30),
45 FLAG_RETURNS_FP = 1 << (31-29),
46 FLAG_RETURNS_64BITS = 1 << (31-28),
47
48 FLAG_RETURNS_STRUCT = 1 << (31-27), /* This goes in cr6 */
Thomas Hellerd4c93202006-03-08 19:35:11 +000049
50 FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
51 FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
52 FLAG_4_GPR_ARGUMENTS = 1 << (31- 5),
53 FLAG_RETVAL_REFERENCE = 1 << (31- 4)
54};
55
56/* About the DARWIN ABI. */
57enum {
58 NUM_GPR_ARG_REGISTERS = 8,
doko@ubuntu.com2a918762012-06-26 17:56:44 +020059 NUM_FPR_ARG_REGISTERS = 13,
60 LINKAGE_AREA_GPRS = 6
Thomas Hellerd4c93202006-03-08 19:35:11 +000061};
doko@ubuntu.com2a918762012-06-26 17:56:44 +020062
63enum { ASM_NEEDS_REGISTERS = 4 }; /* r28-r31 */
Thomas Hellerd4c93202006-03-08 19:35:11 +000064
65/* ffi_prep_args is called by the assembly routine once stack space
66 has been allocated for the function's arguments.
doko@ubuntu.com2a918762012-06-26 17:56:44 +020067
68 m32/m64
Thomas Hellerd4c93202006-03-08 19:35:11 +000069
70 The stack layout we want looks like this:
71
72 | Return address from ffi_call_DARWIN | higher addresses
73 |--------------------------------------------|
doko@ubuntu.com2a918762012-06-26 17:56:44 +020074 | Previous backchain pointer 4/8 | stack pointer here
Thomas Hellerd4c93202006-03-08 19:35:11 +000075 |--------------------------------------------|<+ <<< on entry to
doko@ubuntu.com2a918762012-06-26 17:56:44 +020076 | ASM_NEEDS_REGISTERS=r28-r31 4*(4/8) | | ffi_call_DARWIN
Thomas Hellerd4c93202006-03-08 19:35:11 +000077 |--------------------------------------------| |
doko@ubuntu.com2a918762012-06-26 17:56:44 +020078 | When we have any FP activity... the | |
79 | FPRs occupy NUM_FPR_ARG_REGISTERS slots | |
80 | here fp13 .. fp1 from high to low addr. | |
81 ~ ~ ~
82 | Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS
Thomas Hellerd4c93202006-03-08 19:35:11 +000083 |--------------------------------------------| |
doko@ubuntu.com2a918762012-06-26 17:56:44 +020084 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
Thomas Hellerd4c93202006-03-08 19:35:11 +000085 |--------------------------------------------| | stack |
doko@ubuntu.com2a918762012-06-26 17:56:44 +020086 | Reserved 2*4/8 | | grows |
Thomas Hellerd4c93202006-03-08 19:35:11 +000087 |--------------------------------------------| | down V
doko@ubuntu.com2a918762012-06-26 17:56:44 +020088 | Space for callee's LR 4/8 | |
Thomas Hellerd4c93202006-03-08 19:35:11 +000089 |--------------------------------------------| | lower addresses
doko@ubuntu.com2a918762012-06-26 17:56:44 +020090 | Saved CR [low word for m64] 4/8 | |
Thomas Hellerd4c93202006-03-08 19:35:11 +000091 |--------------------------------------------| | stack pointer here
doko@ubuntu.com2a918762012-06-26 17:56:44 +020092 | Current backchain pointer 4/8 |-/ during
Thomas Hellerd4c93202006-03-08 19:35:11 +000093 |--------------------------------------------| <<< ffi_call_DARWIN
94
95 */
96
doko@ubuntu.com2a918762012-06-26 17:56:44 +020097#if defined(POWERPC_DARWIN64)
98static void
99darwin64_pass_struct_by_value
100 (ffi_type *, char *, unsigned, unsigned *, double **, unsigned long **);
101#endif
102
103/* This depends on GPR_SIZE = sizeof (unsigned long) */
104
Matthias Klosea8349752010-03-15 13:25:28 +0000105void
106ffi_prep_args (extended_cif *ecif, unsigned long *const stack)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000107{
108 const unsigned bytes = ecif->cif->bytes;
109 const unsigned flags = ecif->cif->flags;
Matthias Klosea8349752010-03-15 13:25:28 +0000110 const unsigned nargs = ecif->cif->nargs;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200111#if !defined(POWERPC_DARWIN64)
Matthias Klosea8349752010-03-15 13:25:28 +0000112 const ffi_abi abi = ecif->cif->abi;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200113#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000114
115 /* 'stacktop' points at the previous backchain pointer. */
Matthias Klosea8349752010-03-15 13:25:28 +0000116 unsigned long *const stacktop = stack + (bytes / sizeof(unsigned long));
Thomas Hellerd4c93202006-03-08 19:35:11 +0000117
118 /* 'fpr_base' points at the space for fpr1, and grows upwards as
119 we use FPR registers. */
Matthias Klosea8349752010-03-15 13:25:28 +0000120 double *fpr_base = (double *) (stacktop - ASM_NEEDS_REGISTERS) - NUM_FPR_ARG_REGISTERS;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200121 int gp_count = 0, fparg_count = 0;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000122
123 /* 'next_arg' grows up as we put parameters in it. */
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200124 unsigned long *next_arg = stack + LINKAGE_AREA_GPRS; /* 6 reserved positions. */
Thomas Hellerd4c93202006-03-08 19:35:11 +0000125
Matthias Klosea8349752010-03-15 13:25:28 +0000126 int i;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000127 double double_tmp;
128 void **p_argv = ecif->avalue;
Matthias Klosea8349752010-03-15 13:25:28 +0000129 unsigned long gprvalue;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000130 ffi_type** ptr = ecif->cif->arg_types;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200131#if !defined(POWERPC_DARWIN64)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000132 char *dest_cpy;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200133#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000134 unsigned size_al = 0;
135
136 /* Check that everything starts aligned properly. */
Matthias Klosea8349752010-03-15 13:25:28 +0000137 FFI_ASSERT(((unsigned) (char *) stack & 0xF) == 0);
138 FFI_ASSERT(((unsigned) (char *) stacktop & 0xF) == 0);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000139 FFI_ASSERT((bytes & 0xF) == 0);
140
141 /* Deal with return values that are actually pass-by-reference.
142 Rule:
143 Return values are referenced by r3, so r4 is the first parameter. */
144
145 if (flags & FLAG_RETVAL_REFERENCE)
Matthias Klosea8349752010-03-15 13:25:28 +0000146 *next_arg++ = (unsigned long) (char *) ecif->rvalue;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000147
148 /* Now for the arguments. */
Matthias Klosea8349752010-03-15 13:25:28 +0000149 for (i = nargs; i > 0; i--, ptr++, p_argv++)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000150 {
151 switch ((*ptr)->type)
152 {
153 /* If a floating-point parameter appears before all of the general-
154 purpose registers are filled, the corresponding GPRs that match
155 the size of the floating-point parameter are skipped. */
156 case FFI_TYPE_FLOAT:
Matthias Klosea8349752010-03-15 13:25:28 +0000157 double_tmp = *(float *) *p_argv;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200158 if (fparg_count < NUM_FPR_ARG_REGISTERS)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000159 *fpr_base++ = double_tmp;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200160#if defined(POWERPC_DARWIN)
161 *(float *)next_arg = *(float *) *p_argv;
162#else
163 *(double *)next_arg = double_tmp;
164#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000165 next_arg++;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200166 gp_count++;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000167 fparg_count++;
168 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
169 break;
170
171 case FFI_TYPE_DOUBLE:
Matthias Klosea8349752010-03-15 13:25:28 +0000172 double_tmp = *(double *) *p_argv;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200173 if (fparg_count < NUM_FPR_ARG_REGISTERS)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000174 *fpr_base++ = double_tmp;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200175 *(double *)next_arg = double_tmp;
Matthias Klosea8349752010-03-15 13:25:28 +0000176#ifdef POWERPC64
177 next_arg++;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200178 gp_count++;
Matthias Klosea8349752010-03-15 13:25:28 +0000179#else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000180 next_arg += 2;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200181 gp_count += 2;
Matthias Klosea8349752010-03-15 13:25:28 +0000182#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000183 fparg_count++;
184 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
185 break;
186
187#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
188
189 case FFI_TYPE_LONGDOUBLE:
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200190# if defined(POWERPC64) && !defined(POWERPC_DARWIN64)
191 /* ??? This will exceed the regs count when the value starts at fp13
192 and it will not put the extra bit on the stack. */
Matthias Klosea8349752010-03-15 13:25:28 +0000193 if (fparg_count < NUM_FPR_ARG_REGISTERS)
194 *(long double *) fpr_base++ = *(long double *) *p_argv;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000195 else
Matthias Klosea8349752010-03-15 13:25:28 +0000196 *(long double *) next_arg = *(long double *) *p_argv;
197 next_arg += 2;
198 fparg_count += 2;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200199# else
Matthias Klosea8349752010-03-15 13:25:28 +0000200 double_tmp = ((double *) *p_argv)[0];
201 if (fparg_count < NUM_FPR_ARG_REGISTERS)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000202 *fpr_base++ = double_tmp;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200203 *(double *) next_arg = double_tmp;
204# if defined(POWERPC_DARWIN64)
205 next_arg++;
206 gp_count++;
207# else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000208 next_arg += 2;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200209 gp_count += 2;
210# endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000211 fparg_count++;
Matthias Klosea8349752010-03-15 13:25:28 +0000212 double_tmp = ((double *) *p_argv)[1];
213 if (fparg_count < NUM_FPR_ARG_REGISTERS)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000214 *fpr_base++ = double_tmp;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200215 *(double *) next_arg = double_tmp;
216# if defined(POWERPC_DARWIN64)
217 next_arg++;
218 gp_count++;
219# else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000220 next_arg += 2;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200221 gp_count += 2;
222# endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000223 fparg_count++;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200224# endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000225 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
226 break;
227#endif
228 case FFI_TYPE_UINT64:
229 case FFI_TYPE_SINT64:
Matthias Klosea8349752010-03-15 13:25:28 +0000230#ifdef POWERPC64
231 gprvalue = *(long long *) *p_argv;
232 goto putgpr;
233#else
234 *(long long *) next_arg = *(long long *) *p_argv;
235 next_arg += 2;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200236 gp_count += 2;
Matthias Klosea8349752010-03-15 13:25:28 +0000237#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000238 break;
Matthias Klosea8349752010-03-15 13:25:28 +0000239 case FFI_TYPE_POINTER:
240 gprvalue = *(unsigned long *) *p_argv;
241 goto putgpr;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000242 case FFI_TYPE_UINT8:
Matthias Klosea8349752010-03-15 13:25:28 +0000243 gprvalue = *(unsigned char *) *p_argv;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000244 goto putgpr;
245 case FFI_TYPE_SINT8:
Matthias Klosea8349752010-03-15 13:25:28 +0000246 gprvalue = *(signed char *) *p_argv;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000247 goto putgpr;
248 case FFI_TYPE_UINT16:
Matthias Klosea8349752010-03-15 13:25:28 +0000249 gprvalue = *(unsigned short *) *p_argv;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000250 goto putgpr;
251 case FFI_TYPE_SINT16:
Matthias Klosea8349752010-03-15 13:25:28 +0000252 gprvalue = *(signed short *) *p_argv;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000253 goto putgpr;
254
255 case FFI_TYPE_STRUCT:
Matthias Klosea8349752010-03-15 13:25:28 +0000256 size_al = (*ptr)->size;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200257#if defined(POWERPC_DARWIN64)
258 next_arg = (unsigned long *)ALIGN((char *)next_arg, (*ptr)->alignment);
259 darwin64_pass_struct_by_value (*ptr, (char *) *p_argv,
260 (unsigned) size_al,
261 (unsigned int *) &fparg_count,
262 &fpr_base, &next_arg);
Matthias Klosea8349752010-03-15 13:25:28 +0000263#else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000264 dest_cpy = (char *) next_arg;
265
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200266 /* If the first member of the struct is a double, then include enough
267 padding in the struct size to align it to double-word. */
268 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
269 size_al = ALIGN((*ptr)->size, 8);
270
271# if defined(POWERPC64)
272 FFI_ASSERT (abi != FFI_DARWIN);
273 memcpy ((char *) dest_cpy, (char *) *p_argv, size_al);
274 next_arg += (size_al + 7) / 8;
275# else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000276 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
277 SI 4 bytes) are aligned as if they were those modes.
278 Structures with 3 byte in size are padded upwards. */
Matthias Klosea8349752010-03-15 13:25:28 +0000279 if (size_al < 3 && abi == FFI_DARWIN)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000280 dest_cpy += 4 - size_al;
281
Matthias Klosea8349752010-03-15 13:25:28 +0000282 memcpy((char *) dest_cpy, (char *) *p_argv, size_al);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000283 next_arg += (size_al + 3) / 4;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200284# endif
Matthias Klosea8349752010-03-15 13:25:28 +0000285#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000286 break;
287
288 case FFI_TYPE_INT:
Thomas Hellerd4c93202006-03-08 19:35:11 +0000289 case FFI_TYPE_SINT32:
Matthias Klosea8349752010-03-15 13:25:28 +0000290 gprvalue = *(signed int *) *p_argv;
291 goto putgpr;
292
293 case FFI_TYPE_UINT32:
294 gprvalue = *(unsigned int *) *p_argv;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000295 putgpr:
296 *next_arg++ = gprvalue;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200297 gp_count++;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000298 break;
299 default:
300 break;
301 }
302 }
303
304 /* Check that we didn't overrun the stack... */
305 //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
306 //FFI_ASSERT((unsigned *)fpr_base
307 // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
308 //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
309}
310
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200311#if defined(POWERPC_DARWIN64)
312
313/* See if we can put some of the struct into fprs.
314 This should not be called for structures of size 16 bytes, since these are not
315 broken out this way. */
316static void
317darwin64_scan_struct_for_floats (ffi_type *s, unsigned *nfpr)
318{
319 int i;
320
321 FFI_ASSERT (s->type == FFI_TYPE_STRUCT)
322
323 for (i = 0; s->elements[i] != NULL; i++)
324 {
325 ffi_type *p = s->elements[i];
326 switch (p->type)
327 {
328 case FFI_TYPE_STRUCT:
329 darwin64_scan_struct_for_floats (p, nfpr);
330 break;
331 case FFI_TYPE_LONGDOUBLE:
332 (*nfpr) += 2;
333 break;
334 case FFI_TYPE_DOUBLE:
335 case FFI_TYPE_FLOAT:
336 (*nfpr) += 1;
337 break;
338 default:
339 break;
340 }
341 }
342}
343
344static int
345darwin64_struct_size_exceeds_gprs_p (ffi_type *s, char *src, unsigned *nfpr)
346{
347 unsigned struct_offset=0, i;
348
349 for (i = 0; s->elements[i] != NULL; i++)
350 {
351 char *item_base;
352 ffi_type *p = s->elements[i];
353 /* Find the start of this item (0 for the first one). */
354 if (i > 0)
355 struct_offset = ALIGN(struct_offset, p->alignment);
356
357 item_base = src + struct_offset;
358
359 switch (p->type)
360 {
361 case FFI_TYPE_STRUCT:
362 if (darwin64_struct_size_exceeds_gprs_p (p, item_base, nfpr))
363 return 1;
364 break;
365 case FFI_TYPE_LONGDOUBLE:
366 if (*nfpr >= NUM_FPR_ARG_REGISTERS)
367 return 1;
368 (*nfpr) += 1;
369 item_base += 8;
370 /* FALL THROUGH */
371 case FFI_TYPE_DOUBLE:
372 if (*nfpr >= NUM_FPR_ARG_REGISTERS)
373 return 1;
374 (*nfpr) += 1;
375 break;
376 case FFI_TYPE_FLOAT:
377 if (*nfpr >= NUM_FPR_ARG_REGISTERS)
378 return 1;
379 (*nfpr) += 1;
380 break;
381 default:
382 /* If we try and place any item, that is non-float, once we've
383 exceeded the 8 GPR mark, then we can't fit the struct. */
384 if ((unsigned long)item_base >= 8*8)
385 return 1;
386 break;
387 }
388 /* now count the size of what we just used. */
389 struct_offset += p->size;
390 }
391 return 0;
392}
393
394/* Can this struct be returned by value? */
395int
396darwin64_struct_ret_by_value_p (ffi_type *s)
397{
398 unsigned nfp = 0;
399
400 FFI_ASSERT (s && s->type == FFI_TYPE_STRUCT);
401
402 /* The largest structure we can return is 8long + 13 doubles. */
403 if (s->size > 168)
404 return 0;
405
406 /* We can't pass more than 13 floats. */
407 darwin64_scan_struct_for_floats (s, &nfp);
408 if (nfp > 13)
409 return 0;
410
411 /* If there are not too many floats, and the struct is
412 small enough to accommodate in the GPRs, then it must be OK. */
413 if (s->size <= 64)
414 return 1;
415
416 /* Well, we have to look harder. */
417 nfp = 0;
418 if (darwin64_struct_size_exceeds_gprs_p (s, NULL, &nfp))
419 return 0;
420
421 return 1;
422}
423
424void
425darwin64_pass_struct_floats (ffi_type *s, char *src,
426 unsigned *nfpr, double **fprs)
427{
428 int i;
429 double *fpr_base = *fprs;
430 unsigned struct_offset = 0;
431
432 /* We don't assume anything about the alignment of the source. */
433 for (i = 0; s->elements[i] != NULL; i++)
434 {
435 char *item_base;
436 ffi_type *p = s->elements[i];
437 /* Find the start of this item (0 for the first one). */
438 if (i > 0)
439 struct_offset = ALIGN(struct_offset, p->alignment);
440 item_base = src + struct_offset;
441
442 switch (p->type)
443 {
444 case FFI_TYPE_STRUCT:
445 darwin64_pass_struct_floats (p, item_base, nfpr,
446 &fpr_base);
447 break;
448 case FFI_TYPE_LONGDOUBLE:
449 if (*nfpr < NUM_FPR_ARG_REGISTERS)
450 *fpr_base++ = *(double *)item_base;
451 (*nfpr) += 1;
452 item_base += 8;
453 /* FALL THROUGH */
454 case FFI_TYPE_DOUBLE:
455 if (*nfpr < NUM_FPR_ARG_REGISTERS)
456 *fpr_base++ = *(double *)item_base;
457 (*nfpr) += 1;
458 break;
459 case FFI_TYPE_FLOAT:
460 if (*nfpr < NUM_FPR_ARG_REGISTERS)
461 *fpr_base++ = (double) *(float *)item_base;
462 (*nfpr) += 1;
463 break;
464 default:
465 break;
466 }
467 /* now count the size of what we just used. */
468 struct_offset += p->size;
469 }
470 /* Update the scores. */
471 *fprs = fpr_base;
472}
473
474/* Darwin64 special rules.
475 Break out a struct into params and float registers. */
476static void
477darwin64_pass_struct_by_value (ffi_type *s, char *src, unsigned size,
478 unsigned *nfpr, double **fprs, unsigned long **arg)
479{
480 unsigned long *next_arg = *arg;
481 char *dest_cpy = (char *)next_arg;
482
483 FFI_ASSERT (s->type == FFI_TYPE_STRUCT)
484
485 if (!size)
486 return;
487
488 /* First... special cases. */
489 if (size < 3
490 || (size == 4
491 && s->elements[0]
492 && s->elements[0]->type != FFI_TYPE_FLOAT))
493 {
494 /* Must be at least one GPR, padding is unspecified in value,
495 let's make it zero. */
496 *next_arg = 0UL;
497 dest_cpy += 8 - size;
498 memcpy ((char *) dest_cpy, src, size);
499 next_arg++;
500 }
501 else if (size == 16)
502 {
503 memcpy ((char *) dest_cpy, src, size);
504 next_arg += 2;
505 }
506 else
507 {
508 /* now the general case, we consider embedded floats. */
509 memcpy ((char *) dest_cpy, src, size);
510 darwin64_pass_struct_floats (s, src, nfpr, fprs);
511 next_arg += (size+7)/8;
512 }
513
514 *arg = next_arg;
515}
516
517double *
518darwin64_struct_floats_to_mem (ffi_type *s, char *dest, double *fprs, unsigned *nf)
519{
520 int i;
521 unsigned struct_offset = 0;
522
523 /* We don't assume anything about the alignment of the source. */
524 for (i = 0; s->elements[i] != NULL; i++)
525 {
526 char *item_base;
527 ffi_type *p = s->elements[i];
528 /* Find the start of this item (0 for the first one). */
529 if (i > 0)
530 struct_offset = ALIGN(struct_offset, p->alignment);
531 item_base = dest + struct_offset;
532
533 switch (p->type)
534 {
535 case FFI_TYPE_STRUCT:
536 fprs = darwin64_struct_floats_to_mem (p, item_base, fprs, nf);
537 break;
538 case FFI_TYPE_LONGDOUBLE:
539 if (*nf < NUM_FPR_ARG_REGISTERS)
540 {
541 *(double *)item_base = *fprs++ ;
542 (*nf) += 1;
543 }
544 item_base += 8;
545 /* FALL THROUGH */
546 case FFI_TYPE_DOUBLE:
547 if (*nf < NUM_FPR_ARG_REGISTERS)
548 {
549 *(double *)item_base = *fprs++ ;
550 (*nf) += 1;
551 }
552 break;
553 case FFI_TYPE_FLOAT:
554 if (*nf < NUM_FPR_ARG_REGISTERS)
555 {
556 *(float *)item_base = (float) *fprs++ ;
557 (*nf) += 1;
558 }
559 break;
560 default:
561 break;
562 }
563 /* now count the size of what we just used. */
564 struct_offset += p->size;
565 }
566 return fprs;
567}
568
569#endif
570
Christian Heimes78644762008-03-04 23:39:23 +0000571/* Adjust the size of S to be correct for Darwin.
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200572 On Darwin m32, the first field of a structure has natural alignment.
573 On Darwin m64, all fields have natural alignment. */
Christian Heimes78644762008-03-04 23:39:23 +0000574
575static void
576darwin_adjust_aggregate_sizes (ffi_type *s)
577{
578 int i;
579
580 if (s->type != FFI_TYPE_STRUCT)
581 return;
582
583 s->size = 0;
584 for (i = 0; s->elements[i] != NULL; i++)
585 {
586 ffi_type *p;
587 int align;
588
589 p = s->elements[i];
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200590 if (p->type == FFI_TYPE_STRUCT)
591 darwin_adjust_aggregate_sizes (p);
592#if defined(POWERPC_DARWIN64)
593 /* Natural alignment for all items. */
594 align = p->alignment;
595#else
596 /* Natrual alignment for the first item... */
597 if (i == 0)
598 align = p->alignment;
Christian Heimes78644762008-03-04 23:39:23 +0000599 else if (p->alignment == 16 || p->alignment < 4)
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200600 /* .. subsequent items with vector or align < 4 have natural align. */
Christian Heimes78644762008-03-04 23:39:23 +0000601 align = p->alignment;
602 else
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200603 /* .. or align is 4. */
Christian Heimes78644762008-03-04 23:39:23 +0000604 align = 4;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200605#endif
606 /* Pad, if necessary, before adding the current item. */
Christian Heimes78644762008-03-04 23:39:23 +0000607 s->size = ALIGN(s->size, align) + p->size;
608 }
609
610 s->size = ALIGN(s->size, s->alignment);
611
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200612 /* This should not be necessary on m64, but harmless. */
Christian Heimes78644762008-03-04 23:39:23 +0000613 if (s->elements[0]->type == FFI_TYPE_UINT64
614 || s->elements[0]->type == FFI_TYPE_SINT64
615 || s->elements[0]->type == FFI_TYPE_DOUBLE
616 || s->elements[0]->alignment == 8)
617 s->alignment = s->alignment > 8 ? s->alignment : 8;
618 /* Do not add additional tail padding. */
619}
620
Matthias Klosea8349752010-03-15 13:25:28 +0000621/* Adjust the size of S to be correct for AIX.
622 Word-align double unless it is the first member of a structure. */
623
624static void
625aix_adjust_aggregate_sizes (ffi_type *s)
626{
627 int i;
628
629 if (s->type != FFI_TYPE_STRUCT)
630 return;
631
632 s->size = 0;
633 for (i = 0; s->elements[i] != NULL; i++)
634 {
635 ffi_type *p;
636 int align;
637
638 p = s->elements[i];
639 aix_adjust_aggregate_sizes (p);
640 align = p->alignment;
641 if (i != 0 && p->type == FFI_TYPE_DOUBLE)
642 align = 4;
643 s->size = ALIGN(s->size, align) + p->size;
644 }
645
646 s->size = ALIGN(s->size, s->alignment);
647
648 if (s->elements[0]->type == FFI_TYPE_UINT64
649 || s->elements[0]->type == FFI_TYPE_SINT64
650 || s->elements[0]->type == FFI_TYPE_DOUBLE
651 || s->elements[0]->alignment == 8)
652 s->alignment = s->alignment > 8 ? s->alignment : 8;
653 /* Do not add additional tail padding. */
654}
655
Thomas Hellerd4c93202006-03-08 19:35:11 +0000656/* Perform machine dependent cif processing. */
Matthias Klosea8349752010-03-15 13:25:28 +0000657ffi_status
658ffi_prep_cif_machdep (ffi_cif *cif)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000659{
660 /* All this is for the DARWIN ABI. */
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200661 unsigned i;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000662 ffi_type **ptr;
663 unsigned bytes;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200664 unsigned fparg_count = 0, intarg_count = 0;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000665 unsigned flags = 0;
666 unsigned size_al = 0;
667
668 /* All the machine-independent calculation of cif->bytes will be wrong.
Christian Heimes78644762008-03-04 23:39:23 +0000669 All the calculation of structure sizes will also be wrong.
Thomas Hellerd4c93202006-03-08 19:35:11 +0000670 Redo the calculation for DARWIN. */
671
Christian Heimes78644762008-03-04 23:39:23 +0000672 if (cif->abi == FFI_DARWIN)
673 {
674 darwin_adjust_aggregate_sizes (cif->rtype);
675 for (i = 0; i < cif->nargs; i++)
676 darwin_adjust_aggregate_sizes (cif->arg_types[i]);
677 }
678
Matthias Klosea8349752010-03-15 13:25:28 +0000679 if (cif->abi == FFI_AIX)
680 {
681 aix_adjust_aggregate_sizes (cif->rtype);
682 for (i = 0; i < cif->nargs; i++)
683 aix_adjust_aggregate_sizes (cif->arg_types[i]);
684 }
685
Thomas Hellerd4c93202006-03-08 19:35:11 +0000686 /* Space for the frame pointer, callee's LR, CR, etc, and for
687 the asm's temp regs. */
688
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200689 bytes = (LINKAGE_AREA_GPRS + ASM_NEEDS_REGISTERS) * sizeof(unsigned long);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000690
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200691 /* Return value handling.
692 The rules m32 are as follows:
Thomas Hellerd4c93202006-03-08 19:35:11 +0000693 - 32-bit (or less) integer values are returned in gpr3;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200694 - structures of size <= 4 bytes also returned in gpr3;
695 - 64-bit integer values [??? and structures between 5 and 8 bytes] are
696 returned in gpr3 and gpr4;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000697 - Single/double FP values are returned in fpr1;
698 - Long double FP (if not equivalent to double) values are returned in
699 fpr1 and fpr2;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200700 m64:
701 - 64-bit or smaller integral values are returned in GPR3
702 - Single/double FP values are returned in fpr1;
703 - Long double FP values are returned in fpr1 and fpr2;
704 m64 Structures:
705 - If the structure could be accommodated in registers were it to be the
706 first argument to a routine, then it is returned in those registers.
707 m32/m64 structures otherwise:
Thomas Hellerd4c93202006-03-08 19:35:11 +0000708 - Larger structures values are allocated space and a pointer is passed
709 as the first argument. */
710 switch (cif->rtype->type)
711 {
712
713#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
714 case FFI_TYPE_LONGDOUBLE:
715 flags |= FLAG_RETURNS_128BITS;
716 flags |= FLAG_RETURNS_FP;
717 break;
718#endif
719
720 case FFI_TYPE_DOUBLE:
721 flags |= FLAG_RETURNS_64BITS;
722 /* Fall through. */
723 case FFI_TYPE_FLOAT:
724 flags |= FLAG_RETURNS_FP;
725 break;
726
727 case FFI_TYPE_UINT64:
728 case FFI_TYPE_SINT64:
Matthias Klosea8349752010-03-15 13:25:28 +0000729#ifdef POWERPC64
730 case FFI_TYPE_POINTER:
731#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000732 flags |= FLAG_RETURNS_64BITS;
733 break;
734
735 case FFI_TYPE_STRUCT:
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200736#if defined(POWERPC_DARWIN64)
737 {
738 /* Can we fit the struct into regs? */
739 if (darwin64_struct_ret_by_value_p (cif->rtype))
740 {
741 unsigned nfpr = 0;
742 flags |= FLAG_RETURNS_STRUCT;
743 if (cif->rtype->size != 16)
744 darwin64_scan_struct_for_floats (cif->rtype, &nfpr) ;
745 else
746 flags |= FLAG_RETURNS_128BITS;
747 /* Will be 0 for 16byte struct. */
748 if (nfpr)
749 flags |= FLAG_RETURNS_FP;
750 }
751 else /* By ref. */
752 {
753 flags |= FLAG_RETVAL_REFERENCE;
754 flags |= FLAG_RETURNS_NOTHING;
755 intarg_count++;
756 }
757 }
758#elif defined(DARWIN_PPC)
759 if (cif->rtype->size <= 4)
760 flags |= FLAG_RETURNS_STRUCT;
761 else /* else by reference. */
762 {
763 flags |= FLAG_RETVAL_REFERENCE;
764 flags |= FLAG_RETURNS_NOTHING;
765 intarg_count++;
766 }
767#else /* assume we pass by ref. */
Thomas Hellerd4c93202006-03-08 19:35:11 +0000768 flags |= FLAG_RETVAL_REFERENCE;
769 flags |= FLAG_RETURNS_NOTHING;
770 intarg_count++;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200771#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000772 break;
773 case FFI_TYPE_VOID:
774 flags |= FLAG_RETURNS_NOTHING;
775 break;
776
777 default:
778 /* Returns 32-bit integer, or similar. Nothing to do here. */
779 break;
780 }
781
782 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
783 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200784 goes on the stack.
785 ??? Structures are passed as a pointer to a copy of the structure.
786 Stuff on the stack needs to keep proper alignment.
787 For m64 the count is effectively of half-GPRs. */
Thomas Hellerd4c93202006-03-08 19:35:11 +0000788 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
789 {
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200790 unsigned align_words;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000791 switch ((*ptr)->type)
792 {
793 case FFI_TYPE_FLOAT:
794 case FFI_TYPE_DOUBLE:
795 fparg_count++;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200796#if !defined(POWERPC_DARWIN64)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000797 /* If this FP arg is going on the stack, it must be
798 8-byte-aligned. */
799 if (fparg_count > NUM_FPR_ARG_REGISTERS
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200800 && (intarg_count & 0x01) != 0)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000801 intarg_count++;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200802#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000803 break;
804
805#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
Thomas Hellerd4c93202006-03-08 19:35:11 +0000806 case FFI_TYPE_LONGDOUBLE:
807 fparg_count += 2;
808 /* If this FP arg is going on the stack, it must be
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200809 16-byte-aligned. */
810 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
811#if defined (POWERPC64)
812 intarg_count = ALIGN(intarg_count, 2);
813#else
814 intarg_count = ALIGN(intarg_count, 4);
815#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000816 break;
817#endif
818
819 case FFI_TYPE_UINT64:
820 case FFI_TYPE_SINT64:
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200821#if defined(POWERPC64)
822 intarg_count++;
823#else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000824 /* 'long long' arguments are passed as two words, but
825 either both words must fit in registers or both go
826 on the stack. If they go on the stack, they must
827 be 8-byte-aligned. */
828 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200829 || (intarg_count >= NUM_GPR_ARG_REGISTERS
830 && (intarg_count & 0x01) != 0))
Thomas Hellerd4c93202006-03-08 19:35:11 +0000831 intarg_count++;
832 intarg_count += 2;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200833#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000834 break;
835
836 case FFI_TYPE_STRUCT:
837 size_al = (*ptr)->size;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200838#if defined(POWERPC_DARWIN64)
839 align_words = (*ptr)->alignment >> 3;
840 if (align_words)
841 intarg_count = ALIGN(intarg_count, align_words);
842 /* Base size of the struct. */
Matthias Klosea8349752010-03-15 13:25:28 +0000843 intarg_count += (size_al + 7) / 8;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200844 /* If 16 bytes then don't worry about floats. */
845 if (size_al != 16)
846 /* Scan through for floats to be placed in regs. */
847 darwin64_scan_struct_for_floats (*ptr, &fparg_count) ;
Matthias Klosea8349752010-03-15 13:25:28 +0000848#else
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200849 align_words = (*ptr)->alignment >> 2;
850 if (align_words)
851 intarg_count = ALIGN(intarg_count, align_words);
852 /* If the first member of the struct is a double, then align
853 the struct to double-word.
854 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
855 size_al = ALIGN((*ptr)->size, 8); */
856# ifdef POWERPC64
857 intarg_count += (size_al + 7) / 8;
858# else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000859 intarg_count += (size_al + 3) / 4;
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200860# endif
Matthias Klosea8349752010-03-15 13:25:28 +0000861#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000862 break;
863
864 default:
865 /* Everything else is passed as a 4-byte word in a GPR, either
866 the object itself or a pointer to it. */
867 intarg_count++;
868 break;
869 }
870 }
871
872 if (fparg_count != 0)
873 flags |= FLAG_FP_ARGUMENTS;
874
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200875#if defined(POWERPC_DARWIN64)
876 /* Space to image the FPR registers, if needed - which includes when they might be
877 used in a struct return. */
878 if (fparg_count != 0
879 || ((flags & FLAG_RETURNS_STRUCT)
880 && (flags & FLAG_RETURNS_FP)))
881 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
882#else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000883 /* Space for the FPR registers, if needed. */
884 if (fparg_count != 0)
885 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200886#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000887
888 /* Stack space. */
Matthias Klosea8349752010-03-15 13:25:28 +0000889#ifdef POWERPC64
890 if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS)
891 bytes += (intarg_count + fparg_count) * sizeof(long);
892#else
Thomas Hellerd4c93202006-03-08 19:35:11 +0000893 if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
894 bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
Matthias Klosea8349752010-03-15 13:25:28 +0000895#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000896 else
897 bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
898
899 /* The stack space allocated needs to be a multiple of 16 bytes. */
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200900 bytes = ALIGN(bytes, 16) ;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000901
902 cif->flags = flags;
903 cif->bytes = bytes;
904
905 return FFI_OK;
906}
907
Matthias Klosea8349752010-03-15 13:25:28 +0000908extern void ffi_call_AIX(extended_cif *, long, unsigned, unsigned *,
Christian Heimes78644762008-03-04 23:39:23 +0000909 void (*fn)(void), void (*fn2)(void));
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200910
Matthias Klosea8349752010-03-15 13:25:28 +0000911extern void ffi_call_DARWIN(extended_cif *, long, unsigned, unsigned *,
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200912 void (*fn)(void), void (*fn2)(void), ffi_type*);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000913
Matthias Klosea8349752010-03-15 13:25:28 +0000914void
915ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000916{
917 extended_cif ecif;
918
919 ecif.cif = cif;
920 ecif.avalue = avalue;
921
922 /* If the return value is a struct and we don't have a return
923 value address then we need to make one. */
924
925 if ((rvalue == NULL) &&
926 (cif->rtype->type == FFI_TYPE_STRUCT))
927 {
Matthias Klosea8349752010-03-15 13:25:28 +0000928 ecif.rvalue = alloca (cif->rtype->size);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000929 }
930 else
931 ecif.rvalue = rvalue;
932
933 switch (cif->abi)
934 {
935 case FFI_AIX:
Matthias Klosea8349752010-03-15 13:25:28 +0000936 ffi_call_AIX(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200937 FFI_FN(ffi_prep_args));
Thomas Hellerd4c93202006-03-08 19:35:11 +0000938 break;
939 case FFI_DARWIN:
Matthias Klosea8349752010-03-15 13:25:28 +0000940 ffi_call_DARWIN(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200941 FFI_FN(ffi_prep_args), cif->rtype);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000942 break;
943 default:
944 FFI_ASSERT(0);
945 break;
946 }
947}
948
949static void flush_icache(char *);
950static void flush_range(char *, int);
951
952/* The layout of a function descriptor. A C function pointer really
953 points to one of these. */
954
955typedef struct aix_fd_struct {
956 void *code_pointer;
957 void *toc;
958} aix_fd;
959
960/* here I'd like to add the stack frame layout we use in darwin_closure.S
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200961 and aix_closure.S
Thomas Hellerd4c93202006-03-08 19:35:11 +0000962
doko@ubuntu.com2a918762012-06-26 17:56:44 +0200963 m32/m64
964
965 The stack layout looks like this:
966
967 | Additional params... | | Higher address
968 ~ ~ ~
969 | Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS
970 |--------------------------------------------| |
971 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
972 |--------------------------------------------| |
973 | Reserved 2*4/8 | |
974 |--------------------------------------------| |
975 | Space for callee's LR 4/8 | |
976 |--------------------------------------------| |
977 | Saved CR [low word for m64] 4/8 | |
978 |--------------------------------------------| |
979 | Current backchain pointer 4/8 |-/ Parent's frame.
980 |--------------------------------------------| <+ <<< on entry to ffi_closure_ASM
981 | Result Bytes 16 | |
982 |--------------------------------------------| |
983 ~ padding to 16-byte alignment ~ ~
984 |--------------------------------------------| |
985 | NUM_FPR_ARG_REGISTERS slots | |
986 | here fp13 .. fp1 13*8 | |
987 |--------------------------------------------| |
988 | R3..R10 8*4/8=32/64 | | NUM_GPR_ARG_REGISTERS
989 |--------------------------------------------| |
990 | TOC=R2 (AIX) Reserved (Darwin) 4/8 | |
991 |--------------------------------------------| | stack |
992 | Reserved [compiler,binder] 2*4/8 | | grows |
993 |--------------------------------------------| | down V
994 | Space for callee's LR 4/8 | |
995 |--------------------------------------------| | lower addresses
996 | Saved CR [low word for m64] 4/8 | |
997 |--------------------------------------------| | stack pointer here
998 | Current backchain pointer 4/8 |-/ during
999 |--------------------------------------------| <<< ffi_closure_ASM.
Thomas Hellerd4c93202006-03-08 19:35:11 +00001000
1001*/
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001002
Thomas Hellerd4c93202006-03-08 19:35:11 +00001003ffi_status
Christian Heimes78644762008-03-04 23:39:23 +00001004ffi_prep_closure_loc (ffi_closure* closure,
1005 ffi_cif* cif,
1006 void (*fun)(ffi_cif*, void*, void**, void*),
1007 void *user_data,
1008 void *codeloc)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001009{
1010 unsigned int *tramp;
1011 struct ffi_aix_trampoline_struct *tramp_aix;
1012 aix_fd *fd;
1013
1014 switch (cif->abi)
1015 {
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001016 case FFI_DARWIN:
Thomas Hellerd4c93202006-03-08 19:35:11 +00001017
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001018 FFI_ASSERT (cif->abi == FFI_DARWIN);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001019
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001020 tramp = (unsigned int *) &closure->tramp[0];
1021#if defined(POWERPC_DARWIN64)
1022 tramp[0] = 0x7c0802a6; /* mflr r0 */
1023 tramp[1] = 0x429f0015; /* bcl- 20,4*cr7+so, +0x18 (L1) */
1024 /* We put the addresses here. */
1025 tramp[6] = 0x7d6802a6; /*L1: mflr r11 */
1026 tramp[7] = 0xe98b0000; /* ld r12,0(r11) function address */
1027 tramp[8] = 0x7c0803a6; /* mtlr r0 */
1028 tramp[9] = 0x7d8903a6; /* mtctr r12 */
1029 tramp[10] = 0xe96b0008; /* lwz r11,8(r11) static chain */
1030 tramp[11] = 0x4e800420; /* bctr */
Thomas Hellerd4c93202006-03-08 19:35:11 +00001031
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001032 *((unsigned long *)&tramp[2]) = (unsigned long) ffi_closure_ASM; /* function */
1033 *((unsigned long *)&tramp[4]) = (unsigned long) codeloc; /* context */
1034#else
1035 tramp[0] = 0x7c0802a6; /* mflr r0 */
1036 tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
1037 tramp[4] = 0x7d6802a6; /* mflr r11 */
1038 tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */
1039 tramp[6] = 0x7c0803a6; /* mtlr r0 */
1040 tramp[7] = 0x7d8903a6; /* mtctr r12 */
1041 tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
1042 tramp[9] = 0x4e800420; /* bctr */
1043 tramp[2] = (unsigned long) ffi_closure_ASM; /* function */
1044 tramp[3] = (unsigned long) codeloc; /* context */
1045#endif
1046 closure->cif = cif;
1047 closure->fun = fun;
1048 closure->user_data = user_data;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001049
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001050 /* Flush the icache. Only necessary on Darwin. */
1051 flush_range(codeloc, FFI_TRAMPOLINE_SIZE);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001052
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001053 break;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001054
1055 case FFI_AIX:
1056
1057 tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
1058 fd = (aix_fd *)(void *)ffi_closure_ASM;
1059
1060 FFI_ASSERT (cif->abi == FFI_AIX);
1061
1062 tramp_aix->code_pointer = fd->code_pointer;
1063 tramp_aix->toc = fd->toc;
Christian Heimes78644762008-03-04 23:39:23 +00001064 tramp_aix->static_chain = codeloc;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001065 closure->cif = cif;
1066 closure->fun = fun;
1067 closure->user_data = user_data;
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001068 break;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001069
1070 default:
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001071 return FFI_BAD_ABI;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001072 break;
1073 }
1074 return FFI_OK;
1075}
1076
1077static void
1078flush_icache(char *addr)
1079{
1080#ifndef _AIX
1081 __asm__ volatile (
1082 "dcbf 0,%0\n"
1083 "\tsync\n"
1084 "\ticbi 0,%0\n"
1085 "\tsync\n"
1086 "\tisync"
1087 : : "r"(addr) : "memory");
1088#endif
1089}
1090
1091static void
1092flush_range(char * addr1, int size)
1093{
1094#define MIN_LINE_SIZE 32
1095 int i;
1096 for (i = 0; i < size; i += MIN_LINE_SIZE)
1097 flush_icache(addr1+i);
1098 flush_icache(addr1+size-1);
1099}
1100
1101typedef union
1102{
1103 float f;
1104 double d;
1105} ffi_dblfl;
1106
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001107ffi_type *
Matthias Klosea8349752010-03-15 13:25:28 +00001108ffi_closure_helper_DARWIN (ffi_closure *, void *,
1109 unsigned long *, ffi_dblfl *);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001110
1111/* Basically the trampoline invokes ffi_closure_ASM, and on
1112 entry, r11 holds the address of the closure.
1113 After storing the registers that could possibly contain
1114 parameters to be passed into the stack frame and setting
1115 up space for a return value, ffi_closure_ASM invokes the
1116 following helper function to do most of the work. */
1117
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001118ffi_type *
Matthias Klosea8349752010-03-15 13:25:28 +00001119ffi_closure_helper_DARWIN (ffi_closure *closure, void *rvalue,
1120 unsigned long *pgr, ffi_dblfl *pfr)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001121{
1122 /* rvalue is the pointer to space for return value in closure assembly
1123 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
1124 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
1125
1126 typedef double ldbits[2];
1127
1128 union ldu
1129 {
1130 ldbits lb;
1131 long double ld;
1132 };
1133
1134 void ** avalue;
1135 ffi_type ** arg_types;
1136 long i, avn;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001137 ffi_cif * cif;
Matthias Klosea8349752010-03-15 13:25:28 +00001138 ffi_dblfl * end_pfr = pfr + NUM_FPR_ARG_REGISTERS;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001139 unsigned size_al;
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001140#if defined(POWERPC_DARWIN64)
1141 unsigned fpsused = 0;
1142#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001143
1144 cif = closure->cif;
Matthias Klosea8349752010-03-15 13:25:28 +00001145 avalue = alloca (cif->nargs * sizeof(void *));
Thomas Hellerd4c93202006-03-08 19:35:11 +00001146
Thomas Hellerd4c93202006-03-08 19:35:11 +00001147 if (cif->rtype->type == FFI_TYPE_STRUCT)
1148 {
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001149#if defined(POWERPC_DARWIN64)
1150 if (!darwin64_struct_ret_by_value_p (cif->rtype))
1151 {
1152 /* Won't fit into the regs - return by ref. */
1153 rvalue = (void *) *pgr;
1154 pgr++;
1155 }
1156#elif defined(DARWIN_PPC)
1157 if (cif->rtype->size > 4)
1158 {
1159 rvalue = (void *) *pgr;
1160 pgr++;
1161 }
1162#else /* assume we return by ref. */
Thomas Hellerd4c93202006-03-08 19:35:11 +00001163 rvalue = (void *) *pgr;
1164 pgr++;
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001165#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001166 }
1167
1168 i = 0;
1169 avn = cif->nargs;
1170 arg_types = cif->arg_types;
1171
1172 /* Grab the addresses of the arguments from the stack frame. */
1173 while (i < avn)
1174 {
1175 switch (arg_types[i]->type)
1176 {
1177 case FFI_TYPE_SINT8:
1178 case FFI_TYPE_UINT8:
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001179#if defined(POWERPC64)
Matthias Klosea8349752010-03-15 13:25:28 +00001180 avalue[i] = (char *) pgr + 7;
1181#else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001182 avalue[i] = (char *) pgr + 3;
Matthias Klosea8349752010-03-15 13:25:28 +00001183#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001184 pgr++;
1185 break;
1186
1187 case FFI_TYPE_SINT16:
1188 case FFI_TYPE_UINT16:
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001189#if defined(POWERPC64)
Matthias Klosea8349752010-03-15 13:25:28 +00001190 avalue[i] = (char *) pgr + 6;
1191#else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001192 avalue[i] = (char *) pgr + 2;
Matthias Klosea8349752010-03-15 13:25:28 +00001193#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001194 pgr++;
1195 break;
1196
1197 case FFI_TYPE_SINT32:
1198 case FFI_TYPE_UINT32:
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001199#if defined(POWERPC64)
Matthias Klosea8349752010-03-15 13:25:28 +00001200 avalue[i] = (char *) pgr + 4;
1201#else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001202 case FFI_TYPE_POINTER:
1203 avalue[i] = pgr;
Matthias Klosea8349752010-03-15 13:25:28 +00001204#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001205 pgr++;
1206 break;
1207
1208 case FFI_TYPE_STRUCT:
Matthias Klosea8349752010-03-15 13:25:28 +00001209 size_al = arg_types[i]->size;
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001210#if defined(POWERPC_DARWIN64)
1211 pgr = (unsigned long *)ALIGN((char *)pgr, arg_types[i]->alignment);
1212 if (size_al < 3 || size_al == 4)
1213 {
1214 avalue[i] = ((char *)pgr)+8-size_al;
1215 if (arg_types[i]->elements[0]->type == FFI_TYPE_FLOAT
1216 && fpsused < NUM_FPR_ARG_REGISTERS)
1217 {
1218 *(float *)pgr = (float) *(double *)pfr;
1219 pfr++;
1220 fpsused++;
1221 }
1222 }
1223 else
1224 {
1225 if (size_al != 16)
1226 pfr = (ffi_dblfl *)
1227 darwin64_struct_floats_to_mem (arg_types[i], (char *)pgr,
1228 (double *)pfr, &fpsused);
1229 avalue[i] = pgr;
1230 }
Matthias Klosea8349752010-03-15 13:25:28 +00001231 pgr += (size_al + 7) / 8;
1232#else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001233 /* If the first member of the struct is a double, then align
Matthias Klosea8349752010-03-15 13:25:28 +00001234 the struct to double-word. */
1235 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001236 size_al = ALIGN(arg_types[i]->size, 8);
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001237# if defined(POWERPC64)
1238 FFI_ASSERT (cif->abi != FFI_DARWIN);
1239 avalue[i] = pgr;
1240 pgr += (size_al + 7) / 8;
1241# else
1242 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
1243 SI 4 bytes) are aligned as if they were those modes. */
Thomas Hellerd4c93202006-03-08 19:35:11 +00001244 if (size_al < 3 && cif->abi == FFI_DARWIN)
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001245 avalue[i] = (char*) pgr + 4 - size_al;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001246 else
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001247 avalue[i] = pgr;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001248 pgr += (size_al + 3) / 4;
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001249# endif
Matthias Klosea8349752010-03-15 13:25:28 +00001250#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001251 break;
1252
1253 case FFI_TYPE_SINT64:
1254 case FFI_TYPE_UINT64:
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001255#if defined(POWERPC64)
Matthias Klosea8349752010-03-15 13:25:28 +00001256 case FFI_TYPE_POINTER:
1257 avalue[i] = pgr;
1258 pgr++;
1259 break;
1260#else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001261 /* Long long ints are passed in two gpr's. */
1262 avalue[i] = pgr;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001263 pgr += 2;
1264 break;
Matthias Klosea8349752010-03-15 13:25:28 +00001265#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001266
1267 case FFI_TYPE_FLOAT:
1268 /* A float value consumes a GPR.
1269 There are 13 64bit floating point registers. */
Matthias Klosea8349752010-03-15 13:25:28 +00001270 if (pfr < end_pfr)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001271 {
Matthias Klosea8349752010-03-15 13:25:28 +00001272 double temp = pfr->d;
1273 pfr->f = (float) temp;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001274 avalue[i] = pfr;
1275 pfr++;
1276 }
1277 else
1278 {
1279 avalue[i] = pgr;
1280 }
Thomas Hellerd4c93202006-03-08 19:35:11 +00001281 pgr++;
1282 break;
1283
1284 case FFI_TYPE_DOUBLE:
1285 /* A double value consumes two GPRs.
1286 There are 13 64bit floating point registers. */
Matthias Klosea8349752010-03-15 13:25:28 +00001287 if (pfr < end_pfr)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001288 {
1289 avalue[i] = pfr;
1290 pfr++;
1291 }
1292 else
1293 {
1294 avalue[i] = pgr;
1295 }
Matthias Klosea8349752010-03-15 13:25:28 +00001296#ifdef POWERPC64
1297 pgr++;
1298#else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001299 pgr += 2;
Matthias Klosea8349752010-03-15 13:25:28 +00001300#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001301 break;
1302
1303#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1304
1305 case FFI_TYPE_LONGDOUBLE:
Matthias Klosea8349752010-03-15 13:25:28 +00001306#ifdef POWERPC64
1307 if (pfr + 1 < end_pfr)
1308 {
1309 avalue[i] = pfr;
1310 pfr += 2;
1311 }
1312 else
1313 {
1314 if (pfr < end_pfr)
1315 {
1316 *pgr = *(unsigned long *) pfr;
1317 pfr++;
1318 }
1319 avalue[i] = pgr;
1320 }
1321 pgr += 2;
1322#else /* POWERPC64 */
Thomas Hellerd4c93202006-03-08 19:35:11 +00001323 /* A long double value consumes four GPRs and two FPRs.
1324 There are 13 64bit floating point registers. */
Matthias Klosea8349752010-03-15 13:25:28 +00001325 if (pfr + 1 < end_pfr)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001326 {
1327 avalue[i] = pfr;
1328 pfr += 2;
1329 }
1330 /* Here we have the situation where one part of the long double
1331 is stored in fpr13 and the other part is already on the stack.
1332 We use a union to pass the long double to avalue[i]. */
Matthias Klosea8349752010-03-15 13:25:28 +00001333 else if (pfr + 1 == end_pfr)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001334 {
Matthias Klosea8349752010-03-15 13:25:28 +00001335 union ldu temp_ld;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001336 memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits));
1337 memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits));
1338 avalue[i] = &temp_ld.ld;
Matthias Klosea8349752010-03-15 13:25:28 +00001339 pfr++;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001340 }
1341 else
1342 {
1343 avalue[i] = pgr;
1344 }
Thomas Hellerd4c93202006-03-08 19:35:11 +00001345 pgr += 4;
Matthias Klosea8349752010-03-15 13:25:28 +00001346#endif /* POWERPC64 */
Thomas Hellerd4c93202006-03-08 19:35:11 +00001347 break;
1348#endif
1349 default:
1350 FFI_ASSERT(0);
1351 }
1352 i++;
1353 }
1354
1355 (closure->fun) (cif, rvalue, avalue, closure->user_data);
1356
1357 /* Tell ffi_closure_ASM to perform return type promotions. */
doko@ubuntu.com2a918762012-06-26 17:56:44 +02001358 return cif->rtype;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001359}