7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
18 ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
21#if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (GCC_VERSION >= 40600)
22#define TOOLCHAIN_HAS_PRAGMA_DIAG 1
25#if !defined(TOOLCHAIN_HAS_C_GENERIC) && (GCC_VERSION >= 40900)
26#define TOOLCHAIN_HAS_C_GENERIC 1
29#if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (GCC_VERSION >= 40900)
30#define TOOLCHAIN_HAS_C_AUTO_TYPE 1
40#ifndef __ORDER_BIG_ENDIAN__
41#define __ORDER_BIG_ENDIAN__ (1)
44#ifndef __ORDER_LITTLE_ENDIAN__
45#define __ORDER_LITTLE_ENDIAN__ (2)
49#if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
50 defined(__THUMBEB__) || defined(__AARCH64EB__) || \
51 defined(__MIPSEB__) || defined(__TC32EB__)
53#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
55#elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
56 defined(__THUMBEL__) || defined(__AARCH64EL__) || \
57 defined(__MIPSEL__) || defined(__TC32EL__)
59#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
62#error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
68#if defined(__cplusplus) && (__cplusplus >= 201103L)
69#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
75#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
76 (__STDC_VERSION__) >= 201100
77#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
79#define BUILD_ASSERT(EXPR, MSG...)
83#define ZRESTRICT __restrict
85#define ZRESTRICT restrict
91#define ALIAS_OF(of) __attribute__((alias(#of)))
93#define FUNC_ALIAS(real_func, new_alias, return_type) \
94 return_type new_alias() ALIAS_OF(real_func)
96#if defined(CONFIG_ARCH_POSIX)
100#define CODE_UNREACHABLE \
102 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
103 __FILE__, __LINE__);\
104 __builtin_unreachable(); \
107#define CODE_UNREACHABLE __builtin_unreachable()
109#define FUNC_NORETURN __attribute__((__noreturn__))
114#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
115#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
117#define _NODATA_SECTION(segment) \
118 __attribute__((section(#segment ",\"wa\",@nobits#")))
122#define UNALIGNED_GET(p) \
124 struct __attribute__((__packed__)) { \
125 __typeof__(*(p)) __v; \
126 } *__p = (__typeof__(__p)) (p); \
131#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
142#define UNALIGNED_PUT(v, p) \
144 struct __attribute__((__packed__)) { \
145 __typeof__(*p) __v; \
146 } *__p = (__typeof__(__p)) (p); \
148 compiler_barrier(); \
153#define UNALIGNED_PUT(v, p) \
155 struct __attribute__((__packed__)) { \
156 __typeof__(*p) __v; \
157 } *__p = (__typeof__(__p)) (p); \
166#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
167#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
169#define __GENERIC_DOT_SECTION(segment) \
170 __attribute__((section("." STRINGIFY(segment))))
171#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
173#define ___in_section(a, b, c) \
174 __attribute__((section("." Z_STRINGIFY(a) \
176 "." Z_STRINGIFY(c))))
177#define __in_section(a, b, c) ___in_section(a, b, c)
179#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
181#define __in_section_unique_named(seg, name) \
182 ___in_section(seg, __FILE__, name)
190#if !defined(CONFIG_XIP)
192#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
193#define __ramfunc __attribute__((noinline)) \
194 __attribute__((long_call, section(".ramfunc")))
199#define __fallthrough __attribute__((fallthrough))
206#define __packed __attribute__((__packed__))
210#define __aligned(x) __attribute__((__aligned__(x)))
213#define __may_alias __attribute__((__may_alias__))
216#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
217#define __printf_like(f, a) __attribute__((format (printf, f, a)))
228#define __printf_like(f, a)
232#define __used __attribute__((__used__))
233#define __unused __attribute__((__unused__))
234#define __maybe_unused __attribute__((__unused__))
237#define __deprecated __attribute__((deprecated))
240#ifndef __attribute_const__
241#define __attribute_const__ __attribute__((__const__))
245#define __must_check __attribute__((warn_unused_result))
248#define ARG_UNUSED(x) (void)(x)
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)
254#ifndef __no_optimization
255#define __no_optimization __attribute__((optimize("-O0")))
259#define __weak __attribute__((__weak__))
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
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
290#define __WARN(msg) __WARN1(GCC warning msg)
291#define __WARN1(s) _Pragma(#s)
294#ifndef __DEPRECATED_MACRO
295#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
300#if defined(_ASMLANGUAGE)
302#if defined(CONFIG_ARM)
304#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
306#define FUNC_CODE() .thumb;
311#define FUNC_CODE() .code 32
332#if defined(_ASMLANGUAGE)
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)
347.macro glbl_text symbol
349 .type \symbol, %
function
352.macro glbl_data symbol
354 .type \symbol, %
object
357.macro weak_data symbol
359 .type \symbol, %
object
362#define GTEXT(sym) glbl_text sym
363#define GDATA(sym) glbl_data sym
364#define WDATA(sym) weak_data sym
367#define GTEXT(sym) .globl sym; .type sym, @function
368#define GDATA(sym) .globl sym; .type sym, @object
381#if defined(CONFIG_ARC)
390.macro section_var section, symbol
391 .section .\section\().\symbol
395.macro section_func section, symbol
396 .section .\section\().\symbol,
"ax"
403.macro section_subsec_func section, subsection, symbol
404 .section .\section\().\subsection, "ax"
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
415#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
416#define SECTION_FUNC(sect, sym) \
417 .section .sect.sym, "ax"; \
419 PERFOPT_ALIGN; sym : \
421#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
422 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
428#if defined(_ASMLANGUAGE)
429#if defined(CONFIG_ARM)
430#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
432#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
434#define _ASM_FILE_PROLOGUE .text; .code 32
436#elif defined(CONFIG_ARM64)
437#define _ASM_FILE_PROLOGUE .text
447#define GEN_OFFSET_EXTERN(name) extern const char name[]
449#define GEN_ABS_SYM_BEGIN(name) \
450 EXTERN_C void name(void); \
454#define GEN_ABS_SYM_END }
473#if defined(CONFIG_ARM)
483#define GEN_ABSOLUTE_SYM(name, value) \
484 __asm__(".globl\t" #name "\n\t.equ\t" #name \
486 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
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")
493#elif defined(CONFIG_X86)
495#define GEN_ABSOLUTE_SYM(name, value) \
496 __asm__(".globl\t" #name "\n\t.equ\t" #name \
498 "\n\t.type\t" #name ",@object" : : "n"(value))
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")
505#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
507#define GEN_ABSOLUTE_SYM(name, value) \
508 __asm__(".globl\t" #name "\n\t.equ\t" #name \
510 "\n\t.type\t" #name ",@object" : : "n"(value))
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")
517#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
518 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
521#define GEN_ABSOLUTE_SYM(name, value) \
522 __asm__(".globl\t" #name "\n\t.equ\t" #name \
524 "\n\t.type\t" #name ",%%object" : : "n"(value))
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")
531#elif defined(CONFIG_ARCH_POSIX)
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_SPARC)
543#define GEN_ABSOLUTE_SYM(name, value) \
544 __asm__(".global\t" #name "\n\t.equ\t" #name \
546 "\n\t.type\t" #name ",#object" : : "n"(value))
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")
554#error processor architecture not supported
557#define compiler_barrier() do { \
558 __asm__ __volatile__ ("" ::: "memory"); \
570#define Z_MAX(a, b) ({ \
572 __typeof__(a) _value_a_ = (a); \
573 __typeof__(b) _value_b_ = (b); \
574 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
582#define Z_MIN(a, b) ({ \
584 __typeof__(a) _value_a_ = (a); \
585 __typeof__(b) _value_b_ = (b); \
586 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
594#define Z_CLAMP(val, low, high) ({ \
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_ : \
610#define Z_POW2_CEIL(x) \
611 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
619#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
621#if defined(CONFIG_ASAN) && defined(__clang__)
622#define __noasan __attribute__((no_sanitize("address")))
Common toolchain abstraction.