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)
78#elif !defined(__cplusplus) && \
79 ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
80 (__STDC_VERSION__) >= 201100)
81#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
83#define BUILD_ASSERT(EXPR, MSG...)
87#define ZRESTRICT __restrict
89#define ZRESTRICT restrict
95#define ALIAS_OF(of) __attribute__((alias(#of)))
97#define FUNC_ALIAS(real_func, new_alias, return_type) \
98 return_type new_alias() ALIAS_OF(real_func)
100#if defined(CONFIG_ARCH_POSIX)
104#define CODE_UNREACHABLE \
106 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
107 __FILE__, __LINE__);\
108 __builtin_unreachable(); \
111#define CODE_UNREACHABLE __builtin_unreachable()
113#define FUNC_NORETURN __attribute__((__noreturn__))
118#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
119#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
121#define _NODATA_SECTION(segment) \
122 __attribute__((section(#segment ",\"wa\",@nobits#")))
126#define UNALIGNED_GET(p) \
128 struct __attribute__((__packed__)) { \
129 __typeof__(*(p)) __v; \
130 } *__p = (__typeof__(__p)) (p); \
135#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
146#define UNALIGNED_PUT(v, p) \
148 struct __attribute__((__packed__)) { \
149 __typeof__(*p) __v; \
150 } *__p = (__typeof__(__p)) (p); \
152 compiler_barrier(); \
157#define UNALIGNED_PUT(v, p) \
159 struct __attribute__((__packed__)) { \
160 __typeof__(*p) __v; \
161 } *__p = (__typeof__(__p)) (p); \
170#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
171#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
173#define __GENERIC_DOT_SECTION(segment) \
174 __attribute__((section("." STRINGIFY(segment))))
175#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
177#define ___in_section(a, b, c) \
178 __attribute__((section("." Z_STRINGIFY(a) \
180 "." Z_STRINGIFY(c))))
181#define __in_section(a, b, c) ___in_section(a, b, c)
183#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
185#define __in_section_unique_named(seg, name) \
186 ___in_section(seg, __FILE__, name)
194#if !defined(CONFIG_XIP)
196#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
197#define __ramfunc __attribute__((noinline)) \
198 __attribute__((long_call, section(".ramfunc")))
203#define __fallthrough __attribute__((fallthrough))
210#define __packed __attribute__((__packed__))
214#define __aligned(x) __attribute__((__aligned__(x)))
217#define __may_alias __attribute__((__may_alias__))
220#ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
221#define __printf_like(f, a) __attribute__((format (printf, f, a)))
232#define __printf_like(f, a)
236#define __used __attribute__((__used__))
237#define __unused __attribute__((__unused__))
238#define __maybe_unused __attribute__((__unused__))
241#define __deprecated __attribute__((deprecated))
244#ifndef __attribute_const__
245#define __attribute_const__ __attribute__((__const__))
249#define __must_check __attribute__((warn_unused_result))
252#define ARG_UNUSED(x) (void)(x)
254#define likely(x) (__builtin_expect((bool)!!(x), true) != 0L)
255#define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
256#define POPCOUNT(x) __builtin_popcount(x)
258#ifndef __no_optimization
259#define __no_optimization __attribute__((optimize("-O0")))
263#define __weak __attribute__((__weak__))
268#define HAS_BUILTIN___builtin_add_overflow 1
269#define HAS_BUILTIN___builtin_sub_overflow 1
270#define HAS_BUILTIN___builtin_mul_overflow 1
271#define HAS_BUILTIN___builtin_div_overflow 1
274#define HAS_BUILTIN___builtin_clz 1
275#define HAS_BUILTIN___builtin_clzl 1
276#define HAS_BUILTIN___builtin_clzll 1
277#define HAS_BUILTIN___builtin_ctz 1
278#define HAS_BUILTIN___builtin_ctzl 1
279#define HAS_BUILTIN___builtin_ctzll 1
294#define __WARN(msg) __WARN1(GCC warning msg)
295#define __WARN1(s) _Pragma(#s)
298#ifndef __DEPRECATED_MACRO
299#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
304#if defined(_ASMLANGUAGE)
306#if defined(CONFIG_ARM)
308#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
310#define FUNC_CODE() .thumb;
315#define FUNC_CODE() .code 32;
336#if defined(_ASMLANGUAGE)
338#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
339 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
340 || defined(CONFIG_MIPS)
341#define GTEXT(sym) .global sym; .type sym, %function
342#define GDATA(sym) .global sym; .type sym, %object
343#define WTEXT(sym) .weak sym; .type sym, %function
344#define WDATA(sym) .weak sym; .type sym, %object
345#elif defined(CONFIG_ARC)
351.macro glbl_text symbol
353 .type \symbol, %
function
356.macro glbl_data symbol
358 .type \symbol, %
object
361.macro weak_data symbol
363 .type \symbol, %
object
366#define GTEXT(sym) glbl_text sym
367#define GDATA(sym) glbl_data sym
368#define WDATA(sym) weak_data sym
371#define GTEXT(sym) .globl sym; .type sym, @function
372#define GDATA(sym) .globl sym; .type sym, @object
385#if defined(CONFIG_ARC)
394.macro section_var section, symbol
395 .section .\section\().\symbol
399.macro section_func section, symbol
400 .section .\section\().\symbol,
"ax"
407.macro section_subsec_func section, subsection, symbol
408 .section .\section\().\subsection, "ax"
413#define SECTION_VAR(sect, sym) section_var sect, sym
414#define SECTION_FUNC(sect, sym) section_func sect, sym
415#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
416 section_subsec_func sect, subsec, sym
419#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
420#define SECTION_FUNC(sect, sym) \
421 .section .sect.sym, "ax"; \
423 PERFOPT_ALIGN; sym : \
425#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
426 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
432#if defined(_ASMLANGUAGE)
433#if defined(CONFIG_ARM)
434#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
436#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
438#define _ASM_FILE_PROLOGUE .text; .code 32
440#elif defined(CONFIG_ARM64)
441#define _ASM_FILE_PROLOGUE .text
451#define GEN_OFFSET_EXTERN(name) extern const char name[]
453#define GEN_ABS_SYM_BEGIN(name) \
454 EXTERN_C void name(void); \
458#define GEN_ABS_SYM_END }
477#if defined(CONFIG_ARM)
487#define GEN_ABSOLUTE_SYM(name, value) \
488 __asm__(".globl\t" #name "\n\t.equ\t" #name \
490 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
492#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
493 __asm__(".globl\t" #name \
494 "\n\t.equ\t" #name "," #value \
495 "\n\t.type\t" #name ",%object")
497#elif defined(CONFIG_X86)
499#define GEN_ABSOLUTE_SYM(name, value) \
500 __asm__(".globl\t" #name "\n\t.equ\t" #name \
502 "\n\t.type\t" #name ",@object" : : "n"(value))
504#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
505 __asm__(".globl\t" #name \
506 "\n\t.equ\t" #name "," #value \
507 "\n\t.type\t" #name ",@object")
509#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
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_NIOS2) || defined(CONFIG_RISCV) || \
522 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
525#define GEN_ABSOLUTE_SYM(name, value) \
526 __asm__(".globl\t" #name "\n\t.equ\t" #name \
528 "\n\t.type\t" #name ",%%object" : : "n"(value))
530#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
531 __asm__(".globl\t" #name \
532 "\n\t.equ\t" #name "," #value \
533 "\n\t.type\t" #name ",%object")
535#elif defined(CONFIG_ARCH_POSIX)
536#define GEN_ABSOLUTE_SYM(name, value) \
537 __asm__(".globl\t" #name "\n\t.equ\t" #name \
539 "\n\t.type\t" #name ",@object" : : "n"(value))
541#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
542 __asm__(".globl\t" #name \
543 "\n\t.equ\t" #name "," #value \
544 "\n\t.type\t" #name ",@object")
546#elif defined(CONFIG_SPARC)
547#define GEN_ABSOLUTE_SYM(name, value) \
548 __asm__(".global\t" #name "\n\t.equ\t" #name \
550 "\n\t.type\t" #name ",#object" : : "n"(value))
552#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
553 __asm__(".globl\t" #name \
554 "\n\t.equ\t" #name "," #value \
555 "\n\t.type\t" #name ",#object")
558#error processor architecture not supported
561#define compiler_barrier() do { \
562 __asm__ __volatile__ ("" ::: "memory"); \
574#define Z_MAX(a, b) ({ \
576 __typeof__(a) _value_a_ = (a); \
577 __typeof__(b) _value_b_ = (b); \
578 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
586#define Z_MIN(a, b) ({ \
588 __typeof__(a) _value_a_ = (a); \
589 __typeof__(b) _value_b_ = (b); \
590 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
598#define Z_CLAMP(val, low, high) ({ \
600 __typeof__(val) _value_val_ = (val); \
601 __typeof__(low) _value_low_ = (low); \
602 __typeof__(high) _value_high_ = (high); \
603 (_value_val_ < _value_low_) ? _value_low_ : \
604 (_value_val_ > _value_high_) ? _value_high_ : \
614#define Z_POW2_CEIL(x) \
615 ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
623#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
625#if defined(CONFIG_ASAN) && defined(__clang__)
626#define __noasan __attribute__((no_sanitize("address")))
636#if (TOOLCHAIN_GCC_VERSION >= 110000) || (TOOLCHAIN_CLANG_VERSION >= 70000)
637#define FUNC_NO_STACK_PROTECTOR __attribute__((no_stack_protector))
639#define FUNC_NO_STACK_PROTECTOR
Common toolchain abstraction.