Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libffi / src / prep_cif.c
blob0faa5ddeaa3893ea5fa2765fff9646279cea63ec
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>
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
34 specifications. */
36 static ffi_status initialize_aggregate(/*@out@*/ ffi_type *arg)
38 ffi_type **ptr;
40 FFI_ASSERT(arg != NULL);
42 /*@-usedef@*/
44 FFI_ASSERT(arg->elements != NULL);
45 FFI_ASSERT(arg->size == 0);
46 FFI_ASSERT(arg->alignment == 0);
48 ptr = &(arg->elements[0]);
50 while ((*ptr) != NULL)
52 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
53 return FFI_BAD_TYPEDEF;
55 /* Perform a sanity check on the argument type */
56 FFI_ASSERT_VALID_TYPE(*ptr);
58 arg->size = ALIGN(arg->size, (*ptr)->alignment);
59 arg->size += (*ptr)->size;
61 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
62 arg->alignment : (*ptr)->alignment;
64 ptr++;
67 /* Structure size includes tail padding. This is important for
68 structures that fit in one register on ABIs like the PowerPC64
69 Linux ABI that right justify small structs in a register.
70 It's also needed for nested structure layout, for example
71 struct A { long a; char b; }; struct B { struct A x; char y; };
72 should find y at an offset of 2*sizeof(long) and result in a
73 total size of 3*sizeof(long). */
74 arg->size = ALIGN (arg->size, arg->alignment);
76 if (arg->size == 0)
77 return FFI_BAD_TYPEDEF;
78 else
79 return FFI_OK;
81 /*@=usedef@*/
84 #ifndef __CRIS__
85 /* The CRIS ABI specifies structure elements to have byte
86 alignment only, so it completely overrides this functions,
87 which assumes "natural" alignment and padding. */
89 /* Perform machine independent ffi_cif preparation, then call
90 machine dependent routine. */
92 ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif,
93 ffi_abi abi, unsigned int nargs,
94 /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype,
95 /*@dependent@*/ ffi_type **atypes)
97 unsigned bytes = 0;
98 unsigned int i;
99 ffi_type **ptr;
101 FFI_ASSERT(cif != NULL);
102 FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
104 cif->abi = abi;
105 cif->arg_types = atypes;
106 cif->nargs = nargs;
107 cif->rtype = rtype;
109 cif->flags = 0;
111 /* Initialize the return type if necessary */
112 /*@-usedef@*/
113 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
114 return FFI_BAD_TYPEDEF;
115 /*@=usedef@*/
117 /* Perform a sanity check on the return type */
118 FFI_ASSERT_VALID_TYPE(cif->rtype);
120 /* x86-64 and s390 stack space allocation is handled in prep_machdep. */
121 #if !defined M68K && !defined __x86_64__ && !defined S390 && !defined PA
122 /* Make space for the return structure pointer */
123 if (cif->rtype->type == FFI_TYPE_STRUCT
124 #ifdef SPARC
125 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
126 #endif
128 bytes = STACK_ARG_SIZE(sizeof(void*));
129 #endif
131 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
134 /* Initialize any uninitialized aggregate type definitions */
135 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
136 return FFI_BAD_TYPEDEF;
138 /* Perform a sanity check on the argument type, do this
139 check after the initialization. */
140 FFI_ASSERT_VALID_TYPE(*ptr);
142 #if !defined __x86_64__ && !defined S390 && !defined PA
143 #ifdef SPARC
144 if (((*ptr)->type == FFI_TYPE_STRUCT
145 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
146 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
147 && cif->abi != FFI_V9))
148 bytes += sizeof(void*);
149 else
150 #endif
152 /* Add any padding if necessary */
153 if (((*ptr)->alignment - 1) & bytes)
154 bytes = ALIGN(bytes, (*ptr)->alignment);
156 bytes += STACK_ARG_SIZE((*ptr)->size);
158 #endif
161 cif->bytes = bytes;
163 /* Perform machine dependent cif processing */
164 return ffi_prep_cif_machdep(cif);
166 #endif /* not __CRIS__ */