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. */
30 #include <ffi_common.h>
35 ffi_raw_size (ffi_cif
*cif
)
40 ffi_type
**at
= cif
->arg_types
;
42 for (i
= cif
->nargs
-1; i
>= 0; i
--, at
++)
45 if ((*at
)->type
== FFI_TYPE_STRUCT
)
46 result
+= ALIGN (sizeof (void*), FFI_SIZEOF_ARG
);
49 result
+= ALIGN ((*at
)->size
, FFI_SIZEOF_ARG
);
57 ffi_raw_to_ptrarray (ffi_cif
*cif
, ffi_raw
*raw
, void **args
)
60 ffi_type
**tp
= cif
->arg_types
;
64 for (i
= 0; i
< cif
->nargs
; i
++, tp
++, args
++)
70 *args
= (void*) ((char*)(raw
++) + FFI_SIZEOF_ARG
- 1);
75 *args
= (void*) ((char*)(raw
++) + FFI_SIZEOF_ARG
- 2);
78 #if FFI_SIZEOF_ARG >= 4
81 *args
= (void*) ((char*)(raw
++) + FFI_SIZEOF_ARG
- 4);
91 case FFI_TYPE_COMPLEX
:
95 case FFI_TYPE_POINTER
:
96 *args
= (void*) &(raw
++)->ptr
;
101 raw
+= ALIGN ((*tp
)->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
105 #else /* WORDS_BIGENDIAN */
109 /* then assume little endian */
110 for (i
= 0; i
< cif
->nargs
; i
++, tp
++, args
++)
113 if ((*tp
)->type
== FFI_TYPE_STRUCT
)
115 *args
= (raw
++)->ptr
;
119 if ((*tp
)->type
== FFI_TYPE_COMPLEX
)
121 *args
= (raw
++)->ptr
;
126 raw
+= ALIGN ((*tp
)->size
, sizeof (void*)) / sizeof (void*);
131 #error "pdp endian not supported"
134 #endif /* WORDS_BIGENDIAN */
138 ffi_ptrarray_to_raw (ffi_cif
*cif
, void **args
, ffi_raw
*raw
)
141 ffi_type
**tp
= cif
->arg_types
;
143 for (i
= 0; i
< cif
->nargs
; i
++, tp
++, args
++)
148 (raw
++)->uint
= *(UINT8
*) (*args
);
152 (raw
++)->sint
= *(SINT8
*) (*args
);
155 case FFI_TYPE_UINT16
:
156 (raw
++)->uint
= *(UINT16
*) (*args
);
159 case FFI_TYPE_SINT16
:
160 (raw
++)->sint
= *(SINT16
*) (*args
);
163 #if FFI_SIZEOF_ARG >= 4
164 case FFI_TYPE_UINT32
:
165 (raw
++)->uint
= *(UINT32
*) (*args
);
168 case FFI_TYPE_SINT32
:
169 (raw
++)->sint
= *(SINT32
*) (*args
);
174 case FFI_TYPE_STRUCT
:
175 (raw
++)->ptr
= *args
;
179 case FFI_TYPE_COMPLEX
:
180 (raw
++)->ptr
= *args
;
183 case FFI_TYPE_POINTER
:
184 (raw
++)->ptr
= **(void***) args
;
188 memcpy ((void*) raw
->data
, (void*)*args
, (*tp
)->size
);
189 raw
+= ALIGN ((*tp
)->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
194 #if !FFI_NATIVE_RAW_API
197 /* This is a generic definition of ffi_raw_call, to be used if the
198 * native system does not provide a machine-specific implementation.
199 * Having this, allows code to be written for the raw API, without
200 * the need for system-specific code to handle input in that format;
201 * these following couple of functions will handle the translation forth
202 * and back automatically. */
204 void ffi_raw_call (ffi_cif
*cif
, void (*fn
)(void), void *rvalue
, ffi_raw
*raw
)
206 void **avalue
= (void**) alloca (cif
->nargs
* sizeof (void*));
207 ffi_raw_to_ptrarray (cif
, raw
, avalue
);
208 ffi_call (cif
, fn
, rvalue
, avalue
);
211 #if FFI_CLOSURES /* base system provides closures */
214 ffi_translate_args (ffi_cif
*cif
, void *rvalue
,
215 void **avalue
, void *user_data
)
217 ffi_raw
*raw
= (ffi_raw
*)alloca (ffi_raw_size (cif
));
218 ffi_raw_closure
*cl
= (ffi_raw_closure
*)user_data
;
220 ffi_ptrarray_to_raw (cif
, avalue
, raw
);
221 (*cl
->fun
) (cif
, rvalue
, raw
, cl
->user_data
);
225 ffi_prep_raw_closure_loc (ffi_raw_closure
* cl
,
227 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
233 status
= ffi_prep_closure_loc ((ffi_closure
*) cl
,
238 if (status
== FFI_OK
)
241 cl
->user_data
= user_data
;
247 #endif /* FFI_CLOSURES */
248 #endif /* !FFI_NATIVE_RAW_API */
252 /* Again, here is the generic version of ffi_prep_raw_closure, which
253 * will install an intermediate "hub" for translation of arguments from
254 * the pointer-array format, to the raw format */
257 ffi_prep_raw_closure (ffi_raw_closure
* cl
,
259 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
262 return ffi_prep_raw_closure_loc (cl
, cif
, fun
, user_data
, cl
);
265 #endif /* FFI_CLOSURES */
267 #endif /* !FFI_NO_RAW_API */