compiler.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Keep all the ugly #ifdef for system stuff here
  3. */
  4. #ifndef __COMPILER_H__
  5. #define __COMPILER_H__
  6. #include <stddef.h>
  7. #include <stdbool.h>
  8. #ifdef USE_HOSTCC
  9. #if defined(__BEOS__) || \
  10. defined(__NetBSD__) || \
  11. defined(__FreeBSD__) || \
  12. defined(__sun__) || \
  13. defined(__APPLE__)
  14. # include <inttypes.h>
  15. #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__)
  16. # include <stdint.h>
  17. #endif
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #if !defined(__WIN32__) && !defined(__MINGW32__)
  24. # include <sys/mman.h>
  25. #endif
  26. /* Not all systems (like Windows) has this define, and yes
  27. * we do replace/emulate mmap() on those systems ...
  28. */
  29. #ifndef MAP_FAILED
  30. # define MAP_FAILED ((void *)-1)
  31. #endif
  32. #include <fcntl.h>
  33. #ifndef O_BINARY /* should be define'd on __WIN32__ */
  34. #define O_BINARY 0
  35. #endif
  36. #ifdef __linux__
  37. # include <endian.h>
  38. # include <byteswap.h>
  39. #elif defined(__MACH__) || defined(__FreeBSD__)
  40. # include <machine/endian.h>
  41. #endif
  42. #ifdef __FreeBSD__
  43. # include <sys/endian.h> /* htole32 and friends */
  44. # define __BYTE_ORDER BYTE_ORDER
  45. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  46. # define __BIG_ENDIAN BIG_ENDIAN
  47. #elif defined(__OpenBSD__)
  48. # include <endian.h>
  49. # define __BYTE_ORDER BYTE_ORDER
  50. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  51. # define __BIG_ENDIAN BIG_ENDIAN
  52. #endif
  53. #include <time.h>
  54. typedef uint8_t __u8;
  55. typedef uint16_t __u16;
  56. typedef uint32_t __u32;
  57. typedef unsigned int uint;
  58. typedef unsigned long ulong;
  59. #define uswap_16(x) \
  60. ((((x) & 0xff00) >> 8) | \
  61. (((x) & 0x00ff) << 8))
  62. #define uswap_32(x) \
  63. ((((x) & 0xff000000) >> 24) | \
  64. (((x) & 0x00ff0000) >> 8) | \
  65. (((x) & 0x0000ff00) << 8) | \
  66. (((x) & 0x000000ff) << 24))
  67. #define _uswap_64(x, sfx) \
  68. ((((x) & 0xff00000000000000##sfx) >> 56) | \
  69. (((x) & 0x00ff000000000000##sfx) >> 40) | \
  70. (((x) & 0x0000ff0000000000##sfx) >> 24) | \
  71. (((x) & 0x000000ff00000000##sfx) >> 8) | \
  72. (((x) & 0x00000000ff000000##sfx) << 8) | \
  73. (((x) & 0x0000000000ff0000##sfx) << 24) | \
  74. (((x) & 0x000000000000ff00##sfx) << 40) | \
  75. (((x) & 0x00000000000000ff##sfx) << 56))
  76. #if defined(__GNUC__)
  77. # define uswap_64(x) _uswap_64(x, ull)
  78. #else
  79. # define uswap_64(x) _uswap_64(x, )
  80. #endif
  81. #if __BYTE_ORDER == __LITTLE_ENDIAN
  82. # define cpu_to_le16(x) (x)
  83. # define cpu_to_le32(x) (x)
  84. # define cpu_to_le64(x) (x)
  85. # define le16_to_cpu(x) (x)
  86. # define le32_to_cpu(x) (x)
  87. # define le64_to_cpu(x) (x)
  88. # define cpu_to_be16(x) uswap_16(x)
  89. # define cpu_to_be32(x) uswap_32(x)
  90. # define cpu_to_be64(x) uswap_64(x)
  91. # define be16_to_cpu(x) uswap_16(x)
  92. # define be32_to_cpu(x) uswap_32(x)
  93. # define be64_to_cpu(x) uswap_64(x)
  94. #else
  95. # define cpu_to_le16(x) uswap_16(x)
  96. # define cpu_to_le32(x) uswap_32(x)
  97. # define cpu_to_le64(x) uswap_64(x)
  98. # define le16_to_cpu(x) uswap_16(x)
  99. # define le32_to_cpu(x) uswap_32(x)
  100. # define le64_to_cpu(x) uswap_64(x)
  101. # define cpu_to_be16(x) (x)
  102. # define cpu_to_be32(x) (x)
  103. # define cpu_to_be64(x) (x)
  104. # define be16_to_cpu(x) (x)
  105. # define be32_to_cpu(x) (x)
  106. # define be64_to_cpu(x) (x)
  107. #endif
  108. #else /* !USE_HOSTCC */
  109. /* Type for `void *' pointers. */
  110. typedef unsigned long int uintptr_t;
  111. #include <linux/string.h>
  112. #include <linux/types.h>
  113. #include <asm/byteorder.h>
  114. #if __SIZEOF_LONG__ == 8
  115. # define __WORDSIZE 64
  116. #elif __SIZEOF_LONG__ == 4
  117. # define __WORDSIZE 32
  118. #else
  119. /*
  120. * Assume 32-bit for now - only newer toolchains support this feature and
  121. * this is only required for sandbox support at present.
  122. */
  123. #define __WORDSIZE 32
  124. #endif
  125. #endif /* USE_HOSTCC */
  126. #define likely(x) __builtin_expect(!!(x), 1)
  127. #define unlikely(x) __builtin_expect(!!(x), 0)
  128. #ifdef __LP64__
  129. #define MEM_SUPPORT_64BIT_DATA 1
  130. #else
  131. #define MEM_SUPPORT_64BIT_DATA 0
  132. #endif
  133. static inline bool host_build(void) {
  134. #ifdef USE_HOSTCC
  135. return true;
  136. #else
  137. return false;
  138. #endif
  139. }
  140. #endif