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 ----------------------------------------------------------------------- */
25 #include <ffi_common.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
36 static ffi_status initialize_aggregate(/*@out@*/ ffi_type *arg)
40 FFI_ASSERT(arg != NULL);
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;
68 return FFI_BAD_TYPEDEF;
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)
87 FFI_ASSERT(cif != NULL);
88 FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi < FFI_LAST_ABI));
91 cif->arg_types = atypes;
97 /* Initialize the return type if necessary */
99 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
100 return FFI_BAD_TYPEDEF;
103 /* Perform a sanity check on the return type */
104 FFI_ASSERT(ffi_type_test(cif->rtype));
107 /* Make space for the return structure pointer */
108 if (cif->rtype->type == FFI_TYPE_STRUCT
110 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
113 bytes = STACK_ARG_SIZE(sizeof(void*));
116 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
118 /* Perform a sanity check on the argument type */
119 FFI_ASSERT(ffi_type_test(*ptr));
121 /* Initialize any uninitialized aggregate type definitions */
122 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
123 return FFI_BAD_TYPEDEF;
126 if (((*ptr)->type == FFI_TYPE_STRUCT
127 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
128 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
129 && cif->abi != FFI_V9))
130 bytes += sizeof(void*);
134 /* Add any padding if necessary */
135 if (((*ptr)->alignment - 1) & bytes)
136 bytes = ALIGN(bytes, (*ptr)->alignment);
138 bytes += STACK_ARG_SIZE((*ptr)->size);
144 /* Perform machine dependent cif processing */
145 return ffi_prep_cif_machdep(cif);