* arm.c (ROUND_UP_WORD): Renamed from ROUND_UP.
[official-gcc.git] / libffi / src / prep_cif.c
blobd3c89c365e95ed15bcf8a9a752932ffeddacfe59
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 #if !defined M68K && !defined __x86_64__ && !defined S390
108 /* Make space for the return structure pointer */
109 if (cif->rtype->type == FFI_TYPE_STRUCT
110 #ifdef SPARC
111 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
112 #endif
114 bytes = STACK_ARG_SIZE(sizeof(void*));
115 #endif
117 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
119 /* Perform a sanity check on the argument type */
120 FFI_ASSERT(ffi_type_test(*ptr));
122 /* Initialize any uninitialized aggregate type definitions */
123 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
124 return FFI_BAD_TYPEDEF;
126 #if !defined __x86_64__ && !defined S390
127 #ifdef SPARC
128 if (((*ptr)->type == FFI_TYPE_STRUCT
129 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
130 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
131 && cif->abi != FFI_V9))
132 bytes += sizeof(void*);
133 else
134 #endif
136 /* Add any padding if necessary */
137 if (((*ptr)->alignment - 1) & bytes)
138 bytes = ALIGN(bytes, (*ptr)->alignment);
140 bytes += STACK_ARG_SIZE((*ptr)->size);
142 #endif
145 cif->bytes = bytes;
147 /* Perform machine dependent cif processing */
148 return ffi_prep_cif_machdep(cif);