compiler_abstraction.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Copyright (c) 2010 - 2020, Nordic Semiconductor ASA
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form, except as embedded into a Nordic
  9. Semiconductor ASA integrated circuit in a product or a software update for
  10. such product, must reproduce the above copyright notice, this list of
  11. conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  14. contributors may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. 4. This software, with or without modification, must only be used with a
  17. Nordic Semiconductor ASA integrated circuit.
  18. 5. Any software provided in binary form under this license must not be reverse
  19. engineered, decompiled, modified and/or disassembled.
  20. THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  21. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  29. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef _COMPILER_ABSTRACTION_H
  32. #define _COMPILER_ABSTRACTION_H
  33. /*lint ++flb "Enter library region" */
  34. #ifndef NRF_STRING_CONCATENATE_IMPL
  35. #define NRF_STRING_CONCATENATE_IMPL(lhs, rhs) lhs ## rhs
  36. #endif
  37. #ifndef NRF_STRING_CONCATENATE
  38. #define NRF_STRING_CONCATENATE(lhs, rhs) NRF_STRING_CONCATENATE_IMPL(lhs, rhs)
  39. #endif
  40. #if __LINT__ == 1
  41. #ifndef NRF_STATIC_ASSERT
  42. #define NRF_STATIC_ASSERT(cond, msg)
  43. #endif
  44. #endif
  45. #if defined ( __CC_ARM )
  46. #ifndef __ASM
  47. #define __ASM __asm
  48. #endif
  49. #ifndef __INLINE
  50. #define __INLINE __inline
  51. #endif
  52. #ifndef __WEAK
  53. #define __WEAK __weak
  54. #endif
  55. #ifndef __ALIGN
  56. #define __ALIGN(n) __align(n)
  57. #endif
  58. #ifndef __PACKED
  59. #define __PACKED __packed
  60. #endif
  61. #ifndef __UNUSED
  62. #define __UNUSED __attribute__((unused))
  63. #endif
  64. #define GET_SP() __current_sp()
  65. #ifndef NRF_STATIC_ASSERT
  66. #define NRF_STATIC_ASSERT(cond, msg) \
  67. ;enum { NRF_STRING_CONCATENATE(static_assert_on_line_, __LINE__) = 1 / (!!(cond)) }
  68. #endif
  69. #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  70. #ifndef __ASM
  71. #define __ASM __asm
  72. #endif
  73. #ifndef __INLINE
  74. #define __INLINE __inline
  75. #endif
  76. #ifndef __WEAK
  77. #define __WEAK __attribute__((weak))
  78. #endif
  79. #ifndef __ALIGN
  80. #define __ALIGN(n) __attribute__((aligned(n)))
  81. #endif
  82. #ifndef __PACKED
  83. #define __PACKED __attribute__((packed, aligned(1)))
  84. #endif
  85. #ifndef __UNUSED
  86. #define __UNUSED __attribute__((unused))
  87. #endif
  88. #define GET_SP() __current_sp()
  89. #ifndef NRF_STATIC_ASSERT
  90. #define NRF_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
  91. #endif
  92. #elif defined ( __ICCARM__ )
  93. #ifndef __ASM
  94. #define __ASM __asm
  95. #endif
  96. #ifndef __INLINE
  97. #define __INLINE inline
  98. #endif
  99. #ifndef __WEAK
  100. #define __WEAK __weak
  101. #endif
  102. #ifndef __ALIGN
  103. #define STRING_PRAGMA(x) _Pragma(#x)
  104. #define __ALIGN(n) STRING_PRAGMA(data_alignment = n)
  105. #endif
  106. #ifndef __PACKED
  107. #define __PACKED __packed
  108. #endif
  109. #ifndef __UNUSED
  110. #define __UNUSED
  111. #endif
  112. #define GET_SP() __get_SP()
  113. #ifndef NRF_STATIC_ASSERT
  114. #define NRF_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
  115. #endif
  116. #elif defined ( __GNUC__ ) || defined ( __clang__ )
  117. #ifndef __ASM
  118. #define __ASM __asm
  119. #endif
  120. #ifndef __INLINE
  121. #define __INLINE inline
  122. #endif
  123. #ifndef __WEAK
  124. #define __WEAK __attribute__((weak))
  125. #endif
  126. #ifndef __ALIGN
  127. #define __ALIGN(n) __attribute__((aligned(n)))
  128. #endif
  129. #ifndef __PACKED
  130. #define __PACKED __attribute__((packed))
  131. #endif
  132. #ifndef __UNUSED
  133. #define __UNUSED __attribute__((unused))
  134. #endif
  135. #define GET_SP() gcc_current_sp()
  136. static inline unsigned int gcc_current_sp(void)
  137. {
  138. unsigned int stack_pointer = 0;
  139. __asm__ __volatile__ ("mov %0, sp" : "=r"(stack_pointer));
  140. return stack_pointer;
  141. }
  142. #ifndef NRF_STATIC_ASSERT
  143. #define NRF_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
  144. #endif
  145. #elif defined ( __TASKING__ )
  146. #ifndef __ASM
  147. #define __ASM __asm
  148. #endif
  149. #ifndef __INLINE
  150. #define __INLINE inline
  151. #endif
  152. #ifndef __WEAK
  153. #define __WEAK __attribute__((weak))
  154. #endif
  155. #ifndef __ALIGN
  156. #define __ALIGN(n) __align(n)
  157. #endif
  158. /* Not defined for TASKING. */
  159. #ifndef __PACKED
  160. #define __PACKED
  161. #endif
  162. #ifndef __UNUSED
  163. #define __UNUSED __attribute__((unused))
  164. #endif
  165. #define GET_SP() __get_MSP()
  166. #ifndef NRF_STATIC_ASSERT
  167. #define NRF_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
  168. #endif
  169. #endif
  170. #define NRF_MDK_VERSION_ASSERT_AT_LEAST(major, minor, micro) \
  171. NRF_STATIC_ASSERT( \
  172. ( \
  173. (major < MDK_MAJOR_VERSION) || \
  174. (major == MDK_MAJOR_VERSION && minor < MDK_MINOR_VERSION) || \
  175. (major == MDK_MAJOR_VERSION && minor == MDK_MINOR_VERSION && micro < MDK_MICRO_VERSION) \
  176. ), "MDK version mismatch.")
  177. #define NRF_MDK_VERSION_ASSERT_EXACT(major, minor, micro) \
  178. NRF_STATIC_ASSERT( \
  179. ( \
  180. (major != MDK_MAJOR_VERSION) || \
  181. (major != MDK_MAJOR_VERSION) || \
  182. (major != MDK_MAJOR_VERSION) \
  183. ), "MDK version mismatch.")
  184. /*lint --flb "Leave library region" */
  185. #endif