12
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
15
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 ----------------------------------------------------------------------- */
25
26 #include <ffi.h>
27 #include <ffi_common.h>
28 #include <stdlib.h>
29
30 /* Round up to FFI_SIZEOF_ARG. */
31
32 #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
33
34 /* Perform machine independent initialization of aggregate type
35 specifications. */
36
37 static ffi_status initialize_aggregate(ffi_type *arg)
38 {
39 ffi_type **ptr;
40
41 if (UNLIKELY(arg == NULL || arg->elements == NULL))
42 return FFI_BAD_TYPEDEF;
43
44 arg->size = 0;
45 arg->alignment = 0;
46
47 ptr = &(arg->elements[0]);
48
49 if (UNLIKELY(ptr == 0))
50 return FFI_BAD_TYPEDEF;
51
52 while ((*ptr) != NULL)
53 {
54 if (UNLIKELY(((*ptr)->size == 0)
55 && (initialize_aggregate((*ptr)) != FFI_OK)))
56 return FFI_BAD_TYPEDEF;
57
58 /* Perform a sanity check on the argument type */
59 FFI_ASSERT_VALID_TYPE(*ptr);
60
61 arg->size = ALIGN(arg->size, (*ptr)->alignment);
62 arg->size += (*ptr)->size;
63
64 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
65 arg->alignment : (*ptr)->alignment;
66
67 ptr++;
68 }
69
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);
78
79 /* On some targets, the ABI defines that structures have an additional
80 alignment beyond the "natural" one based on their elements. */
81 #ifdef FFI_AGGREGATE_ALIGNMENT
82 if (FFI_AGGREGATE_ALIGNMENT > arg->alignment)
83 arg->alignment = FFI_AGGREGATE_ALIGNMENT;
84 #endif
85
86 if (arg->size == 0)
87 return FFI_BAD_TYPEDEF;
88 else
89 return FFI_OK;
90 }
91
92 #ifndef __CRIS__
93 /* The CRIS ABI specifies structure elements to have byte
94 alignment only, so it completely overrides this functions,
95 which assumes "natural" alignment and padding. */
96
97 /* Perform machine independent ffi_cif preparation, then call
110 unsigned int ntotalargs,
111 ffi_type *rtype, ffi_type **atypes)
112 {
113 unsigned bytes = 0;
114 unsigned int i;
115 ffi_type **ptr;
116
117 FFI_ASSERT(cif != NULL);
118 FFI_ASSERT((!isvariadic) || (nfixedargs >= 1));
119 FFI_ASSERT(nfixedargs <= ntotalargs);
120
121 if (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI))
122 return FFI_BAD_ABI;
123
124 cif->abi = abi;
125 cif->arg_types = atypes;
126 cif->nargs = ntotalargs;
127 cif->rtype = rtype;
128
129 cif->flags = 0;
130
131 #if HAVE_LONG_DOUBLE_VARIANT
132 ffi_prep_types (abi);
133 #endif
134
135 /* Initialize the return type if necessary */
136 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
137 return FFI_BAD_TYPEDEF;
138
139 #ifndef FFI_TARGET_HAS_COMPLEX_TYPE
140 if (rtype->type == FFI_TYPE_COMPLEX)
141 abort();
142 #endif
143 /* Perform a sanity check on the return type */
144 FFI_ASSERT_VALID_TYPE(cif->rtype);
145
146 /* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
147 #if !defined FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
148 /* Make space for the return structure pointer */
149 if (cif->rtype->type == FFI_TYPE_STRUCT
150 #ifdef SPARC
151 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
152 #endif
153 #ifdef TILE
154 && (cif->rtype->size > 10 * FFI_SIZEOF_ARG)
155 #endif
156 #ifdef XTENSA
157 && (cif->rtype->size > 16)
158 #endif
159 #ifdef NIOS2
160 && (cif->rtype->size > 8)
161 #endif
162 )
163 bytes = STACK_ARG_SIZE(sizeof(void*));
164 #endif
165
166 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
167 {
168
169 /* Initialize any uninitialized aggregate type definitions */
170 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
171 return FFI_BAD_TYPEDEF;
172
173 #ifndef FFI_TARGET_HAS_COMPLEX_TYPE
174 if ((*ptr)->type == FFI_TYPE_COMPLEX)
175 abort();
176 #endif
177 /* Perform a sanity check on the argument type, do this
178 check after the initialization. */
179 FFI_ASSERT_VALID_TYPE(*ptr);
180
181 #if !defined FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
182 #ifdef SPARC
183 if (((*ptr)->type == FFI_TYPE_STRUCT
184 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
185 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
186 && cif->abi != FFI_V9))
187 bytes += sizeof(void*);
188 else
189 #endif
190 {
191 /* Add any padding if necessary */
192 if (((*ptr)->alignment - 1) & bytes)
193 bytes = (unsigned)ALIGN(bytes, (*ptr)->alignment);
194
195 #ifdef TILE
196 if (bytes < 10 * FFI_SIZEOF_ARG &&
197 bytes + STACK_ARG_SIZE((*ptr)->size) > 10 * FFI_SIZEOF_ARG)
198 {
199 /* An argument is never split between the 10 parameter
200 registers and the stack. */
201 bytes = 10 * FFI_SIZEOF_ARG;
202 }
203 #endif
204 #ifdef XTENSA
205 if (bytes <= 6*4 && bytes + STACK_ARG_SIZE((*ptr)->size) > 6*4)
206 bytes = 6*4;
207 #endif
208
209 bytes += STACK_ARG_SIZE((*ptr)->size);
210 }
211 #endif
212 }
213
214 cif->bytes = bytes;
215
216 /* Perform machine dependent cif processing */
217 #ifdef FFI_TARGET_SPECIFIC_VARIADIC
218 if (isvariadic)
219 return ffi_prep_cif_machdep_var(cif, nfixedargs, ntotalargs);
220 #endif
221
222 return ffi_prep_cif_machdep(cif);
223 }
224 #endif /* not __CRIS__ */
225
226 ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
227 ffi_type *rtype, ffi_type **atypes)
228 {
229 return ffi_prep_cif_core(cif, abi, 0, nargs, nargs, rtype, atypes);
234 unsigned int nfixedargs,
235 unsigned int ntotalargs,
236 ffi_type *rtype,
237 ffi_type **atypes)
238 {
239 return ffi_prep_cif_core(cif, abi, 1, nfixedargs, ntotalargs, rtype, atypes);
240 }
241
242 #if FFI_CLOSURES
243
244 ffi_status
245 ffi_prep_closure (ffi_closure* closure,
246 ffi_cif* cif,
247 void (*fun)(ffi_cif*,void*,void**,void*),
248 void *user_data)
249 {
250 return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);
251 }
252
253 #endif
|
12
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
15
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 ----------------------------------------------------------------------- */
25
26 #include <ffi.h>
27 #include <ffi_common.h>
28 #include <stdlib.h>
29
30 /* Round up to FFI_SIZEOF_ARG. */
31
32 #define STACK_ARG_SIZE(x) FFI_ALIGN(x, FFI_SIZEOF_ARG)
33
34 /* Perform machine independent initialization of aggregate type
35 specifications. */
36
37 static ffi_status initialize_aggregate(ffi_type *arg, size_t *offsets)
38 {
39 ffi_type **ptr;
40
41 if (UNLIKELY(arg == NULL || arg->elements == NULL))
42 return FFI_BAD_TYPEDEF;
43
44 arg->size = 0;
45 arg->alignment = 0;
46
47 ptr = &(arg->elements[0]);
48
49 if (UNLIKELY(ptr == 0))
50 return FFI_BAD_TYPEDEF;
51
52 while ((*ptr) != NULL)
53 {
54 if (UNLIKELY(((*ptr)->size == 0)
55 && (initialize_aggregate((*ptr), NULL) != FFI_OK)))
56 return FFI_BAD_TYPEDEF;
57
58 /* Perform a sanity check on the argument type */
59 FFI_ASSERT_VALID_TYPE(*ptr);
60
61 arg->size = FFI_ALIGN(arg->size, (*ptr)->alignment);
62 if (offsets)
63 *offsets++ = arg->size;
64 arg->size += (*ptr)->size;
65
66 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
67 arg->alignment : (*ptr)->alignment;
68
69 ptr++;
70 }
71
72 /* Structure size includes tail padding. This is important for
73 structures that fit in one register on ABIs like the PowerPC64
74 Linux ABI that right justify small structs in a register.
75 It's also needed for nested structure layout, for example
76 struct A { long a; char b; }; struct B { struct A x; char y; };
77 should find y at an offset of 2*sizeof(long) and result in a
78 total size of 3*sizeof(long). */
79 arg->size = FFI_ALIGN (arg->size, arg->alignment);
80
81 /* On some targets, the ABI defines that structures have an additional
82 alignment beyond the "natural" one based on their elements. */
83 #ifdef FFI_AGGREGATE_ALIGNMENT
84 if (FFI_AGGREGATE_ALIGNMENT > arg->alignment)
85 arg->alignment = FFI_AGGREGATE_ALIGNMENT;
86 #endif
87
88 if (arg->size == 0)
89 return FFI_BAD_TYPEDEF;
90 else
91 return FFI_OK;
92 }
93
94 #ifndef __CRIS__
95 /* The CRIS ABI specifies structure elements to have byte
96 alignment only, so it completely overrides this functions,
97 which assumes "natural" alignment and padding. */
98
99 /* Perform machine independent ffi_cif preparation, then call
112 unsigned int ntotalargs,
113 ffi_type *rtype, ffi_type **atypes)
114 {
115 unsigned bytes = 0;
116 unsigned int i;
117 ffi_type **ptr;
118
119 FFI_ASSERT(cif != NULL);
120 FFI_ASSERT((!isvariadic) || (nfixedargs >= 1));
121 FFI_ASSERT(nfixedargs <= ntotalargs);
122
123 if (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI))
124 return FFI_BAD_ABI;
125
126 cif->abi = abi;
127 cif->arg_types = atypes;
128 cif->nargs = ntotalargs;
129 cif->rtype = rtype;
130
131 cif->flags = 0;
132 #ifdef _M_ARM64
133 cif->is_variadic = isvariadic;
134 #endif
135 #if HAVE_LONG_DOUBLE_VARIANT
136 ffi_prep_types (abi);
137 #endif
138
139 /* Initialize the return type if necessary */
140 if ((cif->rtype->size == 0)
141 && (initialize_aggregate(cif->rtype, NULL) != FFI_OK))
142 return FFI_BAD_TYPEDEF;
143
144 #ifndef FFI_TARGET_HAS_COMPLEX_TYPE
145 if (rtype->type == FFI_TYPE_COMPLEX)
146 abort();
147 #endif
148 /* Perform a sanity check on the return type */
149 FFI_ASSERT_VALID_TYPE(cif->rtype);
150
151 /* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
152 #if !defined FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
153 /* Make space for the return structure pointer */
154 if (cif->rtype->type == FFI_TYPE_STRUCT
155 #ifdef TILE
156 && (cif->rtype->size > 10 * FFI_SIZEOF_ARG)
157 #endif
158 #ifdef XTENSA
159 && (cif->rtype->size > 16)
160 #endif
161 #ifdef NIOS2
162 && (cif->rtype->size > 8)
163 #endif
164 )
165 bytes = STACK_ARG_SIZE(sizeof(void*));
166 #endif
167
168 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
169 {
170
171 /* Initialize any uninitialized aggregate type definitions */
172 if (((*ptr)->size == 0)
173 && (initialize_aggregate((*ptr), NULL) != FFI_OK))
174 return FFI_BAD_TYPEDEF;
175
176 #ifndef FFI_TARGET_HAS_COMPLEX_TYPE
177 if ((*ptr)->type == FFI_TYPE_COMPLEX)
178 abort();
179 #endif
180 /* Perform a sanity check on the argument type, do this
181 check after the initialization. */
182 FFI_ASSERT_VALID_TYPE(*ptr);
183
184 #if !defined FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
185 {
186 /* Add any padding if necessary */
187 if (((*ptr)->alignment - 1) & bytes)
188 bytes = (unsigned)FFI_ALIGN(bytes, (*ptr)->alignment);
189
190 #ifdef TILE
191 if (bytes < 10 * FFI_SIZEOF_ARG &&
192 bytes + STACK_ARG_SIZE((*ptr)->size) > 10 * FFI_SIZEOF_ARG)
193 {
194 /* An argument is never split between the 10 parameter
195 registers and the stack. */
196 bytes = 10 * FFI_SIZEOF_ARG;
197 }
198 #endif
199 #ifdef XTENSA
200 if (bytes <= 6*4 && bytes + STACK_ARG_SIZE((*ptr)->size) > 6*4)
201 bytes = 6*4;
202 #endif
203
204 bytes += (unsigned int)STACK_ARG_SIZE((*ptr)->size);
205 }
206 #endif
207 }
208
209 cif->bytes = bytes;
210
211 /* Perform machine dependent cif processing */
212 #ifdef FFI_TARGET_SPECIFIC_VARIADIC
213 if (isvariadic)
214 return ffi_prep_cif_machdep_var(cif, nfixedargs, ntotalargs);
215 #endif
216
217 return ffi_prep_cif_machdep(cif);
218 }
219 #endif /* not __CRIS__ */
220
221 ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
222 ffi_type *rtype, ffi_type **atypes)
223 {
224 return ffi_prep_cif_core(cif, abi, 0, nargs, nargs, rtype, atypes);
229 unsigned int nfixedargs,
230 unsigned int ntotalargs,
231 ffi_type *rtype,
232 ffi_type **atypes)
233 {
234 return ffi_prep_cif_core(cif, abi, 1, nfixedargs, ntotalargs, rtype, atypes);
235 }
236
237 #if FFI_CLOSURES
238
239 ffi_status
240 ffi_prep_closure (ffi_closure* closure,
241 ffi_cif* cif,
242 void (*fun)(ffi_cif*,void*,void**,void*),
243 void *user_data)
244 {
245 return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);
246 }
247
248 #endif
249
250 ffi_status
251 ffi_get_struct_offsets (ffi_abi abi, ffi_type *struct_type, size_t *offsets)
252 {
253 if (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI))
254 return FFI_BAD_ABI;
255 if (struct_type->type != FFI_TYPE_STRUCT)
256 return FFI_BAD_TYPEDEF;
257
258 #if HAVE_LONG_DOUBLE_VARIANT
259 ffi_prep_types (abi);
260 #endif
261
262 return initialize_aggregate(struct_type, offsets);
263 }
|