1 /* -----------------------------------------------------------------------
2 ffi.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
9 PowerPC Foreign Function Interface
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"
33 #include "ffi_powerpc.h"
35 #if HAVE_LONG_DOUBLE_VARIANT
36 /* Adjust ffi_type_longdouble. */
38 ffi_prep_types (ffi_abi abi
)
40 # if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
42 ffi_prep_types_linux64 (abi
);
44 ffi_prep_types_sysv (abi
);
50 /* Perform machine dependent cif processing */
52 ffi_prep_cif_machdep (ffi_cif
*cif
)
55 return ffi_prep_cif_linux64 (cif
);
57 return ffi_prep_cif_sysv (cif
);
62 ffi_prep_cif_machdep_var (ffi_cif
*cif
,
63 unsigned int nfixedargs MAYBE_UNUSED
,
64 unsigned int ntotalargs MAYBE_UNUSED
)
67 return ffi_prep_cif_linux64_var (cif
, nfixedargs
, ntotalargs
);
69 return ffi_prep_cif_sysv (cif
);
74 ffi_call_int (ffi_cif
*cif
,
80 /* The final SYSV ABI says that structures smaller or equal 8 bytes
81 are returned in r3/r4. A draft ABI used by linux instead returns
84 We bounce-buffer SYSV small struct return values so that sysv.S
85 can write r3 and r4 to memory without worrying about struct size.
87 For ELFv2 ABI, use a bounce buffer for homogeneous structs too,
88 for similar reasons. */
89 unsigned long smst_buffer
[8];
96 if ((cif
->flags
& FLAG_RETURNS_SMST
) != 0)
97 ecif
.rvalue
= smst_buffer
;
98 /* Ensure that we have a valid struct return value.
99 FIXME: Isn't this just papering over a user problem? */
100 else if (!rvalue
&& cif
->rtype
->type
== FFI_TYPE_STRUCT
)
101 ecif
.rvalue
= alloca (cif
->rtype
->size
);
104 ffi_call_LINUX64 (&ecif
, fn
, ecif
.rvalue
, cif
->flags
, closure
,
107 ffi_call_SYSV (&ecif
, fn
, ecif
.rvalue
, cif
->flags
, closure
, -cif
->bytes
);
110 /* Check for a bounce-buffered return value */
111 if (rvalue
&& ecif
.rvalue
== smst_buffer
)
113 unsigned int rsize
= cif
->rtype
->size
;
114 #ifndef __LITTLE_ENDIAN__
115 /* The SYSV ABI returns a structure of up to 4 bytes in size
116 left-padded in r3. */
119 memcpy (rvalue
, (char *) smst_buffer
+ 4 - rsize
, rsize
);
122 /* The SYSV ABI returns a structure of up to 8 bytes in size
123 left-padded in r3/r4, and the ELFv2 ABI similarly returns a
124 structure of up to 8 bytes in size left-padded in r3. */
126 memcpy (rvalue
, (char *) smst_buffer
+ 8 - rsize
, rsize
);
129 memcpy (rvalue
, smst_buffer
, rsize
);
134 ffi_call (ffi_cif
*cif
, void (*fn
) (void), void *rvalue
, void **avalue
)
136 ffi_call_int (cif
, fn
, rvalue
, avalue
, NULL
);
140 ffi_call_go (ffi_cif
*cif
, void (*fn
) (void), void *rvalue
, void **avalue
,
143 ffi_call_int (cif
, fn
, rvalue
, avalue
, closure
);
147 ffi_prep_closure_loc (ffi_closure
*closure
,
149 void (*fun
) (ffi_cif
*, void *, void **, void *),
154 return ffi_prep_closure_loc_linux64 (closure
, cif
, fun
, user_data
, codeloc
);
156 return ffi_prep_closure_loc_sysv (closure
, cif
, fun
, user_data
, codeloc
);
161 ffi_prep_go_closure (ffi_go_closure
*closure
,
163 void (*fun
) (ffi_cif
*, void *, void **, void *))
166 closure
->tramp
= ffi_go_closure_linux64
;
168 closure
->tramp
= ffi_go_closure_sysv
;