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. */
32
#define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
34
/* Perform machine independent initialization of aggregate type
37
static ffi_status initialize_aggregate(/*@out@*/ ffi_type *arg)
41
FFI_ASSERT(arg != NULL);
45
FFI_ASSERT(arg->elements != NULL);
46
FFI_ASSERT(arg->size == 0);
47
FFI_ASSERT(arg->alignment == 0);
49
ptr = &(arg->elements[0]);
51
while ((*ptr) != NULL)
53
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
54
return FFI_BAD_TYPEDEF;
56
/* Perform a sanity check on the argument type */
57
FFI_ASSERT_VALID_TYPE(*ptr);
59
arg->size = ALIGN(arg->size, (*ptr)->alignment);
60
arg->size += (*ptr)->size;
62
arg->alignment = (arg->alignment > (*ptr)->alignment) ?
63
arg->alignment : (*ptr)->alignment;
68
/* Structure size includes tail padding. This is important for
69
structures that fit in one register on ABIs like the PowerPC64
70
Linux ABI that right justify small structs in a register.
71
It's also needed for nested structure layout, for example
72
struct A { long a; char b; }; struct B { struct A x; char y; };
73
should find y at an offset of 2*sizeof(long) and result in a
74
total size of 3*sizeof(long). */
75
arg->size = ALIGN (arg->size, arg->alignment);
78
return FFI_BAD_TYPEDEF;
85
#if defined X86_DARWIN || defined X86_FREEBSD
86
static int is_result_on_stack_sub(ffi_type *rtype)
91
for (i = 0; rtype->elements[i] != NULL; ++i)
93
ffi_type *elem = rtype->elements[i];
94
if (elem->type == FFI_TYPE_STRUCT)
96
if (is_result_on_stack_sub(elem))
106
size = ffi_type_uint8.size;
108
case FFI_TYPE_UINT16:
109
case FFI_TYPE_SINT16:
110
size = ffi_type_sint16.size;
113
case FFI_TYPE_UINT32:
114
case FFI_TYPE_SINT32:
115
size = ffi_type_uint32.size;
117
case FFI_TYPE_UINT64:
118
case FFI_TYPE_SINT64:
119
size = ffi_type_uint64.size;
121
case FFI_TYPE_POINTER:
122
size = ffi_type_pointer.size;
125
size = ffi_type_float.size;
127
case FFI_TYPE_DOUBLE:
128
size = ffi_type_double.size;
130
case FFI_TYPE_LONGDOUBLE:
131
size = ffi_type_longdouble.size;
134
if (size < elem->size)
141
static int is_result_on_stack(ffi_type *rtype)
146
if (rtype->type == FFI_TYPE_STRUCT)
147
return is_result_on_stack_sub(rtype);
154
/* The CRIS ABI specifies structure elements to have byte
155
alignment only, so it completely overrides this functions,
156
which assumes "natural" alignment and padding. */
158
/* Perform machine independent ffi_cif preparation, then call
159
machine dependent routine. */
161
ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif,
162
ffi_abi abi, unsigned int nargs,
163
/*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype,
164
/*@dependent@*/ ffi_type **atypes)
170
FFI_ASSERT(cif != NULL);
171
FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
174
cif->arg_types = atypes;
180
/* Initialize the return type if necessary */
182
if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
183
return FFI_BAD_TYPEDEF;
186
/* Perform a sanity check on the return type */
187
FFI_ASSERT_VALID_TYPE(cif->rtype);
189
/* x86-64 and s390 stack space allocation is handled in prep_machdep. */
190
#if !defined M68K && !defined __x86_64__ && !defined S390 && !defined PA
191
/* Make space for the return structure pointer */
192
if (cif->rtype->type == FFI_TYPE_STRUCT
194
&& (cif->abi != FFI_V9 || cif->rtype->size > 32)
195
#elif defined X86_DARWIN || defined X86_FREEBSD
196
&& is_result_on_stack(cif->rtype)
199
bytes = STACK_ARG_SIZE(sizeof(void*));
202
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
205
/* Initialize any uninitialized aggregate type definitions */
206
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
207
return FFI_BAD_TYPEDEF;
209
/* Perform a sanity check on the argument type, do this
210
check after the initialization. */
211
FFI_ASSERT_VALID_TYPE(*ptr);
213
#if !defined __x86_64__ && !defined S390 && !defined PA && !defined X86_DARWIN && !defined X86_FREEBSD
215
if (((*ptr)->type == FFI_TYPE_STRUCT
216
&& ((*ptr)->size > 16 || cif->abi != FFI_V9))
217
|| ((*ptr)->type == FFI_TYPE_LONGDOUBLE
218
&& cif->abi != FFI_V9))
219
bytes += sizeof(void*);
223
/* Add any padding if necessary */
224
if (((*ptr)->alignment - 1) & bytes)
225
bytes = ALIGN(bytes, (*ptr)->alignment);
227
bytes += STACK_ARG_SIZE((*ptr)->size);
231
int align = (*ptr)->alignment;
232
if (align < 4) align = 4;
233
if ((align - 1) & bytes)
234
bytes = ALIGN(bytes, align);
236
bytes += STACK_ARG_SIZE((*ptr)->size);
241
#if defined X86_DARWIN || defined X86_FREEBSD
242
cif->bytes = ((bytes + 15) & ~0xf) + 8;
244
if (is_result_on_stack(cif->rtype))
246
cif->flags = FFI_TYPE_STRUCT;
250
cif->flags = FFI_TYPE_SINT64; /* same as int64 type */
256
/* Perform machine dependent cif processing */
257
return ffi_prep_cif_machdep(cif);
259
#endif /* not __CRIS__ */