7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
10#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_H_
11#error Please do not include toolchain-specific headers directly, use <zephyr/toolchain.h> instead
21#define TOOLCHAIN_GCC_VERSION \
22 ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
25#if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (TOOLCHAIN_GCC_VERSION >= 40600)
26#define TOOLCHAIN_HAS_PRAGMA_DIAG 1
29#if !defined(TOOLCHAIN_HAS_C_GENERIC) && (TOOLCHAIN_GCC_VERSION >= 40900)
30#define TOOLCHAIN_HAS_C_GENERIC 1
33#if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (TOOLCHAIN_GCC_VERSION >= 40900)
34#define TOOLCHAIN_HAS_C_AUTO_TYPE 1
37#define TOOLCHAIN_HAS_ZLA 1
46#ifndef __ORDER_BIG_ENDIAN__
47#define __ORDER_BIG_ENDIAN__ (1)
50#ifndef __ORDER_LITTLE_ENDIAN__
51#define __ORDER_LITTLE_ENDIAN__ (2)
55#if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
56 defined(__THUMBEB__) || defined(__AARCH64EB__) || \
57 defined(__MIPSEB__) || defined(__TC32EB__)
59#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
61#elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
62 defined(__THUMBEL__) || defined(__AARCH64EL__) || \
63 defined(__MIPSEL__) || defined(__TC32EL__)
65#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
68#error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
75#if defined(__cplusplus) && (__cplusplus >= 201103L)
76#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
84#elif !defined(__cplusplus) && \
85 (((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))) || \
86 (__STDC_VERSION__) >= 201100)
87#define BUILD_ASSERT(EXPR, MSG...) _Static_assert((EXPR), "" MSG)
89#define BUILD_ASSERT(EXPR, MSG...)
93#define ZRESTRICT __restrict
95#define ZRESTRICT restrict
101#define ALIAS_OF(of) __attribute__((alias(#of)))
103#define FUNC_ALIAS(real_func, new_alias, return_type) \
104 return_type new_alias() ALIAS_OF(real_func)
106#if TOOLCHAIN_GCC_VERSION < 40500
107#define __builtin_unreachable() __builtin_trap()
110#if defined(CONFIG_ARCH_POSIX) && !defined(_ASMLANGUAGE)
114#define CODE_UNREACHABLE \
116 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
117 __FILE__, __LINE__);\
118 __builtin_unreachable(); \
121#define CODE_UNREACHABLE __builtin_unreachable()
123#define FUNC_NORETURN __attribute__((__noreturn__))
128#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
129#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
131#define _NODATA_SECTION(segment) \
132 __attribute__((section(#segment ",\"wa\",@nobits#")))
136#define UNALIGNED_GET(g) \
138 struct __attribute__((__packed__)) { \
139 __typeof__(*(g)) __v; \
140 } *__g = (__typeof__(__g)) (g); \
145#if (__GNUC__ >= 7) && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
156#define UNALIGNED_PUT(v, p) \
158 struct __attribute__((__packed__)) { \
159 __typeof__(*p) __v; \
160 } *__p = (__typeof__(__p)) (p); \
162 compiler_barrier(); \
167#define UNALIGNED_PUT(v, p) \
169 struct __attribute__((__packed__)) { \
170 __typeof__(*p) __v; \
171 } *__p = (__typeof__(__p)) (p); \
180#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
181#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
183#define __GENERIC_DOT_SECTION(segment) \
184 __attribute__((section("." STRINGIFY(segment))))
185#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
187#define ___in_section(a, b, c) \
188 __attribute__((section("." Z_STRINGIFY(a) \
190 "." Z_STRINGIFY(c))))
191#define __in_section(a, b, c) ___in_section(a, b, c)
193#ifndef __in_section_unique
194#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
197#ifndef __in_section_unique_named
198#define __in_section_unique_named(seg, name) \
199 ___in_section(seg, __FILE__, name)
208#if !defined(CONFIG_XIP)
210#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
211#if defined(CONFIG_ARM)
212#define __ramfunc __attribute__((noinline)) \
213 __attribute__((long_call, section(".ramfunc")))
215#define __ramfunc __attribute__((noinline)) \
216 __attribute__((section(".ramfunc")))
222#define __fallthrough __attribute__((fallthrough))
229#define __packed __attribute__((__packed__))
233#define __aligned(x) __attribute__((__aligned__(x)))
237#define __noinline __attribute__((noinline))
240#define __may_alias __attribute__((__may_alias__))
243#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
244#define __printf_like(f, a) __attribute__((format (printf, f, a)))
255#define __printf_like(f, a)
259#define __used __attribute__((__used__))
260#define __unused __attribute__((__unused__))
261#define __maybe_unused __attribute__((__unused__))
264#define __deprecated __attribute__((deprecated))
270#ifndef __attribute_const__
271#define __attribute_const__ __attribute__((__const__))
275#define __must_check __attribute__((warn_unused_result))
278#define ARG_UNUSED(x) (void)(x)
280#define likely(x) (__builtin_expect((bool)!!(x), true) != 0L)
281#define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
282#define POPCOUNT(x) __builtin_popcount(x)
284#ifndef __no_optimization
285#define __no_optimization __attribute__((optimize("-O0")))
289#define __weak __attribute__((__weak__))
292#ifndef __attribute_nonnull
293#define __attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
298#define HAS_BUILTIN___builtin_add_overflow 1
299#define HAS_BUILTIN___builtin_sub_overflow 1
300#define HAS_BUILTIN___builtin_mul_overflow 1
301#define HAS_BUILTIN___builtin_div_overflow 1
304#define HAS_BUILTIN___builtin_clz 1
305#define HAS_BUILTIN___builtin_clzl 1
306#define HAS_BUILTIN___builtin_clzll 1
307#define HAS_BUILTIN___builtin_ctz 1
308#define HAS_BUILTIN___builtin_ctzl 1
309#define HAS_BUILTIN___builtin_ctzll 1
324#define __WARN(msg) __WARN1(GCC warning msg)
325#define __WARN1(s) _Pragma(#s)
328#ifndef __DEPRECATED_MACRO
329#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
337#if defined(_ASMLANGUAGE)
339#if defined(CONFIG_ARM)
341#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
343#define FUNC_CODE() .thumb;
348#define FUNC_CODE() .code 32;
369#if defined(_ASMLANGUAGE)
371#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
372 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
373 || defined(CONFIG_MIPS)
374#define GTEXT(sym) .global sym; .type sym, %function
375#define GDATA(sym) .global sym; .type sym, %object
376#define WTEXT(sym) .weak sym; .type sym, %function
377#define WDATA(sym) .weak sym; .type sym, %object
378#elif defined(CONFIG_ARC)
384.macro glbl_text symbol
386 .type \symbol, %function
389.macro glbl_data symbol
391 .type \symbol, %
object
394.macro weak_data symbol
396 .type \symbol, %
object
399#define GTEXT(sym) glbl_text sym
400#define GDATA(sym) glbl_data sym
401#define WDATA(sym) weak_data sym
404#define GTEXT(sym) .globl sym; .type sym, @function
405#define GDATA(sym) .globl sym; .type sym, @object
418#if defined(CONFIG_ARC)
427.macro section_var section, symbol
428 .section .\section\().\symbol
432.macro section_func section, symbol
433 .section .\section\().\symbol,
"ax"
440.macro section_subsec_func section, subsection, symbol
441 .section .\section\().\subsection,
"ax"
446#define SECTION_VAR(sect, sym) section_var sect, sym
447#define SECTION_FUNC(sect, sym) section_func sect, sym
448#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
449 section_subsec_func sect, subsec, sym
452#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
453#define SECTION_FUNC(sect, sym) \
454 .section .sect.sym, "ax"; \
456 PERFOPT_ALIGN; sym : \
458#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
459 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
465#if defined(_ASMLANGUAGE)
466#if defined(CONFIG_ARM)
467#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
469#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
471#define _ASM_FILE_PROLOGUE .text; .code 32
473#elif defined(CONFIG_ARM64)
474#define _ASM_FILE_PROLOGUE .text
484#define GEN_OFFSET_EXTERN(name) extern const char name[]
486#define GEN_ABS_SYM_BEGIN(name) \
487 EXTERN_C void name(void); \
491#define GEN_ABS_SYM_END }
510#if defined(CONFIG_ARM)
520#define GEN_ABSOLUTE_SYM(name, value) \
521 __asm__(".globl\t" #name "\n\t.equ\t" #name \
523 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
525#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
526 __asm__(".globl\t" #name \
527 "\n\t.equ\t" #name "," #value \
528 "\n\t.type\t" #name ",%object")
530#elif defined(CONFIG_X86)
532#define GEN_ABSOLUTE_SYM(name, value) \
533 __asm__(".globl\t" #name "\n\t.equ\t" #name \
535 "\n\t.type\t" #name ",@object" : : "n"(value))
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")
542#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
544#define GEN_ABSOLUTE_SYM(name, value) \
545 __asm__(".globl\t" #name "\n\t.equ\t" #name \
547 "\n\t.type\t" #name ",@object" : : "n"(value))
549#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
550 __asm__(".globl\t" #name \
551 "\n\t.equ\t" #name "," #value \
552 "\n\t.type\t" #name ",@object")
554#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
555 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
558#define GEN_ABSOLUTE_SYM(name, value) \
559 __asm__(".globl\t" #name "\n\t.equ\t" #name \
561 "\n\t.type\t" #name ",%%object" : : "n"(value))
563#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
564 __asm__(".globl\t" #name \
565 "\n\t.equ\t" #name "," #value \
566 "\n\t.type\t" #name ",%object")
568#elif defined(CONFIG_ARCH_POSIX)
569#define GEN_ABSOLUTE_SYM(name, value) \
570 __asm__(".globl\t" #name "\n\t.equ\t" #name \
572 "\n\t.type\t" #name ",@object" : : "n"(value))
574#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
575 __asm__(".globl\t" #name \
576 "\n\t.equ\t" #name "," #value \
577 "\n\t.type\t" #name ",@object")
579#elif defined(CONFIG_SPARC)
580#define GEN_ABSOLUTE_SYM(name, value) \
581 __asm__(".global\t" #name "\n\t.equ\t" #name \
583 "\n\t.type\t" #name ",#object" : : "n"(value))
585#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
586 __asm__(".globl\t" #name \
587 "\n\t.equ\t" #name "," #value \
588 "\n\t.type\t" #name ",#object")
591#error processor architecture not supported
594#define compiler_barrier() do { \
595 __asm__ __volatile__ ("" ::: "memory"); \
607#define Z_MAX(a, b) ({ \
609 __typeof__(a) _value_a_ = (a); \
610 __typeof__(b) _value_b_ = (b); \
611 (_value_a_ > _value_b_) ? _value_a_ : _value_b_; \
619#define Z_MIN(a, b) ({ \
621 __typeof__(a) _value_a_ = (a); \
622 __typeof__(b) _value_b_ = (b); \
623 (_value_a_ < _value_b_) ? _value_a_ : _value_b_; \
631#define Z_CLAMP(val, low, high) ({ \
633 __typeof__(val) _value_val_ = (val); \
634 __typeof__(low) _value_low_ = (low); \
635 __typeof__(high) _value_high_ = (high); \
636 (_value_val_ < _value_low_) ? _value_low_ : \
637 (_value_val_ > _value_high_) ? _value_high_ : \
647#define Z_POW2_CEIL(x) \
648 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
656#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
658#if defined(CONFIG_ASAN) && defined(__clang__)
659#define __noasan __attribute__((no_sanitize("address")))
664#if defined(CONFIG_UBSAN)
665#define __noubsan __attribute__((no_sanitize("undefined")))
675#if (TOOLCHAIN_GCC_VERSION >= 110000) || \
676 (defined(TOOLCHAIN_CLANG_VERSION) && (TOOLCHAIN_CLANG_VERSION >= 70000))
677#define FUNC_NO_STACK_PROTECTOR __attribute__((no_stack_protector))
679#define FUNC_NO_STACK_PROTECTOR
682#define TOOLCHAIN_IGNORE_WSHADOW_BEGIN \
683 _Pragma("GCC diagnostic push") \
684 _Pragma("GCC diagnostic ignored \"-Wshadow\"")
686#define TOOLCHAIN_IGNORE_WSHADOW_END \
687 _Pragma("GCC diagnostic pop")