1 |
732 |
jeremybenn |
/* -----------------------------------------------------------------------
|
2 |
|
|
prep_cif.c - Copyright (c) 1996, 1998, 2007 Red Hat, Inc.
|
3 |
|
|
|
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:
|
11 |
|
|
|
12 |
|
|
The above copyright notice and this permission notice shall be included
|
13 |
|
|
in all copies or substantial portions of the Software.
|
14 |
|
|
|
15 |
|
|
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
|
16 |
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17 |
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18 |
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19 |
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20 |
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
22 |
|
|
DEALINGS IN THE SOFTWARE.
|
23 |
|
|
----------------------------------------------------------------------- */
|
24 |
|
|
|
25 |
|
|
#include <ffi.h>
|
26 |
|
|
#include <ffi_common.h>
|
27 |
|
|
#include <stdlib.h>
|
28 |
|
|
|
29 |
|
|
/* Round up to FFI_SIZEOF_ARG. */
|
30 |
|
|
|
31 |
|
|
#define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
|
32 |
|
|
|
33 |
|
|
/* Perform machine independent initialization of aggregate type
|
34 |
|
|
specifications. */
|
35 |
|
|
|
36 |
|
|
static ffi_status initialize_aggregate(ffi_type *arg)
|
37 |
|
|
{
|
38 |
|
|
ffi_type **ptr;
|
39 |
|
|
|
40 |
|
|
FFI_ASSERT(arg != NULL);
|
41 |
|
|
|
42 |
|
|
FFI_ASSERT(arg->elements != NULL);
|
43 |
|
|
FFI_ASSERT(arg->size == 0);
|
44 |
|
|
FFI_ASSERT(arg->alignment == 0);
|
45 |
|
|
|
46 |
|
|
ptr = &(arg->elements[0]);
|
47 |
|
|
|
48 |
|
|
while ((*ptr) != NULL)
|
49 |
|
|
{
|
50 |
|
|
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
|
51 |
|
|
return FFI_BAD_TYPEDEF;
|
52 |
|
|
|
53 |
|
|
/* Perform a sanity check on the argument type */
|
54 |
|
|
FFI_ASSERT_VALID_TYPE(*ptr);
|
55 |
|
|
|
56 |
|
|
arg->size = ALIGN(arg->size, (*ptr)->alignment);
|
57 |
|
|
arg->size += (*ptr)->size;
|
58 |
|
|
|
59 |
|
|
arg->alignment = (arg->alignment > (*ptr)->alignment) ?
|
60 |
|
|
arg->alignment : (*ptr)->alignment;
|
61 |
|
|
|
62 |
|
|
ptr++;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/* Structure size includes tail padding. This is important for
|
66 |
|
|
structures that fit in one register on ABIs like the PowerPC64
|
67 |
|
|
Linux ABI that right justify small structs in a register.
|
68 |
|
|
It's also needed for nested structure layout, for example
|
69 |
|
|
struct A { long a; char b; }; struct B { struct A x; char y; };
|
70 |
|
|
should find y at an offset of 2*sizeof(long) and result in a
|
71 |
|
|
total size of 3*sizeof(long). */
|
72 |
|
|
arg->size = ALIGN (arg->size, arg->alignment);
|
73 |
|
|
|
74 |
|
|
if (arg->size == 0)
|
75 |
|
|
return FFI_BAD_TYPEDEF;
|
76 |
|
|
else
|
77 |
|
|
return FFI_OK;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
#ifndef __CRIS__
|
81 |
|
|
/* The CRIS ABI specifies structure elements to have byte
|
82 |
|
|
alignment only, so it completely overrides this functions,
|
83 |
|
|
which assumes "natural" alignment and padding. */
|
84 |
|
|
|
85 |
|
|
/* Perform machine independent ffi_cif preparation, then call
|
86 |
|
|
machine dependent routine. */
|
87 |
|
|
|
88 |
|
|
ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
|
89 |
|
|
ffi_type *rtype, ffi_type **atypes)
|
90 |
|
|
{
|
91 |
|
|
unsigned bytes = 0;
|
92 |
|
|
unsigned int i;
|
93 |
|
|
ffi_type **ptr;
|
94 |
|
|
|
95 |
|
|
FFI_ASSERT(cif != NULL);
|
96 |
|
|
#ifndef X86_WIN32
|
97 |
|
|
FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
|
98 |
|
|
#else
|
99 |
|
|
FFI_ASSERT(abi > FFI_FIRST_ABI && abi <= FFI_DEFAULT_ABI
|
100 |
|
|
|| abi == FFI_THISCALL);
|
101 |
|
|
#endif
|
102 |
|
|
|
103 |
|
|
cif->abi = abi;
|
104 |
|
|
cif->arg_types = atypes;
|
105 |
|
|
cif->nargs = nargs;
|
106 |
|
|
cif->rtype = rtype;
|
107 |
|
|
|
108 |
|
|
cif->flags = 0;
|
109 |
|
|
|
110 |
|
|
/* Initialize the return type if necessary */
|
111 |
|
|
if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
|
112 |
|
|
return FFI_BAD_TYPEDEF;
|
113 |
|
|
|
114 |
|
|
/* Perform a sanity check on the return type */
|
115 |
|
|
FFI_ASSERT_VALID_TYPE(cif->rtype);
|
116 |
|
|
|
117 |
|
|
/* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
|
118 |
|
|
#if !defined M68K && !defined __i386__ && !defined __x86_64__ && !defined S390 && !defined PA
|
119 |
|
|
/* Make space for the return structure pointer */
|
120 |
|
|
if (cif->rtype->type == FFI_TYPE_STRUCT
|
121 |
|
|
#ifdef SPARC
|
122 |
|
|
&& (cif->abi != FFI_V9 || cif->rtype->size > 32)
|
123 |
|
|
#endif
|
124 |
|
|
)
|
125 |
|
|
bytes = STACK_ARG_SIZE(sizeof(void*));
|
126 |
|
|
#endif
|
127 |
|
|
|
128 |
|
|
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
|
129 |
|
|
{
|
130 |
|
|
|
131 |
|
|
/* Initialize any uninitialized aggregate type definitions */
|
132 |
|
|
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
|
133 |
|
|
return FFI_BAD_TYPEDEF;
|
134 |
|
|
|
135 |
|
|
/* Perform a sanity check on the argument type, do this
|
136 |
|
|
check after the initialization. */
|
137 |
|
|
FFI_ASSERT_VALID_TYPE(*ptr);
|
138 |
|
|
|
139 |
|
|
#if !defined __i386__ && !defined __x86_64__ && !defined S390 && !defined PA
|
140 |
|
|
#ifdef SPARC
|
141 |
|
|
if (((*ptr)->type == FFI_TYPE_STRUCT
|
142 |
|
|
&& ((*ptr)->size > 16 || cif->abi != FFI_V9))
|
143 |
|
|
|| ((*ptr)->type == FFI_TYPE_LONGDOUBLE
|
144 |
|
|
&& cif->abi != FFI_V9))
|
145 |
|
|
bytes += sizeof(void*);
|
146 |
|
|
else
|
147 |
|
|
#endif
|
148 |
|
|
{
|
149 |
|
|
/* Add any padding if necessary */
|
150 |
|
|
if (((*ptr)->alignment - 1) & bytes)
|
151 |
|
|
bytes = ALIGN(bytes, (*ptr)->alignment);
|
152 |
|
|
|
153 |
|
|
bytes += STACK_ARG_SIZE((*ptr)->size);
|
154 |
|
|
}
|
155 |
|
|
#endif
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
cif->bytes = bytes;
|
159 |
|
|
|
160 |
|
|
/* Perform machine dependent cif processing */
|
161 |
|
|
return ffi_prep_cif_machdep(cif);
|
162 |
|
|
}
|
163 |
|
|
#endif /* not __CRIS__ */
|
164 |
|
|
|
165 |
|
|
#if FFI_CLOSURES
|
166 |
|
|
|
167 |
|
|
ffi_status
|
168 |
|
|
ffi_prep_closure (ffi_closure* closure,
|
169 |
|
|
ffi_cif* cif,
|
170 |
|
|
void (*fun)(ffi_cif*,void*,void**,void*),
|
171 |
|
|
void *user_data)
|
172 |
|
|
{
|
173 |
|
|
return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
#endif
|