XenevaOS
Loading...
Searching...
No Matches
compiler.h
Go to the documentation of this file.
1
2#ifndef __COMPILER_H__
3#define __COMPILER_H__
4
5/* mainline's first line: this header *is* the <linux/compiler_types.h> fan-
6 * out (attributes, address-space annotations, context-lock analysis). Without
7 * it a TU that reaches <linux/kthread.h> or <linux/ww_mutex.h> first sees
8 * __noreturn / __counted_by / context_lock_struct as unknown identifiers,
9 * because DCL's other fan-out points (iosys-map.h, linkage.h) are DRM-only
10 * and a tty-side file never touches them. */
12
13/*
14 * Order matters: clang for aarch64-unknown-windows defines _MSC_VER (1933)
15 * as well as __clang__, so an _MSC_VER-first ladder picks the MSVC arm and
16 * expands `__assume` -- which <linux/compiler_types.h> spells as
17 * `__attribute__((__assume__(expr)))`, not an expression -- and every
18 * `return unlikely(...)` in xarray.h then dies with "expected expression".
19 * GNU builtins come first; the MSVC arm is left for a compiler that has no
20 * other arm to take.
21 */
22#if defined(__GNUC__) || defined(__clang__)
23#define likely(x) __builtin_expect(!!(x),1)
24#define unlikely(x) __builtin_expect(!!(x), 0)
25#elif defined(_MSC_VER)
26#define likely(x) ((x) ? (1) : (__assume(0), 0))
27#define unlikely(x) ((x) ? (__assume(0), 1) : 0)
28#else
29#define likely(x) (x)
30#define unlikely(x) (x)
31#endif
32
33
34
35/*
36 * Address-space / provenance annotations mainline hangs on declarations.
37 *
38 * They are empty here, and that is the entire job: on a mainline build they
39 * carry a pointer's provenance (user, kernel, MMIO, sparse's bitwise) through
40 * a checker DCL does not run -- there is no sparse in this toolchain, only
41 * -Wall -Wextra over a freestanding aarch64 target.
42 *
43 * What is not optional is that the *names* exist. `typedef u64 __bitwise
44 * upf_t;` with no __bitwise defined turns the annotation into an ordinary
45 * identifier, and clang parses it as the typedef's NAME: __bitwise comes out
46 * as the declared type, upf_t is left stray, and the next line's
47 * `typedef unsigned int __bitwise upstat_t;` becomes a redefinition of
48 * __bitwise. The diagnostic then names upstat_t and points at the second
49 * line, so the fault reads as "upstat_t conflicts with upf_t" when the real
50 * problem is a missing macro six tokens earlier. That is a very cheap way to
51 * lose an hour, hence the note rather than a bare #define.
52 *
53 * Only the annotations the tty/serial headers actually reference are defined,
54 * not mainline's full set -- <linux/cleanup.h> records the same preference.
55 */
56#ifndef __bitwise
57#define __bitwise
58#endif
59#ifndef __force
60#define __force
61#endif
62#ifndef __user
63#define __user
64#endif
65#ifndef __kernel
66#define __kernel
67#endif
68#ifndef __iomem
69#define __iomem
70#endif
71
72
73/*
74 * fallthrough -- mainline's spelling in linux/compiler_attributes.h, where it
75 * is a statement because that is the whole vocabulary: the source says
76 * `fallthrough;` as its own statement (8250_port.c:1779), and on a build
77 * with -Wimplicit-fallthrough the macro is what turns that into an explicit
78 * annotation instead of a warning. Expanding to an empty do-while is the
79 * correct body -- there is no attribute here to attach, because DCL does not
80 * enable the warning either -- and an expression-less *statement* is required,
81 * not an expression: `do {} while (0)` is the one that both parses where the
82 * source put it and disappears.
83 *
84 * #ifndef: compiler.h pulls compiler_types.h -> compiler_attributes.h at its
85 * own line 11 now, and mainline defines `fallthrough` there first; this body
86 * is only for the TUs where nothing did.
87 */
88#ifndef fallthrough
89#define fallthrough do {} while (0)
90#endif
91
92
93/*
94 * __must_check -- and it is deliberately not the same kind of thing as the
95 * six annotations above it.
96 *
97 * __bitwise/__force/__user/__kernel/__iomem are sparse annotations: they
98 * carry meaning for a checker that is not running, and clang has no spelling
99 * for them, so empty is the truthful definition. __must_check is different
100 * -- clang does act on it (warn_unused_result), and defining it empty here
101 * would make a promise the source is making and the compiler is no longer
102 * keeping. The first user is Vendored/drivers/tty/tty.h:100:
103 *
104 * int __must_check tty_ldisc_init(struct tty_struct *tty);
105 *
106 * i.e. "this returns an error code and the caller must look at it". A port
107 * that dropped the annotation would compile exactly the same and let a failed
108 * ldisc attach read as a successful one, which is the specific bug the
109 * annotation exists to prevent.
110 *
111 * mainline: linux/compiler_attributes.h.
112 */
113#ifndef __must_check
114#define __must_check __attribute__((warn_unused_result))
115#endif
116
117/*
118 * __sched -- "put this in the scheduler section". mainline's is a section
119 * attribute (linux/compiler_attributes.h); DCL links one flat image with no
120 * such section, so it is empty -- but it has to *be* something, because it is
121 * spelled as a declaration-level annotation in front of a pointer:
122 *
123 * static struct ld_semaphore __sched *
124 * down_read_failed(...)
125 *
126 * (tty_ldsem.c:155 and :229). Left undefined, that line parses as a
127 * declaration of an object named __sched and the real function never gets
128 * named, which is why both showed up in the implicit-call list rather than as
129 * "unknown type". Empty is also correct: the annotation describes placement,
130 * and there is no placement to control.
131 */
132#define __sched
133
134/*
135 * __printf(fmt_idx, arg_idx) -- format-string checking for varargs
136 * declarations. mainline: linux/compiler_attributes.h. kobject.h decorates
137 * every varargs entry point with it (kobject_set_name, kobject_add,
138 * kobject_init_and_add, add_uevent_var), so it has to parse as a
139 * declaration-level annotation in front of a return type -- left undefined,
140 * "the line parses as a declaration of an object named __printf" and the
141 * function it belongs to is never declared, exactly like __sched above.
142 */
143#ifndef __printf
144#define __printf(fmt_idx, arg_idx) __attribute__((__format__(__printf__, fmt_idx, arg_idx)))
145#endif
146
147/*
148 * __same_type(a, b) -- "are these two the same type?" mainline: compiler.h,
149 * spelled __builtin_types_compatible_p. kobject.h:183 calls it outright
150 * (not merely as a macro argument), so undefined it is an undeclared-function
151 * error, not a quiet one.
152 */
153#ifndef __same_type
154#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
155#endif
156
157/*
158 * OPTIMIZER_HIDE_VAR -- "the compiler may assume nothing about this
159 * variable's value from here on". mainline: compiler.h; nospec.h reaches it
160 * through kernel.h and uses it so the bounds check above an
161 * array_index_nospec() cannot let the mask fold away.
162 *
163 * The "0" (var) tie is load-bearing. Output-only -- how this was first
164 * spelled here -- leaves the register unconstrained: the compiler may
165 * allocate any dead register to the empty asm and compute with whatever it
166 * last held. On drm_ioctl()'s core dispatch it picked the register holding
167 * `nr - 0x40`, stale from the driver-ioctl range test, so the nospec OR went
168 * negative for every core nr < 0x40, the mask came out 0 and the dispatch
169 * read entry 0 -- GET_CAP (nr 0x0C) ran the VERSION handler. With the tie
170 * the register must hold the variable's current value while the compiler
171 * still cannot reason about it; the empty asm emits no instruction either
172 * way, so the tie costs nothing.
173 */
174#ifndef OPTIMIZER_HIDE_VAR
175/* Make the optimizer believe the variable can be manipulated arbitrarily. */
176#define OPTIMIZER_HIDE_VAR(var) \
177 __asm__ ("" : "=r" (var) : "0" (var))
178#endif
179
180/*
181 * __BUILD_BUG_ON_ZERO_MSG / __is_array / __must_be_array / __is_constexpr --
182 * the three that <linux/overflow.h> needs and that mainline keeps in this
183 * header (:198, :204, :205, :322) rather than beside their only user. They
184 * were missing, and because nothing in the tree included <linux/overflow.h>
185 * at all the absence was invisible until dma-resv.c asked for struct_size()
186 * and got no macro back -- the preprocessor left
187 * `kmalloc_size_roundup(struct_size(list, table, max_fences))` standing as a
188 * plain function call, so the error was "use of undeclared identifier
189 * 'table'" pointing at the second *argument* rather than at struct_size,
190 * which is a long way from the real cause.
191 *
192 * __is_constexpr's test is mainline's sizeof trick: the conditional picks a
193 * void* or an int* depending on whether x folded to a constant, and the
194 * sizeof of the dereference is then 8 or 4. It is what lets
195 * __builtin_choose_expr in flex_array_size() pick the constant path for a
196 * literal count and the size_add/size_mul path for a variable one -- both
197 * arms are always type-checked, which is why __must_be_array has to resolve
198 * even in the branch that is discarded.
199 */
200#ifndef __BUILD_BUG_ON_ZERO_MSG
201/*
202 * The `0 *` is not decoration: this is zero on every ABI, while mainline's
203 * spelling -- (int)sizeof(struct{...}), overflow.h's own at :106 -- is zero
204 * only where an empty struct is 0 bytes, which is mainline's GCC/ELF ABI and
205 * not this target's. aarch64-unknown-windows gives the memberless struct 4
206 * bytes (probed), so every consumer that *adds* this value to a size was
207 * silently allocating 4 bytes more than asked -- <linux/overflow.h>'s
208 * struct_size(), flex_array_size() and struct_size_t() are all byte-identical
209 * to v7.2 and therefore not the place to fix it, and dma-resv.c's
210 * kmalloc_size_roundup(struct_size(list, table, max_fences)) was the first to
211 * pay it. Multiplying still forms the struct type -- which is what runs the
212 * _Static_assert, message and all -- and the product is an integer constant
213 * expression, which struct_size_t()'s `u8 bytes[...]` bound (overflow.h:481)
214 * requires.
215 */
216#define __BUILD_BUG_ON_ZERO_MSG(e, msg, ...) \
217 ((int)(0 * sizeof(struct {_Static_assert(!(e), msg);})))
218#endif
219
220/* &a[0] degrades to a pointer: a different type from an array */
221#ifndef __is_array
222#define __is_array(a) (!__same_type((a), &(a)[0]))
223#endif
224
225#ifndef __must_be_array
226#define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
227 "must be array")
228#endif
229
230#ifndef __is_constexpr
231#define __is_constexpr(x) \
232 (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
233#endif
234
235/*
236 * __read_mostly -- "put this in the .data..read_mostly section". mainline:
237 * compiler_attributes.h (a section attribute). DCL links one flat image with
238 * no such section, so it is empty -- but it has to *be* something: debug_locks.h
239 * writes `extern int debug_locks __read_mostly;`, and left undefined that line
240 * parses as a declaration of an object named __read_mostly rather than as the
241 * extern int, which is how it showed up as "expected ';' after top level
242 * declarator" rather than as an unknown name. Same reasoning as __sched above.
243 */
244#ifndef __read_mostly
245#define __read_mostly
246#endif
247#endif