/Modules/_ctypes/libffi/include/ffi.h.in

http://unladen-swallow.googlecode.com/ · Autoconf · 393 lines · 196 code · 80 blank · 117 comment · 11 complexity · 4cc0c9fa7d1303e7cf8bcc92874488d2 MD5 · raw file

  1. /* -----------------------------------------------------------------*-C-*-
  2. libffi @VERSION@ - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. ``Software''), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  16. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. ----------------------------------------------------------------------- */
  21. /* -------------------------------------------------------------------
  22. The basic API is described in the README file.
  23. The raw API is designed to bypass some of the argument packing
  24. and unpacking on architectures for which it can be avoided.
  25. The closure API allows interpreted functions to be packaged up
  26. inside a C function pointer, so that they can be called as C functions,
  27. with no understanding on the client side that they are interpreted.
  28. It can also be used in other cases in which it is necessary to package
  29. up a user specified parameter and a function pointer as a single
  30. function pointer.
  31. The closure API must be implemented in order to get its functionality,
  32. e.g. for use by gij. Routines are provided to emulate the raw API
  33. if the underlying platform doesn't allow faster implementation.
  34. More details on the raw and cloure API can be found in:
  35. http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
  36. and
  37. http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
  38. -------------------------------------------------------------------- */
  39. #ifndef LIBFFI_H
  40. #define LIBFFI_H
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /* Specify which architecture libffi is configured for. */
  45. #ifndef @TARGET@
  46. #define @TARGET@
  47. #endif
  48. /* ---- System configuration information --------------------------------- */
  49. #include <ffitarget.h>
  50. #ifndef LIBFFI_ASM
  51. #include <stddef.h>
  52. #include <limits.h>
  53. /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
  54. But we can find it either under the correct ANSI name, or under GNU
  55. C's internal name. */
  56. #ifdef LONG_LONG_MAX
  57. # define FFI_LONG_LONG_MAX LONG_LONG_MAX
  58. #else
  59. # ifdef LLONG_MAX
  60. # define FFI_LONG_LONG_MAX LLONG_MAX
  61. # else
  62. # ifdef __GNUC__
  63. # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
  64. # endif
  65. # endif
  66. #endif
  67. /* The closure code assumes that this works on pointers, i.e. a size_t */
  68. /* can hold a pointer. */
  69. typedef struct _ffi_type
  70. {
  71. size_t size;
  72. unsigned short alignment;
  73. unsigned short type;
  74. struct _ffi_type **elements;
  75. } ffi_type;
  76. #ifndef LIBFFI_HIDE_BASIC_TYPES
  77. #if SCHAR_MAX == 127
  78. # define ffi_type_uchar ffi_type_uint8
  79. # define ffi_type_schar ffi_type_sint8
  80. #else
  81. #error "char size not supported"
  82. #endif
  83. #if SHRT_MAX == 32767
  84. # define ffi_type_ushort ffi_type_uint16
  85. # define ffi_type_sshort ffi_type_sint16
  86. #elif SHRT_MAX == 2147483647
  87. # define ffi_type_ushort ffi_type_uint32
  88. # define ffi_type_sshort ffi_type_sint32
  89. #else
  90. #error "short size not supported"
  91. #endif
  92. #if INT_MAX == 32767
  93. # define ffi_type_uint ffi_type_uint16
  94. # define ffi_type_sint ffi_type_sint16
  95. #elif INT_MAX == 2147483647
  96. # define ffi_type_uint ffi_type_uint32
  97. # define ffi_type_sint ffi_type_sint32
  98. #elif INT_MAX == 9223372036854775807
  99. # define ffi_type_uint ffi_type_uint64
  100. # define ffi_type_sint ffi_type_sint64
  101. #else
  102. #error "int size not supported"
  103. #endif
  104. #if LONG_MAX == 2147483647
  105. # if FFI_LONG_LONG_MAX != 9223372036854775807
  106. #error "no 64-bit data type supported"
  107. # endif
  108. #elif LONG_MAX != 9223372036854775807
  109. #error "long size not supported"
  110. #endif
  111. #if LONG_MAX == 2147483647
  112. # define ffi_type_ulong ffi_type_uint32
  113. # define ffi_type_slong ffi_type_sint32
  114. #elif LONG_MAX == 9223372036854775807
  115. # define ffi_type_ulong ffi_type_uint64
  116. # define ffi_type_slong ffi_type_sint64
  117. #else
  118. #error "long size not supported"
  119. #endif
  120. /* These are defined in types.c */
  121. extern ffi_type ffi_type_void;
  122. extern ffi_type ffi_type_uint8;
  123. extern ffi_type ffi_type_sint8;
  124. extern ffi_type ffi_type_uint16;
  125. extern ffi_type ffi_type_sint16;
  126. extern ffi_type ffi_type_uint32;
  127. extern ffi_type ffi_type_sint32;
  128. extern ffi_type ffi_type_uint64;
  129. extern ffi_type ffi_type_sint64;
  130. extern ffi_type ffi_type_float;
  131. extern ffi_type ffi_type_double;
  132. extern ffi_type ffi_type_pointer;
  133. #if @HAVE_LONG_DOUBLE@
  134. extern ffi_type ffi_type_longdouble;
  135. #else
  136. #define ffi_type_longdouble ffi_type_double
  137. #endif
  138. #endif /* LIBFFI_HIDE_BASIC_TYPES */
  139. typedef enum {
  140. FFI_OK = 0,
  141. FFI_BAD_TYPEDEF,
  142. FFI_BAD_ABI
  143. } ffi_status;
  144. typedef unsigned FFI_TYPE;
  145. typedef struct {
  146. ffi_abi abi;
  147. unsigned nargs;
  148. ffi_type **arg_types;
  149. ffi_type *rtype;
  150. unsigned bytes;
  151. unsigned flags;
  152. #ifdef FFI_EXTRA_CIF_FIELDS
  153. FFI_EXTRA_CIF_FIELDS;
  154. #endif
  155. } ffi_cif;
  156. /* ---- Definitions for the raw API -------------------------------------- */
  157. #ifndef FFI_SIZEOF_ARG
  158. # if LONG_MAX == 2147483647
  159. # define FFI_SIZEOF_ARG 4
  160. # elif LONG_MAX == 9223372036854775807
  161. # define FFI_SIZEOF_ARG 8
  162. # endif
  163. #endif
  164. #ifndef FFI_SIZEOF_JAVA_RAW
  165. # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
  166. #endif
  167. typedef union {
  168. ffi_sarg sint;
  169. ffi_arg uint;
  170. float flt;
  171. char data[FFI_SIZEOF_ARG];
  172. void* ptr;
  173. } ffi_raw;
  174. #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
  175. /* This is a special case for mips64/n32 ABI (and perhaps others) where
  176. sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */
  177. typedef union {
  178. signed int sint;
  179. unsigned int uint;
  180. float flt;
  181. char data[FFI_SIZEOF_JAVA_RAW];
  182. void* ptr;
  183. } ffi_java_raw;
  184. #else
  185. typedef ffi_raw ffi_java_raw;
  186. #endif
  187. void ffi_raw_call (ffi_cif *cif,
  188. void (*fn)(void),
  189. void *rvalue,
  190. ffi_raw *avalue);
  191. void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
  192. void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
  193. size_t ffi_raw_size (ffi_cif *cif);
  194. /* This is analogous to the raw API, except it uses Java parameter */
  195. /* packing, even on 64-bit machines. I.e. on 64-bit machines */
  196. /* longs and doubles are followed by an empty 64-bit word. */
  197. void ffi_java_raw_call (ffi_cif *cif,
  198. void (*fn)(void),
  199. void *rvalue,
  200. ffi_java_raw *avalue);
  201. void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw);
  202. void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args);
  203. size_t ffi_java_raw_size (ffi_cif *cif);
  204. /* ---- Definitions for closures ----------------------------------------- */
  205. #if FFI_CLOSURES
  206. typedef struct {
  207. char tramp[FFI_TRAMPOLINE_SIZE];
  208. ffi_cif *cif;
  209. void (*fun)(ffi_cif*,void*,void**,void*);
  210. void *user_data;
  211. } ffi_closure __attribute__((aligned (8)));
  212. void *ffi_closure_alloc (size_t size, void **code);
  213. void ffi_closure_free (void *);
  214. ffi_status
  215. ffi_prep_closure (ffi_closure*,
  216. ffi_cif *,
  217. void (*fun)(ffi_cif*,void*,void**,void*),
  218. void *user_data);
  219. ffi_status
  220. ffi_prep_closure_loc (ffi_closure*,
  221. ffi_cif *,
  222. void (*fun)(ffi_cif*,void*,void**,void*),
  223. void *user_data,
  224. void*codeloc);
  225. typedef struct {
  226. char tramp[FFI_TRAMPOLINE_SIZE];
  227. ffi_cif *cif;
  228. #if !FFI_NATIVE_RAW_API
  229. /* if this is enabled, then a raw closure has the same layout
  230. as a regular closure. We use this to install an intermediate
  231. handler to do the transaltion, void** -> ffi_raw*. */
  232. void (*translate_args)(ffi_cif*,void*,void**,void*);
  233. void *this_closure;
  234. #endif
  235. void (*fun)(ffi_cif*,void*,ffi_raw*,void*);
  236. void *user_data;
  237. } ffi_raw_closure;
  238. typedef struct {
  239. char tramp[FFI_TRAMPOLINE_SIZE];
  240. ffi_cif *cif;
  241. #if !FFI_NATIVE_RAW_API
  242. /* if this is enabled, then a raw closure has the same layout
  243. as a regular closure. We use this to install an intermediate
  244. handler to do the transaltion, void** -> ffi_raw*. */
  245. void (*translate_args)(ffi_cif*,void*,void**,void*);
  246. void *this_closure;
  247. #endif
  248. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
  249. void *user_data;
  250. } ffi_java_raw_closure;
  251. ffi_status
  252. ffi_prep_raw_closure (ffi_raw_closure*,
  253. ffi_cif *cif,
  254. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  255. void *user_data);
  256. ffi_status
  257. ffi_prep_raw_closure_loc (ffi_raw_closure*,
  258. ffi_cif *cif,
  259. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  260. void *user_data,
  261. void *codeloc);
  262. ffi_status
  263. ffi_prep_java_raw_closure (ffi_java_raw_closure*,
  264. ffi_cif *cif,
  265. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  266. void *user_data);
  267. ffi_status
  268. ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
  269. ffi_cif *cif,
  270. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  271. void *user_data,
  272. void *codeloc);
  273. #endif /* FFI_CLOSURES */
  274. /* ---- Public interface definition -------------------------------------- */
  275. ffi_status ffi_prep_cif(ffi_cif *cif,
  276. ffi_abi abi,
  277. unsigned int nargs,
  278. ffi_type *rtype,
  279. ffi_type **atypes);
  280. void ffi_call(ffi_cif *cif,
  281. void (*fn)(void),
  282. void *rvalue,
  283. void **avalue);
  284. /* Useful for eliminating compiler warnings */
  285. #define FFI_FN(f) ((void (*)(void))f)
  286. /* ---- Definitions shared with assembly code ---------------------------- */
  287. #endif
  288. /* If these change, update src/mips/ffitarget.h. */
  289. #define FFI_TYPE_VOID 0
  290. #define FFI_TYPE_INT 1
  291. #define FFI_TYPE_FLOAT 2
  292. #define FFI_TYPE_DOUBLE 3
  293. #if @HAVE_LONG_DOUBLE@
  294. #define FFI_TYPE_LONGDOUBLE 4
  295. #else
  296. #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
  297. #endif
  298. #define FFI_TYPE_UINT8 5
  299. #define FFI_TYPE_SINT8 6
  300. #define FFI_TYPE_UINT16 7
  301. #define FFI_TYPE_SINT16 8
  302. #define FFI_TYPE_UINT32 9
  303. #define FFI_TYPE_SINT32 10
  304. #define FFI_TYPE_UINT64 11
  305. #define FFI_TYPE_SINT64 12
  306. #define FFI_TYPE_STRUCT 13
  307. #define FFI_TYPE_POINTER 14
  308. /* This should always refer to the last type code (for sanity checks) */
  309. #define FFI_TYPE_LAST FFI_TYPE_POINTER
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif