Add x prefix to v850e case for handling --with-cpu=v850e.
[official-gcc.git] / libffi / src / prep_cif.c
blob985136490ceea04f23ac729516b4623b862f19dd
1 /* -----------------------------------------------------------------------
2 prep_cif.c - Copyright (c) 1996, 1998 Cygnus Solutions
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 SIZEOF_ARG. */
31 #define STACK_ARG_SIZE(x) ALIGN(x, 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(ffi_type_test((*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 if (arg->size == 0)
68 return FFI_BAD_TYPEDEF;
69 else
70 return FFI_OK;
72 /*@=usedef@*/
75 /* Perform machine independent ffi_cif preparation, then call
76 machine dependent routine. */
78 ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif,
79 ffi_abi abi, unsigned int nargs,
80 /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype,
81 /*@dependent@*/ ffi_type **atypes)
83 unsigned bytes = 0;
84 unsigned int i;
85 ffi_type **ptr;
87 FFI_ASSERT(cif != NULL);
88 FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi < FFI_LAST_ABI));
90 cif->abi = abi;
91 cif->arg_types = atypes;
92 cif->nargs = nargs;
93 cif->rtype = rtype;
95 cif->flags = 0;
97 /* Initialize the return type if necessary */
98 /*@-usedef@*/
99 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
100 return FFI_BAD_TYPEDEF;
101 /*@=usedef@*/
103 /* Perform a sanity check on the return type */
104 FFI_ASSERT(ffi_type_test(cif->rtype));
106 /* x86-64 and s390 stack space allocation is handled in prep_machdep.
107 TODO: Disable this for s390 too? */
108 #if !defined M68K && !defined __x86_64__
109 /* Make space for the return structure pointer */
110 if (cif->rtype->type == FFI_TYPE_STRUCT
111 #ifdef SPARC
112 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
113 #endif
115 bytes = STACK_ARG_SIZE(sizeof(void*));
116 #endif
118 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
120 /* Perform a sanity check on the argument type */
121 FFI_ASSERT(ffi_type_test(*ptr));
123 /* Initialize any uninitialized aggregate type definitions */
124 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
125 return FFI_BAD_TYPEDEF;
127 /* TODO: Disable this calculation for s390 too? */
128 #ifndef __x86_64__
129 #ifdef SPARC
130 if (((*ptr)->type == FFI_TYPE_STRUCT
131 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
132 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
133 && cif->abi != FFI_V9))
134 bytes += sizeof(void*);
135 else
136 #endif
138 /* Add any padding if necessary */
139 if (((*ptr)->alignment - 1) & bytes)
140 bytes = ALIGN(bytes, (*ptr)->alignment);
142 bytes += STACK_ARG_SIZE((*ptr)->size);
144 #endif
147 cif->bytes = bytes;
149 /* Perform machine dependent cif processing */
150 return ffi_prep_cif_machdep(cif);