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 #ifndef likely
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 
51 #define sizeof_field(type, field) sizeof(((type *)0)->field)
52 
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 
60 /* Convert from a base type to a parent type, with compile time checking.  */
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);}))
65 
66 #define typeof_field(type, field) typeof(((type *)0)->field)
67 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
68 
69 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
70     struct { \
71         int:(x) ? -1 : 1; \
72     }
73 
74 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
75 
76 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
77 
78 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
79                                    sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
80 
81 #if !defined(__clang__) && defined(_WIN32)
82 /*
83  * Map __printf__ to __gnu_printf__ because we want standard format strings even
84  * when MinGW or GLib include files use __printf__.
85  */
86 # define __printf__ __gnu_printf__
87 #endif
88 
89 #ifndef __has_warning
90 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
91 #endif
92 
93 #ifndef __has_feature
94 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
95 #endif
96 
97 #ifndef __has_builtin
98 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
99 #endif
100 
101 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
102 #define HAS_ASSUME_ALIGNED
103 #endif
104 
105 #ifndef __has_attribute
106 #define __has_attribute(x) 0 /* compatibility with older GCC */
107 #endif
108 
109 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
110 # define QEMU_SANITIZE_ADDRESS 1
111 #endif
112 
113 #if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
114 # define QEMU_SANITIZE_THREAD 1
115 #endif
116 
117 /*
118  * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
119  * versions we support have the "flatten" attribute. Clang may not have the
120  * "flatten" attribute but always has __has_attribute() to check for it.
121  */
122 #if __has_attribute(flatten) || !defined(__clang__)
123 # define QEMU_FLATTEN __attribute__((flatten))
124 #else
125 # define QEMU_FLATTEN
126 #endif
127 
128 /*
129  * If __attribute__((error)) is present, use it to produce an error at
130  * compile time.  Otherwise, one must wait for the linker to diagnose
131  * the missing symbol.
132  */
133 #if __has_attribute(error)
134 # define QEMU_ERROR(X) __attribute__((error(X)))
135 #else
136 # define QEMU_ERROR(X)
137 #endif
138 
139 /*
140  * The nonstring variable attribute specifies that an object or member
141  * declaration with type array of char or pointer to char is intended
142  * to store character arrays that do not necessarily contain a terminating
143  * NUL character. This is useful in detecting uses of such arrays or pointers
144  * with functions that expect NUL-terminated strings, and to avoid warnings
145  * when such an array or pointer is used as an argument to a bounded string
146  * manipulation function such as strncpy.
147  */
148 #if __has_attribute(nonstring)
149 # define QEMU_NONSTRING __attribute__((nonstring))
150 #else
151 # define QEMU_NONSTRING
152 #endif
153 
154 /*
155  * Forced inlining may be desired to encourage constant propagation
156  * of function parameters.  However, it can also make debugging harder,
157  * so disable it for a non-optimizing build.
158  */
159 #if defined(__OPTIMIZE__)
160 #define QEMU_ALWAYS_INLINE  __attribute__((always_inline))
161 #else
162 #define QEMU_ALWAYS_INLINE
163 #endif
164 
165 /**
166  * In most cases, normal "fallthrough" comments are good enough for
167  * switch-case statements, but sometimes the compiler has problems
168  * with those. In that case you can use QEMU_FALLTHROUGH instead.
169  */
170 #if __has_attribute(fallthrough)
171 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
172 #else
173 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
174 #endif
175 
176 #ifdef CONFIG_CFI
177 /*
178  * If CFI is enabled, use an attribute to disable cfi-icall on the following
179  * function
180  */
181 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
182 #else
183 /* If CFI is not enabled, use an empty define to not change the behavior */
184 #define QEMU_DISABLE_CFI
185 #endif
186 
187 #endif /* COMPILER_H */
188