1 #ifndef __LINUX_COMPILER_H
2 #define __LINUX_COMPILER_H
3 
4 #if !defined(__GNUC__) || (__GNUC__ < 4)
5 #error Sorry, your compiler is too old/not recognized.
6 #endif
7 
8 #define barrier()     __asm__ __volatile__("": : :"memory")
9 
10 #define likely(x)     __builtin_expect(!!(x),1)
11 #define unlikely(x)   __builtin_expect(!!(x),0)
12 
13 #define inline        __inline__
14 #define always_inline __inline__ __attribute__ ((__always_inline__))
15 #define noinline      __attribute__((__noinline__))
16 
17 #define noreturn      __attribute__((__noreturn__))
18 
19 #define __packed      __attribute__((__packed__))
20 
21 #define __weak        __attribute__((__weak__))
22 
23 #if !defined(__clang__)
24 # define nocall       __attribute__((__error__("Nonstandard ABI")))
25 #else
26 # define nocall
27 #endif
28 
29 #if (!defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5))
30 #define unreachable() do {} while (1)
31 #else
32 #define unreachable() __builtin_unreachable()
33 #endif
34 
35 #ifdef __clang__
36 /* Clang can replace some vars with new automatic ones that go in .data;
37  * mark all explicit-segment vars 'used' to prevent that. */
38 #define __section(s)      __used __attribute__((__section__(s)))
39 #else
40 #define __section(s)      __attribute__((__section__(s)))
41 #endif
42 #define __used_section(s) __used __attribute__((__section__(s)))
43 #define __text_section(s) __attribute__((__section__(s)))
44 
45 #define __aligned(a) __attribute__((__aligned__(a)))
46 
47 #ifdef INIT_SECTIONS_ONLY
48 /*
49  * For sources indicated to have only init code, make sure even
50  * inline functions not expanded inline get placed in .init.text.
51  */
52 #include <xen/init.h>
53 #define __inline__ __inline__ __init
54 #endif
55 
56 #define __attribute_pure__  __attribute__((__pure__))
57 #define __attribute_const__ __attribute__((__const__))
58 #define __transparent__     __attribute__((__transparent_union__))
59 
60 /*
61  * The difference between the following two attributes is that __used is
62  * intended to be used in cases where a reference to an identifier may be
63  * invisible to the compiler (e.g. an inline assembly operand not listed
64  * in the asm()'s operands), preventing the compiler from eliminating the
65  * variable or function.
66  * __maybe_unused otoh is to be used to merely prevent warnings (e.g. when
67  * an identifier is used only inside a preprocessor conditional, yet putting
68  * its declaration/definition inside another conditional would harm code
69  * readability).
70  */
71 #define __used         __attribute__((__used__))
72 #define __maybe_unused __attribute__((__unused__))
73 
74 #define __must_check __attribute__((__warn_unused_result__))
75 #define __nonnull(...) __attribute__((__nonnull__(__VA_ARGS__)))
76 
77 #define offsetof(a,b) __builtin_offsetof(a,b)
78 
79 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
80 #define alignof __alignof__
81 #endif
82 
83 /* &a[0] degrades to a pointer: a different type from an array */
84 #define __must_be_array(a) \
85   BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
86 
87 #ifdef CONFIG_CC_HAS_VISIBILITY_ATTRIBUTE
88 /* Results in more efficient PIC code (no indirections through GOT or PLT). */
89 #pragma GCC visibility push(hidden)
90 #endif
91 
92 /* Make the optimizer believe the variable can be manipulated arbitrarily. */
93 #define OPTIMIZER_HIDE_VAR(var) __asm__ ( "" : "+g" (var) )
94 
95 /* This macro obfuscates arithmetic on a variable address so that gcc
96    shouldn't recognize the original var, and make assumptions about it */
97 /*
98  * Versions of the ppc64 compiler before 4.1 had a bug where use of
99  * RELOC_HIDE could trash r30. The bug can be worked around by changing
100  * the inline assembly constraint from =g to =r, in this particular
101  * case either is valid.
102  */
103 #define RELOC_HIDE(ptr, off)                    \
104   ({ unsigned long __ptr;                       \
105     __asm__ ("" : "=r"(__ptr) : "0"(ptr));      \
106     (typeof(ptr)) (__ptr + (off)); })
107 
108 #ifdef __GCC_ASM_FLAG_OUTPUTS__
109 # define ASM_FLAG_OUT(yes, no) yes
110 #else
111 # define ASM_FLAG_OUT(yes, no) no
112 #endif
113 
114 /*
115  * NB: we need to disable the gcc-compat warnings for clang in some places or
116  * else it will complain with: "'break' is bound to loop, GCC binds it to
117  * switch" when a switch is used inside of a while expression inside of a
118  * switch statement, ie:
119  *
120  * switch ( ... )
121  * {
122  * case ...:
123  *      while ( ({ int x; switch ( foo ) { case 1: x = 1; break; } x }) )
124  *      {
125  *              ...
126  *
127  * This has already been reported upstream:
128  * http://bugs.llvm.org/show_bug.cgi?id=32595
129  */
130 #ifdef __clang__
131 # define CLANG_DISABLE_WARN_GCC_COMPAT_START                    \
132     _Pragma("clang diagnostic push")                            \
133     _Pragma("clang diagnostic ignored \"-Wgcc-compat\"")
134 # define CLANG_DISABLE_WARN_GCC_COMPAT_END                      \
135     _Pragma("clang diagnostic pop")
136 #else
137 # define CLANG_DISABLE_WARN_GCC_COMPAT_START
138 # define CLANG_DISABLE_WARN_GCC_COMPAT_END
139 #endif
140 
141 #endif /* __LINUX_COMPILER_H */
142