e90a95245fa6513629f9a8092d922f36515ecfd7
[libffi.git] / .pc / bad-abi-fix / src / prep_cif.c
1 /* -----------------------------------------------------------------------
2 prep_cif.c - Copyright (c) 2011 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 if (! (abi > FFI_FIRST_ABI && abi < FFI_LAST_ABI))
108 return FFI_BAD_ABI;
109
110 cif->abi = abi;
111 cif->arg_types = atypes;
112 cif->nargs = nargs;
113 cif->rtype = rtype;
114
115 cif->flags = 0;
116
117 /* Initialize the return type if necessary */
118 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
119 return FFI_BAD_TYPEDEF;
120
121 /* Perform a sanity check on the return type */
122 FFI_ASSERT_VALID_TYPE(cif->rtype);
123
124 /* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
125 #if !defined M68K && !defined X86_ANY && !defined S390 && !defined PA
126 /* Make space for the return structure pointer */
127 if (cif->rtype->type == FFI_TYPE_STRUCT
128 #ifdef SPARC
129 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
130 #endif
131 )
132 bytes = STACK_ARG_SIZE(sizeof(void*));
133 #endif
134
135 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
136 {
137
138 /* Initialize any uninitialized aggregate type definitions */
139 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
140 return FFI_BAD_TYPEDEF;
141
142 /* Perform a sanity check on the argument type, do this
143 check after the initialization. */
144 FFI_ASSERT_VALID_TYPE(*ptr);
145
146 #if !defined X86_ANY && !defined S390 && !defined PA
147 #ifdef SPARC
148 if (((*ptr)->type == FFI_TYPE_STRUCT
149 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
150 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
151 && cif->abi != FFI_V9))
152 bytes += sizeof(void*);
153 else
154 #endif
155 {
156 /* Add any padding if necessary */
157 if (((*ptr)->alignment - 1) & bytes)
158 bytes = ALIGN(bytes, (*ptr)->alignment);
159
160 bytes += STACK_ARG_SIZE((*ptr)->size);
161 }
162 #endif
163 }
164
165 cif->bytes = bytes;
166
167 /* Perform machine dependent cif processing */
168 return ffi_prep_cif_machdep(cif);
169 }
170 #endif /* not __CRIS__ */
171
172 #if FFI_CLOSURES
173
174 ffi_status
175 ffi_prep_closure (ffi_closure* closure,
176 ffi_cif* cif,
177 void (*fun)(ffi_cif*,void*,void**,void*),
178 void *user_data)
179 {
180 return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);
181 }
182
183 #endif