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