ffi.h.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* -----------------------------------------------------------------*-C-*-
  2. libffi @VERSION@ - Copyright (c) 2011, 2014 Anthony Green
  3. - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc.
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the ``Software''), to deal in the Software without
  7. restriction, including without limitation the rights to use, copy,
  8. modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. /* -------------------------------------------------------------------
  23. The basic API is described in the README file.
  24. The raw API is designed to bypass some of the argument packing
  25. and unpacking on architectures for which it can be avoided.
  26. The closure API allows interpreted functions to be packaged up
  27. inside a C function pointer, so that they can be called as C functions,
  28. with no understanding on the client side that they are interpreted.
  29. It can also be used in other cases in which it is necessary to package
  30. up a user specified parameter and a function pointer as a single
  31. function pointer.
  32. The closure API must be implemented in order to get its functionality,
  33. e.g. for use by gij. Routines are provided to emulate the raw API
  34. if the underlying platform doesn't allow faster implementation.
  35. More details on the raw and cloure API can be found in:
  36. http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
  37. and
  38. http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
  39. -------------------------------------------------------------------- */
  40. #ifndef LIBFFI_H
  41. #define LIBFFI_H
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /* Specify which architecture libffi is configured for. */
  46. #ifndef @TARGET@
  47. #define @TARGET@
  48. #endif
  49. /* ---- System configuration information --------------------------------- */
  50. #include <ffitarget.h>
  51. #ifndef LIBFFI_ASM
  52. #if defined(_MSC_VER) && !defined(__clang__)
  53. #define __attribute__(X)
  54. #endif
  55. #include <stddef.h>
  56. #include <limits.h>
  57. /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
  58. But we can find it either under the correct ANSI name, or under GNU
  59. C's internal name. */
  60. #define FFI_64_BIT_MAX 9223372036854775807
  61. #ifdef LONG_LONG_MAX
  62. # define FFI_LONG_LONG_MAX LONG_LONG_MAX
  63. #else
  64. # ifdef LLONG_MAX
  65. # define FFI_LONG_LONG_MAX LLONG_MAX
  66. # ifdef _AIX52 /* or newer has C99 LLONG_MAX */
  67. # undef FFI_64_BIT_MAX
  68. # define FFI_64_BIT_MAX 9223372036854775807LL
  69. # endif /* _AIX52 or newer */
  70. # else
  71. # ifdef __GNUC__
  72. # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
  73. # endif
  74. # ifdef _AIX /* AIX 5.1 and earlier have LONGLONG_MAX */
  75. # ifndef __PPC64__
  76. # if defined (__IBMC__) || defined (__IBMCPP__)
  77. # define FFI_LONG_LONG_MAX LONGLONG_MAX
  78. # endif
  79. # endif /* __PPC64__ */
  80. # undef FFI_64_BIT_MAX
  81. # define FFI_64_BIT_MAX 9223372036854775807LL
  82. # endif
  83. # endif
  84. #endif
  85. /* The closure code assumes that this works on pointers, i.e. a size_t */
  86. /* can hold a pointer. */
  87. typedef struct _ffi_type
  88. {
  89. size_t size;
  90. unsigned short alignment;
  91. unsigned short type;
  92. struct _ffi_type **elements;
  93. } ffi_type;
  94. #ifndef LIBFFI_HIDE_BASIC_TYPES
  95. #if SCHAR_MAX == 127
  96. # define ffi_type_uchar ffi_type_uint8
  97. # define ffi_type_schar ffi_type_sint8
  98. #else
  99. #error "char size not supported"
  100. #endif
  101. #if SHRT_MAX == 32767
  102. # define ffi_type_ushort ffi_type_uint16
  103. # define ffi_type_sshort ffi_type_sint16
  104. #elif SHRT_MAX == 2147483647
  105. # define ffi_type_ushort ffi_type_uint32
  106. # define ffi_type_sshort ffi_type_sint32
  107. #else
  108. #error "short size not supported"
  109. #endif
  110. #if INT_MAX == 32767
  111. # define ffi_type_uint ffi_type_uint16
  112. # define ffi_type_sint ffi_type_sint16
  113. #elif INT_MAX == 2147483647
  114. # define ffi_type_uint ffi_type_uint32
  115. # define ffi_type_sint ffi_type_sint32
  116. #elif INT_MAX == 9223372036854775807
  117. # define ffi_type_uint ffi_type_uint64
  118. # define ffi_type_sint ffi_type_sint64
  119. #else
  120. #error "int size not supported"
  121. #endif
  122. #if LONG_MAX == 2147483647
  123. # if FFI_LONG_LONG_MAX != FFI_64_BIT_MAX
  124. #error "no 64-bit data type supported"
  125. # endif
  126. #elif LONG_MAX != FFI_64_BIT_MAX
  127. #error "long size not supported"
  128. #endif
  129. #if LONG_MAX == 2147483647
  130. # define ffi_type_ulong ffi_type_uint32
  131. # define ffi_type_slong ffi_type_sint32
  132. #elif LONG_MAX == FFI_64_BIT_MAX
  133. # define ffi_type_ulong ffi_type_uint64
  134. # define ffi_type_slong ffi_type_sint64
  135. #else
  136. #error "long size not supported"
  137. #endif
  138. /* Need minimal decorations for DLLs to works on Windows. */
  139. /* GCC has autoimport and autoexport. Rely on Libtool to */
  140. /* help MSVC export from a DLL, but always declare data */
  141. /* to be imported for MSVC clients. This costs an extra */
  142. /* indirection for MSVC clients using the static version */
  143. /* of the library, but don't worry about that. Besides, */
  144. /* as a workaround, they can define FFI_BUILDING if they */
  145. /* *know* they are going to link with the static library. */
  146. #if defined _MSC_VER && !defined FFI_BUILDING
  147. #define FFI_EXTERN extern __declspec(dllimport)
  148. #else
  149. #define FFI_EXTERN extern
  150. #endif
  151. /* These are defined in types.c */
  152. FFI_EXTERN ffi_type ffi_type_void;
  153. FFI_EXTERN ffi_type ffi_type_uint8;
  154. FFI_EXTERN ffi_type ffi_type_sint8;
  155. FFI_EXTERN ffi_type ffi_type_uint16;
  156. FFI_EXTERN ffi_type ffi_type_sint16;
  157. FFI_EXTERN ffi_type ffi_type_uint32;
  158. FFI_EXTERN ffi_type ffi_type_sint32;
  159. FFI_EXTERN ffi_type ffi_type_uint64;
  160. FFI_EXTERN ffi_type ffi_type_sint64;
  161. FFI_EXTERN ffi_type ffi_type_float;
  162. FFI_EXTERN ffi_type ffi_type_double;
  163. FFI_EXTERN ffi_type ffi_type_pointer;
  164. #if @HAVE_LONG_DOUBLE@
  165. FFI_EXTERN ffi_type ffi_type_longdouble;
  166. #else
  167. #define ffi_type_longdouble ffi_type_double
  168. #endif
  169. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  170. FFI_EXTERN ffi_type ffi_type_complex_float;
  171. FFI_EXTERN ffi_type ffi_type_complex_double;
  172. #if @HAVE_LONG_DOUBLE@
  173. FFI_EXTERN ffi_type ffi_type_complex_longdouble;
  174. #else
  175. #define ffi_type_complex_longdouble ffi_type_complex_double
  176. #endif
  177. #endif
  178. #endif /* LIBFFI_HIDE_BASIC_TYPES */
  179. typedef enum {
  180. FFI_OK = 0,
  181. FFI_BAD_TYPEDEF,
  182. FFI_BAD_ABI
  183. } ffi_status;
  184. typedef unsigned FFI_TYPE;
  185. typedef struct {
  186. ffi_abi abi;
  187. unsigned nargs;
  188. ffi_type **arg_types;
  189. ffi_type *rtype;
  190. unsigned bytes;
  191. unsigned flags;
  192. #ifdef FFI_EXTRA_CIF_FIELDS
  193. FFI_EXTRA_CIF_FIELDS;
  194. #endif
  195. } ffi_cif;
  196. #if @HAVE_LONG_DOUBLE_VARIANT@
  197. /* Used to adjust size/alignment of ffi types. */
  198. void ffi_prep_types (ffi_abi abi);
  199. #endif
  200. /* Used internally, but overridden by some architectures */
  201. ffi_status ffi_prep_cif_core(ffi_cif *cif,
  202. ffi_abi abi,
  203. unsigned int isvariadic,
  204. unsigned int nfixedargs,
  205. unsigned int ntotalargs,
  206. ffi_type *rtype,
  207. ffi_type **atypes);
  208. /* ---- Definitions for the raw API -------------------------------------- */
  209. #ifndef FFI_SIZEOF_ARG
  210. # if LONG_MAX == 2147483647
  211. # define FFI_SIZEOF_ARG 4
  212. # elif LONG_MAX == FFI_64_BIT_MAX
  213. # define FFI_SIZEOF_ARG 8
  214. # endif
  215. #endif
  216. #ifndef FFI_SIZEOF_JAVA_RAW
  217. # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
  218. #endif
  219. typedef union {
  220. ffi_sarg sint;
  221. ffi_arg uint;
  222. float flt;
  223. char data[FFI_SIZEOF_ARG];
  224. void* ptr;
  225. } ffi_raw;
  226. #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
  227. /* This is a special case for mips64/n32 ABI (and perhaps others) where
  228. sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */
  229. typedef union {
  230. signed int sint;
  231. unsigned int uint;
  232. float flt;
  233. char data[FFI_SIZEOF_JAVA_RAW];
  234. void* ptr;
  235. } ffi_java_raw;
  236. #else
  237. typedef ffi_raw ffi_java_raw;
  238. #endif
  239. void ffi_raw_call (ffi_cif *cif,
  240. void (*fn)(void),
  241. void *rvalue,
  242. ffi_raw *avalue);
  243. void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
  244. void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
  245. size_t ffi_raw_size (ffi_cif *cif);
  246. /* This is analogous to the raw API, except it uses Java parameter */
  247. /* packing, even on 64-bit machines. I.e. on 64-bit machines */
  248. /* longs and doubles are followed by an empty 64-bit word. */
  249. void ffi_java_raw_call (ffi_cif *cif,
  250. void (*fn)(void),
  251. void *rvalue,
  252. ffi_java_raw *avalue);
  253. void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw);
  254. void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args);
  255. size_t ffi_java_raw_size (ffi_cif *cif);
  256. /* ---- Definitions for closures ----------------------------------------- */
  257. #if FFI_CLOSURES
  258. #ifdef _MSC_VER
  259. __declspec(align(8))
  260. #endif
  261. typedef struct {
  262. #if @FFI_EXEC_TRAMPOLINE_TABLE@
  263. void *trampoline_table;
  264. void *trampoline_table_entry;
  265. #else
  266. char tramp[FFI_TRAMPOLINE_SIZE];
  267. #endif
  268. ffi_cif *cif;
  269. void (*fun)(ffi_cif*,void*,void**,void*);
  270. void *user_data;
  271. #ifdef __GNUC__
  272. } ffi_closure __attribute__((aligned (8)));
  273. #else
  274. } ffi_closure;
  275. # ifdef __sgi
  276. # pragma pack 0
  277. # endif
  278. #endif
  279. void *ffi_closure_alloc (size_t size, void **code);
  280. void ffi_closure_free (void *);
  281. ffi_status
  282. ffi_prep_closure (ffi_closure*,
  283. ffi_cif *,
  284. void (*fun)(ffi_cif*,void*,void**,void*),
  285. void *user_data);
  286. ffi_status
  287. ffi_prep_closure_loc (ffi_closure*,
  288. ffi_cif *,
  289. void (*fun)(ffi_cif*,void*,void**,void*),
  290. void *user_data,
  291. void*codeloc);
  292. #ifdef __sgi
  293. # pragma pack 8
  294. #endif
  295. typedef struct {
  296. #if @FFI_EXEC_TRAMPOLINE_TABLE@
  297. void *trampoline_table;
  298. void *trampoline_table_entry;
  299. #else
  300. char tramp[FFI_TRAMPOLINE_SIZE];
  301. #endif
  302. ffi_cif *cif;
  303. #if !FFI_NATIVE_RAW_API
  304. /* if this is enabled, then a raw closure has the same layout
  305. as a regular closure. We use this to install an intermediate
  306. handler to do the transaltion, void** -> ffi_raw*. */
  307. void (*translate_args)(ffi_cif*,void*,void**,void*);
  308. void *this_closure;
  309. #endif
  310. void (*fun)(ffi_cif*,void*,ffi_raw*,void*);
  311. void *user_data;
  312. } ffi_raw_closure;
  313. typedef struct {
  314. #if @FFI_EXEC_TRAMPOLINE_TABLE@
  315. void *trampoline_table;
  316. void *trampoline_table_entry;
  317. #else
  318. char tramp[FFI_TRAMPOLINE_SIZE];
  319. #endif
  320. ffi_cif *cif;
  321. #if !FFI_NATIVE_RAW_API
  322. /* if this is enabled, then a raw closure has the same layout
  323. as a regular closure. We use this to install an intermediate
  324. handler to do the transaltion, void** -> ffi_raw*. */
  325. void (*translate_args)(ffi_cif*,void*,void**,void*);
  326. void *this_closure;
  327. #endif
  328. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
  329. void *user_data;
  330. } ffi_java_raw_closure;
  331. ffi_status
  332. ffi_prep_raw_closure (ffi_raw_closure*,
  333. ffi_cif *cif,
  334. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  335. void *user_data);
  336. ffi_status
  337. ffi_prep_raw_closure_loc (ffi_raw_closure*,
  338. ffi_cif *cif,
  339. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  340. void *user_data,
  341. void *codeloc);
  342. ffi_status
  343. ffi_prep_java_raw_closure (ffi_java_raw_closure*,
  344. ffi_cif *cif,
  345. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  346. void *user_data);
  347. ffi_status
  348. ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
  349. ffi_cif *cif,
  350. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  351. void *user_data,
  352. void *codeloc);
  353. #endif /* FFI_CLOSURES */
  354. #if FFI_GO_CLOSURES
  355. typedef struct {
  356. void *tramp;
  357. ffi_cif *cif;
  358. void (*fun)(ffi_cif*,void*,void**,void*);
  359. } ffi_go_closure;
  360. ffi_status ffi_prep_go_closure (ffi_go_closure*, ffi_cif *,
  361. void (*fun)(ffi_cif*,void*,void**,void*));
  362. void ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
  363. void **avalue, void *closure);
  364. #endif /* FFI_GO_CLOSURES */
  365. /* ---- Public interface definition -------------------------------------- */
  366. ffi_status ffi_prep_cif(ffi_cif *cif,
  367. ffi_abi abi,
  368. unsigned int nargs,
  369. ffi_type *rtype,
  370. ffi_type **atypes);
  371. ffi_status ffi_prep_cif_var(ffi_cif *cif,
  372. ffi_abi abi,
  373. unsigned int nfixedargs,
  374. unsigned int ntotalargs,
  375. ffi_type *rtype,
  376. ffi_type **atypes);
  377. void ffi_call(ffi_cif *cif,
  378. void (*fn)(void),
  379. void *rvalue,
  380. void **avalue);
  381. /* Useful for eliminating compiler warnings */
  382. #define FFI_FN(f) ((void (*)(void))f)
  383. /* ---- Definitions shared with assembly code ---------------------------- */
  384. #endif
  385. /* If these change, update src/mips/ffitarget.h. */
  386. #define FFI_TYPE_VOID 0
  387. #define FFI_TYPE_INT 1
  388. #define FFI_TYPE_FLOAT 2
  389. #define FFI_TYPE_DOUBLE 3
  390. #if @HAVE_LONG_DOUBLE@
  391. #define FFI_TYPE_LONGDOUBLE 4
  392. #else
  393. #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
  394. #endif
  395. #define FFI_TYPE_UINT8 5
  396. #define FFI_TYPE_SINT8 6
  397. #define FFI_TYPE_UINT16 7
  398. #define FFI_TYPE_SINT16 8
  399. #define FFI_TYPE_UINT32 9
  400. #define FFI_TYPE_SINT32 10
  401. #define FFI_TYPE_UINT64 11
  402. #define FFI_TYPE_SINT64 12
  403. #define FFI_TYPE_STRUCT 13
  404. #define FFI_TYPE_POINTER 14
  405. #define FFI_TYPE_COMPLEX 15
  406. /* This should always refer to the last type code (for sanity checks) */
  407. /* ??? Ideally, anyway. There are assembly files that still depend
  408. on this not including COMPLEX. */
  409. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  410. # define FFI_TYPE_LAST FFI_TYPE_COMPLEX
  411. #else
  412. # define FFI_TYPE_LAST FFI_TYPE_POINTER
  413. #endif
  414. #ifdef __cplusplus
  415. }
  416. #endif
  417. #endif