Debian Codegraph @

src/powerpc/ffi_sysv.c

raw
1 /* -----------------------------------------------------------------------
2 ffi_sysv.c - Copyright (C) 2013 IBM
3 Copyright (C) 2011 Anthony Green
4 Copyright (C) 2011 Kyle Moffett
5 Copyright (C) 2008 Red Hat, Inc
6 Copyright (C) 2007, 2008 Free Software Foundation, Inc
7 Copyright (c) 1998 Geoffrey Keating
8
9 PowerPC Foreign Function Interface
10
11 Permission is hereby granted, free of charge, to any person obtaining
12 a copy of this software and associated documentation files (the
13 ``Software''), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sublicense, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 OTHER DEALINGS IN THE SOFTWARE.
29 ----------------------------------------------------------------------- */
30
31 #include "ffi.h"
32 #include <tramp.h>
33
34 #ifndef POWERPC64
35 #include "ffi_common.h"
36 #include "ffi_powerpc.h"
37
38
39 /* About the SYSV ABI. */
40 #define ASM_NEEDS_REGISTERS 6
41 #define NUM_GPR_ARG_REGISTERS 8
42 #define NUM_FPR_ARG_REGISTERS 8
43
44
45 #if HAVE_LONG_DOUBLE_VARIANT && FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
46 /* Adjust size of ffi_type_longdouble. */
47 void FFI_HIDDEN
48 ffi_prep_types_sysv (ffi_abi abi)
49 {
50 if ((abi & (FFI_SYSV | FFI_SYSV_LONG_DOUBLE_128)) == FFI_SYSV)
51 {
52 ffi_type_longdouble.size = 8;
53 ffi_type_longdouble.alignment = 8;
54 }
55 else
56 {
57 ffi_type_longdouble.size = 16;
58 ffi_type_longdouble.alignment = 16;
59 }
60 }
61 #endif
62
63 /* Transform long double, double and float to other types as per abi. */
64 static int
65 translate_float (int abi, int type)
66 {
67 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
68 if (type == FFI_TYPE_LONGDOUBLE
69 && (abi & FFI_SYSV_LONG_DOUBLE_128) == 0)
70 type = FFI_TYPE_DOUBLE;
71 #endif
72 if ((abi & FFI_SYSV_SOFT_FLOAT) != 0)
73 {
74 if (type == FFI_TYPE_FLOAT)
75 type = FFI_TYPE_UINT32;
76 else if (type == FFI_TYPE_DOUBLE)
77 type = FFI_TYPE_UINT64;
78 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
79 else if (type == FFI_TYPE_LONGDOUBLE)
80 type = FFI_TYPE_UINT128;
81 }
82 else if ((abi & FFI_SYSV_IBM_LONG_DOUBLE) == 0)
83 {
84 if (type == FFI_TYPE_LONGDOUBLE)
85 type = FFI_TYPE_STRUCT;
86 #endif
87 }
88 return type;
89 }
90
91 /* Perform machine dependent cif processing */
92 static ffi_status
93 ffi_prep_cif_sysv_core (ffi_cif *cif)
94 {
95 ffi_type **ptr;
96 unsigned bytes;
97 unsigned i, fpr_count = 0, gpr_count = 0, stack_count = 0;
98 unsigned flags = cif->flags;
99 unsigned struct_copy_size = 0;
100 unsigned type = cif->rtype->type;
101 unsigned size = cif->rtype->size;
102
103 /* The machine-independent calculation of cif->bytes doesn't work
104 for us. Redo the calculation. */
105
106 /* Space for the frame pointer, callee's LR, and the asm's temp regs. */
107 bytes = (2 + ASM_NEEDS_REGISTERS) * sizeof (int);
108
109 /* Space for the GPR registers. */
110 bytes += NUM_GPR_ARG_REGISTERS * sizeof (int);
111
112 /* Return value handling. The rules for SYSV are as follows:
113 - 32-bit (or less) integer values are returned in gpr3;
114 - Structures of size <= 4 bytes also returned in gpr3;
115 - 64-bit integer values and structures between 5 and 8 bytes are returned
116 in gpr3 and gpr4;
117 - Larger structures are allocated space and a pointer is passed as
118 the first argument.
119 - Single/double FP values are returned in fpr1;
120 - long doubles (if not equivalent to double) are returned in
121 fpr1,fpr2 for Linux and as for large structs for SysV. */
122
123 type = translate_float (cif->abi, type);
124
125 switch (type)
126 {
127 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
128 case FFI_TYPE_LONGDOUBLE:
129 flags |= FLAG_RETURNS_128BITS;
130 /* Fall through. */
131 #endif
132 case FFI_TYPE_DOUBLE:
133 flags |= FLAG_RETURNS_64BITS;
134 /* Fall through. */
135 case FFI_TYPE_FLOAT:
136 flags |= FLAG_RETURNS_FP;
137 #ifdef __NO_FPRS__
138 return FFI_BAD_ABI;
139 #endif
140 break;
141
142 case FFI_TYPE_UINT128:
143 flags |= FLAG_RETURNS_128BITS;
144 /* Fall through. */
145 case FFI_TYPE_UINT64:
146 case FFI_TYPE_SINT64:
147 flags |= FLAG_RETURNS_64BITS;
148 break;
149
150 case FFI_TYPE_STRUCT:
151 /* The final SYSV ABI says that structures smaller or equal 8 bytes
152 are returned in r3/r4. A draft ABI used by linux instead
153 returns them in memory. */
154 if ((cif->abi & FFI_SYSV_STRUCT_RET) != 0 && size <= 8)
155 {
156 flags |= FLAG_RETURNS_SMST;
157 break;
158 }
159 gpr_count++;
160 flags |= FLAG_RETVAL_REFERENCE;
161 /* Fall through. */
162 case FFI_TYPE_VOID:
163 flags |= FLAG_RETURNS_NOTHING;
164 break;
165
166 default:
167 /* Returns 32-bit integer, or similar. Nothing to do here. */
168 break;
169 }
170
171 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
172 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
173 goes on the stack. Structures and long doubles (if not equivalent
174 to double) are passed as a pointer to a copy of the structure.
175 Stuff on the stack needs to keep proper alignment. */
176 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
177 {
178 unsigned short typenum = (*ptr)->type;
179
180 typenum = translate_float (cif->abi, typenum);
181
182 switch (typenum)
183 {
184 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
185 case FFI_TYPE_LONGDOUBLE:
186 if (fpr_count >= NUM_FPR_ARG_REGISTERS - 1)
187 {
188 fpr_count = NUM_FPR_ARG_REGISTERS;
189 /* 8-byte align long doubles. */
190 stack_count += stack_count & 1;
191 stack_count += 4;
192 }
193 else
194 fpr_count += 2;
195 #ifdef __NO_FPRS__
196 return FFI_BAD_ABI;
197 #endif
198 break;
199 #endif
200
201 case FFI_TYPE_DOUBLE:
202 if (fpr_count >= NUM_FPR_ARG_REGISTERS)
203 {
204 /* 8-byte align doubles. */
205 stack_count += stack_count & 1;
206 stack_count += 2;
207 }
208 else
209 fpr_count += 1;
210 #ifdef __NO_FPRS__
211 return FFI_BAD_ABI;
212 #endif
213 break;
214
215 case FFI_TYPE_FLOAT:
216 if (fpr_count >= NUM_FPR_ARG_REGISTERS)
217 /* Yes, we don't follow the ABI, but neither does gcc. */
218 stack_count += 1;
219 else
220 fpr_count += 1;
221 #ifdef __NO_FPRS__
222 return FFI_BAD_ABI;
223 #endif
224 break;
225
226 case FFI_TYPE_UINT128:
227 /* A long double in FFI_LINUX_SOFT_FLOAT can use only a set
228 of four consecutive gprs. If we do not have enough, we
229 have to adjust the gpr_count value. */
230 if (gpr_count >= NUM_GPR_ARG_REGISTERS - 3)
231 gpr_count = NUM_GPR_ARG_REGISTERS;
232 if (gpr_count >= NUM_GPR_ARG_REGISTERS)
233 stack_count += 4;
234 else
235 gpr_count += 4;
236 break;
237
238 case FFI_TYPE_UINT64:
239 case FFI_TYPE_SINT64:
240 /* 'long long' arguments are passed as two words, but
241 either both words must fit in registers or both go
242 on the stack. If they go on the stack, they must
243 be 8-byte-aligned.
244
245 Also, only certain register pairs can be used for
246 passing long long int -- specifically (r3,r4), (r5,r6),
247 (r7,r8), (r9,r10). */
248 gpr_count += gpr_count & 1;
249 if (gpr_count >= NUM_GPR_ARG_REGISTERS)
250 {
251 stack_count += stack_count & 1;
252 stack_count += 2;
253 }
254 else
255 gpr_count += 2;
256 break;
257
258 case FFI_TYPE_STRUCT:
259 /* We must allocate space for a copy of these to enforce
260 pass-by-value. Pad the space up to a multiple of 16
261 bytes (the maximum alignment required for anything under
262 the SYSV ABI). */
263 struct_copy_size += ((*ptr)->size + 15) & ~0xF;
264 /* Fall through (allocate space for the pointer). */
265
266 case FFI_TYPE_POINTER:
267 case FFI_TYPE_INT:
268 case FFI_TYPE_UINT32:
269 case FFI_TYPE_SINT32:
270 case FFI_TYPE_UINT16:
271 case FFI_TYPE_SINT16:
272 case FFI_TYPE_UINT8:
273 case FFI_TYPE_SINT8:
274 /* Everything else is passed as a 4-byte word in a GPR, either
275 the object itself or a pointer to it. */
276 if (gpr_count >= NUM_GPR_ARG_REGISTERS)
277 stack_count += 1;
278 else
279 gpr_count += 1;
280 break;
281
282 default:
283 FFI_ASSERT (0);
284 }
285 }
286
287 if (fpr_count != 0)
288 flags |= FLAG_FP_ARGUMENTS;
289 if (gpr_count > 4)
290 flags |= FLAG_4_GPR_ARGUMENTS;
291 if (struct_copy_size != 0)
292 flags |= FLAG_ARG_NEEDS_COPY;
293
294 /* Space for the FPR registers, if needed. */
295 if (fpr_count != 0)
296 bytes += NUM_FPR_ARG_REGISTERS * sizeof (double);
297
298 /* Stack space. */
299 bytes += stack_count * sizeof (int);
300
301 /* The stack space allocated needs to be a multiple of 16 bytes. */
302 bytes = (bytes + 15) & ~0xF;
303
304 /* Add in the space for the copied structures. */
305 bytes += struct_copy_size;
306
307 cif->flags = flags;
308 cif->bytes = bytes;
309
310 return FFI_OK;
311 }
312
313 ffi_status FFI_HIDDEN
314 ffi_prep_cif_sysv (ffi_cif *cif)
315 {
316 if ((cif->abi & FFI_SYSV) == 0)
317 {
318 /* This call is from old code. Translate to new ABI values. */
319 cif->flags |= FLAG_COMPAT;
320 switch (cif->abi)
321 {
322 default:
323 return FFI_BAD_ABI;
324
325 case FFI_COMPAT_SYSV:
326 cif->abi = FFI_SYSV | FFI_SYSV_STRUCT_RET | FFI_SYSV_LONG_DOUBLE_128;
327 break;
328
329 case FFI_COMPAT_GCC_SYSV:
330 cif->abi = FFI_SYSV | FFI_SYSV_LONG_DOUBLE_128;
331 break;
332
333 case FFI_COMPAT_LINUX:
334 cif->abi = (FFI_SYSV | FFI_SYSV_IBM_LONG_DOUBLE
335 | FFI_SYSV_LONG_DOUBLE_128);
336 break;
337
338 case FFI_COMPAT_LINUX_SOFT_FLOAT:
339 cif->abi = (FFI_SYSV | FFI_SYSV_SOFT_FLOAT | FFI_SYSV_IBM_LONG_DOUBLE
340 | FFI_SYSV_LONG_DOUBLE_128);
341 break;
342 }
343 }
344 return ffi_prep_cif_sysv_core (cif);
345 }
346
347 /* ffi_prep_args_SYSV is called by the assembly routine once stack space
348 has been allocated for the function's arguments.
349
350 The stack layout we want looks like this:
351
352 | Return address from ffi_call_SYSV 4bytes | higher addresses
353 |--------------------------------------------|
354 | Previous backchain pointer 4 | stack pointer here
355 |--------------------------------------------|<+ <<< on entry to
356 | Saved r28-r31 4*4 | | ffi_call_SYSV
357 |--------------------------------------------| |
358 | GPR registers r3-r10 8*4 | | ffi_call_SYSV
359 |--------------------------------------------| |
360 | FPR registers f1-f8 (optional) 8*8 | |
361 |--------------------------------------------| | stack |
362 | Space for copied structures | | grows |
363 |--------------------------------------------| | down V
364 | Parameters that didn't fit in registers | |
365 |--------------------------------------------| | lower addresses
366 | Space for callee's LR 4 | |
367 |--------------------------------------------| | stack pointer here
368 | Current backchain pointer 4 |-/ during
369 |--------------------------------------------| <<< ffi_call_SYSV
370
371 */
372
373 void FFI_HIDDEN
374 ffi_prep_args_SYSV (extended_cif *ecif, unsigned *const stack)
375 {
376 const unsigned bytes = ecif->cif->bytes;
377 const unsigned flags = ecif->cif->flags;
378
379 typedef union
380 {
381 char *c;
382 unsigned *u;
383 long long *ll;
384 float *f;
385 double *d;
386 } valp;
387
388 /* 'stacktop' points at the previous backchain pointer. */
389 valp stacktop;
390
391 /* 'gpr_base' points at the space for gpr3, and grows upwards as
392 we use GPR registers. */
393 valp gpr_base;
394 valp gpr_end;
395
396 #ifndef __NO_FPRS__
397 /* 'fpr_base' points at the space for fpr1, and grows upwards as
398 we use FPR registers. */
399 valp fpr_base;
400 valp fpr_end;
401 #endif
402
403 /* 'copy_space' grows down as we put structures in it. It should
404 stay 16-byte aligned. */
405 valp copy_space;
406
407 /* 'next_arg' grows up as we put parameters in it. */
408 valp next_arg;
409
410 int i;
411 ffi_type **ptr;
412 #ifndef __NO_FPRS__
413 double double_tmp;
414 #endif
415 union
416 {
417 void **v;
418 char **c;
419 signed char **sc;
420 unsigned char **uc;
421 signed short **ss;
422 unsigned short **us;
423 unsigned int **ui;
424 long long **ll;
425 float **f;
426 double **d;
427 } p_argv;
428 size_t struct_copy_size;
429 unsigned gprvalue;
430
431 stacktop.c = (char *) stack + bytes;
432 gpr_end.u = stacktop.u - ASM_NEEDS_REGISTERS;
433 gpr_base.u = gpr_end.u - NUM_GPR_ARG_REGISTERS;
434 #ifndef __NO_FPRS__
435 fpr_end.d = gpr_base.d;
436 fpr_base.d = fpr_end.d - NUM_FPR_ARG_REGISTERS;
437 copy_space.c = ((flags & FLAG_FP_ARGUMENTS) ? fpr_base.c : gpr_base.c);
438 #else
439 copy_space.c = gpr_base.c;
440 #endif
441 next_arg.u = stack + 2;
442
443 /* Check that everything starts aligned properly. */
444 FFI_ASSERT (((unsigned long) (char *) stack & 0xF) == 0);
445 FFI_ASSERT (((unsigned long) copy_space.c & 0xF) == 0);
446 FFI_ASSERT (((unsigned long) stacktop.c & 0xF) == 0);
447 FFI_ASSERT ((bytes & 0xF) == 0);
448 FFI_ASSERT (copy_space.c >= next_arg.c);
449
450 /* Deal with return values that are actually pass-by-reference. */
451 if (flags & FLAG_RETVAL_REFERENCE)
452 *gpr_base.u++ = (unsigned) (char *) ecif->rvalue;
453
454 /* Now for the arguments. */
455 p_argv.v = ecif->avalue;
456 for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
457 i > 0;
458 i--, ptr++, p_argv.v++)
459 {
460 unsigned int typenum = (*ptr)->type;
461
462 typenum = translate_float (ecif->cif->abi, typenum);
463
464 /* Now test the translated value */
465 switch (typenum)
466 {
467 #ifndef __NO_FPRS__
468 # if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
469 case FFI_TYPE_LONGDOUBLE:
470 double_tmp = (*p_argv.d)[0];
471
472 if (fpr_base.d >= fpr_end.d - 1)
473 {
474 fpr_base.d = fpr_end.d;
475 if (((next_arg.u - stack) & 1) != 0)
476 next_arg.u += 1;
477 *next_arg.d = double_tmp;
478 next_arg.u += 2;
479 double_tmp = (*p_argv.d)[1];
480 *next_arg.d = double_tmp;
481 next_arg.u += 2;
482 }
483 else
484 {
485 *fpr_base.d++ = double_tmp;
486 double_tmp = (*p_argv.d)[1];
487 *fpr_base.d++ = double_tmp;
488 }
489 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
490 break;
491 # endif
492 case FFI_TYPE_DOUBLE:
493 double_tmp = **p_argv.d;
494
495 if (fpr_base.d >= fpr_end.d)
496 {
497 if (((next_arg.u - stack) & 1) != 0)
498 next_arg.u += 1;
499 *next_arg.d = double_tmp;
500 next_arg.u += 2;
501 }
502 else
503 *fpr_base.d++ = double_tmp;
504 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
505 break;
506
507 case FFI_TYPE_FLOAT:
508 double_tmp = **p_argv.f;
509 if (fpr_base.d >= fpr_end.d)
510 {
511 *next_arg.f = (float) double_tmp;
512 next_arg.u += 1;
513 }
514 else
515 *fpr_base.d++ = double_tmp;
516 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
517 break;
518 #endif /* have FPRs */
519
520 case FFI_TYPE_UINT128:
521 /* The soft float ABI for long doubles works like this, a long double
522 is passed in four consecutive GPRs if available. A maximum of 2
523 long doubles can be passed in gprs. If we do not have 4 GPRs
524 left, the long double is passed on the stack, 4-byte aligned. */
525 if (gpr_base.u >= gpr_end.u - 3)
526 {
527 unsigned int ii;
528 gpr_base.u = gpr_end.u;
529 for (ii = 0; ii < 4; ii++)
530 {
531 unsigned int int_tmp = (*p_argv.ui)[ii];
532 *next_arg.u++ = int_tmp;
533 }
534 }
535 else
536 {
537 unsigned int ii;
538 for (ii = 0; ii < 4; ii++)
539 {
540 unsigned int int_tmp = (*p_argv.ui)[ii];
541 *gpr_base.u++ = int_tmp;
542 }
543 }
544 break;
545
546 case FFI_TYPE_UINT64:
547 case FFI_TYPE_SINT64:
548 if (gpr_base.u >= gpr_end.u - 1)
549 {
550 gpr_base.u = gpr_end.u;
551 if (((next_arg.u - stack) & 1) != 0)
552 next_arg.u++;
553 *next_arg.ll = **p_argv.ll;
554 next_arg.u += 2;
555 }
556 else
557 {
558 /* The abi states only certain register pairs can be
559 used for passing long long int specifically (r3,r4),
560 (r5,r6), (r7,r8), (r9,r10). If next arg is long long
561 but not correct starting register of pair then skip
562 until the proper starting register. */
563 if (((gpr_end.u - gpr_base.u) & 1) != 0)
564 gpr_base.u++;
565 *gpr_base.ll++ = **p_argv.ll;
566 }
567 break;
568
569 case FFI_TYPE_STRUCT:
570 struct_copy_size = ((*ptr)->size + 15) & ~0xF;
571 copy_space.c -= struct_copy_size;
572 memcpy (copy_space.c, *p_argv.c, (*ptr)->size);
573
574 gprvalue = (unsigned long) copy_space.c;
575
576 FFI_ASSERT (copy_space.c > next_arg.c);
577 FFI_ASSERT (flags & FLAG_ARG_NEEDS_COPY);
578 goto putgpr;
579
580 case FFI_TYPE_UINT8:
581 gprvalue = **p_argv.uc;
582 goto putgpr;
583 case FFI_TYPE_SINT8:
584 gprvalue = **p_argv.sc;
585 goto putgpr;
586 case FFI_TYPE_UINT16:
587 gprvalue = **p_argv.us;
588 goto putgpr;
589 case FFI_TYPE_SINT16:
590 gprvalue = **p_argv.ss;
591 goto putgpr;
592
593 case FFI_TYPE_INT:
594 case FFI_TYPE_UINT32:
595 case FFI_TYPE_SINT32:
596 case FFI_TYPE_POINTER:
597
598 gprvalue = **p_argv.ui;
599
600 putgpr:
601 if (gpr_base.u >= gpr_end.u)
602 *next_arg.u++ = gprvalue;
603 else
604 *gpr_base.u++ = gprvalue;
605 break;
606 }
607 }
608
609 /* Check that we didn't overrun the stack... */
610 FFI_ASSERT (copy_space.c >= next_arg.c);
611 FFI_ASSERT (gpr_base.u <= gpr_end.u);
612 #ifndef __NO_FPRS__
613 FFI_ASSERT (fpr_base.u <= fpr_end.u);
614 #endif
615 FFI_ASSERT (((flags & FLAG_4_GPR_ARGUMENTS) != 0)
616 == (gpr_end.u - gpr_base.u < 4));
617 }
618
619 #define MIN_CACHE_LINE_SIZE 8
620
621 static void
622 flush_icache (char *wraddr, char *xaddr, int size)
623 {
624 int i;
625 for (i = 0; i < size; i += MIN_CACHE_LINE_SIZE)
626 __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;"
627 : : "r" (xaddr + i), "r" (wraddr + i) : "memory");
628 __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;" "sync;" "isync;"
629 : : "r"(xaddr + size - 1), "r"(wraddr + size - 1)
630 : "memory");
631 }
632
633 ffi_status FFI_HIDDEN
634 ffi_prep_closure_loc_sysv (ffi_closure *closure,
635 ffi_cif *cif,
636 void (*fun) (ffi_cif *, void *, void **, void *),
637 void *user_data,
638 void *codeloc)
639 {
640 if (cif->abi < FFI_SYSV || cif->abi >= FFI_LAST_ABI)
641 return FFI_BAD_ABI;
642
643 #ifdef FFI_EXEC_STATIC_TRAMP
644 if (ffi_tramp_is_present(closure))
645 {
646 /* Initialize the static trampoline's parameters. */
647 void (*dest)(void) = ffi_closure_SYSV;
648 ffi_tramp_set_parms (closure->ftramp, dest, closure);
649 }
650 else
651 #endif
652 {
653 unsigned int *tramp = (unsigned int *) &closure->tramp[0];
654 tramp[0] = 0x7c0802a6; /* mflr r0 */
655 tramp[1] = 0x429f0005; /* bcl 20,31,.+4 */
656 tramp[2] = 0x7d6802a6; /* mflr r11 */
657 tramp[3] = 0x7c0803a6; /* mtlr r0 */
658 tramp[4] = 0x800b0018; /* lwz r0,24(r11) */
659 tramp[5] = 0x816b001c; /* lwz r11,28(r11) */
660 tramp[6] = 0x7c0903a6; /* mtctr r0 */
661 tramp[7] = 0x4e800420; /* bctr */
662 *(void **) &tramp[8] = (void *) ffi_closure_SYSV; /* function */
663 *(void **) &tramp[9] = codeloc; /* context */
664
665 /* Flush the icache. */
666 flush_icache ((char *)tramp, (char *)codeloc, 8 * 4);
667 }
668
669 closure->cif = cif;
670 closure->fun = fun;
671 closure->user_data = user_data;
672
673 return FFI_OK;
674 }
675
676 /* Basically the trampoline invokes ffi_closure_SYSV, and on
677 entry, r11 holds the address of the closure.
678 After storing the registers that could possibly contain
679 parameters to be passed into the stack frame and setting
680 up space for a return value, ffi_closure_SYSV invokes the
681 following helper function to do most of the work. */
682
683 int
684 ffi_closure_helper_SYSV (ffi_cif *cif,
685 void (*fun) (ffi_cif *, void *, void **, void *),
686 void *user_data,
687 void *rvalue,
688 unsigned long *pgr,
689 ffi_dblfl *pfr,
690 unsigned long *pst)
691 {
692 /* rvalue is the pointer to space for return value in closure assembly */
693 /* pgr is the pointer to where r3-r10 are stored in ffi_closure_SYSV */
694 /* pfr is the pointer to where f1-f8 are stored in ffi_closure_SYSV */
695 /* pst is the pointer to outgoing parameter stack in original caller */
696
697 void ** avalue;
698 ffi_type ** arg_types;
699 long i, avn;
700 #ifndef __NO_FPRS__
701 long nf = 0; /* number of floating registers already used */
702 #endif
703 long ng = 0; /* number of general registers already used */
704
705 unsigned size = cif->rtype->size;
706 unsigned short rtypenum = cif->rtype->type;
707
708 avalue = alloca (cif->nargs * sizeof (void *));
709
710 /* First translate for softfloat/nonlinux */
711 rtypenum = translate_float (cif->abi, rtypenum);
712
713 /* Copy the caller's structure return value address so that the closure
714 returns the data directly to the caller.
715 For FFI_SYSV the result is passed in r3/r4 if the struct size is less
716 or equal 8 bytes. */
717 if (rtypenum == FFI_TYPE_STRUCT
718 && !((cif->abi & FFI_SYSV_STRUCT_RET) != 0 && size <= 8))
719 {
720 rvalue = (void *) *pgr;
721 ng++;
722 pgr++;
723 }
724
725 i = 0;
726 avn = cif->nargs;
727 arg_types = cif->arg_types;
728
729 /* Grab the addresses of the arguments from the stack frame. */
730 while (i < avn) {
731 unsigned short typenum = arg_types[i]->type;
732
733 /* We may need to handle some values depending on ABI. */
734 typenum = translate_float (cif->abi, typenum);
735
736 switch (typenum)
737 {
738 #ifndef __NO_FPRS__
739 case FFI_TYPE_FLOAT:
740 /* Unfortunately float values are stored as doubles
741 in the ffi_closure_SYSV code (since we don't check
742 the type in that routine). */
743 if (nf < NUM_FPR_ARG_REGISTERS)
744 {
745 /* FIXME? here we are really changing the values
746 stored in the original calling routines outgoing
747 parameter stack. This is probably a really
748 naughty thing to do but... */
749 double temp = pfr->d;
750 pfr->f = (float) temp;
751 avalue[i] = pfr;
752 nf++;
753 pfr++;
754 }
755 else
756 {
757 avalue[i] = pst;
758 pst += 1;
759 }
760 break;
761
762 case FFI_TYPE_DOUBLE:
763 if (nf < NUM_FPR_ARG_REGISTERS)
764 {
765 avalue[i] = pfr;
766 nf++;
767 pfr++;
768 }
769 else
770 {
771 if (((long) pst) & 4)
772 pst++;
773 avalue[i] = pst;
774 pst += 2;
775 }
776 break;
777
778 # if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
779 case FFI_TYPE_LONGDOUBLE:
780 if (nf < NUM_FPR_ARG_REGISTERS - 1)
781 {
782 avalue[i] = pfr;
783 pfr += 2;
784 nf += 2;
785 }
786 else
787 {
788 if (((long) pst) & 4)
789 pst++;
790 avalue[i] = pst;
791 pst += 4;
792 nf = 8;
793 }
794 break;
795 # endif
796 #endif
797
798 case FFI_TYPE_UINT128:
799 /* Test if for the whole long double, 4 gprs are available.
800 otherwise the stuff ends up on the stack. */
801 if (ng < NUM_GPR_ARG_REGISTERS - 3)
802 {
803 avalue[i] = pgr;
804 pgr += 4;
805 ng += 4;
806 }
807 else
808 {
809 avalue[i] = pst;
810 pst += 4;
811 ng = 8+4;
812 }
813 break;
814
815 case FFI_TYPE_SINT8:
816 case FFI_TYPE_UINT8:
817 #ifndef __LITTLE_ENDIAN__
818 if (ng < NUM_GPR_ARG_REGISTERS)
819 {
820 avalue[i] = (char *) pgr + 3;
821 ng++;
822 pgr++;
823 }
824 else
825 {
826 avalue[i] = (char *) pst + 3;
827 pst++;
828 }
829 break;
830 #endif
831
832 case FFI_TYPE_SINT16:
833 case FFI_TYPE_UINT16:
834 #ifndef __LITTLE_ENDIAN__
835 if (ng < NUM_GPR_ARG_REGISTERS)
836 {
837 avalue[i] = (char *) pgr + 2;
838 ng++;
839 pgr++;
840 }
841 else
842 {
843 avalue[i] = (char *) pst + 2;
844 pst++;
845 }
846 break;
847 #endif
848
849 case FFI_TYPE_SINT32:
850 case FFI_TYPE_UINT32:
851 case FFI_TYPE_POINTER:
852 if (ng < NUM_GPR_ARG_REGISTERS)
853 {
854 avalue[i] = pgr;
855 ng++;
856 pgr++;
857 }
858 else
859 {
860 avalue[i] = pst;
861 pst++;
862 }
863 break;
864
865 case FFI_TYPE_STRUCT:
866 /* Structs are passed by reference. The address will appear in a
867 gpr if it is one of the first 8 arguments. */
868 if (ng < NUM_GPR_ARG_REGISTERS)
869 {
870 avalue[i] = (void *) *pgr;
871 ng++;
872 pgr++;
873 }
874 else
875 {
876 avalue[i] = (void *) *pst;
877 pst++;
878 }
879 break;
880
881 case FFI_TYPE_SINT64:
882 case FFI_TYPE_UINT64:
883 /* Passing long long ints are complex, they must
884 be passed in suitable register pairs such as
885 (r3,r4) or (r5,r6) or (r6,r7), or (r7,r8) or (r9,r10)
886 and if the entire pair aren't available then the outgoing
887 parameter stack is used for both but an alignment of 8
888 must will be kept. So we must either look in pgr
889 or pst to find the correct address for this type
890 of parameter. */
891 if (ng < NUM_GPR_ARG_REGISTERS - 1)
892 {
893 if (ng & 1)
894 {
895 /* skip r4, r6, r8 as starting points */
896 ng++;
897 pgr++;
898 }
899 avalue[i] = pgr;
900 ng += 2;
901 pgr += 2;
902 }
903 else
904 {
905 if (((long) pst) & 4)
906 pst++;
907 avalue[i] = pst;
908 pst += 2;
909 ng = NUM_GPR_ARG_REGISTERS;
910 }
911 break;
912
913 default:
914 FFI_ASSERT (0);
915 }
916
917 i++;
918 }
919
920 (*fun) (cif, rvalue, avalue, user_data);
921
922 /* Tell ffi_closure_SYSV how to perform return type promotions.
923 Because the FFI_SYSV ABI returns the structures <= 8 bytes in
924 r3/r4 we have to tell ffi_closure_SYSV how to treat them. We
925 combine the base type FFI_SYSV_TYPE_SMALL_STRUCT with the size of
926 the struct less one. We never have a struct with size zero.
927 See the comment in ffitarget.h about ordering. */
928 if (rtypenum == FFI_TYPE_STRUCT
929 && (cif->abi & FFI_SYSV_STRUCT_RET) != 0 && size <= 8)
930 return FFI_SYSV_TYPE_SMALL_STRUCT - 1 + size;
931 return rtypenum;
932 }
933 #endif