compiler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __COMPILER_H
  24. #define __COMPILER_H
  25. #ifndef __ASSEMBLY__
  26. #if __GNUC__
  27. #define likely(x) __builtin_expect(!!(x), 1)
  28. #define unlikely(x) __builtin_expect(!!(x), 0)
  29. #define __UNUSED __attribute__((__unused__))
  30. #define __PACKED __attribute__((packed))
  31. #define __ALIGNED(x) __attribute__((aligned(x)))
  32. #define __PRINTFLIKE(__fmt,__varargs) __attribute__((__format__ (__printf__, __fmt, __varargs)))
  33. #define __SCANFLIKE(__fmt,__varargs) __attribute__((__format__ (__scanf__, __fmt, __varargs)))
  34. #define __SECTION(x) __attribute((section(x)))
  35. #define __PURE __attribute((pure))
  36. #define __CONST __attribute((const))
  37. #define __NO_RETURN __attribute__((noreturn))
  38. #define __MALLOC __attribute__((malloc))
  39. #define __WEAK __attribute__((weak))
  40. #define __GNU_INLINE __attribute__((gnu_inline))
  41. #define __GET_CALLER(x) __builtin_return_address(0)
  42. #define __GET_FRAME(x) __builtin_frame_address(0)
  43. #define INCBIN(symname, sizename, filename, section) \
  44. __asm__ (".section " section "; .align 4; .globl "#symname); \
  45. __asm__ (""#symname ":\n.incbin \"" filename "\""); \
  46. __asm__ (".section " section "; .align 1;"); \
  47. __asm__ (""#symname "_end:"); \
  48. __asm__ (".section " section "; .align 4; .globl "#sizename); \
  49. __asm__ (""#sizename ": .long "#symname "_end - "#symname " - 1"); \
  50. extern unsigned char symname[]; \
  51. extern unsigned int sizename
  52. #define INCFILE(symname, sizename, filename) INCBIN(symname, sizename, filename, ".rodata")
  53. /* look for gcc 3.0 and above */
  54. #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 0)
  55. #define __ALWAYS_INLINE __attribute__((always_inline))
  56. #else
  57. #define __ALWAYS_INLINE
  58. #endif
  59. /* look for gcc 3.1 and above */
  60. #if !defined(__DEPRECATED) // seems to be built in in some versions of the compiler
  61. #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
  62. #define __DEPRECATED __attribute((deprecated))
  63. #else
  64. #define __DEPRECATED
  65. #endif
  66. #endif
  67. /* look for gcc 3.3 and above */
  68. #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
  69. /* the may_alias attribute was introduced in gcc 3.3; before that, there
  70. * was no way to specify aliasiang rules on a type-by-type basis */
  71. #define __MAY_ALIAS __attribute__((may_alias))
  72. /* nonnull was added in gcc 3.3 as well */
  73. #define __NONNULL(x) __attribute((nonnull x))
  74. #else
  75. #define __MAY_ALIAS
  76. #define __NONNULL(x)
  77. #endif
  78. /* look for gcc 3.4 and above */
  79. #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  80. #define __WARN_UNUSED_RESULT __attribute((warn_unused_result))
  81. #else
  82. #define __WARN_UNUSED_RESULT
  83. #endif
  84. #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && !defined(__clang__))
  85. #define __EXTERNALLY_VISIBLE __attribute__((externally_visible))
  86. #else
  87. #define __EXTERNALLY_VISIBLE
  88. #endif
  89. #else
  90. #define likely(x) (x)
  91. #define unlikely(x) (x)
  92. #define __UNUSED
  93. #define __PACKED
  94. #define __ALIGNED(x)
  95. #define __PRINTFLIKE(__fmt,__varargs)
  96. #define __SCANFLIKE(__fmt,__varargs)
  97. #define __SECTION(x)
  98. #define __PURE
  99. #define __CONST
  100. #define __NONNULL(x)
  101. #define __DEPRECATED
  102. #define __WARN_UNUSED_RESULT
  103. #define __ALWAYS_INLINE
  104. #define __MAY_ALIAS
  105. #define __NO_RETURN
  106. #endif
  107. #endif
  108. /* TODO: add type check */
  109. #define countof(a) (sizeof(a) / sizeof((a)[0]))
  110. #endif