ffi-x86_64.h 13 KB

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