1
2
3
4
5
6
7#ifndef COMPILER_H
8#define COMPILER_H
9
10#if defined __clang_analyzer__ || defined __COVERITY__
11#define QEMU_STATIC_ANALYSIS 1
12#endif
13
14
15
16
17
18#if defined(__GNUC__) && defined(__GNUC_MINOR__)
19# define QEMU_GNUC_PREREQ(maj, min) \
20 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
21#else
22# define QEMU_GNUC_PREREQ(maj, min) 0
23#endif
24
25#define QEMU_NORETURN __attribute__ ((__noreturn__))
26
27#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
28
29#define QEMU_SENTINEL __attribute__((sentinel))
30
31#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
32# define QEMU_PACKED __attribute__((gcc_struct, packed))
33#else
34# define QEMU_PACKED __attribute__((packed))
35#endif
36
37#define QEMU_ALIGNED(X) __attribute__((aligned(X)))
38
39#ifndef glue
40#define xglue(x, y) x ## y
41#define glue(x, y) xglue(x, y)
42#define stringify(s) tostring(s)
43#define tostring(s) #s
44#endif
45
46#ifndef likely
47#if __GNUC__ < 3
48#define __builtin_expect(x, n) (x)
49#endif
50
51#define likely(x) __builtin_expect(!!(x), 1)
52#define unlikely(x) __builtin_expect(!!(x), 0)
53#endif
54
55#ifndef container_of
56#define container_of(ptr, type, member) ({ \
57 const typeof(((type *) 0)->member) *__mptr = (ptr); \
58 (type *) ((char *) __mptr - offsetof(type, member));})
59#endif
60
61#define sizeof_field(type, field) sizeof(((type *)0)->field)
62
63
64
65
66
67#define endof(container, field) \
68 (offsetof(container, field) + sizeof_field(container, field))
69
70
71#ifdef __GNUC__
72#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
73 char __attribute__((unused)) offset_must_be_zero[ \
74 -offsetof(type, field)]; \
75 container_of(dev, type, field);}))
76#else
77#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
78#endif
79
80#define typeof_field(type, field) typeof(((type *)0)->field)
81#define type_check(t1,t2) ((t1*)0 - (t2*)0)
82
83#define QEMU_BUILD_BUG_ON_STRUCT(x) \
84 struct { \
85 int:(x) ? -1 : 1; \
86 }
87
88
89
90
91
92#if defined(CONFIG_STATIC_ASSERT)
93#define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
94#elif defined(__COUNTER__)
95#define QEMU_BUILD_BUG_MSG(x, msg) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
96 glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))
97#else
98#define QEMU_BUILD_BUG_MSG(x, msg)
99#endif
100
101#define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
102
103#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
104 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
105
106#if defined __GNUC__
107# if !QEMU_GNUC_PREREQ(4, 4)
108
109# define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
110# else
111
112# define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
113# if defined(_WIN32)
114
115
116# define __printf__ __gnu_printf__
117# endif
118# endif
119#else
120#define GCC_FMT_ATTR(n, m)
121#endif
122
123#ifndef __has_warning
124#define __has_warning(x) 0
125#endif
126
127#ifndef __has_feature
128#define __has_feature(x) 0
129#endif
130
131#ifndef __has_builtin
132#define __has_builtin(x) 0
133#endif
134
135#if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
136#define HAS_ASSUME_ALIGNED
137#endif
138
139#ifndef __has_attribute
140#define __has_attribute(x) 0
141#endif
142
143
144
145
146
147
148#if __has_attribute(flatten) || !defined(__clang__)
149# define QEMU_FLATTEN __attribute__((flatten))
150#else
151# define QEMU_FLATTEN
152#endif
153
154
155
156
157
158
159#if __has_attribute(error)
160# define QEMU_ERROR(X) __attribute__((error(X)))
161#else
162# define QEMU_ERROR(X)
163#endif
164
165
166
167
168
169
170
171
172
173
174#if __has_attribute(nonstring)
175# define QEMU_NONSTRING __attribute__((nonstring))
176#else
177# define QEMU_NONSTRING
178#endif
179
180
181
182
183
184
185#if defined(__OPTIMIZE__)
186#define QEMU_ALWAYS_INLINE __attribute__((always_inline))
187#else
188#define QEMU_ALWAYS_INLINE
189#endif
190
191
192
193
194
195
196
197
198
199
200#define QEMU_GENERIC(x, ...) \
201 QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
202
203
204#define QEMU_GENERIC_(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ...) \
205 QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
206
207
208
209
210#define QEMU_FIRST_(a, b) a
211#define QEMU_SECOND_(a, b) b
212
213
214#define QEMU_GENERIC_IF(x, type_then, else_) \
215 __builtin_choose_expr(__builtin_types_compatible_p(x, \
216 QEMU_FIRST_ type_then), \
217 QEMU_SECOND_ type_then, else_)
218
219
220#define QEMU_GENERIC1(x, a0, ...) (a0)
221#define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__))
222#define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__))
223#define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__))
224#define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__))
225#define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__))
226#define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__))
227#define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__))
228#define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))
229#define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))
230
231
232
233
234
235
236
237
238
239#if defined(__OPTIMIZE__) && !defined(__NO_INLINE__)
240extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
241 qemu_build_not_reached(void);
242#else
243#define qemu_build_not_reached() g_assert_not_reached()
244#endif
245
246#endif
247