7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
24#ifndef __ORDER_BIG_ENDIAN__
25#define __ORDER_BIG_ENDIAN__ (1)
28#ifndef __ORDER_LITTLE_ENDIAN__
29#define __ORDER_LITTLE_ENDIAN__ (2)
33#if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
34 defined(__THUMBEB__) || defined(__AARCH64EB__) || \
35 defined(__MIPSEB__) || defined(__TC32EB__)
37#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
39#elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
40 defined(__THUMBEL__) || defined(__AARCH64EL__) || \
41 defined(__MIPSEL__) || defined(__TC32EL__)
43#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
46#error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
53#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
59#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
60 (__STDC_VERSION__) >= 201100
61#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
63#define BUILD_ASSERT(EXPR, MSG...)
69#define ALIAS_OF(of) __attribute__((alias(#of)))
71#define FUNC_ALIAS(real_func, new_alias, return_type) \
72 return_type new_alias() ALIAS_OF(real_func)
74#if defined(CONFIG_ARCH_POSIX)
78#define CODE_UNREACHABLE \
80 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
82 __builtin_unreachable(); \
85#define CODE_UNREACHABLE __builtin_unreachable()
87#define FUNC_NORETURN __attribute__((__noreturn__))
92#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
93#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
95#define _NODATA_SECTION(segment) \
96 __attribute__((section(#segment ",\"wa\",@nobits#")))
100#define UNALIGNED_GET(p) \
102 struct __attribute__((__packed__)) { \
103 __typeof__(*(p)) __v; \
104 } *__p = (__typeof__(__p)) (p); \
109#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
120#define UNALIGNED_PUT(v, p) \
122 struct __attribute__((__packed__)) { \
123 __typeof__(*p) __v; \
124 } *__p = (__typeof__(__p)) (p); \
126 compiler_barrier(); \
131#define UNALIGNED_PUT(v, p) \
133 struct __attribute__((__packed__)) { \
134 __typeof__(*p) __v; \
135 } *__p = (__typeof__(__p)) (p); \
144#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
145#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
147#define __GENERIC_DOT_SECTION(segment) \
148 __attribute__((section("." STRINGIFY(segment))))
149#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
151#define ___in_section(a, b, c) \
152 __attribute__((section("." Z_STRINGIFY(a) \
154 "." Z_STRINGIFY(c))))
155#define __in_section(a, b, c) ___in_section(a, b, c)
157#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
159#define __in_section_unique_named(seg, name) \
160 ___in_section(seg, __FILE__, name)
168#if !defined(CONFIG_XIP)
170#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
171#define __ramfunc __attribute__((noinline)) \
172 __attribute__((long_call, section(".ramfunc")))
177#define __fallthrough __attribute__((fallthrough))
184#define __packed __attribute__((__packed__))
187#define __aligned(x) __attribute__((__aligned__(x)))
189#define __may_alias __attribute__((__may_alias__))
191#define __printf_like(f, a) __attribute__((format (printf, f, a)))
193#define __used __attribute__((__used__))
195#define __deprecated __attribute__((deprecated))
197#ifndef __attribute_const__
198#define __attribute_const__ __attribute__((__const__))
201#define __must_check __attribute__((warn_unused_result))
203#define ARG_UNUSED(x) (void)(x)
205#define likely(x) __builtin_expect((bool)!!(x), true)
206#define unlikely(x) __builtin_expect((bool)!!(x), false)
208#define popcount(x) __builtin_popcount(x)
210#ifndef __no_optimization
211#define __no_optimization __attribute__((optimize("-O0")))
215#define __weak __attribute__((__weak__))
217#define __unused __attribute__((__unused__))
221#define HAS_BUILTIN___builtin_add_overflow 1
222#define HAS_BUILTIN___builtin_sub_overflow 1
223#define HAS_BUILTIN___builtin_mul_overflow 1
224#define HAS_BUILTIN___builtin_div_overflow 1
227#define HAS_BUILTIN___builtin_clz 1
228#define HAS_BUILTIN___builtin_clzl 1
229#define HAS_BUILTIN___builtin_clzll 1
230#define HAS_BUILTIN___builtin_ctz 1
231#define HAS_BUILTIN___builtin_ctzl 1
232#define HAS_BUILTIN___builtin_ctzll 1
247#define __WARN(msg) __WARN1(GCC warning msg)
248#define __WARN1(s) _Pragma(#s)
251#ifndef __DEPRECATED_MACRO
252#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
257#if defined(_ASMLANGUAGE)
259#if defined(CONFIG_ARM)
261#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
263#define FUNC_CODE() .thumb;
268#define FUNC_CODE() .code 32
289#if defined(_ASMLANGUAGE)
291#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
292 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64)
293#define GTEXT(sym) .global sym; .type sym, %function
294#define GDATA(sym) .global sym; .type sym, %object
295#define WTEXT(sym) .weak sym; .type sym, %function
296#define WDATA(sym) .weak sym; .type sym, %object
297#elif defined(CONFIG_ARC)
303.macro glbl_text symbol
305 .type \symbol, %function
308.macro glbl_data symbol
310 .type \symbol, %
object
313.macro weak_data symbol
315 .type \symbol, %
object
318#define GTEXT(sym) glbl_text sym
319#define GDATA(sym) glbl_data sym
320#define WDATA(sym) weak_data sym
323#define GTEXT(sym) .globl sym; .type sym, @function
324#define GDATA(sym) .globl sym; .type sym, @object
337#if defined(CONFIG_ARC)
346.macro section_var section, symbol
347 .section .\section\().\symbol
351.macro section_func section, symbol
352 .section .\section\().\symbol,
"ax"
359.macro section_subsec_func section, subsection, symbol
360 .section .\section\().\subsection, "ax"
365#define SECTION_VAR(sect, sym) section_var sect, sym
366#define SECTION_FUNC(sect, sym) section_func sect, sym
367#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
368 section_subsec_func sect, subsec, sym
371#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
372#define SECTION_FUNC(sect, sym) \
373 .section .sect.sym, "ax"; \
375 PERFOPT_ALIGN; sym : \
377#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
378 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
384#if defined(_ASMLANGUAGE)
385#if defined(CONFIG_ARM)
386#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
388#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
390#define _ASM_FILE_PROLOGUE .text; .code 32
392#elif defined(CONFIG_ARM64)
393#define _ASM_FILE_PROLOGUE .text
403#define GEN_OFFSET_EXTERN(name) extern const char name[]
405#define GEN_ABS_SYM_BEGIN(name) \
406 EXTERN_C void name(void); \
410#define GEN_ABS_SYM_END }
429#if defined(CONFIG_ARM)
439#define GEN_ABSOLUTE_SYM(name, value) \
440 __asm__(".globl\t" #name "\n\t.equ\t" #name \
442 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
444#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
445 __asm__(".globl\t" #name \
446 "\n\t.equ\t" #name "," #value \
447 "\n\t.type\t" #name ",%object")
449#elif defined(CONFIG_X86)
451#define GEN_ABSOLUTE_SYM(name, value) \
452 __asm__(".globl\t" #name "\n\t.equ\t" #name \
454 "\n\t.type\t" #name ",@object" : : "n"(value))
456#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
457 __asm__(".globl\t" #name \
458 "\n\t.equ\t" #name "," #value \
459 "\n\t.type\t" #name ",@object")
461#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
463#define GEN_ABSOLUTE_SYM(name, value) \
464 __asm__(".globl\t" #name "\n\t.equ\t" #name \
466 "\n\t.type\t" #name ",@object" : : "n"(value))
468#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
469 __asm__(".globl\t" #name \
470 "\n\t.equ\t" #name "," #value \
471 "\n\t.type\t" #name ",@object")
473#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || defined(CONFIG_XTENSA)
476#define GEN_ABSOLUTE_SYM(name, value) \
477 __asm__(".globl\t" #name "\n\t.equ\t" #name \
479 "\n\t.type\t" #name ",%%object" : : "n"(value))
481#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
482 __asm__(".globl\t" #name \
483 "\n\t.equ\t" #name "," #value \
484 "\n\t.type\t" #name ",%object")
486#elif defined(CONFIG_ARCH_POSIX)
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_SPARC)
498#define GEN_ABSOLUTE_SYM(name, value) \
499 __asm__(".global\t" #name "\n\t.equ\t" #name \
501 "\n\t.type\t" #name ",#object" : : "n"(value))
503#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
504 __asm__(".globl\t" #name \
505 "\n\t.equ\t" #name "," #value \
506 "\n\t.type\t" #name ",#object")
509#error processor architecture not supported
512#define compiler_barrier() do { \
513 __asm__ __volatile__ ("" ::: "memory"); \
525#define Z_MAX(a, b) ({ \
527 __typeof__(a) _value_a_ = (a); \
528 __typeof__(b) _value_b_ = (b); \
529 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
537#define Z_MIN(a, b) ({ \
539 __typeof__(a) _value_a_ = (a); \
540 __typeof__(b) _value_b_ = (b); \
541 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
549#define Z_CLAMP(val, low, high) ({ \
551 __typeof__(val) _value_val_ = (val); \
552 __typeof__(low) _value_low_ = (low); \
553 __typeof__(high) _value_high_ = (high); \
554 (_value_val_ < _value_low_) ? _value_low_ : \
555 (_value_val_ > _value_high_) ? _value_high_ : \
566#define Z_POW2_CEIL(x) ((1UL << (63U - __builtin_clzl(x))) < x ? \
567 1UL << (63U - __builtin_clzl(x) + 1U) : \
568 1UL << (63U - __builtin_clzl(x)))
570#define Z_POW2_CEIL(x) ((1UL << (31U - __builtin_clzl(x))) < x ? \
571 1UL << (31U - __builtin_clzl(x) + 1U) : \
572 1UL << (31U - __builtin_clzl(x)))
Common toolchain abstraction.