ffi_common.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* -----------------------------------------------------------------------
  2. ffi_common.h - Copyright (C) 2011, 2012, 2013 Anthony Green
  3. Copyright (C) 2007 Free Software Foundation, Inc
  4. Copyright (c) 1996 Red Hat, Inc.
  5. Common internal definitions and macros. Only necessary for building
  6. libffi.
  7. Permission is hereby granted, free of charge, to any person
  8. obtaining a copy of this software and associated documentation
  9. files (the ``Software''), to deal in the Software without
  10. restriction, including without limitation the rights to use, copy,
  11. modify, merge, publish, distribute, sublicense, and/or sell copies
  12. of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be
  15. included in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. ----------------------------------------------------------------------- */
  25. #ifndef FFI_COMMON_H
  26. #define FFI_COMMON_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include <fficonfig.h>
  31. /* Do not move this. Some versions of AIX are very picky about where
  32. this is positioned. */
  33. #ifdef __GNUC__
  34. # if HAVE_ALLOCA_H
  35. # include <alloca.h>
  36. # else
  37. /* mingw64 defines this already in malloc.h. */
  38. # ifndef alloca
  39. # define alloca __builtin_alloca
  40. # endif
  41. # endif
  42. # define MAYBE_UNUSED __attribute__((__unused__))
  43. #else
  44. # define MAYBE_UNUSED
  45. # if HAVE_ALLOCA_H
  46. # include <alloca.h>
  47. # else
  48. # ifdef _AIX
  49. # pragma alloca
  50. # else
  51. # ifndef alloca /* predefined by HP cc +Olibcalls */
  52. # ifdef _MSC_VER
  53. # define alloca _alloca
  54. # else
  55. char *alloca ();
  56. # endif
  57. # endif
  58. # endif
  59. # endif
  60. #endif
  61. /* Check for the existence of memcpy. */
  62. #if STDC_HEADERS
  63. # include <string.h>
  64. #else
  65. # ifndef HAVE_MEMCPY
  66. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  67. # endif
  68. #endif
  69. #if defined(FFI_DEBUG)
  70. #include <stdio.h>
  71. #endif
  72. #ifdef FFI_DEBUG
  73. void ffi_assert(char *expr, char *file, int line);
  74. void ffi_stop_here(void);
  75. void ffi_type_test(ffi_type *a, char *file, int line);
  76. #define FFI_ASSERT(x) ((x) ? (void)0 : ffi_assert(#x, __FILE__,__LINE__))
  77. #define FFI_ASSERT_AT(x, f, l) ((x) ? 0 : ffi_assert(#x, (f), (l)))
  78. #define FFI_ASSERT_VALID_TYPE(x) ffi_type_test (x, __FILE__, __LINE__)
  79. #else
  80. #define FFI_ASSERT(x)
  81. #define FFI_ASSERT_AT(x, f, l)
  82. #define FFI_ASSERT_VALID_TYPE(x)
  83. #endif
  84. /* v cast to size_t and aligned up to a multiple of a */
  85. #define FFI_ALIGN(v, a) (((((size_t) (v))-1) | ((a)-1))+1)
  86. /* v cast to size_t and aligned down to a multiple of a */
  87. #define FFI_ALIGN_DOWN(v, a) (((size_t) (v)) & -a)
  88. /* Perform machine dependent cif processing */
  89. ffi_status ffi_prep_cif_machdep(ffi_cif *cif);
  90. ffi_status ffi_prep_cif_machdep_var(ffi_cif *cif,
  91. unsigned int nfixedargs, unsigned int ntotalargs);
  92. #if HAVE_LONG_DOUBLE_VARIANT
  93. /* Used to adjust size/alignment of ffi types. */
  94. void ffi_prep_types (ffi_abi abi);
  95. #endif
  96. /* Used internally, but overridden by some architectures */
  97. ffi_status ffi_prep_cif_core(ffi_cif *cif,
  98. ffi_abi abi,
  99. unsigned int isvariadic,
  100. unsigned int nfixedargs,
  101. unsigned int ntotalargs,
  102. ffi_type *rtype,
  103. ffi_type **atypes);
  104. /* Translate a data pointer to a code pointer. Needed for closures on
  105. some targets. */
  106. void *ffi_data_to_code_pointer (void *data) FFI_HIDDEN;
  107. /* The arch code calls this to determine if a given closure has a
  108. static trampoline. */
  109. int ffi_tramp_is_present (void *closure) FFI_HIDDEN;
  110. /* Extended cif, used in callback from assembly routine */
  111. typedef struct
  112. {
  113. ffi_cif *cif;
  114. void *rvalue;
  115. void **avalue;
  116. } extended_cif;
  117. /* Terse sized type definitions. */
  118. #if defined(_MSC_VER) || defined(__sgi) || defined(__SUNPRO_C)
  119. typedef unsigned char UINT8;
  120. typedef signed char SINT8;
  121. typedef unsigned short UINT16;
  122. typedef signed short SINT16;
  123. typedef unsigned int UINT32;
  124. typedef signed int SINT32;
  125. # ifdef _MSC_VER
  126. typedef unsigned __int64 UINT64;
  127. typedef signed __int64 SINT64;
  128. # else
  129. # include <inttypes.h>
  130. typedef uint64_t UINT64;
  131. typedef int64_t SINT64;
  132. # endif
  133. #else
  134. typedef unsigned int UINT8 __attribute__((__mode__(__QI__)));
  135. typedef signed int SINT8 __attribute__((__mode__(__QI__)));
  136. typedef unsigned int UINT16 __attribute__((__mode__(__HI__)));
  137. typedef signed int SINT16 __attribute__((__mode__(__HI__)));
  138. typedef unsigned int UINT32 __attribute__((__mode__(__SI__)));
  139. typedef signed int SINT32 __attribute__((__mode__(__SI__)));
  140. typedef unsigned int UINT64 __attribute__((__mode__(__DI__)));
  141. typedef signed int SINT64 __attribute__((__mode__(__DI__)));
  142. #endif
  143. typedef float FLOAT32;
  144. #ifndef __GNUC__
  145. #define __builtin_expect(x, expected_value) (x)
  146. #endif
  147. #define LIKELY(x) __builtin_expect(!!(x),1)
  148. #define UNLIKELY(x) __builtin_expect((x)!=0,0)
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #endif