1
/* -----------------------------------------------------------------------
2
prep_cif.c - Copyright (c) 1996, 1998, 2007 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,
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
DEALINGS IN THE SOFTWARE.
23
----------------------------------------------------------------------- */
26
#include <ffi_common.h>
29
/* 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
36
static ffi_status initialize_aggregate(ffi_type *arg)
40
FFI_ASSERT(arg != NULL);
42
FFI_ASSERT(arg->elements != NULL);
43
FFI_ASSERT(arg->size == 0);
44
FFI_ASSERT(arg->alignment == 0);
46
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);
56
arg->size = ALIGN(arg->size, (*ptr)->alignment);
57
arg->size += (*ptr)->size;
59
arg->alignment = (arg->alignment > (*ptr)->alignment) ?
60
arg->alignment : (*ptr)->alignment;
65
/* Structure size includes tail padding. This is important for
66
structures that fit in one register on ABIs like the PowerPC64
67
Linux ABI that right justify small structs in a register.
68
It's also needed for nested structure layout, for example
69
struct A { long a; char b; }; struct B { struct A x; char y; };
70
should find y at an offset of 2*sizeof(long) and result in a
71
total size of 3*sizeof(long). */
72
arg->size = ALIGN (arg->size, arg->alignment);
75
return FFI_BAD_TYPEDEF;
81
/* The CRIS ABI specifies structure elements to have byte
82
alignment only, so it completely overrides this functions,
83
which assumes "natural" alignment and padding. */
85
/* Perform machine independent ffi_cif preparation, then call
86
machine dependent routine. */
88
ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
89
ffi_type *rtype, ffi_type **atypes)
95
FFI_ASSERT(cif != NULL);
96
FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
99
cif->arg_types = atypes;
105
/* Initialize the return type if necessary */
106
if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
107
return FFI_BAD_TYPEDEF;
109
/* Perform a sanity check on the return type */
110
FFI_ASSERT_VALID_TYPE(cif->rtype);
112
/* x86-64 and s390 stack space allocation is handled in prep_machdep. */
113
#if !defined M68K && !defined __x86_64__ && !defined S390 && !defined PA
114
/* Make space for the return structure pointer */
115
if (cif->rtype->type == FFI_TYPE_STRUCT
117
&& (cif->abi != FFI_V9 || cif->rtype->size > 32)
120
&& (cif->rtype->size > 8)
123
bytes = STACK_ARG_SIZE(sizeof(void*));
126
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
129
/* Initialize any uninitialized aggregate type definitions */
130
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
131
return FFI_BAD_TYPEDEF;
133
/* Perform a sanity check on the argument type, do this
134
check after the initialization. */
135
FFI_ASSERT_VALID_TYPE(*ptr);
137
#if !defined __x86_64__ && !defined S390 && !defined PA
139
if (((*ptr)->type == FFI_TYPE_STRUCT
140
&& ((*ptr)->size > 16 || cif->abi != FFI_V9))
141
|| ((*ptr)->type == FFI_TYPE_LONGDOUBLE
142
&& cif->abi != FFI_V9))
143
bytes += sizeof(void*);
147
/* Add any padding if necessary */
148
if (((*ptr)->alignment - 1) & bytes)
149
bytes = ALIGN(bytes, (*ptr)->alignment);
151
bytes += STACK_ARG_SIZE((*ptr)->size);
158
/* Perform machine dependent cif processing */
159
return ffi_prep_cif_machdep(cif);
161
#endif /* not __CRIS__ */
166
ffi_prep_closure (ffi_closure* closure,
168
void (*fun)(ffi_cif*,void*,void**,void*),
171
return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);