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