7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
17#define TOOLCHAIN_GCC_VERSION \
18 ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
21#if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (TOOLCHAIN_GCC_VERSION >= 40600)
22#define TOOLCHAIN_HAS_PRAGMA_DIAG 1
25#if !defined(TOOLCHAIN_HAS_C_GENERIC) && (TOOLCHAIN_GCC_VERSION >= 40900)
26#define TOOLCHAIN_HAS_C_GENERIC 1
29#if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (TOOLCHAIN_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"
69#if defined(__cplusplus) && (__cplusplus >= 201103L)
70#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
76#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
77 (__STDC_VERSION__) >= 201100
78#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
80#define BUILD_ASSERT(EXPR, MSG...)
84#define ZRESTRICT __restrict
86#define ZRESTRICT restrict
92#define ALIAS_OF(of) __attribute__((alias(#of)))
94#define FUNC_ALIAS(real_func, new_alias, return_type) \
95 return_type new_alias() ALIAS_OF(real_func)
97#if defined(CONFIG_ARCH_POSIX)
101#define CODE_UNREACHABLE \
103 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
104 __FILE__, __LINE__);\
105 __builtin_unreachable(); \
108#define CODE_UNREACHABLE __builtin_unreachable()
110#define FUNC_NORETURN __attribute__((__noreturn__))
115#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
116#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
118#define _NODATA_SECTION(segment) \
119 __attribute__((section(#segment ",\"wa\",@nobits#")))
123#define UNALIGNED_GET(p) \
125 struct __attribute__((__packed__)) { \
126 __typeof__(*(p)) __v; \
127 } *__p = (__typeof__(__p)) (p); \
132#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
143#define UNALIGNED_PUT(v, p) \
145 struct __attribute__((__packed__)) { \
146 __typeof__(*p) __v; \
147 } *__p = (__typeof__(__p)) (p); \
149 compiler_barrier(); \
154#define UNALIGNED_PUT(v, p) \
156 struct __attribute__((__packed__)) { \
157 __typeof__(*p) __v; \
158 } *__p = (__typeof__(__p)) (p); \
167#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
168#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
170#define __GENERIC_DOT_SECTION(segment) \
171 __attribute__((section("." STRINGIFY(segment))))
172#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
174#define ___in_section(a, b, c) \
175 __attribute__((section("." Z_STRINGIFY(a) \
177 "." Z_STRINGIFY(c))))
178#define __in_section(a, b, c) ___in_section(a, b, c)
180#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
182#define __in_section_unique_named(seg, name) \
183 ___in_section(seg, __FILE__, name)
191#if !defined(CONFIG_XIP)
193#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
194#define __ramfunc __attribute__((noinline)) \
195 __attribute__((long_call, section(".ramfunc")))
200#define __fallthrough __attribute__((fallthrough))
207#define __packed __attribute__((__packed__))
211#define __aligned(x) __attribute__((__aligned__(x)))
214#define __may_alias __attribute__((__may_alias__))
217#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
218#define __printf_like(f, a) __attribute__((format (printf, f, a)))
229#define __printf_like(f, a)
233#define __used __attribute__((__used__))
234#define __unused __attribute__((__unused__))
235#define __maybe_unused __attribute__((__unused__))
238#define __deprecated __attribute__((deprecated))
241#ifndef __attribute_const__
242#define __attribute_const__ __attribute__((__const__))
246#define __must_check __attribute__((warn_unused_result))
249#define ARG_UNUSED(x) (void)(x)
251#define likely(x) (__builtin_expect((bool)!!(x), true) != 0L)
252#define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
253#define POPCOUNT(x) __builtin_popcount(x)
255#ifndef __no_optimization
256#define __no_optimization __attribute__((optimize("-O0")))
260#define __weak __attribute__((__weak__))
265#define HAS_BUILTIN___builtin_add_overflow 1
266#define HAS_BUILTIN___builtin_sub_overflow 1
267#define HAS_BUILTIN___builtin_mul_overflow 1
268#define HAS_BUILTIN___builtin_div_overflow 1
271#define HAS_BUILTIN___builtin_clz 1
272#define HAS_BUILTIN___builtin_clzl 1
273#define HAS_BUILTIN___builtin_clzll 1
274#define HAS_BUILTIN___builtin_ctz 1
275#define HAS_BUILTIN___builtin_ctzl 1
276#define HAS_BUILTIN___builtin_ctzll 1
291#define __WARN(msg) __WARN1(GCC warning msg)
292#define __WARN1(s) _Pragma(#s)
295#ifndef __DEPRECATED_MACRO
296#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
301#if defined(_ASMLANGUAGE)
303#if defined(CONFIG_ARM)
305#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
307#define FUNC_CODE() .thumb;
312#define FUNC_CODE() .code 32
333#if defined(_ASMLANGUAGE)
335#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
336 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
337 || defined(CONFIG_MIPS)
338#define GTEXT(sym) .global sym; .type sym, %function
339#define GDATA(sym) .global sym; .type sym, %object
340#define WTEXT(sym) .weak sym; .type sym, %function
341#define WDATA(sym) .weak sym; .type sym, %object
342#elif defined(CONFIG_ARC)
348.macro glbl_text symbol
350 .type \symbol, %
function
353.macro glbl_data symbol
355 .type \symbol, %
object
358.macro weak_data symbol
360 .type \symbol, %
object
363#define GTEXT(sym) glbl_text sym
364#define GDATA(sym) glbl_data sym
365#define WDATA(sym) weak_data sym
368#define GTEXT(sym) .globl sym; .type sym, @function
369#define GDATA(sym) .globl sym; .type sym, @object
382#if defined(CONFIG_ARC)
391.macro section_var section, symbol
392 .section .\section\().\symbol
396.macro section_func section, symbol
397 .section .\section\().\symbol,
"ax"
404.macro section_subsec_func section, subsection, symbol
405 .section .\section\().\subsection, "ax"
410#define SECTION_VAR(sect, sym) section_var sect, sym
411#define SECTION_FUNC(sect, sym) section_func sect, sym
412#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
413 section_subsec_func sect, subsec, sym
416#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
417#define SECTION_FUNC(sect, sym) \
418 .section .sect.sym, "ax"; \
420 PERFOPT_ALIGN; sym : \
422#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
423 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
429#if defined(_ASMLANGUAGE)
430#if defined(CONFIG_ARM)
431#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
433#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
435#define _ASM_FILE_PROLOGUE .text; .code 32
437#elif defined(CONFIG_ARM64)
438#define _ASM_FILE_PROLOGUE .text
448#define GEN_OFFSET_EXTERN(name) extern const char name[]
450#define GEN_ABS_SYM_BEGIN(name) \
451 EXTERN_C void name(void); \
455#define GEN_ABS_SYM_END }
474#if defined(CONFIG_ARM)
484#define GEN_ABSOLUTE_SYM(name, value) \
485 __asm__(".globl\t" #name "\n\t.equ\t" #name \
487 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
489#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
490 __asm__(".globl\t" #name \
491 "\n\t.equ\t" #name "," #value \
492 "\n\t.type\t" #name ",%object")
494#elif defined(CONFIG_X86)
496#define GEN_ABSOLUTE_SYM(name, value) \
497 __asm__(".globl\t" #name "\n\t.equ\t" #name \
499 "\n\t.type\t" #name ",@object" : : "n"(value))
501#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
502 __asm__(".globl\t" #name \
503 "\n\t.equ\t" #name "," #value \
504 "\n\t.type\t" #name ",@object")
506#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
508#define GEN_ABSOLUTE_SYM(name, value) \
509 __asm__(".globl\t" #name "\n\t.equ\t" #name \
511 "\n\t.type\t" #name ",@object" : : "n"(value))
513#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
514 __asm__(".globl\t" #name \
515 "\n\t.equ\t" #name "," #value \
516 "\n\t.type\t" #name ",@object")
518#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
519 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
522#define GEN_ABSOLUTE_SYM(name, value) \
523 __asm__(".globl\t" #name "\n\t.equ\t" #name \
525 "\n\t.type\t" #name ",%%object" : : "n"(value))
527#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
528 __asm__(".globl\t" #name \
529 "\n\t.equ\t" #name "," #value \
530 "\n\t.type\t" #name ",%object")
532#elif defined(CONFIG_ARCH_POSIX)
533#define GEN_ABSOLUTE_SYM(name, value) \
534 __asm__(".globl\t" #name "\n\t.equ\t" #name \
536 "\n\t.type\t" #name ",@object" : : "n"(value))
538#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
539 __asm__(".globl\t" #name \
540 "\n\t.equ\t" #name "," #value \
541 "\n\t.type\t" #name ",@object")
543#elif defined(CONFIG_SPARC)
544#define GEN_ABSOLUTE_SYM(name, value) \
545 __asm__(".global\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")
555#error processor architecture not supported
558#define compiler_barrier() do { \
559 __asm__ __volatile__ ("" ::: "memory"); \
571#define Z_MAX(a, b) ({ \
573 __typeof__(a) _value_a_ = (a); \
574 __typeof__(b) _value_b_ = (b); \
575 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
583#define Z_MIN(a, b) ({ \
585 __typeof__(a) _value_a_ = (a); \
586 __typeof__(b) _value_b_ = (b); \
587 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
595#define Z_CLAMP(val, low, high) ({ \
597 __typeof__(val) _value_val_ = (val); \
598 __typeof__(low) _value_low_ = (low); \
599 __typeof__(high) _value_high_ = (high); \
600 (_value_val_ < _value_low_) ? _value_low_ : \
601 (_value_val_ > _value_high_) ? _value_high_ : \
611#define Z_POW2_CEIL(x) \
612 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
620#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
622#if defined(CONFIG_ASAN) && defined(__clang__)
623#define __noasan __attribute__((no_sanitize("address")))
633#if (TOOLCHAIN_GCC_VERSION >= 110000) || (TOOLCHAIN_CLANG_VERSION >= 70000)
634#define FUNC_NO_STACK_PROTECTOR __attribute__((no_stack_protector))
636#define FUNC_NO_STACK_PROTECTOR
Common toolchain abstraction.