]>
Commit | Line | Data |
---|---|---|
cc9d8a3b FF |
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 | */ | |
5c026320 LC |
6 | |
7 | #ifndef COMPILER_H | |
8 | #define COMPILER_H | |
9 | ||
8bff06a0 PB |
10 | #if defined __clang_analyzer__ || defined __COVERITY__ |
11 | #define QEMU_STATIC_ANALYSIS 1 | |
12 | #endif | |
5c026320 | 13 | |
f8b72754 SW |
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 | |
24 | ||
5c026320 | 25 | #define QEMU_NORETURN __attribute__ ((__noreturn__)) |
f8b72754 | 26 | |
5c026320 | 27 | #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
5c026320 | 28 | |
a31bdae5 | 29 | #define QEMU_SENTINEL __attribute__((sentinel)) |
a31bdae5 | 30 | |
58099c80 RH |
31 | #if QEMU_GNUC_PREREQ(4, 3) |
32 | #define QEMU_ARTIFICIAL __attribute__((always_inline, artificial)) | |
33 | #else | |
34 | #define QEMU_ARTIFICIAL | |
35 | #endif | |
36 | ||
0f7fdd34 SW |
37 | #if defined(_WIN32) |
38 | # define QEMU_PACKED __attribute__((gcc_struct, packed)) | |
39 | #else | |
40 | # define QEMU_PACKED __attribute__((packed)) | |
41 | #endif | |
42 | ||
911a4d22 EC |
43 | #define QEMU_ALIGNED(X) __attribute__((aligned(X))) |
44 | ||
49120868 PM |
45 | #ifndef glue |
46 | #define xglue(x, y) x ## y | |
47 | #define glue(x, y) xglue(x, y) | |
48 | #define stringify(s) tostring(s) | |
49 | #define tostring(s) #s | |
50 | #endif | |
51 | ||
52 | #ifndef likely | |
53 | #if __GNUC__ < 3 | |
54 | #define __builtin_expect(x, n) (x) | |
55 | #endif | |
56 | ||
57 | #define likely(x) __builtin_expect(!!(x), 1) | |
58 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
59 | #endif | |
60 | ||
61 | #ifndef container_of | |
62 | #define container_of(ptr, type, member) ({ \ | |
63 | const typeof(((type *) 0)->member) *__mptr = (ptr); \ | |
64 | (type *) ((char *) __mptr - offsetof(type, member));}) | |
65 | #endif | |
66 | ||
f18793b0 SH |
67 | #define sizeof_field(type, field) sizeof(((type *)0)->field) |
68 | ||
49120868 PM |
69 | /* Convert from a base type to a parent type, with compile time checking. */ |
70 | #ifdef __GNUC__ | |
71 | #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ | |
72 | char __attribute__((unused)) offset_must_be_zero[ \ | |
73 | -offsetof(type, field)]; \ | |
74 | container_of(dev, type, field);})) | |
75 | #else | |
76 | #define DO_UPCAST(type, field, dev) container_of(dev, type, field) | |
77 | #endif | |
78 | ||
79 | #define typeof_field(type, field) typeof(((type *)0)->field) | |
80 | #define type_check(t1,t2) ((t1*)0 - (t2*)0) | |
81 | ||
f291887e MT |
82 | #define QEMU_BUILD_BUG_ON_STRUCT(x) \ |
83 | struct { \ | |
84 | int:(x) ? -1 : 1; \ | |
85 | } | |
86 | ||
9139b567 HR |
87 | /* QEMU_BUILD_BUG_MSG() emits the message given if _Static_assert is |
88 | * supported; otherwise, it will be omitted from the compiler error | |
89 | * message (but as it remains present in the source code, it can still | |
90 | * be useful when debugging). */ | |
49e00a18 | 91 | #if defined(CONFIG_STATIC_ASSERT) |
9139b567 | 92 | #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg) |
49e00a18 | 93 | #elif defined(__COUNTER__) |
9139b567 | 94 | #define QEMU_BUILD_BUG_MSG(x, msg) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ |
f291887e | 95 | glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused)) |
60abf0a5 | 96 | #else |
9139b567 | 97 | #define QEMU_BUILD_BUG_MSG(x, msg) |
60abf0a5 | 98 | #endif |
5c026320 | 99 | |
9139b567 HR |
100 | #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x) |
101 | ||
d757573e MT |
102 | #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \ |
103 | sizeof(QEMU_BUILD_BUG_ON_STRUCT(x))) | |
104 | ||
5c026320 | 105 | #if defined __GNUC__ |
f8b72754 | 106 | # if !QEMU_GNUC_PREREQ(4, 4) |
5c026320 | 107 | /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */ |
5c026320 LC |
108 | # define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m))) |
109 | # else | |
110 | /* Use gnu_printf when supported (qemu uses standard format strings). */ | |
5c026320 | 111 | # define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m))) |
95df51a4 SW |
112 | # if defined(_WIN32) |
113 | /* Map __printf__ to __gnu_printf__ because we want standard format strings | |
114 | * even when MinGW or GLib include files use __printf__. */ | |
115 | # define __printf__ __gnu_printf__ | |
116 | # endif | |
5c026320 LC |
117 | # endif |
118 | #else | |
5c026320 LC |
119 | #define GCC_FMT_ATTR(n, m) |
120 | #endif | |
121 | ||
d83414e1 MAL |
122 | #ifndef __has_feature |
123 | #define __has_feature(x) 0 /* compatibility with non-clang compilers */ | |
124 | #endif | |
5a358b39 PM |
125 | |
126 | #ifndef __has_builtin | |
127 | #define __has_builtin(x) 0 /* compatibility with non-clang compilers */ | |
128 | #endif | |
129 | ||
130 | #if __has_builtin(__builtin_assume_aligned) || QEMU_GNUC_PREREQ(4, 7) | |
131 | #define HAS_ASSUME_ALIGNED | |
132 | #endif | |
97ff87c0 TH |
133 | |
134 | #ifndef __has_attribute | |
135 | #define __has_attribute(x) 0 /* compatibility with older GCC */ | |
136 | #endif | |
137 | ||
138 | /* | |
139 | * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC | |
140 | * versions we support have the "flatten" attribute. Clang may not have the | |
141 | * "flatten" attribute but always has __has_attribute() to check for it. | |
142 | */ | |
143 | #if __has_attribute(flatten) || !defined(__clang__) | |
144 | # define QEMU_FLATTEN __attribute__((flatten)) | |
145 | #else | |
146 | # define QEMU_FLATTEN | |
147 | #endif | |
5a358b39 | 148 | |
e70372fc PB |
149 | /* Implement C11 _Generic via GCC builtins. Example: |
150 | * | |
151 | * QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x) | |
152 | * | |
153 | * The first argument is the discriminator. The last is the default value. | |
154 | * The middle ones are tuples in "(type, expansion)" format. | |
155 | */ | |
156 | ||
157 | /* First, find out the number of generic cases. */ | |
158 | #define QEMU_GENERIC(x, ...) \ | |
159 | QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) | |
160 | ||
161 | /* There will be extra arguments, but they are not used. */ | |
162 | #define QEMU_GENERIC_(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ...) \ | |
163 | QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) | |
164 | ||
165 | /* Two more helper macros, this time to extract items from a parenthesized | |
166 | * list. | |
167 | */ | |
168 | #define QEMU_FIRST_(a, b) a | |
169 | #define QEMU_SECOND_(a, b) b | |
170 | ||
171 | /* ... and a final one for the common part of the "recursion". */ | |
172 | #define QEMU_GENERIC_IF(x, type_then, else_) \ | |
173 | __builtin_choose_expr(__builtin_types_compatible_p(x, \ | |
174 | QEMU_FIRST_ type_then), \ | |
175 | QEMU_SECOND_ type_then, else_) | |
176 | ||
177 | /* CPP poor man's "recursion". */ | |
178 | #define QEMU_GENERIC1(x, a0, ...) (a0) | |
179 | #define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__)) | |
180 | #define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__)) | |
181 | #define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__)) | |
182 | #define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__)) | |
183 | #define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__)) | |
184 | #define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__)) | |
185 | #define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__)) | |
186 | #define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__)) | |
187 | #define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__)) | |
d83414e1 | 188 | |
5c026320 | 189 | #endif /* COMPILER_H */ |