Zephyr Project API  3.2.0
A Scalable Open Source RTOS
gcc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2014,2017 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
9
17#define GCC_VERSION \
18 ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
19
20/* GCC supports #pragma diagnostics since 4.6.0 */
21#if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (GCC_VERSION >= 40600)
22#define TOOLCHAIN_HAS_PRAGMA_DIAG 1
23#endif
24
25#if !defined(TOOLCHAIN_HAS_C_GENERIC) && (GCC_VERSION >= 40900)
26#define TOOLCHAIN_HAS_C_GENERIC 1
27#endif
28
29#if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (GCC_VERSION >= 40900)
30#define TOOLCHAIN_HAS_C_AUTO_TYPE 1
31#endif
32
33/*
34 * Older versions of GCC do not define __BYTE_ORDER__, so it must be manually
35 * detected and defined using arch-specific definitions.
36 */
37
38#ifndef _LINKER
39
40#ifndef __ORDER_BIG_ENDIAN__
41#define __ORDER_BIG_ENDIAN__ (1)
42#endif
43
44#ifndef __ORDER_LITTLE_ENDIAN__
45#define __ORDER_LITTLE_ENDIAN__ (2)
46#endif
47
48#ifndef __BYTE_ORDER__
49#if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
50 defined(__THUMBEB__) || defined(__AARCH64EB__) || \
51 defined(__MIPSEB__) || defined(__TC32EB__)
52
53#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
54
55#elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
56 defined(__THUMBEL__) || defined(__AARCH64EL__) || \
57 defined(__MIPSEL__) || defined(__TC32EL__)
58
59#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
60
61#else
62#error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
63#endif
64#endif
65
66
67/* C++11 has static_assert built in */
68#if defined(__cplusplus) && (__cplusplus >= 201103L)
69#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
70
71/*
72 * GCC 4.6 and higher have the C11 _Static_assert built in, and its
73 * output is easier to understand than the common BUILD_ASSERT macros.
74 */
75#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
76 (__STDC_VERSION__) >= 201100
77#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
78#else
79#define BUILD_ASSERT(EXPR, MSG...)
80#endif
81
82#ifdef __cplusplus
83#define ZRESTRICT __restrict
84#else
85#define ZRESTRICT restrict
86#endif
87
89#include <stdbool.h>
90
91#define ALIAS_OF(of) __attribute__((alias(#of)))
92
93#define FUNC_ALIAS(real_func, new_alias, return_type) \
94 return_type new_alias() ALIAS_OF(real_func)
95
96#if defined(CONFIG_ARCH_POSIX)
98
99/*let's not segfault if this were to happen for some reason*/
100#define CODE_UNREACHABLE \
101{\
102 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
103 __FILE__, __LINE__);\
104 __builtin_unreachable(); \
105}
106#else
107#define CODE_UNREACHABLE __builtin_unreachable()
108#endif
109#define FUNC_NORETURN __attribute__((__noreturn__))
110
111/* The GNU assembler for Cortex-M3 uses # for immediate values, not
112 * comments, so the @nobits# trick does not work.
113 */
114#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
115#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
116#else
117#define _NODATA_SECTION(segment) \
118 __attribute__((section(#segment ",\"wa\",@nobits#")))
119#endif
120
121/* Unaligned access */
122#define UNALIGNED_GET(p) \
123__extension__ ({ \
124 struct __attribute__((__packed__)) { \
125 __typeof__(*(p)) __v; \
126 } *__p = (__typeof__(__p)) (p); \
127 __p->__v; \
128})
129
130
131#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
132
133/* Version of UNALIGNED_PUT() which issues a compiler_barrier() after
134 * the store. It is required to workaround an apparent optimization
135 * bug in GCC for ARM Cortex-M3 and higher targets, when multiple
136 * byte, half-word and word stores (strb, strh, str instructions),
137 * which support unaligned access, can be coalesced into store double
138 * (strd) instruction, which doesn't support unaligned access (the
139 * compilers in question do this optimization ignoring __packed__
140 * attribute).
141 */
142#define UNALIGNED_PUT(v, p) \
143do { \
144 struct __attribute__((__packed__)) { \
145 __typeof__(*p) __v; \
146 } *__p = (__typeof__(__p)) (p); \
147 __p->__v = (v); \
148 compiler_barrier(); \
149} while (false)
150
151#else
152
153#define UNALIGNED_PUT(v, p) \
154do { \
155 struct __attribute__((__packed__)) { \
156 __typeof__(*p) __v; \
157 } *__p = (__typeof__(__p)) (p); \
158 __p->__v = (v); \
159} while (false)
160
161#endif
162
163/* Double indirection to ensure section names are expanded before
164 * stringification
165 */
166#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
167#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
168
169#define __GENERIC_DOT_SECTION(segment) \
170 __attribute__((section("." STRINGIFY(segment))))
171#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
172
173#define ___in_section(a, b, c) \
174 __attribute__((section("." Z_STRINGIFY(a) \
175 "." Z_STRINGIFY(b) \
176 "." Z_STRINGIFY(c))))
177#define __in_section(a, b, c) ___in_section(a, b, c)
178
179#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
180
181#define __in_section_unique_named(seg, name) \
182 ___in_section(seg, __FILE__, name)
183
184/* When using XIP, using '__ramfunc' places a function into RAM instead
185 * of FLASH. Make sure '__ramfunc' is defined only when
186 * CONFIG_ARCH_HAS_RAMFUNC_SUPPORT is defined, so that the compiler can
187 * report an error if '__ramfunc' is used but the architecture does not
188 * support it.
189 */
190#if !defined(CONFIG_XIP)
191#define __ramfunc
192#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
193#define __ramfunc __attribute__((noinline)) \
194 __attribute__((long_call, section(".ramfunc")))
195#endif /* !CONFIG_XIP */
196
197#ifndef __fallthrough
198#if __GNUC__ >= 7
199#define __fallthrough __attribute__((fallthrough))
200#else
201#define __fallthrough
202#endif /* __GNUC__ >= 7 */
203#endif
204
205#ifndef __packed
206#define __packed __attribute__((__packed__))
207#endif
208
209#ifndef __aligned
210#define __aligned(x) __attribute__((__aligned__(x)))
211#endif
212
213#define __may_alias __attribute__((__may_alias__))
214
215#ifndef __printf_like
216#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
217#define __printf_like(f, a) __attribute__((format (printf, f, a)))
218#else
219/*
220 * The Zephyr stdint convention enforces int32_t = int, int64_t = long long,
221 * and intptr_t = long so that short string format length modifiers can be
222 * used universally across ILP32 and LP64 architectures. Without that it
223 * is possible for ILP32 toolchains to have int32_t = long and intptr_t = int
224 * clashing with the Zephyr convention and generating pointless warnings
225 * as they're still the same size. Inhibit the format argument type
226 * validation in that case and let the other configs do it.
227 */
228#define __printf_like(f, a)
229#endif
230#endif
231
232#define __used __attribute__((__used__))
233#define __unused __attribute__((__unused__))
234#define __maybe_unused __attribute__((__unused__))
235
236#ifndef __deprecated
237#define __deprecated __attribute__((deprecated))
238#endif
239
240#ifndef __attribute_const__
241#define __attribute_const__ __attribute__((__const__))
242#endif
243
244#ifndef __must_check
245#define __must_check __attribute__((warn_unused_result))
246#endif
247
248#define ARG_UNUSED(x) (void)(x)
249
250#define likely(x) (__builtin_expect((bool)!!(x), true) != 0L)
251#define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
252#define popcount(x) __builtin_popcount(x)
253
254#ifndef __no_optimization
255#define __no_optimization __attribute__((optimize("-O0")))
256#endif
257
258#ifndef __weak
259#define __weak __attribute__((__weak__))
260#endif
261
262/* Builtins with availability that depend on the compiler version. */
263#if __GNUC__ >= 5
264#define HAS_BUILTIN___builtin_add_overflow 1
265#define HAS_BUILTIN___builtin_sub_overflow 1
266#define HAS_BUILTIN___builtin_mul_overflow 1
267#define HAS_BUILTIN___builtin_div_overflow 1
268#endif
269#if __GNUC__ >= 4
270#define HAS_BUILTIN___builtin_clz 1
271#define HAS_BUILTIN___builtin_clzl 1
272#define HAS_BUILTIN___builtin_clzll 1
273#define HAS_BUILTIN___builtin_ctz 1
274#define HAS_BUILTIN___builtin_ctzl 1
275#define HAS_BUILTIN___builtin_ctzll 1
276#endif
277
278/*
279 * Be *very* careful with these. You cannot filter out __DEPRECATED_MACRO with
280 * -wno-deprecated, which has implications for -Werror.
281 */
282
283/*
284 * Expands to nothing and generates a warning. Used like
285 *
286 * #define FOO __WARN("Please use BAR instead") ...
287 *
288 * The warning points to the location where the macro is expanded.
289 */
290#define __WARN(msg) __WARN1(GCC warning msg)
291#define __WARN1(s) _Pragma(#s)
292
293/* Generic message */
294#ifndef __DEPRECATED_MACRO
295#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
296#endif
297
298/* These macros allow having ARM asm functions callable from thumb */
299
300#if defined(_ASMLANGUAGE)
301
302#if defined(CONFIG_ARM)
303
304#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
305
306#define FUNC_CODE() .thumb;
307#define FUNC_INSTR(a)
308
309#else
310
311#define FUNC_CODE() .code 32
312#define FUNC_INSTR(a)
313
314#endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
315
316#else
317
318#define FUNC_CODE()
319#define FUNC_INSTR(a)
320
321#endif /* CONFIG_ARM */
322
323#endif /* _ASMLANGUAGE */
324
325/*
326 * These macros are used to declare assembly language symbols that need
327 * to be typed properly(func or data) to be visible to the OMF tool.
328 * So that the build tool could mark them as an entry point to be linked
329 * correctly. This is an elfism. Use #if 0 for a.out.
330 */
331
332#if defined(_ASMLANGUAGE)
333
334#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
335 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
336 || defined(CONFIG_MIPS)
337#define GTEXT(sym) .global sym; .type sym, %function
338#define GDATA(sym) .global sym; .type sym, %object
339#define WTEXT(sym) .weak sym; .type sym, %function
340#define WDATA(sym) .weak sym; .type sym, %object
341#elif defined(CONFIG_ARC)
342/*
343 * Need to use assembly macros because ';' is interpreted as the start of
344 * a single line comment in the ARC assembler.
345 */
346
347.macro glbl_text symbol
348 .globl \symbol
349 .type \symbol, %function
350.endm
351
352.macro glbl_data symbol
353 .globl \symbol
354 .type \symbol, %object
355.endm
356
357.macro weak_data symbol
358 .weak \symbol
359 .type \symbol, %object
360.endm
361
362#define GTEXT(sym) glbl_text sym
363#define GDATA(sym) glbl_data sym
364#define WDATA(sym) weak_data sym
365
366#else /* !CONFIG_ARM && !CONFIG_ARC */
367#define GTEXT(sym) .globl sym; .type sym, @function
368#define GDATA(sym) .globl sym; .type sym, @object
369#endif
370
371/*
372 * These macros specify the section in which a given function or variable
373 * resides.
374 *
375 * - SECTION_FUNC allows only one function to reside in a sub-section
376 * - SECTION_SUBSEC_FUNC allows multiple functions to reside in a sub-section
377 * This ensures that garbage collection only discards the section
378 * if all functions in the sub-section are not referenced.
379 */
380
381#if defined(CONFIG_ARC)
382/*
383 * Need to use assembly macros because ';' is interpreted as the start of
384 * a single line comment in the ARC assembler.
385 *
386 * Also, '\‍()' is needed in the .section directive of these macros for
387 * correct substitution of the 'section' variable.
388 */
389
390.macro section_var section, symbol
391 .section .\section\‍().\symbol
392 \symbol :
393.endm
394
395.macro section_func section, symbol
396 .section .\section\().\symbol, "ax"
397 FUNC_CODE()
398 PERFOPT_ALIGN
399 \symbol :
400 FUNC_INSTR(\symbol)
401.endm
402
403.macro section_subsec_func section, subsection, symbol
404 .section .\section\().\subsection, "ax"
405 PERFOPT_ALIGN
406 \symbol :
407.endm
408
409#define SECTION_VAR(sect, sym) section_var sect, sym
410#define SECTION_FUNC(sect, sym) section_func sect, sym
411#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
412 section_subsec_func sect, subsec, sym
413#else /* !CONFIG_ARC */
414
415#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
416#define SECTION_FUNC(sect, sym) \
417 .section .sect.sym, "ax"; \
418 FUNC_CODE() \
419 PERFOPT_ALIGN; sym : \
420 FUNC_INSTR(sym)
421#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
422 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
423
424#endif /* CONFIG_ARC */
425
426#endif /* _ASMLANGUAGE */
427
428#if defined(_ASMLANGUAGE)
429#if defined(CONFIG_ARM)
430#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
431/* '.syntax unified' is a gcc-ism used in thumb-2 asm files */
432#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
433#else
434#define _ASM_FILE_PROLOGUE .text; .code 32
435#endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
436#elif defined(CONFIG_ARM64)
437#define _ASM_FILE_PROLOGUE .text
438#endif /* CONFIG_ARM64 || CONFIG_ARM */
439#endif /* _ASMLANGUAGE */
440
441/*
442 * These macros generate absolute symbols for GCC
443 */
444
445/* create an extern reference to the absolute symbol */
446
447#define GEN_OFFSET_EXTERN(name) extern const char name[]
448
449#define GEN_ABS_SYM_BEGIN(name) \
450 EXTERN_C void name(void); \
451 void name(void) \
452 {
453
454#define GEN_ABS_SYM_END }
455
456/*
457 * Note that GEN_ABSOLUTE_SYM(), depending on the architecture
458 * and toolchain, may restrict the range of values permitted
459 * for assignment to the named symbol.
460 *
461 * For example, on x86, "value" is interpreted as signed
462 * 32-bit integer. Passing in an unsigned 32-bit integer
463 * with MSB set would result in a negative integer.
464 * Moreover, GCC would error out if an integer larger
465 * than 2^32-1 is passed as "value".
466 */
467
468/*
469 * GEN_ABSOLUTE_SYM_KCONFIG() is outputted by the build system
470 * to generate named symbol/value pairs for kconfigs.
471 */
472
473#if defined(CONFIG_ARM)
474
475/*
476 * GNU/ARM backend does not have a proper operand modifier which does not
477 * produces prefix # followed by value, such as %0 for PowerPC, Intel, and
478 * MIPS. The workaround performed here is using %B0 which converts
479 * the value to ~(value). Thus "n"(~(value)) is set in operand constraint
480 * to output (value) in the ARM specific GEN_OFFSET macro.
481 */
482
483#define GEN_ABSOLUTE_SYM(name, value) \
484 __asm__(".globl\t" #name "\n\t.equ\t" #name \
485 ",%B0" \
486 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
487
488#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
489 __asm__(".globl\t" #name \
490 "\n\t.equ\t" #name "," #value \
491 "\n\t.type\t" #name ",%object")
492
493#elif defined(CONFIG_X86)
494
495#define GEN_ABSOLUTE_SYM(name, value) \
496 __asm__(".globl\t" #name "\n\t.equ\t" #name \
497 ",%c0" \
498 "\n\t.type\t" #name ",@object" : : "n"(value))
499
500#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
501 __asm__(".globl\t" #name \
502 "\n\t.equ\t" #name "," #value \
503 "\n\t.type\t" #name ",@object")
504
505#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
506
507#define GEN_ABSOLUTE_SYM(name, value) \
508 __asm__(".globl\t" #name "\n\t.equ\t" #name \
509 ",%c0" \
510 "\n\t.type\t" #name ",@object" : : "n"(value))
511
512#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
513 __asm__(".globl\t" #name \
514 "\n\t.equ\t" #name "," #value \
515 "\n\t.type\t" #name ",@object")
516
517#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
518 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
519
520/* No special prefixes necessary for constants in this arch AFAICT */
521#define GEN_ABSOLUTE_SYM(name, value) \
522 __asm__(".globl\t" #name "\n\t.equ\t" #name \
523 ",%0" \
524 "\n\t.type\t" #name ",%%object" : : "n"(value))
525
526#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
527 __asm__(".globl\t" #name \
528 "\n\t.equ\t" #name "," #value \
529 "\n\t.type\t" #name ",%object")
530
531#elif defined(CONFIG_ARCH_POSIX)
532#define GEN_ABSOLUTE_SYM(name, value) \
533 __asm__(".globl\t" #name "\n\t.equ\t" #name \
534 ",%c0" \
535 "\n\t.type\t" #name ",@object" : : "n"(value))
536
537#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
538 __asm__(".globl\t" #name \
539 "\n\t.equ\t" #name "," #value \
540 "\n\t.type\t" #name ",@object")
541
542#elif defined(CONFIG_SPARC)
543#define GEN_ABSOLUTE_SYM(name, value) \
544 __asm__(".global\t" #name "\n\t.equ\t" #name \
545 ",%0" \
546 "\n\t.type\t" #name ",#object" : : "n"(value))
547
548#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
549 __asm__(".globl\t" #name \
550 "\n\t.equ\t" #name "," #value \
551 "\n\t.type\t" #name ",#object")
552
553#else
554#error processor architecture not supported
555#endif
556
557#define compiler_barrier() do { \
558 __asm__ __volatile__ ("" ::: "memory"); \
559} while (false)
560
570#define Z_MAX(a, b) ({ \
571 /* random suffix to avoid naming conflict */ \
572 __typeof__(a) _value_a_ = (a); \
573 __typeof__(b) _value_b_ = (b); \
574 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
575 })
576
582#define Z_MIN(a, b) ({ \
583 /* random suffix to avoid naming conflict */ \
584 __typeof__(a) _value_a_ = (a); \
585 __typeof__(b) _value_b_ = (b); \
586 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
587 })
588
594#define Z_CLAMP(val, low, high) ({ \
595 /* random suffix to avoid naming conflict */ \
596 __typeof__(val) _value_val_ = (val); \
597 __typeof__(low) _value_low_ = (low); \
598 __typeof__(high) _value_high_ = (high); \
599 (_value_val_ < _value_low_) ? _value_low_ : \
600 (_value_val_ > _value_high_) ? _value_high_ : \
601 _value_val_; \
602 })
603
610#define Z_POW2_CEIL(x) \
611 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
612
619#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
620
621#if defined(CONFIG_ASAN) && defined(__clang__)
622#define __noasan __attribute__((no_sanitize("address")))
623#else
624#define __noasan
625#endif
626
627#endif /* !_LINKER */
628#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */
Common toolchain abstraction.