/Modules/_ctypes/libffi_osx/include/ffi.h

http://unladen-swallow.googlecode.com/ · C Header · 352 lines · 231 code · 52 blank · 69 comment · 16 complexity · 8a87f85ed8999b82430c7f71655bffcb MD5 · raw file

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