1 /* -----------------------------------------------------------------------
2 prep_cif.c - Copyright (c) 1996, 1998, 2007 Red Hat, Inc.
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:
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 ----------------------------------------------------------------------- */
26 #include <ffi_common.h>
29 /* Round up to FFI_SIZEOF_ARG. */
31 #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
33 /* Perform machine independent initialization of aggregate type
36 static ffi_status initialize_aggregate(ffi_type *arg)
40 FFI_ASSERT(arg != NULL);
42 FFI_ASSERT(arg->elements != NULL);
43 FFI_ASSERT(arg->size == 0);
44 FFI_ASSERT(arg->alignment == 0);
46 ptr = &(arg->elements[0]);
48 while ((*ptr) != NULL)
50 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
51 return FFI_BAD_TYPEDEF;
53 /* Perform a sanity check on the argument type */
54 FFI_ASSERT_VALID_TYPE(*ptr);
56 arg->size = ALIGN(arg->size, (*ptr)->alignment);
57 arg->size += (*ptr)->size;
59 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
60 arg->alignment : (*ptr)->alignment;
65 /* Structure size includes tail padding. This is important for
66 structures that fit in one register on ABIs like the PowerPC64
67 Linux ABI that right justify small structs in a register.
68 It's also needed for nested structure layout, for example
69 struct A { long a; char b; }; struct B { struct A x; char y; };
70 should find y at an offset of 2*sizeof(long) and result in a
71 total size of 3*sizeof(long). */
72 arg->size = ALIGN (arg->size, arg->alignment);
75 return FFI_BAD_TYPEDEF;
81 /* The CRIS ABI specifies structure elements to have byte
82 alignment only, so it completely overrides this functions,
83 which assumes "natural" alignment and padding. */
85 /* Perform machine independent ffi_cif preparation, then call
86 machine dependent routine. */
88 ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
89 ffi_type *rtype, ffi_type **atypes)
95 FFI_ASSERT(cif != NULL);
97 FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
99 FFI_ASSERT(abi > FFI_FIRST_ABI && abi <= FFI_DEFAULT_ABI
100 || abi == FFI_THISCALL);
104 cif->arg_types = atypes;
110 /* Initialize the return type if necessary */
111 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
112 return FFI_BAD_TYPEDEF;
114 /* Perform a sanity check on the return type */
115 FFI_ASSERT_VALID_TYPE(cif->rtype);
117 /* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
118 #if !defined M68K && !defined __i386__ && !defined __x86_64__ && !defined S390 && !defined PA
119 /* Make space for the return structure pointer */
120 if (cif->rtype->type == FFI_TYPE_STRUCT
122 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
125 bytes = STACK_ARG_SIZE(sizeof(void*));
128 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
131 /* Initialize any uninitialized aggregate type definitions */
132 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
133 return FFI_BAD_TYPEDEF;
135 /* Perform a sanity check on the argument type, do this
136 check after the initialization. */
137 FFI_ASSERT_VALID_TYPE(*ptr);
139 #if !defined __i386__ && !defined __x86_64__ && !defined S390 && !defined PA
141 if (((*ptr)->type == FFI_TYPE_STRUCT
142 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
143 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
144 && cif->abi != FFI_V9))
145 bytes += sizeof(void*);
149 /* Add any padding if necessary */
150 if (((*ptr)->alignment - 1) & bytes)
151 bytes = ALIGN(bytes, (*ptr)->alignment);
153 bytes += STACK_ARG_SIZE((*ptr)->size);
160 /* Perform machine dependent cif processing */
161 return ffi_prep_cif_machdep(cif);
163 #endif /* not __CRIS__ */
168 ffi_prep_closure (ffi_closure* closure,
170 void (*fun)(ffi_cif*,void*,void**,void*),
173 return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);