Merge trunk version 194962 into gupc branch.
[official-gcc.git] / libffi / src / prep_cif.c
blob5d1924bbe584cfd1fe1e6d5ed83101023b7a4d4f
1 /* -----------------------------------------------------------------------
2 prep_cif.c - Copyright (c) 2011, 2012 Anthony Green
3 Copyright (c) 1996, 1998, 2007 Red Hat, Inc.
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 ``Software''), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
24 ----------------------------------------------------------------------- */
26 #include <ffi.h>
27 #include <ffi_common.h>
28 #include <stdlib.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
35 specifications. */
37 static ffi_status initialize_aggregate(ffi_type *arg)
39 ffi_type **ptr;
41 if (UNLIKELY(arg == NULL || arg->elements == NULL))
42 return FFI_BAD_TYPEDEF;
44 arg->size = 0;
45 arg->alignment = 0;
47 ptr = &(arg->elements[0]);
49 if (UNLIKELY(ptr == 0))
50 return FFI_BAD_TYPEDEF;
52 while ((*ptr) != NULL)
54 if (UNLIKELY(((*ptr)->size == 0)
55 && (initialize_aggregate((*ptr)) != FFI_OK)))
56 return FFI_BAD_TYPEDEF;
58 /* Perform a sanity check on the argument type */
59 FFI_ASSERT_VALID_TYPE(*ptr);
61 arg->size = ALIGN(arg->size, (*ptr)->alignment);
62 arg->size += (*ptr)->size;
64 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
65 arg->alignment : (*ptr)->alignment;
67 ptr++;
70 /* Structure size includes tail padding. This is important for
71 structures that fit in one register on ABIs like the PowerPC64
72 Linux ABI that right justify small structs in a register.
73 It's also needed for nested structure layout, for example
74 struct A { long a; char b; }; struct B { struct A x; char y; };
75 should find y at an offset of 2*sizeof(long) and result in a
76 total size of 3*sizeof(long). */
77 arg->size = ALIGN (arg->size, arg->alignment);
79 if (arg->size == 0)
80 return FFI_BAD_TYPEDEF;
81 else
82 return FFI_OK;
85 #ifndef __CRIS__
86 /* The CRIS ABI specifies structure elements to have byte
87 alignment only, so it completely overrides this functions,
88 which assumes "natural" alignment and padding. */
90 /* Perform machine independent ffi_cif preparation, then call
91 machine dependent routine. */
93 /* For non variadic functions isvariadic should be 0 and
94 nfixedargs==ntotalargs.
96 For variadic calls, isvariadic should be 1 and nfixedargs
97 and ntotalargs set as appropriate. nfixedargs must always be >=1 */
100 ffi_status FFI_HIDDEN ffi_prep_cif_core(ffi_cif *cif, ffi_abi abi,
101 unsigned int isvariadic,
102 unsigned int nfixedargs,
103 unsigned int ntotalargs,
104 ffi_type *rtype, ffi_type **atypes)
106 unsigned bytes = 0;
107 unsigned int i;
108 ffi_type **ptr;
110 FFI_ASSERT(cif != NULL);
111 FFI_ASSERT((!isvariadic) || (nfixedargs >= 1));
112 FFI_ASSERT(nfixedargs <= ntotalargs);
114 #ifndef X86_WIN32
115 if (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI))
116 return FFI_BAD_ABI;
117 #else
118 if (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI || abi == FFI_THISCALL))
119 return FFI_BAD_ABI;
120 #endif
122 cif->abi = abi;
123 cif->arg_types = atypes;
124 cif->nargs = ntotalargs;
125 cif->rtype = rtype;
127 cif->flags = 0;
129 /* Initialize the return type if necessary */
130 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
131 return FFI_BAD_TYPEDEF;
133 /* Perform a sanity check on the return type */
134 FFI_ASSERT_VALID_TYPE(cif->rtype);
136 /* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
137 #if !defined M68K && !defined X86_ANY && !defined S390 && !defined PA
138 /* Make space for the return structure pointer */
139 if (cif->rtype->type == FFI_TYPE_STRUCT
140 #ifdef SPARC
141 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
142 #endif
143 #ifdef TILE
144 && (cif->rtype->size > 10 * FFI_SIZEOF_ARG)
145 #endif
147 bytes = STACK_ARG_SIZE(sizeof(void*));
148 #endif
150 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
153 /* Initialize any uninitialized aggregate type definitions */
154 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
155 return FFI_BAD_TYPEDEF;
157 /* Perform a sanity check on the argument type, do this
158 check after the initialization. */
159 FFI_ASSERT_VALID_TYPE(*ptr);
161 #if !defined X86_ANY && !defined S390 && !defined PA
162 #ifdef SPARC
163 if (((*ptr)->type == FFI_TYPE_STRUCT
164 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
165 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
166 && cif->abi != FFI_V9))
167 bytes += sizeof(void*);
168 else
169 #endif
171 /* Add any padding if necessary */
172 if (((*ptr)->alignment - 1) & bytes)
173 bytes = ALIGN(bytes, (*ptr)->alignment);
175 #ifdef TILE
176 if (bytes < 10 * FFI_SIZEOF_ARG &&
177 bytes + STACK_ARG_SIZE((*ptr)->size) > 10 * FFI_SIZEOF_ARG)
179 /* An argument is never split between the 10 parameter
180 registers and the stack. */
181 bytes = 10 * FFI_SIZEOF_ARG;
183 #endif
185 bytes += STACK_ARG_SIZE((*ptr)->size);
187 #endif
190 cif->bytes = bytes;
192 /* Perform machine dependent cif processing */
193 #ifdef FFI_TARGET_SPECIFIC_VARIADIC
194 if (isvariadic)
195 return ffi_prep_cif_machdep_var(cif, nfixedargs, ntotalargs);
196 #endif
198 return ffi_prep_cif_machdep(cif);
200 #endif /* not __CRIS__ */
202 ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
203 ffi_type *rtype, ffi_type **atypes)
205 return ffi_prep_cif_core(cif, abi, 0, nargs, nargs, rtype, atypes);
208 ffi_status ffi_prep_cif_var(ffi_cif *cif,
209 ffi_abi abi,
210 unsigned int nfixedargs,
211 unsigned int ntotalargs,
212 ffi_type *rtype,
213 ffi_type **atypes)
215 return ffi_prep_cif_core(cif, abi, 1, nfixedargs, ntotalargs, rtype, atypes);
218 #if FFI_CLOSURES
220 ffi_status
221 ffi_prep_closure (ffi_closure* closure,
222 ffi_cif* cif,
223 void (*fun)(ffi_cif*,void*,void**,void*),
224 void *user_data)
226 return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);
229 #endif