1 /* -----------------------------------------------------------------------
2 java_raw_api.c - Copyright (c) 1999, 2007, 2008 Red Hat, Inc.
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,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 DEALINGS IN THE SOFTWARE.
30 ----------------------------------------------------------------------- */
32 /* This defines a Java- and 64-bit specific variant of the raw API. */
33 /* It assumes that "raw" argument blocks look like Java stacks on a */
34 /* 64-bit machine. Arguments that can be stored in a single stack */
35 /* stack slots (longs, doubles) occupy 128 bits, but only the first */
36 /* 64 bits are actually used. */
39 #include <ffi_common.h>
42 #if !defined(NO_JAVA_RAW_API)
45 ffi_java_raw_size (ffi_cif
*cif
)
50 ffi_type
**at
= cif
->arg_types
;
52 for (i
= cif
->nargs
-1; i
>= 0; i
--, at
++)
54 switch((*at
) -> type
) {
58 result
+= 2 * FFI_SIZEOF_JAVA_RAW
;
61 /* No structure parameters in Java. */
63 case FFI_TYPE_COMPLEX
:
64 /* Not supported yet. */
67 result
+= FFI_SIZEOF_JAVA_RAW
;
76 ffi_java_raw_to_ptrarray (ffi_cif
*cif
, ffi_java_raw
*raw
, void **args
)
79 ffi_type
**tp
= cif
->arg_types
;
83 for (i
= 0; i
< cif
->nargs
; i
++, tp
++, args
++)
89 *args
= (void*) ((char*)(raw
++) + 3);
94 *args
= (void*) ((char*)(raw
++) + 2);
97 #if FFI_SIZEOF_JAVA_RAW == 8
100 case FFI_TYPE_DOUBLE
:
106 case FFI_TYPE_POINTER
:
107 *args
= (void*) &(raw
++)->ptr
;
110 case FFI_TYPE_COMPLEX
:
111 /* Not supported yet. */
117 ALIGN ((*tp
)->size
, sizeof(ffi_java_raw
)) / sizeof(ffi_java_raw
);
121 #else /* WORDS_BIGENDIAN */
125 /* then assume little endian */
126 for (i
= 0; i
< cif
->nargs
; i
++, tp
++, args
++)
128 #if FFI_SIZEOF_JAVA_RAW == 8
129 switch((*tp
)->type
) {
130 case FFI_TYPE_UINT64
:
131 case FFI_TYPE_SINT64
:
132 case FFI_TYPE_DOUBLE
:
136 case FFI_TYPE_COMPLEX
:
137 /* Not supported yet. */
140 *args
= (void*) raw
++;
142 #else /* FFI_SIZEOF_JAVA_RAW != 8 */
145 ALIGN ((*tp
)->size
, sizeof(ffi_java_raw
)) / sizeof(ffi_java_raw
);
146 #endif /* FFI_SIZEOF_JAVA_RAW == 8 */
150 #error "pdp endian not supported"
153 #endif /* WORDS_BIGENDIAN */
157 ffi_java_ptrarray_to_raw (ffi_cif
*cif
, void **args
, ffi_java_raw
*raw
)
160 ffi_type
**tp
= cif
->arg_types
;
162 for (i
= 0; i
< cif
->nargs
; i
++, tp
++, args
++)
168 *(UINT32
*)(raw
++) = *(UINT8
*) (*args
);
170 (raw
++)->uint
= *(UINT8
*) (*args
);
176 *(SINT32
*)(raw
++) = *(SINT8
*) (*args
);
178 (raw
++)->sint
= *(SINT8
*) (*args
);
182 case FFI_TYPE_UINT16
:
184 *(UINT32
*)(raw
++) = *(UINT16
*) (*args
);
186 (raw
++)->uint
= *(UINT16
*) (*args
);
190 case FFI_TYPE_SINT16
:
192 *(SINT32
*)(raw
++) = *(SINT16
*) (*args
);
194 (raw
++)->sint
= *(SINT16
*) (*args
);
198 case FFI_TYPE_UINT32
:
200 *(UINT32
*)(raw
++) = *(UINT32
*) (*args
);
202 (raw
++)->uint
= *(UINT32
*) (*args
);
206 case FFI_TYPE_SINT32
:
208 *(SINT32
*)(raw
++) = *(SINT32
*) (*args
);
210 (raw
++)->sint
= *(SINT32
*) (*args
);
215 (raw
++)->flt
= *(FLOAT32
*) (*args
);
218 #if FFI_SIZEOF_JAVA_RAW == 8
219 case FFI_TYPE_UINT64
:
220 case FFI_TYPE_SINT64
:
221 case FFI_TYPE_DOUBLE
:
222 raw
->uint
= *(UINT64
*) (*args
);
227 case FFI_TYPE_POINTER
:
228 (raw
++)->ptr
= **(void***) args
;
232 #if FFI_SIZEOF_JAVA_RAW == 8
233 FFI_ASSERT(0); /* Should have covered all cases */
235 memcpy ((void*) raw
->data
, (void*)*args
, (*tp
)->size
);
237 ALIGN ((*tp
)->size
, sizeof(ffi_java_raw
)) / sizeof(ffi_java_raw
);
243 #if !FFI_NATIVE_RAW_API
246 ffi_java_rvalue_to_raw (ffi_cif
*cif
, void *rvalue
)
248 #if WORDS_BIGENDIAN && FFI_SIZEOF_ARG == 8
249 switch (cif
->rtype
->type
)
252 case FFI_TYPE_UINT16
:
253 case FFI_TYPE_UINT32
:
254 *(UINT64
*)rvalue
<<= 32;
258 case FFI_TYPE_SINT16
:
259 case FFI_TYPE_SINT32
:
261 #if FFI_SIZEOF_JAVA_RAW == 4
262 case FFI_TYPE_POINTER
:
264 *(SINT64
*)rvalue
<<= 32;
267 case FFI_TYPE_COMPLEX
:
268 /* Not supported yet. */
278 ffi_java_raw_to_rvalue (ffi_cif
*cif
, void *rvalue
)
280 #if WORDS_BIGENDIAN && FFI_SIZEOF_ARG == 8
281 switch (cif
->rtype
->type
)
284 case FFI_TYPE_UINT16
:
285 case FFI_TYPE_UINT32
:
286 *(UINT64
*)rvalue
>>= 32;
290 case FFI_TYPE_SINT16
:
291 case FFI_TYPE_SINT32
:
293 *(SINT64
*)rvalue
>>= 32;
296 case FFI_TYPE_COMPLEX
:
297 /* Not supported yet. */
306 /* This is a generic definition of ffi_raw_call, to be used if the
307 * native system does not provide a machine-specific implementation.
308 * Having this, allows code to be written for the raw API, without
309 * the need for system-specific code to handle input in that format;
310 * these following couple of functions will handle the translation forth
311 * and back automatically. */
313 void ffi_java_raw_call (ffi_cif
*cif
, void (*fn
)(void), void *rvalue
,
316 void **avalue
= (void**) alloca (cif
->nargs
* sizeof (void*));
317 ffi_java_raw_to_ptrarray (cif
, raw
, avalue
);
318 ffi_call (cif
, fn
, rvalue
, avalue
);
319 ffi_java_rvalue_to_raw (cif
, rvalue
);
322 #if FFI_CLOSURES /* base system provides closures */
325 ffi_java_translate_args (ffi_cif
*cif
, void *rvalue
,
326 void **avalue
, void *user_data
)
328 ffi_java_raw
*raw
= (ffi_java_raw
*)alloca (ffi_java_raw_size (cif
));
329 ffi_raw_closure
*cl
= (ffi_raw_closure
*)user_data
;
331 ffi_java_ptrarray_to_raw (cif
, avalue
, raw
);
332 (*cl
->fun
) (cif
, rvalue
, (ffi_raw
*)raw
, cl
->user_data
);
333 ffi_java_raw_to_rvalue (cif
, rvalue
);
337 ffi_prep_java_raw_closure_loc (ffi_java_raw_closure
* cl
,
339 void (*fun
)(ffi_cif
*,void*,ffi_java_raw
*,void*),
345 status
= ffi_prep_closure_loc ((ffi_closure
*) cl
,
347 &ffi_java_translate_args
,
350 if (status
== FFI_OK
)
353 cl
->user_data
= user_data
;
359 /* Again, here is the generic version of ffi_prep_raw_closure, which
360 * will install an intermediate "hub" for translation of arguments from
361 * the pointer-array format, to the raw format */
364 ffi_prep_java_raw_closure (ffi_java_raw_closure
* cl
,
366 void (*fun
)(ffi_cif
*,void*,ffi_java_raw
*,void*),
369 return ffi_prep_java_raw_closure_loc (cl
, cif
, fun
, user_data
, cl
);
372 #endif /* FFI_CLOSURES */
373 #endif /* !FFI_NATIVE_RAW_API */
374 #endif /* !NO_JAVA_RAW_API */