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__))
209#define __aligned(x) __attribute__((__aligned__(x)))
211#define __may_alias __attribute__((__may_alias__))
213#define __printf_like(f, a) __attribute__((format (printf, f, a)))
215#define __used __attribute__((__used__))
217#define __deprecated __attribute__((deprecated))
219#ifndef __attribute_const__
220#define __attribute_const__ __attribute__((__const__))
223#define __must_check __attribute__((warn_unused_result))
225#define ARG_UNUSED(x) (void)(x)
227#define likely(x) __builtin_expect((bool)!!(x), true)
228#define unlikely(x) __builtin_expect((bool)!!(x), false)
230#define popcount(x) __builtin_popcount(x)
232#ifndef __no_optimization
233#define __no_optimization __attribute__((optimize("-O0")))
237#define __weak __attribute__((__weak__))
239#define __unused __attribute__((__unused__))
243#define HAS_BUILTIN___builtin_add_overflow 1
244#define HAS_BUILTIN___builtin_sub_overflow 1
245#define HAS_BUILTIN___builtin_mul_overflow 1
246#define HAS_BUILTIN___builtin_div_overflow 1
249#define HAS_BUILTIN___builtin_clz 1
250#define HAS_BUILTIN___builtin_clzl 1
251#define HAS_BUILTIN___builtin_clzll 1
252#define HAS_BUILTIN___builtin_ctz 1
253#define HAS_BUILTIN___builtin_ctzl 1
254#define HAS_BUILTIN___builtin_ctzll 1
269#define __WARN(msg) __WARN1(GCC warning msg)
270#define __WARN1(s) _Pragma(#s)
273#ifndef __DEPRECATED_MACRO
274#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
279#if defined(_ASMLANGUAGE)
281#if defined(CONFIG_ARM)
283#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
285#define FUNC_CODE() .thumb;
290#define FUNC_CODE() .code 32
311#if defined(_ASMLANGUAGE)
313#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
314 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
315 || defined(CONFIG_MIPS)
316#define GTEXT(sym) .global sym; .type sym, %function
317#define GDATA(sym) .global sym; .type sym, %object
318#define WTEXT(sym) .weak sym; .type sym, %function
319#define WDATA(sym) .weak sym; .type sym, %object
320#elif defined(CONFIG_ARC)
326.macro glbl_text symbol
328 .type \symbol, %
function
331.macro glbl_data symbol
333 .type \symbol, %
object
336.macro weak_data symbol
338 .type \symbol, %
object
341#define GTEXT(sym) glbl_text sym
342#define GDATA(sym) glbl_data sym
343#define WDATA(sym) weak_data sym
346#define GTEXT(sym) .globl sym; .type sym, @function
347#define GDATA(sym) .globl sym; .type sym, @object
360#if defined(CONFIG_ARC)
369.macro section_var section, symbol
370 .section .\section\().\symbol
374.macro section_func section, symbol
375 .section .\section\().\symbol,
"ax"
382.macro section_subsec_func section, subsection, symbol
383 .section .\section\().\subsection, "ax"
388#define SECTION_VAR(sect, sym) section_var sect, sym
389#define SECTION_FUNC(sect, sym) section_func sect, sym
390#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
391 section_subsec_func sect, subsec, sym
394#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
395#define SECTION_FUNC(sect, sym) \
396 .section .sect.sym, "ax"; \
398 PERFOPT_ALIGN; sym : \
400#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
401 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
407#if defined(_ASMLANGUAGE)
408#if defined(CONFIG_ARM)
409#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
411#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
413#define _ASM_FILE_PROLOGUE .text; .code 32
415#elif defined(CONFIG_ARM64)
416#define _ASM_FILE_PROLOGUE .text
426#define GEN_OFFSET_EXTERN(name) extern const char name[]
428#define GEN_ABS_SYM_BEGIN(name) \
429 EXTERN_C void name(void); \
433#define GEN_ABS_SYM_END }
452#if defined(CONFIG_ARM)
462#define GEN_ABSOLUTE_SYM(name, value) \
463 __asm__(".globl\t" #name "\n\t.equ\t" #name \
465 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
467#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
468 __asm__(".globl\t" #name \
469 "\n\t.equ\t" #name "," #value \
470 "\n\t.type\t" #name ",%object")
472#elif defined(CONFIG_X86)
474#define GEN_ABSOLUTE_SYM(name, value) \
475 __asm__(".globl\t" #name "\n\t.equ\t" #name \
477 "\n\t.type\t" #name ",@object" : : "n"(value))
479#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
480 __asm__(".globl\t" #name \
481 "\n\t.equ\t" #name "," #value \
482 "\n\t.type\t" #name ",@object")
484#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
486#define GEN_ABSOLUTE_SYM(name, value) \
487 __asm__(".globl\t" #name "\n\t.equ\t" #name \
489 "\n\t.type\t" #name ",@object" : : "n"(value))
491#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
492 __asm__(".globl\t" #name \
493 "\n\t.equ\t" #name "," #value \
494 "\n\t.type\t" #name ",@object")
496#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
497 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
500#define GEN_ABSOLUTE_SYM(name, value) \
501 __asm__(".globl\t" #name "\n\t.equ\t" #name \
503 "\n\t.type\t" #name ",%%object" : : "n"(value))
505#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
506 __asm__(".globl\t" #name \
507 "\n\t.equ\t" #name "," #value \
508 "\n\t.type\t" #name ",%object")
510#elif defined(CONFIG_ARCH_POSIX)
511#define GEN_ABSOLUTE_SYM(name, value) \
512 __asm__(".globl\t" #name "\n\t.equ\t" #name \
514 "\n\t.type\t" #name ",@object" : : "n"(value))
516#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
517 __asm__(".globl\t" #name \
518 "\n\t.equ\t" #name "," #value \
519 "\n\t.type\t" #name ",@object")
521#elif defined(CONFIG_SPARC)
522#define GEN_ABSOLUTE_SYM(name, value) \
523 __asm__(".global\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")
533#error processor architecture not supported
536#define compiler_barrier() do { \
537 __asm__ __volatile__ ("" ::: "memory"); \
549#define Z_MAX(a, b) ({ \
551 __typeof__(a) _value_a_ = (a); \
552 __typeof__(b) _value_b_ = (b); \
553 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
561#define Z_MIN(a, b) ({ \
563 __typeof__(a) _value_a_ = (a); \
564 __typeof__(b) _value_b_ = (b); \
565 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
573#define Z_CLAMP(val, low, high) ({ \
575 __typeof__(val) _value_val_ = (val); \
576 __typeof__(low) _value_low_ = (low); \
577 __typeof__(high) _value_high_ = (high); \
578 (_value_val_ < _value_low_) ? _value_low_ : \
579 (_value_val_ > _value_high_) ? _value_high_ : \
590#define Z_POW2_CEIL(x) ((1UL << (63U - __builtin_clzl(x))) < x ? \
591 1UL << (63U - __builtin_clzl(x) + 1U) : \
592 1UL << (63U - __builtin_clzl(x)))
594#define Z_POW2_CEIL(x) ((1UL << (31U - __builtin_clz(x))) < x ? \
595 1UL << (31U - __builtin_clz(x) + 1U) : \
596 1UL << (31U - __builtin_clz(x)))
605#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
Common toolchain abstraction.