ffi_common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ----------------------------------------------------------------------- */
  8. #ifndef FFI_COMMON_H
  9. #define FFI_COMMON_H
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #include <fficonfig.h>
  14. /* Do not move this. Some versions of AIX are very picky about where
  15. this is positioned. */
  16. #ifdef __GNUC__
  17. # if HAVE_ALLOCA_H
  18. # include <alloca.h>
  19. # else
  20. /* mingw64 defines this already in malloc.h. */
  21. # ifndef alloca
  22. # define alloca __builtin_alloca
  23. # endif
  24. # endif
  25. # define MAYBE_UNUSED __attribute__((__unused__))
  26. #else
  27. # define MAYBE_UNUSED
  28. # if HAVE_ALLOCA_H
  29. # include <alloca.h>
  30. # else
  31. # ifdef _AIX
  32. # pragma alloca
  33. # else
  34. # ifndef alloca /* predefined by HP cc +Olibcalls */
  35. # ifdef _MSC_VER
  36. # define alloca _alloca
  37. # else
  38. char *alloca ();
  39. # endif
  40. # endif
  41. # endif
  42. # endif
  43. #endif
  44. /* Check for the existence of memcpy. */
  45. #if STDC_HEADERS
  46. # include <string.h>
  47. #else
  48. # ifndef HAVE_MEMCPY
  49. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  50. # endif
  51. #endif
  52. #if defined(FFI_DEBUG)
  53. #include <stdio.h>
  54. #endif
  55. #ifdef FFI_DEBUG
  56. void ffi_assert(char *expr, char *file, int line);
  57. void ffi_stop_here(void);
  58. void ffi_type_test(ffi_type *a, char *file, int line);
  59. #define FFI_ASSERT(x) ((x) ? (void)0 : ffi_assert(#x, __FILE__,__LINE__))
  60. #define FFI_ASSERT_AT(x, f, l) ((x) ? 0 : ffi_assert(#x, (f), (l)))
  61. #define FFI_ASSERT_VALID_TYPE(x) ffi_type_test (x, __FILE__, __LINE__)
  62. #else
  63. #define FFI_ASSERT(x)
  64. #define FFI_ASSERT_AT(x, f, l)
  65. #define FFI_ASSERT_VALID_TYPE(x)
  66. #endif
  67. /* v cast to size_t and aligned up to a multiple of a */
  68. #define FFI_ALIGN(v, a) (((((size_t) (v))-1) | ((a)-1))+1)
  69. /* v cast to size_t and aligned down to a multiple of a */
  70. #define FFI_ALIGN_DOWN(v, a) (((size_t) (v)) & -a)
  71. /* Perform machine dependent cif processing */
  72. ffi_status ffi_prep_cif_machdep(ffi_cif *cif);
  73. ffi_status ffi_prep_cif_machdep_var(ffi_cif *cif,
  74. unsigned int nfixedargs, unsigned int ntotalargs);
  75. #if HAVE_LONG_DOUBLE_VARIANT
  76. /* Used to adjust size/alignment of ffi types. */
  77. void ffi_prep_types (ffi_abi abi);
  78. #endif
  79. /* Used internally, but overridden by some architectures */
  80. ffi_status ffi_prep_cif_core(ffi_cif *cif,
  81. ffi_abi abi,
  82. unsigned int isvariadic,
  83. unsigned int nfixedargs,
  84. unsigned int ntotalargs,
  85. ffi_type *rtype,
  86. ffi_type **atypes);
  87. /* Translate a data pointer to a code pointer. Needed for closures on
  88. some targets. */
  89. void *ffi_data_to_code_pointer (void *data) FFI_HIDDEN;
  90. /* Extended cif, used in callback from assembly routine */
  91. typedef struct
  92. {
  93. ffi_cif *cif;
  94. void *rvalue;
  95. void **avalue;
  96. } extended_cif;
  97. /* Terse sized type definitions. */
  98. #if defined(_MSC_VER) || defined(__sgi) || defined(__SUNPRO_C)
  99. typedef unsigned char UINT8;
  100. typedef signed char SINT8;
  101. typedef unsigned short UINT16;
  102. typedef signed short SINT16;
  103. typedef unsigned int UINT32;
  104. typedef signed int SINT32;
  105. # ifdef _MSC_VER
  106. typedef unsigned __int64 UINT64;
  107. typedef signed __int64 SINT64;
  108. # else
  109. # include <inttypes.h>
  110. typedef uint64_t UINT64;
  111. typedef int64_t SINT64;
  112. # endif
  113. #else
  114. typedef unsigned int UINT8 __attribute__((__mode__(__QI__)));
  115. typedef signed int SINT8 __attribute__((__mode__(__QI__)));
  116. typedef unsigned int UINT16 __attribute__((__mode__(__HI__)));
  117. typedef signed int SINT16 __attribute__((__mode__(__HI__)));
  118. typedef unsigned int UINT32 __attribute__((__mode__(__SI__)));
  119. typedef signed int SINT32 __attribute__((__mode__(__SI__)));
  120. typedef unsigned int UINT64 __attribute__((__mode__(__DI__)));
  121. typedef signed int SINT64 __attribute__((__mode__(__DI__)));
  122. #endif
  123. typedef float FLOAT32;
  124. #ifndef __GNUC__
  125. #define __builtin_expect(x, expected_value) (x)
  126. #endif
  127. #define LIKELY(x) __builtin_expect(!!(x),1)
  128. #define UNLIKELY(x) __builtin_expect((x)!=0,0)
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif