ffi.h.in 14 KB

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