annotate libffi/src/prep_cif.c @ 111:04ced10e8804

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