Daily bump.
[official-gcc.git] / libffi / src / raw_api.c
blobb3ca511463e3f1654a78f35d00921397e2c9c577
1 /* -----------------------------------------------------------------------
2 raw_api.c - Copyright (c) 1999 Red Hat, Inc.
4 Author: Kresten Krab Thorup <krab@gnu.org>
6 $Id $
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 ``Software''), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
26 ----------------------------------------------------------------------- */
28 /* This file defines generic functions for use with the raw api. */
30 #include <ffi.h>
31 #include <ffi_common.h>
33 #if !FFI_NO_RAW_API
35 size_t
36 ffi_raw_size (ffi_cif *cif)
38 size_t result = 0;
39 int i;
41 ffi_type **at = cif->arg_types;
43 for (i = cif->nargs-1; i >= 0; i--, at++)
45 #if !FFI_NO_STRUCTS
46 if ((*at)->type == FFI_TYPE_STRUCT)
47 result += ALIGN (sizeof (void*), FFI_SIZEOF_ARG);
48 else
49 #endif
50 result += ALIGN ((*at)->size, FFI_SIZEOF_ARG);
53 return result;
57 void
58 ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
60 unsigned i;
61 ffi_type **tp = cif->arg_types;
63 #if WORDS_BIGENDIAN
65 for (i = 0; i < cif->nargs; i++, tp++, args++)
67 switch ((*tp)->type)
69 case FFI_TYPE_UINT8:
70 case FFI_TYPE_SINT8:
71 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 1);
72 break;
74 case FFI_TYPE_UINT16:
75 case FFI_TYPE_SINT16:
76 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 2);
77 break;
79 #if FFI_SIZEOF_ARG >= 4
80 case FFI_TYPE_UINT32:
81 case FFI_TYPE_SINT32:
82 *args = (void*) ((char*)(raw++) + FFI_SIZEOF_ARG - 4);
83 break;
84 #endif
86 #if !FFI_NO_STRUCTS
87 case FFI_TYPE_STRUCT:
88 *args = (raw++)->ptr;
89 break;
90 #endif
92 case FFI_TYPE_POINTER:
93 *args = (void*) &(raw++)->ptr;
94 break;
96 default:
97 *args = raw;
98 raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
102 #else /* WORDS_BIGENDIAN */
104 #if !PDP
106 /* then assume little endian */
107 for (i = 0; i < cif->nargs; i++, tp++, args++)
109 #if !FFI_NO_STRUCTS
110 if ((*tp)->type == FFI_TYPE_STRUCT)
112 *args = (raw++)->ptr;
114 else
115 #endif
117 *args = (void*) raw;
118 raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
122 #else
123 #error "pdp endian not supported"
124 #endif /* ! PDP */
126 #endif /* WORDS_BIGENDIAN */
129 void
130 ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
132 unsigned i;
133 ffi_type **tp = cif->arg_types;
135 for (i = 0; i < cif->nargs; i++, tp++, args++)
137 switch ((*tp)->type)
139 case FFI_TYPE_UINT8:
140 (raw++)->uint = *(UINT8*) (*args);
141 break;
143 case FFI_TYPE_SINT8:
144 (raw++)->sint = *(SINT8*) (*args);
145 break;
147 case FFI_TYPE_UINT16:
148 (raw++)->uint = *(UINT16*) (*args);
149 break;
151 case FFI_TYPE_SINT16:
152 (raw++)->sint = *(SINT16*) (*args);
153 break;
155 #if FFI_SIZEOF_ARG >= 4
156 case FFI_TYPE_UINT32:
157 (raw++)->uint = *(UINT32*) (*args);
158 break;
160 case FFI_TYPE_SINT32:
161 (raw++)->sint = *(SINT32*) (*args);
162 break;
163 #endif
165 #if !FFI_NO_STRUCTS
166 case FFI_TYPE_STRUCT:
167 (raw++)->ptr = *args;
168 break;
169 #endif
171 case FFI_TYPE_POINTER:
172 (raw++)->ptr = **(void***) args;
173 break;
175 default:
176 memcpy ((void*) raw->data, (void*)*args, (*tp)->size);
177 raw += ALIGN ((*tp)->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
182 #if !FFI_NATIVE_RAW_API
185 /* This is a generic definition of ffi_raw_call, to be used if the
186 * native system does not provide a machine-specific implementation.
187 * Having this, allows code to be written for the raw API, without
188 * the need for system-specific code to handle input in that format;
189 * these following couple of functions will handle the translation forth
190 * and back automatically. */
192 void ffi_raw_call (ffi_cif *cif, void (*fn)(), void *rvalue, ffi_raw *raw)
194 void **avalue = (void**) alloca (cif->nargs * sizeof (void*));
195 ffi_raw_to_ptrarray (cif, raw, avalue);
196 ffi_call (cif, fn, rvalue, avalue);
199 #if FFI_CLOSURES /* base system provides closures */
201 static void
202 ffi_translate_args (ffi_cif *cif, void *rvalue,
203 void **avalue, void *user_data)
205 ffi_raw *raw = (ffi_raw*)alloca (ffi_raw_size (cif));
206 ffi_raw_closure *cl = (ffi_raw_closure*)user_data;
208 ffi_ptrarray_to_raw (cif, avalue, raw);
209 (*cl->fun) (cif, rvalue, raw, cl->user_data);
212 ffi_status
213 ffi_prep_raw_closure_loc (ffi_raw_closure* cl,
214 ffi_cif *cif,
215 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
216 void *user_data,
217 void *codeloc)
219 ffi_status status;
221 status = ffi_prep_closure_loc ((ffi_closure*) cl,
222 cif,
223 &ffi_translate_args,
224 codeloc,
225 codeloc);
226 if (status == FFI_OK)
228 cl->fun = fun;
229 cl->user_data = user_data;
232 return status;
235 #endif /* FFI_CLOSURES */
236 #endif /* !FFI_NATIVE_RAW_API */
238 #if FFI_CLOSURES
240 /* Again, here is the generic version of ffi_prep_raw_closure, which
241 * will install an intermediate "hub" for translation of arguments from
242 * the pointer-array format, to the raw format */
244 ffi_status
245 ffi_prep_raw_closure (ffi_raw_closure* cl,
246 ffi_cif *cif,
247 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
248 void *user_data)
250 return ffi_prep_raw_closure_loc (cl, cif, fun, user_data, cl);
253 #endif /* FFI_CLOSURES */
255 #endif /* !FFI_NO_RAW_API */