Remove unused parameter.
[official-gcc/constexpr.git] / libffi / src / raw_api.c
blobce21372e299d74225c5194a1dd5edb05185c38aa
1 /* -----------------------------------------------------------------------
2 raw_api.c - Copyright (c) 1999, 2008 Red Hat, Inc.
4 Author: Kresten Krab Thorup <krab@gnu.org>
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
27 /* This file defines generic functions for use with the raw api. */
29 #include <ffi.h>
30 #include <ffi_common.h>
32 #if !FFI_NO_RAW_API
34 size_t
35 ffi_raw_size (ffi_cif *cif)
37 size_t result = 0;
38 int i;
40 ffi_type **at = cif->arg_types;
42 for (i = cif->nargs-1; i >= 0; i--, at++)
44 #if !FFI_NO_STRUCTS
45 if ((*at)->type == FFI_TYPE_STRUCT)
46 result += ALIGN (sizeof (void*), FFI_SIZEOF_ARG);
47 else
48 #endif
49 result += ALIGN ((*at)->size, FFI_SIZEOF_ARG);
52 return result;
56 void
57 ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
59 unsigned i;
60 ffi_type **tp = cif->arg_types;
62 #if WORDS_BIGENDIAN
64 for (i = 0; i < cif->nargs; i++, tp++, args++)
66 switch ((*tp)->type)
68 case FFI_TYPE_UINT8:
69 case FFI_TYPE_SINT8:
70 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 1);
71 break;
73 case FFI_TYPE_UINT16:
74 case FFI_TYPE_SINT16:
75 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 2);
76 break;
78 #if FFI_SIZEOF_ARG >= 4
79 case FFI_TYPE_UINT32:
80 case FFI_TYPE_SINT32:
81 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 4);
82 break;
83 #endif
85 #if !FFI_NO_STRUCTS
86 case FFI_TYPE_STRUCT:
87 *args = (raw++)->ptr;
88 break;
89 #endif
91 case FFI_TYPE_POINTER:
92 *args = (void*) &(raw++)->ptr;
93 break;
95 default:
96 *args = raw;
97 raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
101 #else /* WORDS_BIGENDIAN */
103 #if !PDP
105 /* then assume little endian */
106 for (i = 0; i < cif->nargs; i++, tp++, args++)
108 #if !FFI_NO_STRUCTS
109 if ((*tp)->type == FFI_TYPE_STRUCT)
111 *args = (raw++)->ptr;
113 else
114 #endif
116 *args = (void*) raw;
117 raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
121 #else
122 #error "pdp endian not supported"
123 #endif /* ! PDP */
125 #endif /* WORDS_BIGENDIAN */
128 void
129 ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
131 unsigned i;
132 ffi_type **tp = cif->arg_types;
134 for (i = 0; i < cif->nargs; i++, tp++, args++)
136 switch ((*tp)->type)
138 case FFI_TYPE_UINT8:
139 (raw++)->uint = *(UINT8*) (*args);
140 break;
142 case FFI_TYPE_SINT8:
143 (raw++)->sint = *(SINT8*) (*args);
144 break;
146 case FFI_TYPE_UINT16:
147 (raw++)->uint = *(UINT16*) (*args);
148 break;
150 case FFI_TYPE_SINT16:
151 (raw++)->sint = *(SINT16*) (*args);
152 break;
154 #if FFI_SIZEOF_ARG >= 4
155 case FFI_TYPE_UINT32:
156 (raw++)->uint = *(UINT32*) (*args);
157 break;
159 case FFI_TYPE_SINT32:
160 (raw++)->sint = *(SINT32*) (*args);
161 break;
162 #endif
164 #if !FFI_NO_STRUCTS
165 case FFI_TYPE_STRUCT:
166 (raw++)->ptr = *args;
167 break;
168 #endif
170 case FFI_TYPE_POINTER:
171 (raw++)->ptr = **(void***) args;
172 break;
174 default:
175 memcpy ((void*) raw->data, (void*)*args, (*tp)->size);
176 raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
181 #if !FFI_NATIVE_RAW_API
184 /* This is a generic definition of ffi_raw_call, to be used if the
185 * native system does not provide a machine-specific implementation.
186 * Having this, allows code to be written for the raw API, without
187 * the need for system-specific code to handle input in that format;
188 * these following couple of functions will handle the translation forth
189 * and back automatically. */
191 void ffi_raw_call (ffi_cif *cif, void (*fn)(void), void *rvalue, ffi_raw *raw)
193 void **avalue = (void**) alloca (cif->nargs * sizeof (void*));
194 ffi_raw_to_ptrarray (cif, raw, avalue);
195 ffi_call (cif, fn, rvalue, avalue);
198 #if FFI_CLOSURES /* base system provides closures */
200 static void
201 ffi_translate_args (ffi_cif *cif, void *rvalue,
202 void **avalue, void *user_data)
204 ffi_raw *raw = (ffi_raw*)alloca (ffi_raw_size (cif));
205 ffi_raw_closure *cl = (ffi_raw_closure*)user_data;
207 ffi_ptrarray_to_raw (cif, avalue, raw);
208 (*cl->fun) (cif, rvalue, raw, cl->user_data);
211 ffi_status
212 ffi_prep_raw_closure_loc (ffi_raw_closure* cl,
213 ffi_cif *cif,
214 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
215 void *user_data,
216 void *codeloc)
218 ffi_status status;
220 status = ffi_prep_closure_loc ((ffi_closure*) cl,
221 cif,
222 &ffi_translate_args,
223 codeloc,
224 codeloc);
225 if (status == FFI_OK)
227 cl->fun = fun;
228 cl->user_data = user_data;
231 return status;
234 #endif /* FFI_CLOSURES */
235 #endif /* !FFI_NATIVE_RAW_API */
237 #if FFI_CLOSURES
239 /* Again, here is the generic version of ffi_prep_raw_closure, which
240 * will install an intermediate "hub" for translation of arguments from
241 * the pointer-array format, to the raw format */
243 ffi_status
244 ffi_prep_raw_closure (ffi_raw_closure* cl,
245 ffi_cif *cif,
246 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
247 void *user_data)
249 return ffi_prep_raw_closure_loc (cl, cif, fun, user_data, cl);
252 #endif /* FFI_CLOSURES */
254 #endif /* !FFI_NO_RAW_API */