1 /* -----------------------------------------------------------------------
2 java_raw_api.c - Copyright (c) 1999 Cygnus Solutions
6 Raw_api.c author: Kresten Krab Thorup <krab@gnu.org>
7 Java_raw_api.c author: Hans-J. Boehm <hboehm@hpl.hp.com>
11 Permission is hereby granted, free of charge, to any person obtaining
12 a copy of this software and associated documentation files (the
13 ``Software''), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sublicense, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
22 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 OTHER DEALINGS IN THE SOFTWARE.
29 ----------------------------------------------------------------------- */
31 /* This defines a Java- and 64-bit specific variant of the raw API. */
32 /* It assumes that "raw" argument blocks look like Java stacks on a */
33 /* 64-bit machine. Arguments that can be stored in a single stack */
34 /* stack slots (longs, doubles) occupy 128 bits, but only the first */
35 /* 64 bits are actually used. */
38 #include <ffi_common.h>
40 #if !defined(NO_JAVA_RAW_API) && !defined(FFI_NO_RAW_API)
43 ffi_java_raw_size (ffi_cif *cif)
48 ffi_type **at = cif->arg_types;
50 for (i = cif->nargs-1; i >= 0; i--, at++)
52 switch((*at) -> type) {
56 result += 2 * SIZEOF_ARG;
59 /* No structure parameters in Java. */
71 ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
74 ffi_type **tp = cif->arg_types;
78 for (i = 0; i < cif->nargs; i++, tp++, args++)
84 *args = (void*) ((char*)(raw++) + SIZEOF_ARG - 1);
89 *args = (void*) ((char*)(raw++) + SIZEOF_ARG - 2);
95 *args = (void*) ((char*)(raw++) + SIZEOF_ARG - 4);
100 case FFI_TYPE_UINT64:
101 case FFI_TYPE_SINT64:
102 case FFI_TYPE_DOUBLE:
108 case FFI_TYPE_POINTER:
109 *args = (void*) &(raw++)->ptr;
114 raw += ALIGN ((*tp)->size, SIZEOF_ARG) / SIZEOF_ARG;
118 #else /* WORDS_BIGENDIAN */
122 /* then assume little endian */
123 for (i = 0; i < cif->nargs; i++, tp++, args++)
126 switch((*tp)->type) {
127 case FFI_TYPE_UINT64:
128 case FFI_TYPE_SINT64:
129 case FFI_TYPE_DOUBLE:
134 *args = (void*) raw++;
136 #else /* SIZEOF_ARG != 8 */
138 raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
139 #endif /* SIZEOF_ARG == 8 */
143 #error "pdp endian not supported"
146 #endif /* WORDS_BIGENDIAN */
150 ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
153 ffi_type **tp = cif->arg_types;
155 for (i = 0; i < cif->nargs; i++, tp++, args++)
160 (raw++)->uint = *(UINT8*) (*args);
164 (raw++)->sint = *(SINT8*) (*args);
167 case FFI_TYPE_UINT16:
168 (raw++)->uint = *(UINT16*) (*args);
171 case FFI_TYPE_SINT16:
172 (raw++)->sint = *(SINT16*) (*args);
176 case FFI_TYPE_UINT32:
177 (raw++)->uint = *(UINT32*) (*args);
180 case FFI_TYPE_SINT32:
181 (raw++)->sint = *(SINT32*) (*args);
185 (raw++)->flt = *(FLOAT32*) (*args);
189 case FFI_TYPE_UINT64:
190 case FFI_TYPE_SINT64:
191 case FFI_TYPE_DOUBLE:
192 raw->uint = *(UINT64*) (*args);
197 case FFI_TYPE_POINTER:
198 (raw++)->ptr = **(void***) args;
203 FFI_ASSERT(FALSE); /* Should have covered all cases */
205 memcpy ((void*) raw->data, (void*)*args, (*tp)->size);
206 raw += ALIGN ((*tp)->size, SIZEOF_ARG) / SIZEOF_ARG;
212 #if !FFI_NATIVE_RAW_API
215 /* This is a generic definition of ffi_raw_call, to be used if the
216 * native system does not provide a machine-specific implementation.
217 * Having this, allows code to be written for the raw API, without
218 * the need for system-specific code to handle input in that format;
219 * these following couple of functions will handle the translation forth
220 * and back automatically. */
222 void ffi_java_raw_call (/*@dependent@*/ ffi_cif *cif,
224 /*@out@*/ void *rvalue,
225 /*@dependent@*/ ffi_raw *raw)
227 void **avalue = (void**) alloca (cif->nargs * sizeof (void*));
228 ffi_java_raw_to_ptrarray (cif, raw, avalue);
229 ffi_call (cif, fn, rvalue, avalue);
232 #if FFI_CLOSURES /* base system provides closures */
235 ffi_java_translate_args (ffi_cif *cif, void *rvalue,
236 void **avalue, void *user_data)
238 ffi_raw *raw = (ffi_raw*)alloca (ffi_java_raw_size (cif));
239 ffi_raw_closure *cl = (ffi_raw_closure*)user_data;
241 ffi_java_ptrarray_to_raw (cif, avalue, raw);
242 (*cl->fun) (cif, rvalue, raw, cl->user_data);
245 /* Again, here is the generic version of ffi_prep_raw_closure, which
246 * will install an intermediate "hub" for translation of arguments from
247 * the pointer-array format, to the raw format */
250 ffi_prep_java_raw_closure (ffi_raw_closure* cl,
252 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
257 status = ffi_prep_closure ((ffi_closure*) cl,
259 &ffi_java_translate_args,
261 if (status == FFI_OK)
264 cl->user_data = user_data;
270 #endif /* FFI_CLOSURES */
271 #endif /* !FFI_NATIVE_RAW_API */
272 #endif /* !FFI_NO_RAW_API */