]>
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 | ||
51965597 MAL |
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 | ||
8bff06a0 PB |
15 | #if defined __clang_analyzer__ || defined __COVERITY__ |
16 | #define QEMU_STATIC_ANALYSIS 1 | |
17 | #endif | |
5c026320 | 18 | |
875df03b PB |
19 | #ifdef __cplusplus |
20 | #define QEMU_EXTERN_C extern "C" | |
21 | #else | |
22 | #define QEMU_EXTERN_C extern | |
23 | #endif | |
24 | ||
48bb55bf | 25 | #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__)) |
0f7fdd34 SW |
26 | # define QEMU_PACKED __attribute__((gcc_struct, packed)) |
27 | #else | |
28 | # define QEMU_PACKED __attribute__((packed)) | |
29 | #endif | |
30 | ||
911a4d22 EC |
31 | #define QEMU_ALIGNED(X) __attribute__((aligned(X))) |
32 | ||
49120868 PM |
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 | #ifndef likely | |
49120868 PM |
41 | #define likely(x) __builtin_expect(!!(x), 1) |
42 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
43 | #endif | |
44 | ||
45 | #ifndef container_of | |
46 | #define container_of(ptr, type, member) ({ \ | |
47 | const typeof(((type *) 0)->member) *__mptr = (ptr); \ | |
48 | (type *) ((char *) __mptr - offsetof(type, member));}) | |
49 | #endif | |
50 | ||
f18793b0 SH |
51 | #define sizeof_field(type, field) sizeof(((type *)0)->field) |
52 | ||
5d5b33c0 HR |
53 | /* |
54 | * Calculate the number of bytes up to and including the given 'field' of | |
55 | * 'container'. | |
56 | */ | |
57 | #define endof(container, field) \ | |
58 | (offsetof(container, field) + sizeof_field(container, field)) | |
59 | ||
49120868 | 60 | /* Convert from a base type to a parent type, with compile time checking. */ |
49120868 PM |
61 | #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ |
62 | char __attribute__((unused)) offset_must_be_zero[ \ | |
63 | -offsetof(type, field)]; \ | |
64 | container_of(dev, type, field);})) | |
49120868 PM |
65 | |
66 | #define typeof_field(type, field) typeof(((type *)0)->field) | |
67 | #define type_check(t1,t2) ((t1*)0 - (t2*)0) | |
68 | ||
f291887e MT |
69 | #define QEMU_BUILD_BUG_ON_STRUCT(x) \ |
70 | struct { \ | |
71 | int:(x) ? -1 : 1; \ | |
72 | } | |
73 | ||
9139b567 | 74 | #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg) |
5c026320 | 75 | |
9139b567 HR |
76 | #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x) |
77 | ||
d757573e MT |
78 | #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \ |
79 | sizeof(QEMU_BUILD_BUG_ON_STRUCT(x))) | |
80 | ||
9edc6313 | 81 | #if !defined(__clang__) && defined(_WIN32) |
28f86163 MAL |
82 | /* |
83 | * Map __printf__ to __gnu_printf__ because we want standard format strings even | |
84 | * when MinGW or GLib include files use __printf__. | |
85 | */ | |
9edc6313 | 86 | # define __printf__ __gnu_printf__ |
5c026320 LC |
87 | #endif |
88 | ||
798b8581 TH |
89 | #ifndef __has_warning |
90 | #define __has_warning(x) 0 /* compatibility with non-clang compilers */ | |
91 | #endif | |
92 | ||
d83414e1 MAL |
93 | #ifndef __has_feature |
94 | #define __has_feature(x) 0 /* compatibility with non-clang compilers */ | |
95 | #endif | |
5a358b39 PM |
96 | |
97 | #ifndef __has_builtin | |
98 | #define __has_builtin(x) 0 /* compatibility with non-clang compilers */ | |
99 | #endif | |
100 | ||
f773b423 | 101 | #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__) |
5a358b39 PM |
102 | #define HAS_ASSUME_ALIGNED |
103 | #endif | |
97ff87c0 TH |
104 | |
105 | #ifndef __has_attribute | |
106 | #define __has_attribute(x) 0 /* compatibility with older GCC */ | |
107 | #endif | |
108 | ||
109 | /* | |
110 | * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC | |
111 | * versions we support have the "flatten" attribute. Clang may not have the | |
112 | * "flatten" attribute but always has __has_attribute() to check for it. | |
113 | */ | |
114 | #if __has_attribute(flatten) || !defined(__clang__) | |
115 | # define QEMU_FLATTEN __attribute__((flatten)) | |
116 | #else | |
117 | # define QEMU_FLATTEN | |
118 | #endif | |
e6cd4bb5 RH |
119 | |
120 | /* | |
121 | * If __attribute__((error)) is present, use it to produce an error at | |
122 | * compile time. Otherwise, one must wait for the linker to diagnose | |
123 | * the missing symbol. | |
124 | */ | |
125 | #if __has_attribute(error) | |
126 | # define QEMU_ERROR(X) __attribute__((error(X))) | |
127 | #else | |
128 | # define QEMU_ERROR(X) | |
129 | #endif | |
1daff2f8 PMD |
130 | |
131 | /* | |
132 | * The nonstring variable attribute specifies that an object or member | |
133 | * declaration with type array of char or pointer to char is intended | |
134 | * to store character arrays that do not necessarily contain a terminating | |
135 | * NUL character. This is useful in detecting uses of such arrays or pointers | |
136 | * with functions that expect NUL-terminated strings, and to avoid warnings | |
137 | * when such an array or pointer is used as an argument to a bounded string | |
138 | * manipulation function such as strncpy. | |
139 | */ | |
140 | #if __has_attribute(nonstring) | |
141 | # define QEMU_NONSTRING __attribute__((nonstring)) | |
142 | #else | |
143 | # define QEMU_NONSTRING | |
144 | #endif | |
c6b716cd RH |
145 | |
146 | /* | |
147 | * Forced inlining may be desired to encourage constant propagation | |
148 | * of function parameters. However, it can also make debugging harder, | |
149 | * so disable it for a non-optimizing build. | |
150 | */ | |
151 | #if defined(__OPTIMIZE__) | |
152 | #define QEMU_ALWAYS_INLINE __attribute__((always_inline)) | |
153 | #else | |
154 | #define QEMU_ALWAYS_INLINE | |
155 | #endif | |
5a358b39 | 156 | |
d84568b7 TH |
157 | /** |
158 | * In most cases, normal "fallthrough" comments are good enough for | |
159 | * switch-case statements, but sometimes the compiler has problems | |
160 | * with those. In that case you can use QEMU_FALLTHROUGH instead. | |
161 | */ | |
162 | #if __has_attribute(fallthrough) | |
163 | # define QEMU_FALLTHROUGH __attribute__((fallthrough)) | |
164 | #else | |
165 | # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */ | |
166 | #endif | |
167 | ||
c905a368 DB |
168 | #ifdef CONFIG_CFI |
169 | /* | |
170 | * If CFI is enabled, use an attribute to disable cfi-icall on the following | |
171 | * function | |
172 | */ | |
173 | #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall"))) | |
174 | #else | |
175 | /* If CFI is not enabled, use an empty define to not change the behavior */ | |
176 | #define QEMU_DISABLE_CFI | |
177 | #endif | |
178 | ||
5c026320 | 179 | #endif /* COMPILER_H */ |