1 /* public domain */ 2 3 #ifndef COMPILER_H 4 #define COMPILER_H 5 6 7 /*---------------------------------------------------------------------------- 8 | The macro QEMU_GNUC_PREREQ tests for minimum version of the GNU C compiler. 9 | The code is a copy of SOFTFLOAT_GNUC_PREREQ, see softfloat-macros.h. 10 *----------------------------------------------------------------------------*/ 11 #if defined(__GNUC__) && defined(__GNUC_MINOR__) 12 # define QEMU_GNUC_PREREQ(maj, min) \ 13 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 14 #else 15 # define QEMU_GNUC_PREREQ(maj, min) 0 16 #endif 17 18 #define QEMU_NORETURN __attribute__ ((__noreturn__)) 19 20 #if QEMU_GNUC_PREREQ(3, 4) 21 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 22 #else 23 #define QEMU_WARN_UNUSED_RESULT 24 #endif 25 26 #if QEMU_GNUC_PREREQ(4, 0) 27 #define QEMU_SENTINEL __attribute__((sentinel)) 28 #else 29 #define QEMU_SENTINEL 30 #endif 31 32 #if QEMU_GNUC_PREREQ(4, 3) 33 #define QEMU_ARTIFICIAL __attribute__((always_inline, artificial)) 34 #else 35 #define QEMU_ARTIFICIAL 36 #endif 37 38 #if defined(_WIN32) 39 # define QEMU_PACKED __attribute__((gcc_struct, packed)) 40 #else 41 # define QEMU_PACKED __attribute__((packed)) 42 #endif 43 44 #ifndef glue 45 #define xglue(x, y) x ## y 46 #define glue(x, y) xglue(x, y) 47 #define stringify(s) tostring(s) 48 #define tostring(s) #s 49 #endif 50 51 #ifndef likely 52 #if __GNUC__ < 3 53 #define __builtin_expect(x, n) (x) 54 #endif 55 56 #define likely(x) __builtin_expect(!!(x), 1) 57 #define unlikely(x) __builtin_expect(!!(x), 0) 58 #endif 59 60 #ifndef container_of 61 #define container_of(ptr, type, member) ({ \ 62 const typeof(((type *) 0)->member) *__mptr = (ptr); \ 63 (type *) ((char *) __mptr - offsetof(type, member));}) 64 #endif 65 66 /* Convert from a base type to a parent type, with compile time checking. */ 67 #ifdef __GNUC__ 68 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ 69 char __attribute__((unused)) offset_must_be_zero[ \ 70 -offsetof(type, field)]; \ 71 container_of(dev, type, field);})) 72 #else 73 #define DO_UPCAST(type, field, dev) container_of(dev, type, field) 74 #endif 75 76 #define typeof_field(type, field) typeof(((type *)0)->field) 77 #define type_check(t1,t2) ((t1*)0 - (t2*)0) 78 79 #define QEMU_BUILD_BUG_ON(x) \ 80 typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused)); 81 82 #if defined __GNUC__ 83 # if !QEMU_GNUC_PREREQ(4, 4) 84 /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */ 85 # define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m))) 86 # else 87 /* Use gnu_printf when supported (qemu uses standard format strings). */ 88 # define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m))) 89 # if defined(_WIN32) 90 /* Map __printf__ to __gnu_printf__ because we want standard format strings 91 * even when MinGW or GLib include files use __printf__. */ 92 # define __printf__ __gnu_printf__ 93 # endif 94 # endif 95 #else 96 #define GCC_FMT_ATTR(n, m) 97 #endif 98 99 #endif /* COMPILER_H */ 100