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