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