1 /* compiler.h: macros to abstract away compiler specifics
2  *
3  * This work is licensed under the terms of the GNU GPL, version 2 or later.
4  * See the COPYING file in the top-level directory.
5  */
6 
7 #ifndef COMPILER_H
8 #define COMPILER_H
9 
10 #define HOST_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
11 
12 /* HOST_LONG_BITS is the size of a native pointer in bits. */
13 #define HOST_LONG_BITS (__SIZEOF_POINTER__ * 8)
14 
15 #if defined __clang_analyzer__ || defined __COVERITY__
16 #define QEMU_STATIC_ANALYSIS 1
17 #endif
18 
19 #ifdef __cplusplus
20 #define QEMU_EXTERN_C extern "C"
21 #else
22 #define QEMU_EXTERN_C extern
23 #endif
24 
25 #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
26 # define QEMU_PACKED __attribute__((gcc_struct, packed))
27 #else
28 # define QEMU_PACKED __attribute__((packed))
29 #endif
30 
31 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
32 
33 #ifndef glue
34 #define xglue(x, y) x ## y
35 #define glue(x, y) xglue(x, y)
36 #define stringify(s) tostring(s)
37 #define tostring(s) #s
38 #endif
39 
40 /* Expands into an identifier stemN, where N is another number each time */
41 #define MAKE_IDENTIFIER(stem) glue(stem, __COUNTER__)
42 
43 #ifndef likely
44 #define likely(x)   __builtin_expect(!!(x), 1)
45 #define unlikely(x)   __builtin_expect(!!(x), 0)
46 #endif
47 
48 #ifndef container_of
49 #define container_of(ptr, type, member) ({                      \
50         const typeof(((type *) 0)->member) *__mptr = (ptr);     \
51         (type *) ((char *) __mptr - offsetof(type, member));})
52 #endif
53 
54 #define sizeof_field(type, field) sizeof(((type *)0)->field)
55 
56 /*
57  * Calculate the number of bytes up to and including the given 'field' of
58  * 'container'.
59  */
60 #define endof(container, field) \
61     (offsetof(container, field) + sizeof_field(container, field))
62 
63 /* Convert from a base type to a parent type, with compile time checking.  */
64 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
65     char __attribute__((unused)) offset_must_be_zero[ \
66         -offsetof(type, field)]; \
67     container_of(dev, type, field);}))
68 
69 #define typeof_field(type, field) typeof(((type *)0)->field)
70 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
71 
72 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
73     struct { \
74         int:(x) ? -1 : 1; \
75     }
76 
77 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
78 
79 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
80 
81 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
82                                    sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
83 
84 #if !defined(__clang__) && defined(_WIN32)
85 /*
86  * Map __printf__ to __gnu_printf__ because we want standard format strings even
87  * when MinGW or GLib include files use __printf__.
88  */
89 # define __printf__ __gnu_printf__
90 #endif
91 
92 #ifndef __has_warning
93 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
94 #endif
95 
96 #ifndef __has_feature
97 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
98 #endif
99 
100 #ifndef __has_builtin
101 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
102 #endif
103 
104 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
105 #define HAS_ASSUME_ALIGNED
106 #endif
107 
108 #ifndef __has_attribute
109 #define __has_attribute(x) 0 /* compatibility with older GCC */
110 #endif
111 
112 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
113 # define QEMU_SANITIZE_ADDRESS 1
114 #endif
115 
116 #if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
117 # define QEMU_SANITIZE_THREAD 1
118 #endif
119 
120 /*
121  * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
122  * versions we support have the "flatten" attribute. Clang may not have the
123  * "flatten" attribute but always has __has_attribute() to check for it.
124  */
125 #if __has_attribute(flatten) || !defined(__clang__)
126 # define QEMU_FLATTEN __attribute__((flatten))
127 #else
128 # define QEMU_FLATTEN
129 #endif
130 
131 /*
132  * If __attribute__((error)) is present, use it to produce an error at
133  * compile time.  Otherwise, one must wait for the linker to diagnose
134  * the missing symbol.
135  */
136 #if __has_attribute(error)
137 # define QEMU_ERROR(X) __attribute__((error(X)))
138 #else
139 # define QEMU_ERROR(X)
140 #endif
141 
142 /*
143  * The nonstring variable attribute specifies that an object or member
144  * declaration with type array of char or pointer to char is intended
145  * to store character arrays that do not necessarily contain a terminating
146  * NUL character. This is useful in detecting uses of such arrays or pointers
147  * with functions that expect NUL-terminated strings, and to avoid warnings
148  * when such an array or pointer is used as an argument to a bounded string
149  * manipulation function such as strncpy.
150  */
151 #if __has_attribute(nonstring)
152 # define QEMU_NONSTRING __attribute__((nonstring))
153 #else
154 # define QEMU_NONSTRING
155 #endif
156 
157 /*
158  * Forced inlining may be desired to encourage constant propagation
159  * of function parameters.  However, it can also make debugging harder,
160  * so disable it for a non-optimizing build.
161  */
162 #if defined(__OPTIMIZE__)
163 #define QEMU_ALWAYS_INLINE  __attribute__((always_inline))
164 #else
165 #define QEMU_ALWAYS_INLINE
166 #endif
167 
168 /**
169  * In most cases, normal "fallthrough" comments are good enough for
170  * switch-case statements, but sometimes the compiler has problems
171  * with those. In that case you can use QEMU_FALLTHROUGH instead.
172  */
173 #if __has_attribute(fallthrough)
174 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
175 #else
176 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
177 #endif
178 
179 #ifdef CONFIG_CFI
180 /*
181  * If CFI is enabled, use an attribute to disable cfi-icall on the following
182  * function
183  */
184 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
185 #else
186 /* If CFI is not enabled, use an empty define to not change the behavior */
187 #define QEMU_DISABLE_CFI
188 #endif
189 
190 /*
191  * Apple clang version 14 has a bug in its __builtin_subcll(); define
192  * BUILTIN_SUBCLL_BROKEN for the offending versions so we can avoid it.
193  * When a version of Apple clang which has this bug fixed is released
194  * we can add an upper bound to this check.
195  * See https://gitlab.com/qemu-project/qemu/-/issues/1631
196  * and https://gitlab.com/qemu-project/qemu/-/issues/1659 for details.
197  * The bug never made it into any upstream LLVM releases, only Apple ones.
198  */
199 #if defined(__apple_build_version__) && __clang_major__ >= 14
200 #define BUILTIN_SUBCLL_BROKEN
201 #endif
202 
203 #if __has_attribute(annotate)
204 #define QEMU_ANNOTATE(x) __attribute__((annotate(x)))
205 #else
206 #define QEMU_ANNOTATE(x)
207 #endif
208 
209 #if __has_attribute(used)
210 # define QEMU_USED __attribute__((used))
211 #else
212 # define QEMU_USED
213 #endif
214 
215 /*
216  * Ugly CPP trick that is like "defined FOO", but also works in C
217  * code.  Useful to replace #ifdef with "if" statements; assumes
218  * the symbol was defined with Meson's "config.set()", so it is empty
219  * if defined.
220  */
221 #define IS_ENABLED(x)                  IS_EMPTY(x)
222 
223 #define IS_EMPTY_JUNK_                 junk,
224 #define IS_EMPTY(value)                IS_EMPTY_(IS_EMPTY_JUNK_##value)
225 
226 /* Expands to either SECOND_ARG(junk, 1, 0) or SECOND_ARG(IS_EMPTY_JUNK_CONFIG_FOO 1, 0)  */
227 #define SECOND_ARG(first, second, ...) second
228 #define IS_EMPTY_(junk_maybecomma)     SECOND_ARG(junk_maybecomma 1, 0)
229 
230 #ifndef __cplusplus
231 /*
232  * Useful in macros that need to declare temporary variables.  For example,
233  * the variable that receives the old value of an atomically-accessed
234  * variable must be non-qualified, because atomic builtins return values
235  * through a pointer-type argument as in __atomic_load(&var, &old, MODEL).
236  *
237  * This macro has to handle types smaller than int manually, because of
238  * implicit promotion.  int and larger types, as well as pointers, can be
239  * converted to a non-qualified type just by applying a binary operator.
240  */
241 #define typeof_strip_qual(expr)                                                    \
242   typeof(                                                                          \
243     __builtin_choose_expr(                                                         \
244       __builtin_types_compatible_p(typeof(expr), bool) ||                          \
245         __builtin_types_compatible_p(typeof(expr), const bool) ||                  \
246         __builtin_types_compatible_p(typeof(expr), volatile bool) ||               \
247         __builtin_types_compatible_p(typeof(expr), const volatile bool),           \
248         (bool)1,                                                                   \
249     __builtin_choose_expr(                                                         \
250       __builtin_types_compatible_p(typeof(expr), signed char) ||                   \
251         __builtin_types_compatible_p(typeof(expr), const signed char) ||           \
252         __builtin_types_compatible_p(typeof(expr), volatile signed char) ||        \
253         __builtin_types_compatible_p(typeof(expr), const volatile signed char),    \
254         (signed char)1,                                                            \
255     __builtin_choose_expr(                                                         \
256       __builtin_types_compatible_p(typeof(expr), unsigned char) ||                 \
257         __builtin_types_compatible_p(typeof(expr), const unsigned char) ||         \
258         __builtin_types_compatible_p(typeof(expr), volatile unsigned char) ||      \
259         __builtin_types_compatible_p(typeof(expr), const volatile unsigned char),  \
260         (unsigned char)1,                                                          \
261     __builtin_choose_expr(                                                         \
262       __builtin_types_compatible_p(typeof(expr), signed short) ||                  \
263         __builtin_types_compatible_p(typeof(expr), const signed short) ||          \
264         __builtin_types_compatible_p(typeof(expr), volatile signed short) ||       \
265         __builtin_types_compatible_p(typeof(expr), const volatile signed short),   \
266         (signed short)1,                                                           \
267     __builtin_choose_expr(                                                         \
268       __builtin_types_compatible_p(typeof(expr), unsigned short) ||                \
269         __builtin_types_compatible_p(typeof(expr), const unsigned short) ||        \
270         __builtin_types_compatible_p(typeof(expr), volatile unsigned short) ||     \
271         __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \
272         (unsigned short)1,                                                         \
273       (expr)+0))))))
274 #endif
275 
276 #endif /* COMPILER_H */
277