1 /**
2  * @file compiler.h
3  * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited
4  */
5 
6 #pragma once
7 
8 #include <stddef.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /**
15  * @addtogroup aos_compiler compiler
16  * Compiler defined macro to add attributes for funtion, variable or type defined.
17  *
18  * @{
19  */
20 
21 /* __CC_ARM  */
22 #if defined(__CC_ARM)
23 
24 /* Define a weak symbol */
25 #ifndef WEAK
26 #ifndef __MINGW32__
27 #define WEAK __attribute__ ((weak))
28 #else
29 #define WEAK
30 #endif
31 #endif
32 
33 /* Tells the compiler that the function won't ever return */
34 #ifndef NORETURN
35 #define NORETURN __declspec(noreturn)
36 #endif
37 
38 /* Code must be emitted even if the function is not referenced */
39 #ifndef USED
40 #define USED __attribute__((used))
41 #endif
42 
43 /* Prevents the compiler from generating warnings if the function is not referenced */
44 #ifndef UNUSED
45 #define UNUSED __attribute__((unused))
46 #endif
47 
48 /* Prevents the compiler from making any use of a constant data value for
49 optimization purposes, without affecting its placement in the object */
50 #ifndef NO_INLINE
51 #define NO_INLINE __attribute__((noinline))
52 #endif
53 
54 /* Forces the compiler to compile a C or C++ function inline */
55 #ifndef ALWAYS_INLINE
56 #define ALWAYS_INLINE __forceinline
57 #endif
58 
59 /* Specifies a minimum alignment for the variable or structure field, measured in bytes */
60 #ifndef ALIGNED
61 #define ALIGNED(size) __attribute__((__aligned__(size)))
62 #endif
63 
64 /* Specifies that a obj must be placed in a particular section */
65 #ifndef SECTION
66 #define SECTION(name) __attribute__((section(name)))
67 #endif
68 
69 /* __GNUC__ */
70 #elif (defined __GNUC__)
71 
72 /* Define a weak symbol */
73 #ifndef WEAK
74 #ifndef __MINGW32__
75 #define WEAK __attribute__ ((weak))
76 #else
77 #define WEAK
78 #endif
79 #endif
80 
81 /* Tells the compiler that the function won't ever return */
82 #ifndef NORETURN
83 #define NORETURN __attribute__((__noreturn__))
84 #endif
85 
86 /* Code must be emitted even if the function is not referenced */
87 #ifndef USED
88 #define USED __attribute__((used))
89 #endif
90 
91 /* Prevents the compiler from generating warnings if the function is not referenced */
92 #ifndef UNUSED
93 #define UNUSED __attribute__((unused))
94 #endif
95 
96 /* Prevents the compiler from making any use of a constant data value for
97 optimization purposes, without affecting its placement in the object */
98 #ifndef NO_INLINE
99 #define NO_INLINE __attribute__((noinline))
100 #endif
101 
102 /* Forces the compiler to compile a C or C++ function inline */
103 #ifndef ALWAYS_INLINE
104 #define ALWAYS_INLINE __attribute__((__always_inline__))
105 #endif
106 
107 /* Specifies a minimum alignment for the variable or structure field, measured in bytes */
108 #ifndef ALIGNED
109 #define ALIGNED(size) __attribute__((__aligned__(size)))
110 #endif
111 
112 /* Specifies that a obj must be placed in a particular section */
113 #ifndef SECTION
114 #define SECTION(name) __attribute__((section(name)))
115 #endif
116 
117 /* __ICCARM__ */
118 #elif (defined __ICCARM__)
119 
120 /* Define a weak symbol */
121 #ifndef WEAK
122 #ifndef __MINGW32__
123 #define WEAK __weak
124 #else
125 #define WEAK
126 #endif
127 #endif
128 
129 /* Tells the compiler that the function won't ever return */
130 #ifndef NORETURN
131 #if __ICCARM_V8
132 #define NORETURN __attribute__((__noreturn__))
133 #else
134 #define NORETURN _Pragma("object_attribute=__noreturn")
135 #endif
136 #endif
137 
138 /* Code must be emitted even if the function is not referenced */
139 #if __ICCARM_V8
140 #define USED __attribute__((used))
141 #else
142 #define USED _Pragma("__root")
143 #endif
144 
145 /* Prevents the compiler from generating warnings if the function is not referenced */
146 #ifndef UNUSED
147 #define UNUSED __attribute__((unused))
148 #endif
149 
150 /* Prevents the compiler from making any use of a constant data value for
151 optimization purposes, without affecting its placement in the object */
152 #ifndef NO_INLINE
153 #define NO_INLINE _Pragma("inline=never")
154 #endif
155 
156 /* Forces the compiler to compile a C or C++ function inline */
157 #ifndef ALWAYS_INLINE
158 #define ALWAYS_INLINE _Pragma("inline=forced")
159 #endif
160 
161 /* Specifies a minimum alignment for the variable or structure field, measured in bytes */
162 #ifndef ALIGNED
163 #define ALIGNED(size) _Pragma(data_alignment = size)
164 #endif
165 
166 /* Specifies that a obj must be placed in a particular section */
167 #ifndef SECTION
168 #define SECTION(name) __attribute__((section(name)))
169 #endif
170 
171 #endif
172 
173 /** @} */
174 
175 #ifdef __cplusplus
176 }
177 #endif
178 
179