Ensure that users don't include ffitarget.h directly
[libffi.git] / .pc / bad-abi-fix / src / prep_cif.c
1 /* -----------------------------------------------------------------------
2 prep_cif.c - Copyright (c) 2011, 2012 Anthony Green
3 Copyright (c) 1996, 1998, 2007 Red Hat, Inc.
4
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:
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 #ifndef __GNUC__
31 #define __builtin_expect(x, expected_value) (x)
32 #endif
33 #define LIKELY(x) __builtin_expect((x),1)
34 #define UNLIKELY(x) __builtin_expect((x),1)
35
36 /* Round up to FFI_SIZEOF_ARG. */
37
38 #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
39
40 /* Perform machine independent initialization of aggregate type
41 specifications. */
42
43 static ffi_status initialize_aggregate(ffi_type *arg)
44 {
45 ffi_type **ptr;
46
47 FFI_ASSERT(arg != NULL);
48
49 FFI_ASSERT(arg->elements != NULL);
50 FFI_ASSERT(arg->size == 0);
51 FFI_ASSERT(arg->alignment == 0);
52
53 ptr = &(arg->elements[0]);
54
55 if (UNLIKELY(ptr == 0))
56 return FFI_BAD_TYPEDEF;
57
58 while ((*ptr) != NULL)
59 {
60 if (UNLIKELY(((*ptr)->size == 0)
61 && (initialize_aggregate((*ptr)) != FFI_OK)))
62 return FFI_BAD_TYPEDEF;
63
64 /* Perform a sanity check on the argument type */
65 FFI_ASSERT_VALID_TYPE(*ptr);
66
67 arg->size = ALIGN(arg->size, (*ptr)->alignment);
68 arg->size += (*ptr)->size;
69
70 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
71 arg->alignment : (*ptr)->alignment;
72
73 ptr++;
74 }
75
76 /* Structure size includes tail padding. This is important for
77 structures that fit in one register on ABIs like the PowerPC64
78 Linux ABI that right justify small structs in a register.
79 It's also needed for nested structure layout, for example
80 struct A { long a; char b; }; struct B { struct A x; char y; };
81 should find y at an offset of 2*sizeof(long) and result in a
82 total size of 3*sizeof(long). */
83 arg->size = ALIGN (arg->size, arg->alignment);
84
85 if (arg->size == 0)
86 return FFI_BAD_TYPEDEF;
87 else
88 return FFI_OK;
89 }
90
91 #ifndef __CRIS__
92 /* The CRIS ABI specifies structure elements to have byte
93 alignment only, so it completely overrides this functions,
94 which assumes "natural" alignment and padding. */
95
96 /* Perform machine independent ffi_cif preparation, then call
97 machine dependent routine. */
98
99 ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
100 ffi_type *rtype, ffi_type **atypes)
101 {
102 unsigned bytes = 0;
103 unsigned int i;
104 ffi_type **ptr;
105
106 FFI_ASSERT(cif != NULL);
107 #ifndef X86_WIN32
108 if (! (abi > FFI_FIRST_ABI) && (abi <= FFI_LAST_ABI))
109 return FFI_BAD_ABI;
110 #else
111 if (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI || abi == FFI_THISCALL))
112 return FFI_BAD_ABI;
113 #endif
114
115 cif->abi = abi;
116 cif->arg_types = atypes;
117 cif->nargs = nargs;
118 cif->rtype = rtype;
119
120 cif->flags = 0;
121
122 /* Initialize the return type if necessary */
123 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
124 return FFI_BAD_TYPEDEF;
125
126 /* Perform a sanity check on the return type */
127 FFI_ASSERT_VALID_TYPE(cif->rtype);
128
129 /* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
130 #if !defined M68K && !defined X86_ANY && !defined S390 && !defined PA
131 /* Make space for the return structure pointer */
132 if (cif->rtype->type == FFI_TYPE_STRUCT
133 #ifdef SPARC
134 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
135 #endif
136 )
137 bytes = STACK_ARG_SIZE(sizeof(void*));
138 #endif
139
140 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
141 {
142
143 /* Initialize any uninitialized aggregate type definitions */
144 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
145 return FFI_BAD_TYPEDEF;
146
147 /* Perform a sanity check on the argument type, do this
148 check after the initialization. */
149 FFI_ASSERT_VALID_TYPE(*ptr);
150
151 #if !defined X86_ANY && !defined S390 && !defined PA
152 #ifdef SPARC
153 if (((*ptr)->type == FFI_TYPE_STRUCT
154 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
155 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
156 && cif->abi != FFI_V9))
157 bytes += sizeof(void*);
158 else
159 #endif
160 {
161 /* Add any padding if necessary */
162 if (((*ptr)->alignment - 1) & bytes)
163 bytes = ALIGN(bytes, (*ptr)->alignment);
164
165 bytes += STACK_ARG_SIZE((*ptr)->size);
166 }
167 #endif
168 }
169
170 cif->bytes = bytes;
171
172 /* Perform machine dependent cif processing */
173 return ffi_prep_cif_machdep(cif);
174 }
175 #endif /* not __CRIS__ */
176
177 #if FFI_CLOSURES
178
179 ffi_status
180 ffi_prep_closure (ffi_closure* closure,
181 ffi_cif* cif,
182 void (*fun)(ffi_cif*,void*,void**,void*),
183 void *user_data)
184 {
185 return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);
186 }
187
188 #endif