PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/translator/c/src/libffi_msvc/ffi.h

https://bitbucket.org/dac_io/pypy
C++ Header | 315 lines | 191 code | 54 blank | 70 comment | 10 complexity | 5cf163425ad0dc04938c2b41a1109756 MD5 | raw file
  1. /* -----------------------------------------------------------------*-C-*-
  2. libffi 2.00-beta - 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 cloure 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. //XXX #define X86
  45. /* ---- System configuration information --------------------------------- */
  46. #include <ffitarget.h>
  47. #ifndef LIBFFI_ASM
  48. #include <stddef.h>
  49. #include <limits.h>
  50. /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
  51. But we can find it either under the correct ANSI name, or under GNU
  52. C's internal name. */
  53. #ifdef LONG_LONG_MAX
  54. # define FFI_LONG_LONG_MAX LONG_LONG_MAX
  55. #else
  56. # ifdef LLONG_MAX
  57. # define FFI_LONG_LONG_MAX LLONG_MAX
  58. # else
  59. # ifdef __GNUC__
  60. # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
  61. # endif
  62. # ifdef _MSC_VER
  63. # define FFI_LONG_LONG_MAX _I64_MAX
  64. # endif
  65. # endif
  66. #endif
  67. #if SCHAR_MAX == 127
  68. # define ffi_type_uchar ffi_type_uint8
  69. # define ffi_type_schar ffi_type_sint8
  70. #else
  71. #error "char size not supported"
  72. #endif
  73. #if SHRT_MAX == 32767
  74. # define ffi_type_ushort ffi_type_uint16
  75. # define ffi_type_sshort ffi_type_sint16
  76. #elif SHRT_MAX == 2147483647
  77. # define ffi_type_ushort ffi_type_uint32
  78. # define ffi_type_sshort ffi_type_sint32
  79. #else
  80. #error "short size not supported"
  81. #endif
  82. #if INT_MAX == 32767
  83. # define ffi_type_uint ffi_type_uint16
  84. # define ffi_type_sint ffi_type_sint16
  85. #elif INT_MAX == 2147483647
  86. # define ffi_type_uint ffi_type_uint32
  87. # define ffi_type_sint ffi_type_sint32
  88. #elif INT_MAX == 9223372036854775807
  89. # define ffi_type_uint ffi_type_uint64
  90. # define ffi_type_sint ffi_type_sint64
  91. #else
  92. #error "int size not supported"
  93. #endif
  94. #define ffi_type_ulong ffi_type_uint64
  95. #define ffi_type_slong ffi_type_sint64
  96. #if LONG_MAX == 2147483647
  97. # if FFI_LONG_LONG_MAX != 9223372036854775807
  98. #error "no 64-bit data type supported"
  99. # endif
  100. #elif LONG_MAX != 9223372036854775807
  101. #error "long size not supported"
  102. #endif
  103. /* The closure code assumes that this works on pointers, i.e. a size_t */
  104. /* can hold a pointer. */
  105. typedef struct _ffi_type
  106. {
  107. size_t size;
  108. unsigned short alignment;
  109. unsigned short type;
  110. /*@null@*/ struct _ffi_type **elements;
  111. } ffi_type;
  112. /* These are defined in types.c */
  113. extern ffi_type ffi_type_void;
  114. extern ffi_type ffi_type_uint8;
  115. extern ffi_type ffi_type_sint8;
  116. extern ffi_type ffi_type_uint16;
  117. extern ffi_type ffi_type_sint16;
  118. extern ffi_type ffi_type_uint32;
  119. extern ffi_type ffi_type_sint32;
  120. extern ffi_type ffi_type_uint64;
  121. extern ffi_type ffi_type_sint64;
  122. extern ffi_type ffi_type_float;
  123. extern ffi_type ffi_type_double;
  124. extern ffi_type ffi_type_longdouble;
  125. extern ffi_type ffi_type_pointer;
  126. typedef enum {
  127. FFI_OK = 0,
  128. FFI_BAD_TYPEDEF,
  129. FFI_BAD_ABI
  130. } ffi_status;
  131. typedef unsigned FFI_TYPE;
  132. typedef struct {
  133. ffi_abi abi;
  134. unsigned nargs;
  135. /*@dependent@*/ ffi_type **arg_types;
  136. /*@dependent@*/ ffi_type *rtype;
  137. unsigned bytes;
  138. unsigned flags;
  139. #ifdef FFI_EXTRA_CIF_FIELDS
  140. FFI_EXTRA_CIF_FIELDS;
  141. #endif
  142. } ffi_cif;
  143. /* ---- Definitions for the raw API -------------------------------------- */
  144. #ifdef _WIN64
  145. #define FFI_SIZEOF_ARG 8
  146. #else
  147. #define FFI_SIZEOF_ARG 4
  148. #endif
  149. typedef union {
  150. ffi_sarg sint;
  151. ffi_arg uint;
  152. float flt;
  153. char data[FFI_SIZEOF_ARG];
  154. void* ptr;
  155. } ffi_raw;
  156. void ffi_raw_call (/*@dependent@*/ ffi_cif *cif,
  157. void (*fn)(),
  158. /*@out@*/ void *rvalue,
  159. /*@dependent@*/ ffi_raw *avalue);
  160. void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
  161. void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
  162. size_t ffi_raw_size (ffi_cif *cif);
  163. /* This is analogous to the raw API, except it uses Java parameter */
  164. /* packing, even on 64-bit machines. I.e. on 64-bit machines */
  165. /* longs and doubles are followed by an empty 64-bit word. */
  166. void ffi_java_raw_call (/*@dependent@*/ ffi_cif *cif,
  167. void (*fn)(),
  168. /*@out@*/ void *rvalue,
  169. /*@dependent@*/ ffi_raw *avalue);
  170. void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
  171. void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
  172. size_t ffi_java_raw_size (ffi_cif *cif);
  173. /* ---- Definitions for closures ----------------------------------------- */
  174. #if FFI_CLOSURES
  175. typedef struct {
  176. char tramp[FFI_TRAMPOLINE_SIZE];
  177. ffi_cif *cif;
  178. void (*fun)(ffi_cif*,void*,void**,void*);
  179. void *user_data;
  180. } ffi_closure;
  181. ffi_status
  182. ffi_prep_closure (ffi_closure*,
  183. ffi_cif *,
  184. void (*fun)(ffi_cif*,void*,void**,void*),
  185. void *user_data);
  186. typedef struct {
  187. char tramp[FFI_TRAMPOLINE_SIZE];
  188. ffi_cif *cif;
  189. #if !FFI_NATIVE_RAW_API
  190. /* if this is enabled, then a raw closure has the same layout
  191. as a regular closure. We use this to install an intermediate
  192. handler to do the transaltion, void** -> ffi_raw*. */
  193. void (*translate_args)(ffi_cif*,void*,void**,void*);
  194. void *this_closure;
  195. #endif
  196. void (*fun)(ffi_cif*,void*,ffi_raw*,void*);
  197. void *user_data;
  198. } ffi_raw_closure;
  199. ffi_status
  200. ffi_prep_raw_closure (ffi_raw_closure*,
  201. ffi_cif *cif,
  202. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  203. void *user_data);
  204. ffi_status
  205. ffi_prep_java_raw_closure (ffi_raw_closure*,
  206. ffi_cif *cif,
  207. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  208. void *user_data);
  209. #endif /* FFI_CLOSURES */
  210. /* ---- Public interface definition -------------------------------------- */
  211. ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif,
  212. ffi_abi abi,
  213. unsigned int nargs,
  214. /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype,
  215. /*@dependent@*/ ffi_type **atypes);
  216. int
  217. ffi_call(/*@dependent@*/ ffi_cif *cif,
  218. void (*fn)(),
  219. /*@out@*/ void *rvalue,
  220. /*@dependent@*/ void **avalue);
  221. /* Useful for eliminating compiler warnings */
  222. #define FFI_FN(f) ((void (*)())f)
  223. /* ---- Definitions shared with assembly code ---------------------------- */
  224. #endif
  225. /* If these change, update src/mips/ffitarget.h. */
  226. #define FFI_TYPE_VOID 0
  227. #define FFI_TYPE_INT 1
  228. #define FFI_TYPE_FLOAT 2
  229. #define FFI_TYPE_DOUBLE 3
  230. #if 1
  231. #define FFI_TYPE_LONGDOUBLE 4
  232. #else
  233. #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
  234. #endif
  235. #define FFI_TYPE_UINT8 5
  236. #define FFI_TYPE_SINT8 6
  237. #define FFI_TYPE_UINT16 7
  238. #define FFI_TYPE_SINT16 8
  239. #define FFI_TYPE_UINT32 9
  240. #define FFI_TYPE_SINT32 10
  241. #define FFI_TYPE_UINT64 11
  242. #define FFI_TYPE_SINT64 12
  243. #define FFI_TYPE_STRUCT 13
  244. #define FFI_TYPE_POINTER 14
  245. /* This should always refer to the last type code (for sanity checks) */
  246. #define FFI_TYPE_LAST FFI_TYPE_POINTER
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif