]> begriffs open source - cmsis/blob - CMSIS/Core/Include/cmsis_compiler.h
CMSIS-Core(M): Fix up MPU implementation.
[cmsis] / CMSIS / Core / Include / cmsis_compiler.h
1 /**************************************************************************//**
2  * @file     cmsis_compiler.h
3  * @brief    CMSIS compiler generic header file
4  * @version  V5.0.2
5  * @date     13. February 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2017 ARM Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
12  * Licensed under the Apache License, Version 2.0 (the License); you may
13  * not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24
25 #ifndef __CMSIS_COMPILER_H
26 #define __CMSIS_COMPILER_H
27
28 #include <stdint.h>
29
30 /*
31  * ARM Compiler 4/5
32  */
33 #if   defined ( __CC_ARM )
34   #include "cmsis_armcc.h"
35
36
37 /*
38  * ARM Compiler 6 (armclang)
39  */
40 #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
41   #include "cmsis_armclang.h"
42
43
44 /*
45  * GNU Compiler
46  */
47 #elif defined ( __GNUC__ )
48   #include "cmsis_gcc.h"
49
50
51 /*
52  * IAR Compiler
53  */
54 #elif defined ( __ICCARM__ )
55
56
57   #ifndef   __ASM
58     #define __ASM                                  __asm
59   #endif
60   #ifndef   __INLINE
61     #define __INLINE                               inline
62   #endif
63   #ifndef   __STATIC_INLINE
64     #define __STATIC_INLINE                        static inline
65   #endif
66
67   #include <cmsis_iar.h>
68
69   /* CMSIS compiler control architecture macros */
70   #if (__CORE__ == __ARM6M__) || (__CORE__ == __ARM6SM__)
71     #ifndef __ARM_ARCH_6M__
72       #define __ARM_ARCH_6M__                      1
73     #endif
74   #elif (__CORE__ == __ARM7M__)
75     #ifndef __ARM_ARCH_7M__
76       #define __ARM_ARCH_7M__                      1
77     #endif
78   #elif (__CORE__ == __ARM7EM__)
79     #ifndef __ARM_ARCH_7EM__
80       #define __ARM_ARCH_7EM__                     1
81     #endif
82   #endif
83
84   #ifndef   __NO_RETURN
85     #define __NO_RETURN                            __noreturn
86   #endif
87   #ifndef   __USED
88     #define __USED                                 __root
89   #endif
90   #ifndef   __WEAK
91     #define __WEAK                                 __weak
92   #endif
93   #ifndef   __PACKED
94     #define __PACKED                               __packed
95   #endif
96   #ifndef   __PACKED_STRUCT
97     #define __PACKED_STRUCT                        __packed struct
98   #endif
99   #ifndef   __PACKED_UNION
100     #define __PACKED_UNION                         __packed union
101   #endif
102   #ifndef   __UNALIGNED_UINT32        /* deprecated */
103     __packed struct T_UINT32 { uint32_t v; };
104     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
105   #endif
106   #ifndef   __UNALIGNED_UINT16_WRITE
107     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
108     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
109   #endif
110   #ifndef   __UNALIGNED_UINT16_READ
111     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
112     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
113   #endif
114   #ifndef   __UNALIGNED_UINT32_WRITE
115     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
116     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
117   #endif
118   #ifndef   __UNALIGNED_UINT32_READ
119     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
120     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
121   #endif
122   #ifndef   __ALIGNED
123     #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
124     #define __ALIGNED(x)
125   #endif
126   #ifndef   __RESTRICT
127     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
128     #define __RESTRICT
129   #endif
130
131   // Workaround for missing __CLZ intrinsic in
132   // various versions of the IAR compilers.
133   // __IAR_FEATURE_CLZ__ should be defined by
134   // the compiler that supports __CLZ internally.
135   #if (defined (__ARM_ARCH_6M__)) && (__ARM_ARCH_6M__ == 1) && (!defined (__IAR_FEATURE_CLZ__))
136     __STATIC_INLINE uint32_t __CLZ(uint32_t data)
137     {
138       if (data == 0u) { return 32u; }
139       
140       uint32_t count = 0;
141       uint32_t mask = 0x80000000;
142       
143       while ((data & mask) == 0)
144       {
145         count += 1u;
146         mask = mask >> 1u;
147       }
148       
149       return (count);
150     }
151   #endif
152
153
154 /*
155  * TI ARM Compiler
156  */
157 #elif defined ( __TI_ARM__ )
158   #include <cmsis_ccs.h>
159
160   #ifndef   __ASM
161     #define __ASM                                  __asm
162   #endif
163   #ifndef   __INLINE
164     #define __INLINE                               inline
165   #endif
166   #ifndef   __STATIC_INLINE
167     #define __STATIC_INLINE                        static inline
168   #endif
169   #ifndef   __NO_RETURN
170     #define __NO_RETURN                            __attribute__((noreturn))
171   #endif
172   #ifndef   __USED
173     #define __USED                                 __attribute__((used))
174   #endif
175   #ifndef   __WEAK
176     #define __WEAK                                 __attribute__((weak))
177   #endif
178   #ifndef   __PACKED
179     #define __PACKED                               __attribute__((packed))
180   #endif
181   #ifndef   __PACKED_STRUCT
182     #define __PACKED_STRUCT                        struct __attribute__((packed))
183   #endif
184   #ifndef   __PACKED_UNION
185     #define __PACKED_UNION                         union __attribute__((packed))
186   #endif
187   #ifndef   __UNALIGNED_UINT32        /* deprecated */
188     struct __attribute__((packed)) T_UINT32 { uint32_t v; };
189     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
190   #endif
191   #ifndef   __UNALIGNED_UINT16_WRITE
192     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
193     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void*)(addr))->v) = (val))
194   #endif
195   #ifndef   __UNALIGNED_UINT16_READ
196     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
197     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
198   #endif
199   #ifndef   __UNALIGNED_UINT32_WRITE
200     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
201     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
202   #endif
203   #ifndef   __UNALIGNED_UINT32_READ
204     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
205     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
206   #endif
207   #ifndef   __ALIGNED
208     #define __ALIGNED(x)                           __attribute__((aligned(x)))
209   #endif
210   #ifndef   __RESTRICT
211     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
212     #define __RESTRICT
213   #endif
214
215
216 /*
217  * TASKING Compiler
218  */
219 #elif defined ( __TASKING__ )
220   /*
221    * The CMSIS functions have been implemented as intrinsics in the compiler.
222    * Please use "carm -?i" to get an up to date list of all intrinsics,
223    * Including the CMSIS ones.
224    */
225
226   #ifndef   __ASM
227     #define __ASM                                  __asm
228   #endif
229   #ifndef   __INLINE
230     #define __INLINE                               inline
231   #endif
232   #ifndef   __STATIC_INLINE
233     #define __STATIC_INLINE                        static inline
234   #endif
235   #ifndef   __NO_RETURN
236     #define __NO_RETURN                            __attribute__((noreturn))
237   #endif
238   #ifndef   __USED
239     #define __USED                                 __attribute__((used))
240   #endif
241   #ifndef   __WEAK
242     #define __WEAK                                 __attribute__((weak))
243   #endif
244   #ifndef   __PACKED
245     #define __PACKED                               __packed__
246   #endif
247   #ifndef   __PACKED_STRUCT
248     #define __PACKED_STRUCT                        struct __packed__
249   #endif
250   #ifndef   __PACKED_UNION
251     #define __PACKED_UNION                         union __packed__
252   #endif
253   #ifndef   __UNALIGNED_UINT32        /* deprecated */
254     struct __packed__ T_UINT32 { uint32_t v; };
255     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
256   #endif
257   #ifndef   __UNALIGNED_UINT16_WRITE
258     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
259     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
260   #endif
261   #ifndef   __UNALIGNED_UINT16_READ
262     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
263     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
264   #endif
265   #ifndef   __UNALIGNED_UINT32_WRITE
266     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
267     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
268   #endif
269   #ifndef   __UNALIGNED_UINT32_READ
270     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
271     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
272   #endif
273   #ifndef   __ALIGNED
274     #define __ALIGNED(x)              __align(x)
275   #endif
276   #ifndef   __RESTRICT
277     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
278     #define __RESTRICT
279   #endif
280
281
282 /*
283  * COSMIC Compiler
284  */
285 #elif defined ( __CSMC__ )
286    #include <cmsis_csm.h>
287
288  #ifndef   __ASM
289     #define __ASM                                  _asm
290   #endif
291   #ifndef   __INLINE
292     #define __INLINE                               inline
293   #endif
294   #ifndef   __STATIC_INLINE
295     #define __STATIC_INLINE                        static inline
296   #endif
297   #ifndef   __NO_RETURN
298     // NO RETURN is automatically detected hence no warning here
299     #define __NO_RETURN
300   #endif
301   #ifndef   __USED
302     #warning No compiler specific solution for __USED. __USED is ignored.
303     #define __USED
304   #endif
305   #ifndef   __WEAK
306     #define __WEAK                                 __weak
307   #endif
308   #ifndef   __PACKED
309     #define __PACKED                               @packed
310   #endif
311   #ifndef   __PACKED_STRUCT
312     #define __PACKED_STRUCT                        @packed struct
313   #endif
314   #ifndef   __PACKED_UNION
315     #define __PACKED_UNION                         @packed union
316   #endif
317   #ifndef   __UNALIGNED_UINT32        /* deprecated */
318     @packed struct T_UINT32 { uint32_t v; };
319     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
320   #endif
321   #ifndef   __UNALIGNED_UINT16_WRITE
322     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
323     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
324   #endif
325   #ifndef   __UNALIGNED_UINT16_READ
326     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
327     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
328   #endif
329   #ifndef   __UNALIGNED_UINT32_WRITE
330     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
331     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
332   #endif
333   #ifndef   __UNALIGNED_UINT32_READ
334     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
335     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
336   #endif
337   #ifndef   __ALIGNED
338     #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
339     #define __ALIGNED(x)
340   #endif
341   #ifndef   __RESTRICT
342     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
343     #define __RESTRICT
344   #endif
345
346
347 #else
348   #error Unknown compiler.
349 #endif
350
351
352 #endif /* __CMSIS_COMPILER_H */
353