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 defined(CONFIG_ARCH_POSIX)
110#define CODE_UNREACHABLE \
112 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
113 __FILE__, __LINE__);\
114 __builtin_unreachable(); \
117#define CODE_UNREACHABLE __builtin_unreachable()
119#define FUNC_NORETURN __attribute__((__noreturn__))
124#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
125#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
127#define _NODATA_SECTION(segment) \
128 __attribute__((section(#segment ",\"wa\",@nobits#")))
132#define UNALIGNED_GET(g) \
134 struct __attribute__((__packed__)) { \
135 __typeof__(*(g)) __v; \
136 } *__g = (__typeof__(__g)) (g); \
141#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
152#define UNALIGNED_PUT(v, p) \
154 struct __attribute__((__packed__)) { \
155 __typeof__(*p) __v; \
156 } *__p = (__typeof__(__p)) (p); \
158 compiler_barrier(); \
163#define UNALIGNED_PUT(v, p) \
165 struct __attribute__((__packed__)) { \
166 __typeof__(*p) __v; \
167 } *__p = (__typeof__(__p)) (p); \
176#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
177#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
179#define __GENERIC_DOT_SECTION(segment) \
180 __attribute__((section("." STRINGIFY(segment))))
181#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
183#define ___in_section(a, b, c) \
184 __attribute__((section("." Z_STRINGIFY(a) \
186 "." Z_STRINGIFY(c))))
187#define __in_section(a, b, c) ___in_section(a, b, c)
189#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
191#define __in_section_unique_named(seg, name) \
192 ___in_section(seg, __FILE__, name)
200#if !defined(CONFIG_XIP)
202#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
203#define __ramfunc __attribute__((noinline)) \
204 __attribute__((long_call, section(".ramfunc")))
209#define __fallthrough __attribute__((fallthrough))
216#define __packed __attribute__((__packed__))
220#define __aligned(x) __attribute__((__aligned__(x)))
223#define __may_alias __attribute__((__may_alias__))
226#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
227#define __printf_like(f, a) __attribute__((format (printf, f, a)))
238#define __printf_like(f, a)
242#define __used __attribute__((__used__))
243#define __unused __attribute__((__unused__))
244#define __maybe_unused __attribute__((__unused__))
247#define __deprecated __attribute__((deprecated))
250#ifndef __attribute_const__
251#define __attribute_const__ __attribute__((__const__))
255#define __must_check __attribute__((warn_unused_result))
258#define ARG_UNUSED(x) (void)(x)
260#define likely(x) (__builtin_expect((bool)!!(x), true) != 0L)
261#define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
262#define POPCOUNT(x) __builtin_popcount(x)
264#ifndef __no_optimization
265#define __no_optimization __attribute__((optimize("-O0")))
269#define __weak __attribute__((__weak__))
274#define HAS_BUILTIN___builtin_add_overflow 1
275#define HAS_BUILTIN___builtin_sub_overflow 1
276#define HAS_BUILTIN___builtin_mul_overflow 1
277#define HAS_BUILTIN___builtin_div_overflow 1
280#define HAS_BUILTIN___builtin_clz 1
281#define HAS_BUILTIN___builtin_clzl 1
282#define HAS_BUILTIN___builtin_clzll 1
283#define HAS_BUILTIN___builtin_ctz 1
284#define HAS_BUILTIN___builtin_ctzl 1
285#define HAS_BUILTIN___builtin_ctzll 1
300#define __WARN(msg) __WARN1(GCC warning msg)
301#define __WARN1(s) _Pragma(#s)
304#ifndef __DEPRECATED_MACRO
305#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
310#if defined(_ASMLANGUAGE)
312#if defined(CONFIG_ARM)
314#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
316#define FUNC_CODE() .thumb;
321#define FUNC_CODE() .code 32;
342#if defined(_ASMLANGUAGE)
344#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
345 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
346 || defined(CONFIG_MIPS)
347#define GTEXT(sym) .global sym; .type sym, %function
348#define GDATA(sym) .global sym; .type sym, %object
349#define WTEXT(sym) .weak sym; .type sym, %function
350#define WDATA(sym) .weak sym; .type sym, %object
351#elif defined(CONFIG_ARC)
357.macro glbl_text symbol
359 .type \symbol, %function
362.macro glbl_data symbol
364 .type \symbol, %
object
367.macro weak_data symbol
369 .type \symbol, %
object
372#define GTEXT(sym) glbl_text sym
373#define GDATA(sym) glbl_data sym
374#define WDATA(sym) weak_data sym
377#define GTEXT(sym) .globl sym; .type sym, @function
378#define GDATA(sym) .globl sym; .type sym, @object
391#if defined(CONFIG_ARC)
400.macro section_var section, symbol
401 .section .\section\().\symbol
405.macro section_func section, symbol
406 .section .\section\().\symbol,
"ax"
413.macro section_subsec_func section, subsection, symbol
414 .section .\section\().\subsection, "ax"
419#define SECTION_VAR(sect, sym) section_var sect, sym
420#define SECTION_FUNC(sect, sym) section_func sect, sym
421#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
422 section_subsec_func sect, subsec, sym
425#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
426#define SECTION_FUNC(sect, sym) \
427 .section .sect.sym, "ax"; \
429 PERFOPT_ALIGN; sym : \
431#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
432 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
438#if defined(_ASMLANGUAGE)
439#if defined(CONFIG_ARM)
440#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
442#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
444#define _ASM_FILE_PROLOGUE .text; .code 32
446#elif defined(CONFIG_ARM64)
447#define _ASM_FILE_PROLOGUE .text
457#define GEN_OFFSET_EXTERN(name) extern const char name[]
459#define GEN_ABS_SYM_BEGIN(name) \
460 EXTERN_C void name(void); \
464#define GEN_ABS_SYM_END }
483#if defined(CONFIG_ARM)
493#define GEN_ABSOLUTE_SYM(name, value) \
494 __asm__(".globl\t" #name "\n\t.equ\t" #name \
496 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
498#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
499 __asm__(".globl\t" #name \
500 "\n\t.equ\t" #name "," #value \
501 "\n\t.type\t" #name ",%object")
503#elif defined(CONFIG_X86)
505#define GEN_ABSOLUTE_SYM(name, value) \
506 __asm__(".globl\t" #name "\n\t.equ\t" #name \
508 "\n\t.type\t" #name ",@object" : : "n"(value))
510#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
511 __asm__(".globl\t" #name \
512 "\n\t.equ\t" #name "," #value \
513 "\n\t.type\t" #name ",@object")
515#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
517#define GEN_ABSOLUTE_SYM(name, value) \
518 __asm__(".globl\t" #name "\n\t.equ\t" #name \
520 "\n\t.type\t" #name ",@object" : : "n"(value))
522#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
523 __asm__(".globl\t" #name \
524 "\n\t.equ\t" #name "," #value \
525 "\n\t.type\t" #name ",@object")
527#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
528 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
531#define GEN_ABSOLUTE_SYM(name, value) \
532 __asm__(".globl\t" #name "\n\t.equ\t" #name \
534 "\n\t.type\t" #name ",%%object" : : "n"(value))
536#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
537 __asm__(".globl\t" #name \
538 "\n\t.equ\t" #name "," #value \
539 "\n\t.type\t" #name ",%object")
541#elif defined(CONFIG_ARCH_POSIX)
542#define GEN_ABSOLUTE_SYM(name, value) \
543 __asm__(".globl\t" #name "\n\t.equ\t" #name \
545 "\n\t.type\t" #name ",@object" : : "n"(value))
547#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
548 __asm__(".globl\t" #name \
549 "\n\t.equ\t" #name "," #value \
550 "\n\t.type\t" #name ",@object")
552#elif defined(CONFIG_SPARC)
553#define GEN_ABSOLUTE_SYM(name, value) \
554 __asm__(".global\t" #name "\n\t.equ\t" #name \
556 "\n\t.type\t" #name ",#object" : : "n"(value))
558#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
559 __asm__(".globl\t" #name \
560 "\n\t.equ\t" #name "," #value \
561 "\n\t.type\t" #name ",#object")
564#error processor architecture not supported
567#define compiler_barrier() do { \
568 __asm__ __volatile__ ("" ::: "memory"); \
580#define Z_MAX(a, b) ({ \
582 __typeof__(a) _value_a_ = (a); \
583 __typeof__(b) _value_b_ = (b); \
584 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
592#define Z_MIN(a, b) ({ \
594 __typeof__(a) _value_a_ = (a); \
595 __typeof__(b) _value_b_ = (b); \
596 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
604#define Z_CLAMP(val, low, high) ({ \
606 __typeof__(val) _value_val_ = (val); \
607 __typeof__(low) _value_low_ = (low); \
608 __typeof__(high) _value_high_ = (high); \
609 (_value_val_ < _value_low_) ? _value_low_ : \
610 (_value_val_ > _value_high_) ? _value_high_ : \
620#define Z_POW2_CEIL(x) \
621 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
629#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
631#if defined(CONFIG_ASAN) && defined(__clang__)
632#define __noasan __attribute__((no_sanitize("address")))
642#if (TOOLCHAIN_GCC_VERSION >= 110000) || (TOOLCHAIN_CLANG_VERSION >= 70000)
643#define FUNC_NO_STACK_PROTECTOR __attribute__((no_stack_protector))
645#define FUNC_NO_STACK_PROTECTOR
648#define TOOLCHAIN_IGNORE_WSHADOW_BEGIN \
649 _Pragma("GCC diagnostic push") \
650 _Pragma("GCC diagnostic ignored \"-Wshadow\"")
652#define TOOLCHAIN_IGNORE_WSHADOW_END \
653 _Pragma("GCC diagnostic pop")
Common toolchain abstraction.