1
/* -----------------------------------------------------------------------
2
prep_cif.c - Copyright (c) 1996, 1998 Red Hat, Inc.
4
Permission is hereby granted, free of charge, to any person obtaining
5
a copy of this software and associated documentation files (the
6
``Software''), to deal in the Software without restriction, including
7
without limitation the rights to use, copy, modify, merge, publish,
8
distribute, sublicense, and/or sell copies of the Software, and to
9
permit persons to whom the Software is furnished to do so, subject to
10
the following conditions:
12
The above copyright notice and this permission notice shall be included
13
in all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
OTHER DEALINGS IN THE SOFTWARE.
22
----------------------------------------------------------------------- */
25
#include <ffi_common.h>
30
/* Round up to FFI_SIZEOF_ARG. */
31
#define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
33
/* Perform machine independent initialization of aggregate type
38
/*@out@*/ ffi_type* arg)
42
if (arg == NULL || arg->elements == NULL ||
43
arg->size != 0 || arg->alignment != 0)
44
return FFI_BAD_TYPEDEF;
46
ffi_type** ptr = &(arg->elements[0]);
48
while ((*ptr) != NULL)
50
if (((*ptr)->size == 0) && (initialize_aggregate(*ptr) != FFI_OK))
51
return FFI_BAD_TYPEDEF;
53
/* Perform a sanity check on the argument type */
54
FFI_ASSERT_VALID_TYPE(*ptr);
57
int curalign = (*ptr)->alignment;
59
if (ptr != &(arg->elements[0]))
61
if (curalign > 4 && curalign != 16)
65
arg->size = ALIGN(arg->size, curalign);
66
arg->size += (*ptr)->size;
67
arg->alignment = (arg->alignment > curalign) ?
68
arg->alignment : curalign;
70
arg->size = ALIGN(arg->size, (*ptr)->alignment);
71
arg->size += (*ptr)->size;
72
arg->alignment = (arg->alignment > (*ptr)->alignment) ?
73
arg->alignment : (*ptr)->alignment;
79
/* Structure size includes tail padding. This is important for
80
structures that fit in one register on ABIs like the PowerPC64
81
Linux ABI that right justify small structs in a register.
82
It's also needed for nested structure layout, for example
83
struct A { long a; char b; }; struct B { struct A x; char y; };
84
should find y at an offset of 2*sizeof(long) and result in a
85
total size of 3*sizeof(long). */
86
arg->size = ALIGN(arg->size, arg->alignment);
89
return FFI_BAD_TYPEDEF;
97
/* The CRIS ABI specifies structure elements to have byte
98
alignment only, so it completely overrides this functions,
99
which assumes "natural" alignment and padding. */
101
/* Perform machine independent ffi_cif preparation, then call
102
machine dependent routine. */
104
#if defined(X86_DARWIN)
113
/* This is not what the ABI says, but is what is really implemented */
127
#endif // defined(X86_DARWIN)
129
// Arguments' ffi_type->alignment must be nonzero.
132
/*@out@*/ /*@partial@*/ ffi_cif* cif,
135
/*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type* rtype,
136
/*@dependent@*/ ffi_type** atypes)
139
return FFI_BAD_TYPEDEF;
141
if (abi <= FFI_FIRST_ABI || abi > FFI_DEFAULT_ABI)
144
unsigned int bytes = 0;
149
cif->arg_types = atypes;
154
/* Initialize the return type if necessary */
156
if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
157
return FFI_BAD_TYPEDEF;
160
/* Perform a sanity check on the return type */
161
FFI_ASSERT_VALID_TYPE(cif->rtype);
163
/* x86-64 and s390 stack space allocation is handled in prep_machdep. */
164
#if !defined M68K && !defined __x86_64__ && !defined S390 && !defined PA
165
/* Make space for the return structure pointer */
166
if (cif->rtype->type == FFI_TYPE_STRUCT
168
&& (cif->abi != FFI_V9 || cif->rtype->size > 32)
171
&& (struct_on_stack(cif->rtype->size))
174
bytes = STACK_ARG_SIZE(sizeof(void*));
177
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
179
/* Initialize any uninitialized aggregate type definitions */
180
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
181
return FFI_BAD_TYPEDEF;
183
if ((*ptr)->alignment == 0)
184
return FFI_BAD_TYPEDEF;
186
/* Perform a sanity check on the argument type, do this
187
check after the initialization. */
188
FFI_ASSERT_VALID_TYPE(*ptr);
190
#if defined(X86_DARWIN)
192
int align = (*ptr)->alignment;
197
if ((align - 1) & bytes)
198
bytes = ALIGN(bytes, align);
200
bytes += STACK_ARG_SIZE((*ptr)->size);
202
#elif !defined __x86_64__ && !defined S390 && !defined PA
204
if (((*ptr)->type == FFI_TYPE_STRUCT
205
&& ((*ptr)->size > 16 || cif->abi != FFI_V9))
206
|| ((*ptr)->type == FFI_TYPE_LONGDOUBLE
207
&& cif->abi != FFI_V9))
208
bytes += sizeof(void*);
212
/* Add any padding if necessary */
213
if (((*ptr)->alignment - 1) & bytes)
214
bytes = ALIGN(bytes, (*ptr)->alignment);
216
bytes += STACK_ARG_SIZE((*ptr)->size);
223
/* Perform machine dependent cif processing */
224
return ffi_prep_cif_machdep(cif);
226
#endif /* not __CRIS__ */