1 /*
2 
3 Copyright (c) 2010 - 2024, Nordic Semiconductor ASA All rights reserved.
4 
5 SPDX-License-Identifier: BSD-3-Clause
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice, this
11    list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright
14    notice, this list of conditions and the following disclaimer in the
15    documentation and/or other materials provided with the distribution.
16 
17 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18    contributors may be used to endorse or promote products derived from this
19    software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32 
33 */
34 
35 #ifndef _COMPILER_ABSTRACTION_H
36 #define _COMPILER_ABSTRACTION_H
37 
38 /*lint ++flb "Enter library region" */
39 
40 #ifndef NRF_STRING_CONCATENATE_IMPL
41     #define NRF_STRING_CONCATENATE_IMPL(lhs, rhs) lhs ## rhs
42 #endif
43 #ifndef NRF_STRING_CONCATENATE
44     #define NRF_STRING_CONCATENATE(lhs, rhs) NRF_STRING_CONCATENATE_IMPL(lhs, rhs)
45 #endif
46 
47 #if defined ( __CC_ARM )
48 
49     #ifndef __ASM
50         #define __ASM               __asm
51     #endif
52 
53     #ifndef __INLINE
54         #define __INLINE            __inline
55     #endif
56 
57     #ifndef __WEAK
58         #define __WEAK              __weak
59     #endif
60 
61     #ifndef __ALIGN
62         #define __ALIGN(n)          __align(n)
63     #endif
64 
65     #ifndef __PACKED
66         #define __PACKED            __packed
67     #endif
68 
69     #ifndef __UNUSED
70         #define __UNUSED            __attribute__((unused))
71     #endif
72 
73     #ifndef __USED
74         #define __USED            __attribute__((used))
75     #endif
76 
77     #ifndef   __HANDLER
78         #define __HANDLER(handler)
79     #endif
80 
81     #define GET_SP()                __current_sp()
82 
83     #ifndef __DEPRECATED
84         #define __DEPRECATED(msg)   __attribute__((deprecated(msg)))
85     #endif
86 
87     #ifndef   __NO_RETURN
88         #define __NO_RETURN         __declspec(noreturn)
89     #endif
90 
91     #ifndef   __RESET_HANDLER_ATTRIBUTE
92         #define __RESET_HANDLER_ATTRIBUTE __NO_RETURN
93     #endif
94 
95     #ifndef   __START
96         #define __START             __main
97     #endif
98 
99     #ifndef __VECTOR_TABLE
100         #define __VECTOR_TABLE      __Vectors
101     #endif
102 
103     #ifndef __VECTOR_TABLE_ATTRIBUTE
104         #define __VECTOR_TABLE_ATTRIBUTE __attribute__((used, section("RESET")))
105     #endif
106 
107     #ifndef __STACK_ATTRIBUTES
108         #define __STACK_ATTRIBUTES(align)
109     #endif
110 
111     #ifndef __HEAP_ATTRIBUTES
112         #define __HEAP_ATTRIBUTES(align)
113     #endif
114 
115     #ifndef NRF_STATIC_ASSERT
116         #define NRF_STATIC_ASSERT(cond, msg) \
117             ;enum { NRF_STRING_CONCATENATE(static_assert_on_line_, __LINE__) = 1 / (!!(cond)) }
118     #endif
119 
120 #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
121 
122     #ifndef __ASM
123         #define __ASM               __asm
124     #endif
125 
126     #ifndef __INLINE
127         #define __INLINE            __inline
128     #endif
129 
130     #ifndef __WEAK
131         #define __WEAK              __attribute__((weak))
132     #endif
133 
134     #ifndef __ALIGN
135         #define __ALIGN(n)          __attribute__((aligned(n)))
136     #endif
137 
138     #ifndef __PACKED
139         #define __PACKED            __attribute__((packed, aligned(1)))
140     #endif
141 
142     #ifndef __UNUSED
143         #define __UNUSED            __attribute__((unused))
144     #endif
145 
146     #ifndef __USED
147         #define __USED              __attribute__((used))
148     #endif
149 
150     #ifndef   __HANDLER
151         #define __HANDLER(handler)    __WEAK __attribute__((alias(handler)))
152     #endif
153 
154     #define GET_SP()                __current_sp()
155 
156     #ifndef __DEPRECATED
157         #define __DEPRECATED(msg)   __attribute__((deprecated(msg)))
158     #endif
159 
160     #ifndef   __NO_RETURN
161         #define __NO_RETURN         __attribute__((__noreturn__))
162     #endif
163 
164     #ifndef   __RESET_HANDLER_ATTRIBUTE
165         #define __RESET_HANDLER_ATTRIBUTE __NO_RETURN
166     #endif
167 
168     #ifndef   __START
169         #define __START             __main
170     #endif
171 
172     #ifndef __VECTOR_TABLE
173         #define __VECTOR_TABLE      __Vectors
174     #endif
175 
176     #ifndef __VECTOR_TABLE_ATTRIBUTE
177         #define __VECTOR_TABLE_ATTRIBUTE  __attribute__((used, section("RESET")))
178     #endif
179 
180     #ifndef __STACK_ATTRIBUTES
181         #define __STACK_ATTRIBUTES(align)  __attribute__ ((aligned(1 << align), used, section(".stack")));
182     #endif
183 
184     #ifndef __HEAP_ATTRIBUTES
185         #define __HEAP_ATTRIBUTES(align)  __attribute__ ((aligned(1 << align), used, section(".heap")));
186     #endif
187 
188     #ifndef NRF_STATIC_ASSERT
189         #ifdef __cplusplus
190             #ifndef _Static_assert
191                 #define _Static_assert static_assert
192             #endif
193         #endif
194         #define NRF_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
195     #endif
196 
197 #elif defined ( __ICCARM__ )
198 
199     #ifndef __ASM
200         #define __ASM               __asm
201     #endif
202 
203     #ifndef __INLINE
204         #define __INLINE            inline
205     #endif
206 
207     #ifndef __WEAK
208         #define __WEAK              __weak
209     #endif
210 
211     #if (__VER__ >= 8000000)
212         #ifndef __ALIGN
213             #define __ALIGN(n) __attribute__((aligned(n)))
214         #endif
215 
216         #ifndef   __PACKED
217             #define __PACKED __attribute__((packed, aligned(1)))
218         #endif
219     #else
220         #ifndef __ALIGN
221             #define STRING_PRAGMA(x) _Pragma(#x)
222             #define __ALIGN(n) STRING_PRAGMA(data_alignment = n)
223         #endif
224 
225         #ifndef   __PACKED
226             #define __PACKED __packed
227         #endif
228     #endif
229 
230     #ifndef __UNUSED
231         #define __UNUSED
232     #endif
233 
234     #ifndef __USED
235         #define __USED __root
236     #endif
237 
238     #ifndef   __HANDLER
239         #define __HANDLER(handler)    __WEAK __attribute__((alias(handler)))
240     #endif
241 
242     #define GET_SP()                __get_SP()
243 
244     #ifndef __DEPRECATED
245         #define __DEPRECATED(msg)   __attribute__((deprecated(msg)))
246     #endif
247 
248     #ifndef   __NO_RETURN
249         #define __NO_RETURN         __attribute__((__noreturn__))
250     #endif
251 
252     #ifndef   __RESET_HANDLER_ATTRIBUTE
253         #define __RESET_HANDLER_ATTRIBUTE __NO_RETURN
254     #endif
255 
256     #ifndef   __START
257         #define __START             __iar_program_start
258     #endif
259 
260     #ifndef __VECTOR_TABLE
261         #define __VECTOR_TABLE      __vector_table
262     #endif
263 
264     #ifndef __VECTOR_TABLE_ATTRIBUTE
265         #define __VECTOR_TABLE_ATTRIBUTE  @".intvec"
266     #endif
267 
268     #ifndef __STACK_ATTRIBUTES
269         #define __STACK_ATTRIBUTES(align)  __attribute__ ((aligned(1 << align), used, section(".stack")));
270     #endif
271 
272     #ifndef __HEAP_ATTRIBUTES
273         #define __HEAP_ATTRIBUTES(align)  __attribute__ ((aligned(1 << align), used, section(".heap")));
274     #endif
275 
276     #ifndef NRF_STATIC_ASSERT
277         #define NRF_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
278     #endif
279 
280 #elif defined   ( __GNUC__ ) ||  defined   ( __clang__ )
281 
282     #ifndef __ASM
283         #define __ASM               __asm
284     #endif
285 
286     #ifndef __INLINE
287         #define __INLINE            inline
288     #endif
289 
290     #ifndef   __STATIC_FORCEINLINE
291         #define __STATIC_FORCEINLINE __attribute__((always_inline)) static inline
292     #endif
293 
294     #ifndef __WEAK
295         #define __WEAK              __attribute__((weak))
296     #endif
297 
298     #ifndef __ALIGN
299         #define __ALIGN(n)          __attribute__((aligned(n)))
300     #endif
301 
302     #ifndef __PACKED
303         #define __PACKED           __attribute__((packed))
304     #endif
305 
306     #ifndef __UNUSED
307         #define __UNUSED            __attribute__((unused))
308     #endif
309 
310     #ifndef __USED
311         #define __USED              __attribute__((used))
312     #endif
313 
314     #ifndef   __HANDLER
315         #define __HANDLER(handler)    __WEAK __attribute__((alias(handler)))
316     #endif
317 
318     #define GET_SP()                gcc_current_sp()
319 
320     #ifndef __DEPRECATED
321         #define __DEPRECATED(msg)   __attribute__((deprecated(msg)))
322     #endif
323 
324     #ifndef   __NO_RETURN
325         #define __NO_RETURN         __attribute__((__noreturn__))
326     #endif
327 
328     #ifndef   __RESET_HANDLER_ATTRIBUTE
329         #define __RESET_HANDLER_ATTRIBUTE __NO_RETURN __attribute__((section(".startup")))
330     #endif
331 
332     #ifndef   __START
333         #define __START             _start
334     #endif
335 
336     #ifndef __VECTOR_TABLE
337         #define __VECTOR_TABLE      __Vectors
338     #endif
339 
340     #ifndef __VECTOR_TABLE_ATTRIBUTE
341         #define __VECTOR_TABLE_ATTRIBUTE  __attribute__((used, section(".isr_vector")))
342     #endif
343 
344     #ifndef __STACK_ATTRIBUTES
345         #define __STACK_ATTRIBUTES(align)  __attribute__ ((aligned(1 << align), used, section(".stack")));
346     #endif
347 
348     #ifndef __HEAP_ATTRIBUTES
349         #define __HEAP_ATTRIBUTES(align)  __attribute__ ((aligned(1 << align), used, section(".heap")));
350     #endif
351 
gcc_current_sp(void)352     static inline unsigned int gcc_current_sp(void)
353     {
354         unsigned int stack_pointer = 0;
355         __asm__ __volatile__ ("mov %0, sp" : "=r"(stack_pointer));
356         return stack_pointer;
357     }
358 
359     #ifndef NRF_STATIC_ASSERT
360         #ifdef __cplusplus
361             #ifndef _Static_assert
362                 #define _Static_assert static_assert
363             #endif
364         #endif
365         #define NRF_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
366     #endif
367 
368 #elif defined   ( __TASKING__ )
369 
370     #ifndef __ASM
371         #define __ASM               __asm
372     #endif
373 
374     #ifndef __INLINE
375         #define __INLINE            inline
376     #endif
377 
378     #ifndef __WEAK
379         #define __WEAK              __attribute__((weak))
380     #endif
381 
382     #ifndef __ALIGN
383         #define __ALIGN(n)          __align(n)
384     #endif
385 
386     /* Not defined for TASKING. */
387     #ifndef __PACKED
388         #define __PACKED
389     #endif
390 
391     #ifndef __UNUSED
392         #define __UNUSED            __attribute__((unused))
393     #endif
394 
395     #ifndef __USED
396         #define __USED              __attribute__((used))
397     #endif
398 
399     #ifndef   __HANDLER
400         #define __HANDLER(handler)  __WEAK __attribute__((alias(handler)))
401     #endif
402 
403     #define GET_SP()                __get_MSP()
404 
405     #ifndef __DEPRECATED
406         #define __DEPRECATED(msg)
407     #endif
408 
409     #ifndef   __NO_RETURN
410         #define __NO_RETURN         __attribute__((__noreturn__))
411     #endif
412 
413     #ifndef   __RESET_HANDLER_ATTRIBUTE
414         #define __RESET_HANDLER_ATTRIBUTE __NO_RETURN __attribute__((section(".startup")))
415     #endif
416 
417     #ifndef   __START
418         #define __START             _start
419     #endif
420 
421     #ifndef __VECTOR_TABLE
422         #define __VECTOR_TABLE      __Vectors
423     #endif
424 
425     #ifndef __VECTOR_TABLE_ATTRIBUTE
426         #define __VECTOR_TABLE_ATTRIBUTE
427     #endif
428 
429     #ifndef __STACK_ATTRIBUTES
430         #define __STACK_ATTRIBUTES(align)
431     #endif
432 
433     #ifndef __HEAP_ATTRIBUTES
434         #define __HEAP_ATTRIBUTES(align)
435     #endif
436 
437     #ifndef NRF_STATIC_ASSERT
438         #define NRF_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
439     #endif
440 
441 #endif
442 
443 #define NRF_MDK_VERSION_ASSERT_AT_LEAST(major, minor, micro) \
444     NRF_STATIC_ASSERT( \
445         ( \
446             (major < MDK_MAJOR_VERSION) || \
447             (major == MDK_MAJOR_VERSION && minor < MDK_MINOR_VERSION) || \
448             (major == MDK_MAJOR_VERSION && minor == MDK_MINOR_VERSION && micro < MDK_MICRO_VERSION) \
449         ), "MDK version mismatch.")
450 
451 #define NRF_MDK_VERSION_ASSERT_EXACT(major, minor, micro) \
452     NRF_STATIC_ASSERT( \
453         ( \
454             (major != MDK_MAJOR_VERSION) || \
455             (major != MDK_MAJOR_VERSION) || \
456             (major != MDK_MAJOR_VERSION) \
457         ), "MDK version mismatch.")
458 
459 /*lint --flb "Leave library region" */
460 
461 #endif
462