1
#if defined(__ppc__) || defined(__ppc64__)
3
/* -----------------------------------------------------------------------
4
ffi.c - Copyright (c) 1998 Geoffrey Keating
6
PowerPC Foreign Function Interface
8
Darwin ABI support (c) 2001 John Hornkvist
9
AIX ABI support (c) 2002 Free Software Foundation, Inc.
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:
19
The above copyright notice and this permission notice shall be included
20
in all copies or substantial portions of the Software.
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
----------------------------------------------------------------------- */
32
#include "ffi_common.h"
37
#include "ppc-darwin.h"
38
#include <architecture/ppc/mode_independent_asm.h>
41
#if defined(POWERPC_DARWIN)
42
#include <libkern/OSCacheControl.h> // for sys_icache_invalidate()
47
/* Explicit prototype instead of including a header to allow compilation
51
#pragma weak sys_icache_invalidate
52
extern void sys_icache_invalidate(void *start, size_t len);
56
extern void ffi_closure_ASM(void);
58
// The layout of a function descriptor. A C function pointer really
59
// points to one of these.
60
typedef struct aix_fd_struct {
65
/* ffi_prep_args is called by the assembly routine once stack space
66
has been allocated for the function's arguments.
68
The stack layout we want looks like this:
70
| Return address from ffi_call_DARWIN | higher addresses
71
|--------------------------------------------|
72
| Previous backchain pointer 4/8 | stack pointer here
73
|--------------------------------------------|-\ <<< on entry to
74
| Saved r28-r31 (4/8)*4 | | ffi_call_DARWIN
75
|--------------------------------------------| |
76
| Parameters (at least 8*(4/8)=32/64) | | (176) +112 - +288
77
|--------------------------------------------| |
78
| Space for GPR2 4/8 | |
79
|--------------------------------------------| | stack |
80
| Reserved (4/8)*2 | | grows |
81
|--------------------------------------------| | down V
82
| Space for callee's LR 4/8 | |
83
|--------------------------------------------| | lower addresses
85
|--------------------------------------------| | stack pointer here
86
| Current backchain pointer 4/8 | | during
87
|--------------------------------------------|-/ <<< ffi_call_DARWIN
89
Note: ppc64 CR is saved in the low word of a long on the stack.
96
unsigned *const stack)
99
/* Copy the ecif to a local var so we can trample the arg.
100
BC note: test this with GP later for possible problems... */
101
volatile extended_cif* ecif = inEcif;
103
const unsigned bytes = ecif->cif->bytes;
104
const unsigned flags = ecif->cif->flags;
106
/* Cast the stack arg from int* to long*. sizeof(long) == 4 in 32-bit mode
107
and 8 in 64-bit mode. */
108
unsigned long *const longStack = (unsigned long *const)stack;
110
/* 'stacktop' points at the previous backchain pointer. */
111
#if defined(__ppc64__)
112
// In ppc-darwin.s, an extra 96 bytes is reserved for the linkage area,
113
// saved registers, and an extra FPR.
114
unsigned long *const stacktop =
115
(unsigned long *)(unsigned long)((char*)longStack + bytes + 96);
116
#elif defined(__ppc__)
117
unsigned long *const stacktop = longStack + (bytes / sizeof(long));
119
#error undefined architecture
122
/* 'fpr_base' points at the space for fpr1, and grows upwards as
123
we use FPR registers. */
124
double* fpr_base = (double*)(stacktop - ASM_NEEDS_REGISTERS) -
125
NUM_FPR_ARG_REGISTERS;
127
#if defined(__ppc64__)
128
// 64-bit saves an extra register, and uses an extra FPR. Knock fpr_base
129
// down a couple pegs.
133
unsigned int fparg_count = 0;
135
/* 'next_arg' grows up as we put parameters in it. */
136
unsigned long* next_arg = longStack + 6; /* 6 reserved positions. */
140
void** p_argv = ecif->avalue;
141
unsigned long gprvalue;
142
ffi_type** ptr = ecif->cif->arg_types;
144
/* Check that everything starts aligned properly. */
145
FFI_ASSERT(stack == SF_ROUND(stack));
146
FFI_ASSERT(stacktop == SF_ROUND(stacktop));
147
FFI_ASSERT(bytes == SF_ROUND(bytes));
149
/* Deal with return values that are actually pass-by-reference.
151
Return values are referenced by r3, so r4 is the first parameter. */
153
if (flags & FLAG_RETVAL_REFERENCE)
154
*next_arg++ = (unsigned long)(char*)ecif->rvalue;
156
/* Now for the arguments. */
157
for (i = ecif->cif->nargs; i > 0; i--, ptr++, p_argv++)
159
switch ((*ptr)->type)
161
/* If a floating-point parameter appears before all of the general-
162
purpose registers are filled, the corresponding GPRs that match
163
the size of the floating-point parameter are shadowed for the
164
benefit of vararg and pre-ANSI functions. */
166
double_tmp = *(float*)*p_argv;
168
if (fparg_count < NUM_FPR_ARG_REGISTERS)
169
*fpr_base++ = double_tmp;
171
*(double*)next_arg = double_tmp;
175
FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
179
case FFI_TYPE_DOUBLE:
180
double_tmp = *(double*)*p_argv;
182
if (fparg_count < NUM_FPR_ARG_REGISTERS)
183
*fpr_base++ = double_tmp;
185
*(double*)next_arg = double_tmp;
187
next_arg += MODE_CHOICE(2,1);
189
FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
193
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
194
case FFI_TYPE_LONGDOUBLE:
195
#if defined(__ppc64__)
196
if (fparg_count < NUM_FPR_ARG_REGISTERS)
197
*(long double*)fpr_base = *(long double*)*p_argv;
198
#elif defined(__ppc__)
199
if (fparg_count < NUM_FPR_ARG_REGISTERS - 1)
200
*(long double*)fpr_base = *(long double*)*p_argv;
201
else if (fparg_count == NUM_FPR_ARG_REGISTERS - 1)
202
*(double*)fpr_base = *(double*)*p_argv;
204
#error undefined architecture
207
*(long double*)next_arg = *(long double*)*p_argv;
210
next_arg += MODE_CHOICE(4,2);
211
FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
214
#endif // FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
216
case FFI_TYPE_UINT64:
217
case FFI_TYPE_SINT64:
218
#if defined(__ppc64__)
219
gprvalue = *(long long*)*p_argv;
221
#elif defined(__ppc__)
222
*(long long*)next_arg = *(long long*)*p_argv;
226
#error undefined architecture
229
case FFI_TYPE_POINTER:
230
gprvalue = *(unsigned long*)*p_argv;
234
gprvalue = *(unsigned char*)*p_argv;
238
gprvalue = *(signed char*)*p_argv;
241
case FFI_TYPE_UINT16:
242
gprvalue = *(unsigned short*)*p_argv;
245
case FFI_TYPE_SINT16:
246
gprvalue = *(signed short*)*p_argv;
249
case FFI_TYPE_STRUCT:
251
#if defined(__ppc64__)
252
unsigned int gprSize = 0;
253
unsigned int fprSize = 0;
255
ffi64_struct_to_reg_form(*ptr, (char*)*p_argv, NULL, &fparg_count,
256
(char*)next_arg, &gprSize, (char*)fpr_base, &fprSize);
257
next_arg += gprSize / sizeof(long);
258
fpr_base += fprSize / sizeof(double);
260
#elif defined(__ppc__)
261
char* dest_cpy = (char*)next_arg;
263
/* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
264
SI 4 bytes) are aligned as if they were those modes.
265
Structures with 3 byte in size are padded upwards. */
266
unsigned size_al = (*ptr)->size;
268
/* If the first member of the struct is a double, then align
269
the struct to double-word. */
270
if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
271
size_al = ALIGN((*ptr)->size, 8);
273
if (ecif->cif->abi == FFI_DARWIN)
276
dest_cpy += 4 - size_al;
279
memcpy((char*)dest_cpy, (char*)*p_argv, size_al);
280
next_arg += (size_al + 3) / 4;
282
#error undefined architecture
288
case FFI_TYPE_UINT32:
289
case FFI_TYPE_SINT32:
290
gprvalue = *(unsigned*)*p_argv;
293
*next_arg++ = gprvalue;
301
/* Check that we didn't overrun the stack... */
302
//FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
303
//FFI_ASSERT((unsigned *)fpr_base
304
// <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
305
//FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
308
#if defined(__ppc64__)
311
ffi64_struct_contains_fp(
312
const ffi_type* inType)
314
bool containsFP = false;
317
for (i = 0; inType->elements[i] != NULL && !containsFP; i++)
319
if (inType->elements[i]->type == FFI_TYPE_FLOAT ||
320
inType->elements[i]->type == FFI_TYPE_DOUBLE ||
321
inType->elements[i]->type == FFI_TYPE_LONGDOUBLE)
323
else if (inType->elements[i]->type == FFI_TYPE_STRUCT)
324
containsFP = ffi64_struct_contains_fp(inType->elements[i]);
330
#endif // defined(__ppc64__)
332
/* Perform machine dependent cif processing. */
334
ffi_prep_cif_machdep(
337
/* All this is for the DARWIN ABI. */
340
int intarg_count = 0;
342
unsigned int flags = 0;
343
unsigned int size_al = 0;
345
/* All the machine-independent calculation of cif->bytes will be wrong.
346
Redo the calculation for DARWIN. */
348
/* Space for the frame pointer, callee's LR, CR, etc, and for
349
the asm's temp regs. */
350
unsigned int bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
352
/* Return value handling. The rules are as follows:
353
- 32-bit (or less) integer values are returned in gpr3;
354
- Structures of size <= 4 bytes also returned in gpr3;
355
- 64-bit integer values and structures between 5 and 8 bytes are
356
returned in gpr3 and gpr4;
357
- Single/double FP values are returned in fpr1;
358
- Long double FP (if not equivalent to double) values are returned in
360
- Larger structures values are allocated space and a pointer is passed
361
as the first argument. */
362
switch (cif->rtype->type)
364
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
365
case FFI_TYPE_LONGDOUBLE:
366
flags |= FLAG_RETURNS_128BITS;
367
flags |= FLAG_RETURNS_FP;
369
#endif // FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
371
case FFI_TYPE_DOUBLE:
372
flags |= FLAG_RETURNS_64BITS;
375
flags |= FLAG_RETURNS_FP;
378
#if defined(__ppc64__)
379
case FFI_TYPE_POINTER:
381
case FFI_TYPE_UINT64:
382
case FFI_TYPE_SINT64:
383
flags |= FLAG_RETURNS_64BITS;
386
case FFI_TYPE_STRUCT:
388
#if defined(__ppc64__)
390
if (ffi64_stret_needs_ptr(cif->rtype, NULL, NULL))
392
flags |= FLAG_RETVAL_REFERENCE;
393
flags |= FLAG_RETURNS_NOTHING;
398
flags |= FLAG_RETURNS_STRUCT;
400
if (ffi64_struct_contains_fp(cif->rtype))
401
flags |= FLAG_STRUCT_CONTAINS_FP;
404
#elif defined(__ppc__)
406
flags |= FLAG_RETVAL_REFERENCE;
407
flags |= FLAG_RETURNS_NOTHING;
411
#error undefined architecture
417
flags |= FLAG_RETURNS_NOTHING;
421
/* Returns 32-bit integer, or similar. Nothing to do here. */
425
/* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
426
first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
427
goes on the stack. Structures are passed as a pointer to a copy of
428
the structure. Stuff on the stack needs to keep proper alignment. */
429
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
431
switch ((*ptr)->type)
434
case FFI_TYPE_DOUBLE:
436
/* If this FP arg is going on the stack, it must be
438
if (fparg_count > NUM_FPR_ARG_REGISTERS
439
&& intarg_count % 2 != 0)
443
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
444
case FFI_TYPE_LONGDOUBLE:
446
/* If this FP arg is going on the stack, it must be
450
#if defined(__ppc64__)
451
fparg_count > NUM_FPR_ARG_REGISTERS + 1
452
#elif defined(__ppc__)
453
fparg_count > NUM_FPR_ARG_REGISTERS
455
#error undefined architecture
457
&& intarg_count % 2 != 0)
462
#endif // FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
464
case FFI_TYPE_UINT64:
465
case FFI_TYPE_SINT64:
466
/* 'long long' arguments are passed as two words, but
467
either both words must fit in registers or both go
468
on the stack. If they go on the stack, they must
469
be 8-byte-aligned. */
470
if (intarg_count == NUM_GPR_ARG_REGISTERS - 1
471
|| (intarg_count >= NUM_GPR_ARG_REGISTERS
472
&& intarg_count % 2 != 0))
475
intarg_count += MODE_CHOICE(2,1);
479
case FFI_TYPE_STRUCT:
480
size_al = (*ptr)->size;
481
/* If the first member of the struct is a double, then align
482
the struct to double-word. */
483
if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
484
size_al = ALIGN((*ptr)->size, 8);
486
#if defined(__ppc64__)
487
// Look for FP struct members.
490
for (j = 0; (*ptr)->elements[j] != NULL; j++)
492
if ((*ptr)->elements[j]->type == FFI_TYPE_FLOAT ||
493
(*ptr)->elements[j]->type == FFI_TYPE_DOUBLE)
497
if (fparg_count > NUM_FPR_ARG_REGISTERS)
500
else if ((*ptr)->elements[j]->type == FFI_TYPE_LONGDOUBLE)
504
if (fparg_count > NUM_FPR_ARG_REGISTERS + 1)
510
#elif defined(__ppc__)
511
intarg_count += (size_al + 3) / 4;
513
#error undefined architecture
519
/* Everything else is passed as a 4/8-byte word in a GPR, either
520
the object itself or a pointer to it. */
526
/* Space for the FPR registers, if needed. */
527
if (fparg_count != 0)
529
flags |= FLAG_FP_ARGUMENTS;
530
#if defined(__ppc64__)
531
bytes += (NUM_FPR_ARG_REGISTERS + 1) * sizeof(double);
532
#elif defined(__ppc__)
533
bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
535
#error undefined architecture
540
#if defined(__ppc64__)
541
if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS)
542
bytes += (intarg_count + fparg_count) * sizeof(long);
543
#elif defined(__ppc__)
544
if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
545
bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
547
#error undefined architecture
550
bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
552
/* The stack space allocated needs to be a multiple of 16/32 bytes. */
553
bytes = SF_ROUND(bytes);
565
/*@out@*/ extended_cif*,
570
void (*fn2)(extended_cif*, unsigned *const));
574
/*@out@*/ extended_cif*,
579
void (*fn2)(extended_cif*, unsigned *const));
585
/*@dependent@*/ ffi_cif* cif,
587
/*@out@*/ void* rvalue,
588
/*@dependent@*/ void** avalue)
593
ecif.avalue = avalue;
595
/* If the return value is a struct and we don't have a return
596
value address then we need to make one. */
597
if ((rvalue == NULL) &&
598
(cif->rtype->type == FFI_TYPE_STRUCT))
601
ecif.rvalue = alloca(cif->rtype->size);
605
ecif.rvalue = rvalue;
611
ffi_call_AIX(&ecif, -cif->bytes,
612
cif->flags, ecif.rvalue, fn, ffi_prep_args);
618
ffi_call_DARWIN(&ecif, -(long)cif->bytes,
619
cif->flags, ecif.rvalue, fn, ffi_prep_args);
629
/* here I'd like to add the stack frame layout we use in darwin_closure.S
632
SP previous -> +---------------------------------------+ <--- child frame
633
| back chain to caller 4 |
634
+---------------------------------------+ 4
636
+---------------------------------------+ 8
638
+---------------------------------------+ 12
639
| reserved for compilers 4 |
640
+---------------------------------------+ 16
641
| reserved for binders 4 |
642
+---------------------------------------+ 20
643
| saved TOC pointer 4 |
644
+---------------------------------------+ 24
645
| always reserved 8*4=32 (previous GPRs)|
646
| according to the linkage convention |
648
+---------------------------------------+ 56
649
| our FPR area 13*8=104 |
653
+---------------------------------------+ 160
655
+---------------------------------------+ 168
656
| alignement to the next multiple of 16 |
657
SP current --> +---------------------------------------+ 176 <- parent frame
658
| back chain to caller 4 |
659
+---------------------------------------+ 180
661
+---------------------------------------+ 184
663
+---------------------------------------+ 188
664
| reserved for compilers 4 |
665
+---------------------------------------+ 192
666
| reserved for binders 4 |
667
+---------------------------------------+ 196
668
| saved TOC pointer 4 |
669
+---------------------------------------+ 200
670
| always reserved 8*4=32 we store our |
675
+---------------------------------------+ 232
677
+---------------------------------------+ xxx
679
+---------------------------------------+ xxx
682
#if !defined(POWERPC_DARWIN)
684
#define MIN_LINE_SIZE 32
697
: : "r" (addr) : "memory");
708
for (i = 0; i < size; i += MIN_LINE_SIZE)
709
flush_icache(addr + i);
711
flush_icache(addr + size - 1);
714
#endif // !defined(POWERPC_DARWIN)
718
ffi_closure* closure,
720
void (*fun)(ffi_cif*, void*, void**, void*),
727
FFI_ASSERT (cif->abi == FFI_DARWIN);
729
unsigned int* tramp = (unsigned int*)&closure->tramp[0];
731
#if defined(__ppc64__)
732
tramp[0] = 0x7c0802a6; // mflr r0
733
tramp[1] = 0x429f0005; // bcl 20,31,+0x8
734
tramp[2] = 0x7d6802a6; // mflr r11
735
tramp[3] = 0x7c0803a6; // mtlr r0
736
tramp[4] = 0xe98b0018; // ld r12,24(r11)
737
tramp[5] = 0x7d8903a6; // mtctr r12
738
tramp[6] = 0xe96b0020; // ld r11,32(r11)
739
tramp[7] = 0x4e800420; // bctr
740
*(unsigned long*)&tramp[8] = (unsigned long)ffi_closure_ASM;
741
*(unsigned long*)&tramp[10] = (unsigned long)closure;
742
#elif defined(__ppc__)
743
tramp[0] = 0x7c0802a6; // mflr r0
744
tramp[1] = 0x429f0005; // bcl 20,31,+0x8
745
tramp[2] = 0x7d6802a6; // mflr r11
746
tramp[3] = 0x7c0803a6; // mtlr r0
747
tramp[4] = 0x818b0018; // lwz r12,24(r11)
748
tramp[5] = 0x7d8903a6; // mtctr r12
749
tramp[6] = 0x816b001c; // lwz r11,28(r11)
750
tramp[7] = 0x4e800420; // bctr
751
tramp[8] = (unsigned long)ffi_closure_ASM;
752
tramp[9] = (unsigned long)closure;
754
#error undefined architecture
759
closure->user_data = user_data;
761
// Flush the icache. Only necessary on Darwin.
762
#if defined(POWERPC_DARWIN)
763
if (sys_icache_invalidate) {
764
sys_icache_invalidate(closure->tramp, FFI_TRAMPOLINE_SIZE);
767
flush_range(closure->tramp, FFI_TRAMPOLINE_SIZE);
775
FFI_ASSERT (cif->abi == FFI_AIX);
777
ffi_aix_trampoline_struct* tramp_aix =
778
(ffi_aix_trampoline_struct*)(closure->tramp);
779
aix_fd* fd = (aix_fd*)(void*)ffi_closure_ASM;
781
tramp_aix->code_pointer = fd->code_pointer;
782
tramp_aix->toc = fd->toc;
783
tramp_aix->static_chain = closure;
786
closure->user_data = user_data;
798
typedef double ldbits[2];
807
/* The trampoline invokes ffi_closure_ASM, and on entry, r11 holds the
808
address of the closure. After storing the registers that could possibly
809
contain parameters to be passed into the stack frame and setting up space
810
for a return value, ffi_closure_ASM invokes the following helper function
811
to do most of the work. */
813
ffi_closure_helper_DARWIN(
814
ffi_closure* closure,
819
/* rvalue is the pointer to space for return value in closure assembly
820
pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
821
pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
829
unsigned int nf = 0; /* number of FPRs already used. */
830
unsigned int ng = 0; /* number of GPRs already used. */
831
ffi_cif* cif = closure->cif;
832
unsigned int avn = cif->nargs;
833
void** avalue = alloca(cif->nargs * sizeof(void*));
834
ffi_type** arg_types = cif->arg_types;
836
/* Copy the caller's structure return value address so that the closure
837
returns the data directly to the caller. */
838
#if defined(__ppc64__)
839
if (cif->rtype->type == FFI_TYPE_STRUCT &&
840
ffi64_stret_needs_ptr(cif->rtype, NULL, NULL))
841
#elif defined(__ppc__)
842
if (cif->rtype->type == FFI_TYPE_STRUCT)
844
#error undefined architecture
847
rvalue = (void*)*pgr;
852
/* Grab the addresses of the arguments from the stack frame. */
853
for (i = 0; i < avn; i++)
855
switch (arg_types[i]->type)
859
avalue[i] = (char*)pgr + MODE_CHOICE(3,7);
864
case FFI_TYPE_SINT16:
865
case FFI_TYPE_UINT16:
866
avalue[i] = (char*)pgr + MODE_CHOICE(2,6);
872
case FFI_TYPE_POINTER:
874
case FFI_TYPE_SINT32:
875
case FFI_TYPE_UINT32:
876
avalue[i] = (char*)pgr + MODE_CHOICE(0,4);
882
case FFI_TYPE_STRUCT:
883
if (cif->abi == FFI_DARWIN)
885
#if defined(__ppc64__)
886
unsigned int gprSize = 0;
887
unsigned int fprSize = 0;
888
unsigned int savedFPRSize = fprSize;
890
avalue[i] = alloca(arg_types[i]->size);
891
ffi64_struct_to_ram_form(arg_types[i], (const char*)pgr,
892
&gprSize, (const char*)pfr, &fprSize, &nf, avalue[i], NULL);
894
ng += gprSize / sizeof(long);
895
pgr += gprSize / sizeof(long);
896
pfr += (fprSize - savedFPRSize) / sizeof(double);
898
#elif defined(__ppc__)
899
/* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
900
SI 4 bytes) are aligned as if they were those modes. */
901
unsigned int size_al = size_al = arg_types[i]->size;
903
/* If the first member of the struct is a double, then align
904
the struct to double-word. */
905
if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
906
size_al = ALIGN(arg_types[i]->size, 8);
909
avalue[i] = (char*)pgr + MODE_CHOICE(4,8) - size_al;
911
avalue[i] = (char*)pgr;
913
ng += (size_al + 3) / sizeof(long);
914
pgr += (size_al + 3) / sizeof(long);
916
#error undefined architecture
922
#if defined(__ppc64__)
923
case FFI_TYPE_POINTER:
925
case FFI_TYPE_SINT64:
926
case FFI_TYPE_UINT64:
927
/* Long long ints are passed in 1 or 2 GPRs. */
929
ng += MODE_CHOICE(2,1);
930
pgr += MODE_CHOICE(2,1);
935
/* A float value consumes a GPR.
936
There are 13 64-bit floating point registers. */
937
if (nf < NUM_FPR_ARG_REGISTERS)
940
pfr->f = (float)temp;
952
case FFI_TYPE_DOUBLE:
953
/* A double value consumes one or two GPRs.
954
There are 13 64bit floating point registers. */
955
if (nf < NUM_FPR_ARG_REGISTERS)
964
ng += MODE_CHOICE(2,1);
965
pgr += MODE_CHOICE(2,1);
969
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
971
case FFI_TYPE_LONGDOUBLE:
972
#if defined(__ppc64__)
973
if (nf < NUM_FPR_ARG_REGISTERS)
978
#elif defined(__ppc__)
979
/* A long double value consumes 2/4 GPRs and 2 FPRs.
980
There are 13 64bit floating point registers. */
981
if (nf < NUM_FPR_ARG_REGISTERS - 1)
986
/* Here we have the situation where one part of the long double
987
is stored in fpr13 and the other part is already on the stack.
988
We use a union to pass the long double to avalue[i]. */
989
else if (nf == NUM_FPR_ARG_REGISTERS - 1)
991
memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits));
992
memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits));
993
avalue[i] = &temp_ld.ld;
996
#error undefined architecture
1002
ng += MODE_CHOICE(4,2);
1003
pgr += MODE_CHOICE(4,2);
1007
#endif /* FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE */
1015
(closure->fun)(cif, rvalue, avalue, closure->user_data);
1017
/* Tell ffi_closure_ASM to perform return type promotions. */
1018
return cif->rtype->type;
1021
#if defined(__ppc64__)
1023
/* ffi64_struct_to_ram_form
1025
Rebuild a struct's natural layout from buffers of concatenated registers.
1026
Return the number of registers used.
1027
inGPRs[0-7] == r3, inFPRs[0-7] == f1 ...
1030
ffi64_struct_to_ram_form(
1031
const ffi_type* inType,
1033
unsigned int* ioGPRMarker,
1035
unsigned int* ioFPRMarker,
1036
unsigned int* ioFPRsUsed,
1037
char* outStruct, // caller-allocated
1038
unsigned int* ioStructMarker)
1040
unsigned int srcGMarker = 0;
1041
unsigned int srcFMarker = 0;
1042
unsigned int savedFMarker = 0;
1043
unsigned int fprsUsed = 0;
1044
unsigned int savedFPRsUsed = 0;
1045
unsigned int destMarker = 0;
1047
static unsigned int recurseCount = 0;
1050
srcGMarker = *ioGPRMarker;
1054
srcFMarker = *ioFPRMarker;
1055
savedFMarker = srcFMarker;
1060
fprsUsed = *ioFPRsUsed;
1061
savedFPRsUsed = fprsUsed;
1065
destMarker = *ioStructMarker;
1069
switch (inType->size)
1071
case 1: case 2: case 4:
1072
srcGMarker += 8 - inType->size;
1079
for (i = 0; inType->elements[i] != NULL; i++)
1081
switch (inType->elements[i]->type)
1083
case FFI_TYPE_FLOAT:
1084
srcFMarker = ALIGN(srcFMarker, 4);
1085
srcGMarker = ALIGN(srcGMarker, 4);
1086
destMarker = ALIGN(destMarker, 4);
1088
if (fprsUsed < NUM_FPR_ARG_REGISTERS)
1090
*(float*)&outStruct[destMarker] =
1091
(float)*(double*)&inFPRs[srcFMarker];
1096
*(float*)&outStruct[destMarker] =
1097
(float)*(double*)&inGPRs[srcGMarker];
1102
// Skip to next GPR if next element won't fit and we're
1103
// not already at a register boundary.
1104
if (inType->elements[i + 1] != NULL && (destMarker % 8))
1106
if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) &&
1107
(!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) ||
1108
(ALIGN(srcGMarker, 8) - srcGMarker) < 2) &&
1109
(!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) ||
1110
(ALIGN(srcGMarker, 8) - srcGMarker) < 4))
1111
srcGMarker = ALIGN(srcGMarker, 8);
1116
case FFI_TYPE_DOUBLE:
1117
srcFMarker = ALIGN(srcFMarker, 8);
1118
destMarker = ALIGN(destMarker, 8);
1120
if (fprsUsed < NUM_FPR_ARG_REGISTERS)
1122
*(double*)&outStruct[destMarker] =
1123
*(double*)&inFPRs[srcFMarker];
1128
*(double*)&outStruct[destMarker] =
1129
*(double*)&inGPRs[srcGMarker];
1135
srcGMarker = ALIGN(srcGMarker, 8);
1139
case FFI_TYPE_LONGDOUBLE:
1140
destMarker = ALIGN(destMarker, 16);
1142
if (fprsUsed < NUM_FPR_ARG_REGISTERS)
1144
srcFMarker = ALIGN(srcFMarker, 8);
1145
srcGMarker = ALIGN(srcGMarker, 8);
1146
*(long double*)&outStruct[destMarker] =
1147
*(long double*)&inFPRs[srcFMarker];
1153
srcFMarker = ALIGN(srcFMarker, 16);
1154
srcGMarker = ALIGN(srcGMarker, 16);
1155
*(long double*)&outStruct[destMarker] =
1156
*(long double*)&inGPRs[srcGMarker];
1163
srcGMarker = ALIGN(srcGMarker, 8);
1167
case FFI_TYPE_UINT8:
1168
case FFI_TYPE_SINT8:
1170
if (inType->alignment == 1) // chars only
1172
if (inType->size == 1)
1173
outStruct[destMarker++] = inGPRs[srcGMarker++];
1174
else if (inType->size == 2)
1176
outStruct[destMarker++] = inGPRs[srcGMarker++];
1177
outStruct[destMarker++] = inGPRs[srcGMarker++];
1182
memcpy(&outStruct[destMarker],
1183
&inGPRs[srcGMarker], inType->size);
1184
srcGMarker += inType->size;
1185
destMarker += inType->size;
1186
i += inType->size - 1;
1189
else // chars and other stuff
1191
outStruct[destMarker++] = inGPRs[srcGMarker++];
1193
// Skip to next GPR if next element won't fit and we're
1194
// not already at a register boundary.
1195
if (inType->elements[i + 1] != NULL && (srcGMarker % 8))
1197
if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) &&
1198
(!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) ||
1199
(ALIGN(srcGMarker, 8) - srcGMarker) < 2) &&
1200
(!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) ||
1201
(ALIGN(srcGMarker, 8) - srcGMarker) < 4))
1202
srcGMarker = ALIGN(srcGMarker, inType->alignment); // was 8
1209
case FFI_TYPE_UINT16:
1210
case FFI_TYPE_SINT16:
1211
srcGMarker = ALIGN(srcGMarker, 2);
1212
destMarker = ALIGN(destMarker, 2);
1214
*(short*)&outStruct[destMarker] =
1215
*(short*)&inGPRs[srcGMarker];
1222
case FFI_TYPE_UINT32:
1223
case FFI_TYPE_SINT32:
1224
srcGMarker = ALIGN(srcGMarker, 4);
1225
destMarker = ALIGN(destMarker, 4);
1227
*(int*)&outStruct[destMarker] =
1228
*(int*)&inGPRs[srcGMarker];
1234
case FFI_TYPE_POINTER:
1235
case FFI_TYPE_UINT64:
1236
case FFI_TYPE_SINT64:
1237
srcGMarker = ALIGN(srcGMarker, 8);
1238
destMarker = ALIGN(destMarker, 8);
1240
*(long long*)&outStruct[destMarker] =
1241
*(long long*)&inGPRs[srcGMarker];
1247
case FFI_TYPE_STRUCT:
1249
ffi64_struct_to_ram_form(inType->elements[i], inGPRs,
1250
&srcGMarker, inFPRs, &srcFMarker, &fprsUsed,
1251
outStruct, &destMarker);
1256
FFI_ASSERT(0); // unknown element type
1261
srcGMarker = ALIGN(srcGMarker, inType->alignment);
1263
// Take care of the special case for 16-byte structs, but not for
1265
if (recurseCount == 0 && srcGMarker == 16)
1267
*(long double*)&outStruct[0] = *(long double*)&inGPRs[0];
1268
srcFMarker = savedFMarker;
1269
fprsUsed = savedFPRsUsed;
1273
*ioGPRMarker = ALIGN(srcGMarker, 8);
1276
*ioFPRMarker = srcFMarker;
1279
*ioFPRsUsed = fprsUsed;
1282
*ioStructMarker = ALIGN(destMarker, 8);
1285
/* ffi64_struct_to_reg_form
1287
Copy a struct's elements into buffers that can be sliced into registers.
1288
Return the sizes of the output buffers in bytes. Pass NULL buffer pointers
1289
to calculate size only.
1290
outGPRs[0-7] == r3, outFPRs[0-7] == f1 ...
1293
ffi64_struct_to_reg_form(
1294
const ffi_type* inType,
1295
const char* inStruct,
1296
unsigned int* ioStructMarker,
1297
unsigned int* ioFPRsUsed,
1298
char* outGPRs, // caller-allocated
1299
unsigned int* ioGPRSize,
1300
char* outFPRs, // caller-allocated
1301
unsigned int* ioFPRSize)
1304
unsigned int srcMarker = 0;
1305
unsigned int destGMarker = 0;
1306
unsigned int destFMarker = 0;
1307
unsigned int savedFMarker = 0;
1308
unsigned int fprsUsed = 0;
1309
unsigned int savedFPRsUsed = 0;
1311
static unsigned int recurseCount = 0;
1314
srcMarker = *ioStructMarker;
1318
fprsUsed = *ioFPRsUsed;
1319
savedFPRsUsed = fprsUsed;
1323
destGMarker = *ioGPRSize;
1327
destFMarker = *ioFPRSize;
1328
savedFMarker = destFMarker;
1331
switch (inType->size)
1333
case 1: case 2: case 4:
1334
destGMarker += 8 - inType->size;
1341
for (i = 0; inType->elements[i] != NULL; i++)
1343
switch (inType->elements[i]->type)
1345
// Shadow floating-point types in GPRs for vararg and pre-ANSI
1347
case FFI_TYPE_FLOAT:
1348
// Nudge markers to next 4/8-byte boundary
1349
srcMarker = ALIGN(srcMarker, 4);
1350
destGMarker = ALIGN(destGMarker, 4);
1351
destFMarker = ALIGN(destFMarker, 8);
1353
if (fprsUsed < NUM_FPR_ARG_REGISTERS)
1355
if (outFPRs != NULL && inStruct != NULL)
1356
*(double*)&outFPRs[destFMarker] =
1357
(double)*(float*)&inStruct[srcMarker];
1363
if (outGPRs != NULL && inStruct != NULL)
1364
*(double*)&outGPRs[destGMarker] =
1365
(double)*(float*)&inStruct[srcMarker];
1370
// Skip to next GPR if next element won't fit and we're
1371
// not already at a register boundary.
1372
if (inType->elements[i + 1] != NULL && (srcMarker % 8))
1374
if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) &&
1375
(!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) ||
1376
(ALIGN(destGMarker, 8) - destGMarker) < 2) &&
1377
(!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) ||
1378
(ALIGN(destGMarker, 8) - destGMarker) < 4))
1379
destGMarker = ALIGN(destGMarker, 8);
1384
case FFI_TYPE_DOUBLE:
1385
srcMarker = ALIGN(srcMarker, 8);
1386
destFMarker = ALIGN(destFMarker, 8);
1388
if (fprsUsed < NUM_FPR_ARG_REGISTERS)
1390
if (outFPRs != NULL && inStruct != NULL)
1391
*(double*)&outFPRs[destFMarker] =
1392
*(double*)&inStruct[srcMarker];
1398
if (outGPRs != NULL && inStruct != NULL)
1399
*(double*)&outGPRs[destGMarker] =
1400
*(double*)&inStruct[srcMarker];
1406
destGMarker = ALIGN(destGMarker, 8);
1410
case FFI_TYPE_LONGDOUBLE:
1411
srcMarker = ALIGN(srcMarker, 16);
1413
if (fprsUsed < NUM_FPR_ARG_REGISTERS)
1415
destFMarker = ALIGN(destFMarker, 8);
1416
destGMarker = ALIGN(destGMarker, 8);
1418
if (outFPRs != NULL && inStruct != NULL)
1419
*(long double*)&outFPRs[destFMarker] =
1420
*(long double*)&inStruct[srcMarker];
1422
if (outGPRs != NULL && inStruct != NULL)
1423
*(long double*)&outGPRs[destGMarker] =
1424
*(long double*)&inStruct[srcMarker];
1431
destGMarker = ALIGN(destGMarker, 16);
1433
if (outGPRs != NULL && inStruct != NULL)
1434
*(long double*)&outGPRs[destGMarker] =
1435
*(long double*)&inStruct[srcMarker];
1439
destGMarker += 16; // Skip next 2 GPRs
1440
destGMarker = ALIGN(destGMarker, 8); // was 16
1444
case FFI_TYPE_UINT8:
1445
case FFI_TYPE_SINT8:
1446
if (inType->alignment == 1) // bytes only
1448
if (inType->size == 1)
1450
if (outGPRs != NULL && inStruct != NULL)
1451
outGPRs[destGMarker] = inStruct[srcMarker];
1456
else if (inType->size == 2)
1458
if (outGPRs != NULL && inStruct != NULL)
1460
outGPRs[destGMarker] = inStruct[srcMarker];
1461
outGPRs[destGMarker + 1] = inStruct[srcMarker + 1];
1471
if (outGPRs != NULL && inStruct != NULL)
1473
// Avoid memcpy for small chunks.
1474
if (inType->size <= sizeof(long))
1475
*(long*)&outGPRs[destGMarker] =
1476
*(long*)&inStruct[srcMarker];
1478
memcpy(&outGPRs[destGMarker],
1479
&inStruct[srcMarker], inType->size);
1482
srcMarker += inType->size;
1483
destGMarker += inType->size;
1484
i += inType->size - 1;
1487
else // bytes and other stuff
1489
if (outGPRs != NULL && inStruct != NULL)
1490
outGPRs[destGMarker] = inStruct[srcMarker];
1495
// Skip to next GPR if next element won't fit and we're
1496
// not already at a register boundary.
1497
if (inType->elements[i + 1] != NULL && (destGMarker % 8))
1499
if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) &&
1500
(!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) ||
1501
(ALIGN(destGMarker, 8) - destGMarker) < 2) &&
1502
(!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) ||
1503
(ALIGN(destGMarker, 8) - destGMarker) < 4))
1504
destGMarker = ALIGN(destGMarker, inType->alignment); // was 8
1510
case FFI_TYPE_UINT16:
1511
case FFI_TYPE_SINT16:
1512
srcMarker = ALIGN(srcMarker, 2);
1513
destGMarker = ALIGN(destGMarker, 2);
1515
if (outGPRs != NULL && inStruct != NULL)
1516
*(short*)&outGPRs[destGMarker] =
1517
*(short*)&inStruct[srcMarker];
1522
if (inType->elements[i + 1] == NULL)
1523
destGMarker = ALIGN(destGMarker, inType->alignment);
1528
case FFI_TYPE_UINT32:
1529
case FFI_TYPE_SINT32:
1530
srcMarker = ALIGN(srcMarker, 4);
1531
destGMarker = ALIGN(destGMarker, 4);
1533
if (outGPRs != NULL && inStruct != NULL)
1534
*(int*)&outGPRs[destGMarker] =
1535
*(int*)&inStruct[srcMarker];
1542
case FFI_TYPE_POINTER:
1543
case FFI_TYPE_UINT64:
1544
case FFI_TYPE_SINT64:
1545
srcMarker = ALIGN(srcMarker, 8);
1546
destGMarker = ALIGN(destGMarker, 8);
1548
if (outGPRs != NULL && inStruct != NULL)
1549
*(long long*)&outGPRs[destGMarker] =
1550
*(long long*)&inStruct[srcMarker];
1555
if (inType->elements[i + 1] == NULL)
1556
destGMarker = ALIGN(destGMarker, inType->alignment);
1560
case FFI_TYPE_STRUCT:
1562
ffi64_struct_to_reg_form(inType->elements[i],
1563
inStruct, &srcMarker, &fprsUsed, outGPRs,
1564
&destGMarker, outFPRs, &destFMarker);
1574
destGMarker = ALIGN(destGMarker, inType->alignment);
1576
// Take care of the special case for 16-byte structs, but not for
1578
if (recurseCount == 0 && destGMarker == 16)
1580
if (outGPRs != NULL && inStruct != NULL)
1581
*(long double*)&outGPRs[0] = *(long double*)&inStruct[0];
1583
destFMarker = savedFMarker;
1584
fprsUsed = savedFPRsUsed;
1588
*ioStructMarker = ALIGN(srcMarker, 8);
1591
*ioFPRsUsed = fprsUsed;
1594
*ioGPRSize = ALIGN(destGMarker, 8);
1597
*ioFPRSize = ALIGN(destFMarker, 8);
1600
/* ffi64_stret_needs_ptr
1602
Determine whether a returned struct needs a pointer in r3 or can fit
1607
ffi64_stret_needs_ptr(
1608
const ffi_type* inType,
1609
unsigned short* ioGPRCount,
1610
unsigned short* ioFPRCount)
1612
// Obvious case first- struct is larger than combined FPR size.
1613
if (inType->size > 14 * 8)
1616
// Now the struct can physically fit in registers, determine if it
1617
// also fits logically.
1618
bool needsPtr = false;
1619
unsigned short gprsUsed = 0;
1620
unsigned short fprsUsed = 0;
1624
gprsUsed = *ioGPRCount;
1627
fprsUsed = *ioFPRCount;
1629
for (i = 0; inType->elements[i] != NULL && !needsPtr; i++)
1631
switch (inType->elements[i]->type)
1633
case FFI_TYPE_FLOAT:
1634
case FFI_TYPE_DOUBLE:
1643
case FFI_TYPE_LONGDOUBLE:
1652
case FFI_TYPE_UINT8:
1653
case FFI_TYPE_SINT8:
1663
if (inType->elements[i + 1] == NULL) // last byte in the struct
1666
// Count possible contiguous bytes ahead, up to 8.
1669
for (j = 1; j < 8; j++)
1671
if (inType->elements[i + j] == NULL ||
1672
!FFI_TYPE_1_BYTE(inType->elements[i + j]->type))
1676
i += j - 1; // allow for i++ before the test condition
1681
case FFI_TYPE_UINT16:
1682
case FFI_TYPE_SINT16:
1684
case FFI_TYPE_UINT32:
1685
case FFI_TYPE_SINT32:
1686
case FFI_TYPE_POINTER:
1687
case FFI_TYPE_UINT64:
1688
case FFI_TYPE_SINT64:
1696
case FFI_TYPE_STRUCT:
1697
needsPtr = ffi64_stret_needs_ptr(
1698
inType->elements[i], &gprsUsed, &fprsUsed);
1709
*ioGPRCount = gprsUsed;
1712
*ioFPRCount = fprsUsed;
1719
Calculate the size in bytes of an ffi type.
1724
const ffi_type* inType)
1726
unsigned int size = 0;
1728
switch (inType->type)
1730
case FFI_TYPE_UINT8:
1731
case FFI_TYPE_SINT8:
1735
case FFI_TYPE_UINT16:
1736
case FFI_TYPE_SINT16:
1741
case FFI_TYPE_UINT32:
1742
case FFI_TYPE_SINT32:
1743
case FFI_TYPE_FLOAT:
1747
case FFI_TYPE_POINTER:
1748
case FFI_TYPE_UINT64:
1749
case FFI_TYPE_SINT64:
1750
case FFI_TYPE_DOUBLE:
1754
case FFI_TYPE_LONGDOUBLE:
1758
case FFI_TYPE_STRUCT:
1759
ffi64_struct_to_reg_form(
1760
inType, NULL, NULL, NULL, NULL, &size, NULL, NULL);
1774
#endif /* defined(__ppc64__) */
1775
#endif /* __ppc__ || __ppc64__ */