hw/ppc/pnv_homer: add PowerNV homer device model
[qemu/ar7.git] / include / qemu / compiler.h
blob7b93c733407eb05a9d2870abb099c7648b867c7c
1 /* compiler.h: macros to abstract away compiler specifics
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 */
7 #ifndef COMPILER_H
8 #define COMPILER_H
10 #if defined __clang_analyzer__ || defined __COVERITY__
11 #define QEMU_STATIC_ANALYSIS 1
12 #endif
14 /*----------------------------------------------------------------------------
15 | The macro QEMU_GNUC_PREREQ tests for minimum version of the GNU C compiler.
16 | The code is a copy of SOFTFLOAT_GNUC_PREREQ, see softfloat-macros.h.
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
25 #define QEMU_NORETURN __attribute__ ((__noreturn__))
27 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
29 #define QEMU_SENTINEL __attribute__((sentinel))
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
37 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
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
46 #ifndef likely
47 #if __GNUC__ < 3
48 #define __builtin_expect(x, n) (x)
49 #endif
51 #define likely(x) __builtin_expect(!!(x), 1)
52 #define unlikely(x) __builtin_expect(!!(x), 0)
53 #endif
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
61 #define sizeof_field(type, field) sizeof(((type *)0)->field)
63 /* Convert from a base type to a parent type, with compile time checking. */
64 #ifdef __GNUC__
65 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
66 char __attribute__((unused)) offset_must_be_zero[ \
67 -offsetof(type, field)]; \
68 container_of(dev, type, field);}))
69 #else
70 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
71 #endif
73 #define typeof_field(type, field) typeof(((type *)0)->field)
74 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
76 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
77 struct { \
78 int:(x) ? -1 : 1; \
81 /* QEMU_BUILD_BUG_MSG() emits the message given if _Static_assert is
82 * supported; otherwise, it will be omitted from the compiler error
83 * message (but as it remains present in the source code, it can still
84 * be useful when debugging). */
85 #if defined(CONFIG_STATIC_ASSERT)
86 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
87 #elif defined(__COUNTER__)
88 #define QEMU_BUILD_BUG_MSG(x, msg) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
89 glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))
90 #else
91 #define QEMU_BUILD_BUG_MSG(x, msg)
92 #endif
94 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
96 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
97 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
99 #if defined __GNUC__
100 # if !QEMU_GNUC_PREREQ(4, 4)
101 /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
102 # define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
103 # else
104 /* Use gnu_printf when supported (qemu uses standard format strings). */
105 # define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
106 # if defined(_WIN32)
107 /* Map __printf__ to __gnu_printf__ because we want standard format strings
108 * even when MinGW or GLib include files use __printf__. */
109 # define __printf__ __gnu_printf__
110 # endif
111 # endif
112 #else
113 #define GCC_FMT_ATTR(n, m)
114 #endif
116 #ifndef __has_warning
117 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
118 #endif
120 #ifndef __has_feature
121 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
122 #endif
124 #ifndef __has_builtin
125 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
126 #endif
128 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
129 #define HAS_ASSUME_ALIGNED
130 #endif
132 #ifndef __has_attribute
133 #define __has_attribute(x) 0 /* compatibility with older GCC */
134 #endif
137 * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
138 * versions we support have the "flatten" attribute. Clang may not have the
139 * "flatten" attribute but always has __has_attribute() to check for it.
141 #if __has_attribute(flatten) || !defined(__clang__)
142 # define QEMU_FLATTEN __attribute__((flatten))
143 #else
144 # define QEMU_FLATTEN
145 #endif
148 * If __attribute__((error)) is present, use it to produce an error at
149 * compile time. Otherwise, one must wait for the linker to diagnose
150 * the missing symbol.
152 #if __has_attribute(error)
153 # define QEMU_ERROR(X) __attribute__((error(X)))
154 #else
155 # define QEMU_ERROR(X)
156 #endif
159 * The nonstring variable attribute specifies that an object or member
160 * declaration with type array of char or pointer to char is intended
161 * to store character arrays that do not necessarily contain a terminating
162 * NUL character. This is useful in detecting uses of such arrays or pointers
163 * with functions that expect NUL-terminated strings, and to avoid warnings
164 * when such an array or pointer is used as an argument to a bounded string
165 * manipulation function such as strncpy.
167 #if __has_attribute(nonstring)
168 # define QEMU_NONSTRING __attribute__((nonstring))
169 #else
170 # define QEMU_NONSTRING
171 #endif
174 * Forced inlining may be desired to encourage constant propagation
175 * of function parameters. However, it can also make debugging harder,
176 * so disable it for a non-optimizing build.
178 #if defined(__OPTIMIZE__)
179 #define QEMU_ALWAYS_INLINE __attribute__((always_inline))
180 #else
181 #define QEMU_ALWAYS_INLINE
182 #endif
184 /* Implement C11 _Generic via GCC builtins. Example:
186 * QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x)
188 * The first argument is the discriminator. The last is the default value.
189 * The middle ones are tuples in "(type, expansion)" format.
192 /* First, find out the number of generic cases. */
193 #define QEMU_GENERIC(x, ...) \
194 QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
196 /* There will be extra arguments, but they are not used. */
197 #define QEMU_GENERIC_(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ...) \
198 QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
200 /* Two more helper macros, this time to extract items from a parenthesized
201 * list.
203 #define QEMU_FIRST_(a, b) a
204 #define QEMU_SECOND_(a, b) b
206 /* ... and a final one for the common part of the "recursion". */
207 #define QEMU_GENERIC_IF(x, type_then, else_) \
208 __builtin_choose_expr(__builtin_types_compatible_p(x, \
209 QEMU_FIRST_ type_then), \
210 QEMU_SECOND_ type_then, else_)
212 /* CPP poor man's "recursion". */
213 #define QEMU_GENERIC1(x, a0, ...) (a0)
214 #define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__))
215 #define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__))
216 #define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__))
217 #define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__))
218 #define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__))
219 #define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__))
220 #define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__))
221 #define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))
222 #define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))
225 * qemu_build_not_reached()
227 * The compiler, during optimization, is expected to prove that a call
228 * to this function cannot be reached and remove it. If the compiler
229 * supports QEMU_ERROR, this will be reported at compile time; otherwise
230 * this will be reported at link time due to the missing symbol.
232 #ifdef __OPTIMIZE__
233 extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
234 qemu_build_not_reached(void);
235 #else
236 #define qemu_build_not_reached() g_assert_not_reached()
237 #endif
239 #endif /* COMPILER_H */