1 /***************************************************************************//**
2  * @file
3  * @brief Silabs Compiler definitions.
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2022 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 
31 #ifndef SL_COMPILER_H
32 #define SL_COMPILER_H
33 
34 /***************************************************************************//**
35  * @addtogroup compiler Compiler definitions
36  * @brief Compiler definitions
37  * @{
38  ******************************************************************************/
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #if defined (__GNUC__)
45 
46 // Fallback for __has_builtin.
47   #ifndef __has_builtin
48     #define __has_builtin(x) (0)
49   #endif
50 
51 // Compiler specific defines.
52   #ifndef   __ASM
53     #define __ASM                  __asm
54   #endif
55   #ifndef   __INLINE
56     #define __INLINE               inline
57   #endif
58   #ifndef   __STATIC_INLINE
59     #define __STATIC_INLINE        static inline
60   #endif
61   #ifndef   __STATIC_FORCEINLINE
62     #define __STATIC_FORCEINLINE   __attribute__((always_inline)) static inline
63   #endif
64   #ifndef   __NO_RETURN
65     #define __NO_RETURN            __attribute__((__noreturn__))
66   #endif
67   #ifndef   __USED
68     #define __USED                 __attribute__((used))
69   #endif
70   #ifndef   __WEAK
71     #define __WEAK                 __attribute__((weak))
72   #endif
73   #ifndef   __PACKED
74     #define __PACKED               __attribute__((packed, aligned(1)))
75   #endif
76   #ifndef   __PACKED_STRUCT
77     #define __PACKED_STRUCT        struct __attribute__((packed, aligned(1)))
78   #endif
79   #ifndef   __PACKED_UNION
80     #define __PACKED_UNION         union __attribute__((packed, aligned(1)))
81   #endif
82   #ifndef   __ALIGNED
83     #define __ALIGNED(x)           __attribute__((aligned(x)))
84   #endif
85   #ifndef   __RESTRICT
86     #define __RESTRICT             __restrict
87   #endif
88 
89 #elif defined(__IAR_SYSTEMS_ICC__)
90 
91   #pragma system_include
92 
93   #if (__VER__ >= 8000000)
94     #define __ICCARM_V8 1
95   #else
96     #define __ICCARM_V8 0
97   #endif
98 
99   #ifndef __ALIGNED
100     #if __ICCARM_V8
101       #define __ALIGNED(x) __attribute__((aligned(x)))
102     #elif (__VER__ >= 7080000)
103 /* Needs IAR language extensions */
104       #define __ALIGNED(x) __attribute__((aligned(x)))
105     #else
106       #warning No compiler specific solution for __ALIGNED.__ALIGNED is ignored.
107       #define __ALIGNED(x)
108     #endif
109   #endif
110 
111   #ifndef __ASM
112     #define __ASM __asm
113   #endif
114 
115   #ifndef __INLINE
116     #define __INLINE inline
117   #endif
118 
119   #ifndef   __NO_RETURN
120     #if __ICCARM_V8
121       #define __NO_RETURN __attribute__((__noreturn__))
122     #else
123       #define __NO_RETURN _Pragma("object_attribute=__noreturn")
124     #endif
125   #endif
126 
127   #ifndef   __PACKED
128     #if __ICCARM_V8
129       #define __PACKED __attribute__((packed, aligned(1)))
130     #else
131 /* Needs IAR language extensions */
132       #define __PACKED __packed
133     #endif
134   #endif
135 
136   #ifndef   __PACKED_STRUCT
137     #if __ICCARM_V8
138       #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
139     #else
140 /* Needs IAR language extensions */
141       #define __PACKED_STRUCT __packed struct
142     #endif
143   #endif
144 
145   #ifndef   __PACKED_UNION
146     #if __ICCARM_V8
147       #define __PACKED_UNION union __attribute__((packed, aligned(1)))
148     #else
149 /* Needs IAR language extensions */
150       #define __PACKED_UNION __packed union
151     #endif
152   #endif
153 
154   #ifndef   __RESTRICT
155     #define __RESTRICT            restrict
156   #endif
157 
158   #ifndef   __STATIC_INLINE
159     #define __STATIC_INLINE       static inline
160   #endif
161 
162   #ifndef   __FORCEINLINE
163     #define __FORCEINLINE         _Pragma("inline=forced")
164   #endif
165 
166   #ifndef   __STATIC_FORCEINLINE
167     #define __STATIC_FORCEINLINE  __FORCEINLINE __STATIC_INLINE
168   #endif
169 
170   #ifndef   __USED
171     #if __ICCARM_V8
172       #define __USED __attribute__((used))
173     #else
174       #define __USED _Pragma("__root")
175     #endif
176   #endif
177 
178   #ifndef   __WEAK
179     #if __ICCARM_V8
180       #define __WEAK __attribute__((weak))
181     #else
182       #define __WEAK _Pragma("__weak")
183     #endif
184   #endif
185 
186 #else
187   #error "Unknown compiler."
188 #endif
189 
190 // IO definitions (access restrictions to peripheral registers).
191 #ifdef __cplusplus
192   #define   __I     volatile        ///< Defines 'read only' permissions
193 #else
194   #define   __I     volatile const  ///< Defines 'read only' permissions
195 #endif
196 #define     __O     volatile        ///< Defines 'write only' permissions
197 #define     __IO    volatile        ///< Defines 'read / write' permissions
198 
199 // The following defines should be used for structure members.
200 #define     __IM    volatile const  ///< Defines 'read only' structure member permissions
201 #define     __OM    volatile        ///< Defines 'write only' structure member permissions
202 #define     __IOM   volatile        ///< Defines 'read / write' structure member permissions
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 /** @} (end group compiler) */
209 
210 #endif // SL_COMPILER_H
211