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