2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / libffi / src / prep_cif.c
blob0e202cfe1f77a002bd297776b555011ee5b17e3b
1 /* -----------------------------------------------------------------------
2 prep_cif.c - Copyright (c) 1996, 1998 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, 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 ----------------------------------------------------------------------- */
24 #include <ffi.h>
25 #include <ffi_common.h>
26 #include <stdlib.h>
28 /* Round up to FFI_SIZEOF_ARG. */
30 #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
32 /* Perform machine independent initialization of aggregate type
33 specifications. */
35 static ffi_status initialize_aggregate(ffi_type *arg)
37 ffi_type **ptr;
39 FFI_ASSERT(arg != NULL);
41 FFI_ASSERT(arg->elements != NULL);
42 FFI_ASSERT(arg->size == 0);
43 FFI_ASSERT(arg->alignment == 0);
45 ptr = &(arg->elements[0]);
47 while ((*ptr) != NULL)
49 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
50 return FFI_BAD_TYPEDEF;
52 /* Perform a sanity check on the argument type */
53 FFI_ASSERT_VALID_TYPE(*ptr);
55 arg->size = ALIGN(arg->size, (*ptr)->alignment);
56 arg->size += (*ptr)->size;
58 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
59 arg->alignment : (*ptr)->alignment;
61 ptr++;
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);
73 if (arg->size == 0)
74 return FFI_BAD_TYPEDEF;
75 else
76 return FFI_OK;
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. */
84 /* Perform machine independent ffi_cif preparation, then call
85 machine dependent routine. */
87 ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
88 ffi_type *rtype, ffi_type **atypes)
90 unsigned bytes = 0;
91 unsigned int i;
92 ffi_type **ptr;
94 FFI_ASSERT(cif != NULL);
95 FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
97 cif->abi = abi;
98 cif->arg_types = atypes;
99 cif->nargs = nargs;
100 cif->rtype = rtype;
102 cif->flags = 0;
104 /* Initialize the return type if necessary */
105 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
106 return FFI_BAD_TYPEDEF;
108 /* Perform a sanity check on the return type */
109 FFI_ASSERT_VALID_TYPE(cif->rtype);
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
122 bytes = STACK_ARG_SIZE(sizeof(void*));
123 #endif
125 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
128 /* Initialize any uninitialized aggregate type definitions */
129 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
130 return FFI_BAD_TYPEDEF;
132 /* Perform a sanity check on the argument type, do this
133 check after the initialization. */
134 FFI_ASSERT_VALID_TYPE(*ptr);
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
146 /* Add any padding if necessary */
147 if (((*ptr)->alignment - 1) & bytes)
148 bytes = ALIGN(bytes, (*ptr)->alignment);
150 bytes += STACK_ARG_SIZE((*ptr)->size);
152 #endif
155 cif->bytes = bytes;
157 /* Perform machine dependent cif processing */
158 return ffi_prep_cif_machdep(cif);
160 #endif /* not __CRIS__ */