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"
52#if defined(__cplusplus) && (__cplusplus >= 201103L)
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...)
67#define ZRESTRICT __restrict
69#define ZRESTRICT restrict
75#define ALIAS_OF(of) __attribute__((alias(#of)))
77#define FUNC_ALIAS(real_func, new_alias, return_type) \
78 return_type new_alias() ALIAS_OF(real_func)
80#if defined(CONFIG_ARCH_POSIX)
84#define CODE_UNREACHABLE \
86 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
88 __builtin_unreachable(); \
91#define CODE_UNREACHABLE __builtin_unreachable()
93#define FUNC_NORETURN __attribute__((__noreturn__))
98#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
99#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
101#define _NODATA_SECTION(segment) \
102 __attribute__((section(#segment ",\"wa\",@nobits#")))
106#define UNALIGNED_GET(p) \
108 struct __attribute__((__packed__)) { \
109 __typeof__(*(p)) __v; \
110 } *__p = (__typeof__(__p)) (p); \
115#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
126#define UNALIGNED_PUT(v, p) \
128 struct __attribute__((__packed__)) { \
129 __typeof__(*p) __v; \
130 } *__p = (__typeof__(__p)) (p); \
132 compiler_barrier(); \
137#define UNALIGNED_PUT(v, p) \
139 struct __attribute__((__packed__)) { \
140 __typeof__(*p) __v; \
141 } *__p = (__typeof__(__p)) (p); \
150#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
151#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
153#define __GENERIC_DOT_SECTION(segment) \
154 __attribute__((section("." STRINGIFY(segment))))
155#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
157#define ___in_section(a, b, c) \
158 __attribute__((section("." Z_STRINGIFY(a) \
160 "." Z_STRINGIFY(c))))
161#define __in_section(a, b, c) ___in_section(a, b, c)
163#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
165#define __in_section_unique_named(seg, name) \
166 ___in_section(seg, __FILE__, name)
174#if !defined(CONFIG_XIP)
176#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
177#define __ramfunc __attribute__((noinline)) \
178 __attribute__((long_call, section(".ramfunc")))
183#define __fallthrough __attribute__((fallthrough))
190#define __packed __attribute__((__packed__))
193#define __aligned(x) __attribute__((__aligned__(x)))
195#define __may_alias __attribute__((__may_alias__))
197#define __printf_like(f, a) __attribute__((format (printf, f, a)))
199#define __used __attribute__((__used__))
201#define __deprecated __attribute__((deprecated))
203#ifndef __attribute_const__
204#define __attribute_const__ __attribute__((__const__))
207#define __must_check __attribute__((warn_unused_result))
209#define ARG_UNUSED(x) (void)(x)
211#define likely(x) __builtin_expect((bool)!!(x), true)
212#define unlikely(x) __builtin_expect((bool)!!(x), false)
214#define popcount(x) __builtin_popcount(x)
216#ifndef __no_optimization
217#define __no_optimization __attribute__((optimize("-O0")))
221#define __weak __attribute__((__weak__))
223#define __unused __attribute__((__unused__))
227#define HAS_BUILTIN___builtin_add_overflow 1
228#define HAS_BUILTIN___builtin_sub_overflow 1
229#define HAS_BUILTIN___builtin_mul_overflow 1
230#define HAS_BUILTIN___builtin_div_overflow 1
233#define HAS_BUILTIN___builtin_clz 1
234#define HAS_BUILTIN___builtin_clzl 1
235#define HAS_BUILTIN___builtin_clzll 1
236#define HAS_BUILTIN___builtin_ctz 1
237#define HAS_BUILTIN___builtin_ctzl 1
238#define HAS_BUILTIN___builtin_ctzll 1
253#define __WARN(msg) __WARN1(GCC warning msg)
254#define __WARN1(s) _Pragma(#s)
257#ifndef __DEPRECATED_MACRO
258#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
263#if defined(_ASMLANGUAGE)
265#if defined(CONFIG_ARM)
267#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
269#define FUNC_CODE() .thumb;
274#define FUNC_CODE() .code 32
295#if defined(_ASMLANGUAGE)
297#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
298 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
299 || defined(CONFIG_MIPS)
300#define GTEXT(sym) .global sym; .type sym, %function
301#define GDATA(sym) .global sym; .type sym, %object
302#define WTEXT(sym) .weak sym; .type sym, %function
303#define WDATA(sym) .weak sym; .type sym, %object
304#elif defined(CONFIG_ARC)
310.macro glbl_text symbol
312 .type \symbol, %
function
315.macro glbl_data symbol
317 .type \symbol, %
object
320.macro weak_data symbol
322 .type \symbol, %
object
325#define GTEXT(sym) glbl_text sym
326#define GDATA(sym) glbl_data sym
327#define WDATA(sym) weak_data sym
330#define GTEXT(sym) .globl sym; .type sym, @function
331#define GDATA(sym) .globl sym; .type sym, @object
344#if defined(CONFIG_ARC)
353.macro section_var section, symbol
354 .section .\section\().\symbol
358.macro section_func section, symbol
359 .section .\section\().\symbol,
"ax"
366.macro section_subsec_func section, subsection, symbol
367 .section .\section\().\subsection, "ax"
372#define SECTION_VAR(sect, sym) section_var sect, sym
373#define SECTION_FUNC(sect, sym) section_func sect, sym
374#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
375 section_subsec_func sect, subsec, sym
378#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
379#define SECTION_FUNC(sect, sym) \
380 .section .sect.sym, "ax"; \
382 PERFOPT_ALIGN; sym : \
384#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
385 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
391#if defined(_ASMLANGUAGE)
392#if defined(CONFIG_ARM)
393#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
395#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
397#define _ASM_FILE_PROLOGUE .text; .code 32
399#elif defined(CONFIG_ARM64)
400#define _ASM_FILE_PROLOGUE .text
410#define GEN_OFFSET_EXTERN(name) extern const char name[]
412#define GEN_ABS_SYM_BEGIN(name) \
413 EXTERN_C void name(void); \
417#define GEN_ABS_SYM_END }
436#if defined(CONFIG_ARM)
446#define GEN_ABSOLUTE_SYM(name, value) \
447 __asm__(".globl\t" #name "\n\t.equ\t" #name \
449 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
451#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
452 __asm__(".globl\t" #name \
453 "\n\t.equ\t" #name "," #value \
454 "\n\t.type\t" #name ",%object")
456#elif defined(CONFIG_X86)
458#define GEN_ABSOLUTE_SYM(name, value) \
459 __asm__(".globl\t" #name "\n\t.equ\t" #name \
461 "\n\t.type\t" #name ",@object" : : "n"(value))
463#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
464 __asm__(".globl\t" #name \
465 "\n\t.equ\t" #name "," #value \
466 "\n\t.type\t" #name ",@object")
468#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
470#define GEN_ABSOLUTE_SYM(name, value) \
471 __asm__(".globl\t" #name "\n\t.equ\t" #name \
473 "\n\t.type\t" #name ",@object" : : "n"(value))
475#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
476 __asm__(".globl\t" #name \
477 "\n\t.equ\t" #name "," #value \
478 "\n\t.type\t" #name ",@object")
480#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
481 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
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_ARCH_POSIX)
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_SPARC)
506#define GEN_ABSOLUTE_SYM(name, value) \
507 __asm__(".global\t" #name "\n\t.equ\t" #name \
509 "\n\t.type\t" #name ",#object" : : "n"(value))
511#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
512 __asm__(".globl\t" #name \
513 "\n\t.equ\t" #name "," #value \
514 "\n\t.type\t" #name ",#object")
517#error processor architecture not supported
520#define compiler_barrier() do { \
521 __asm__ __volatile__ ("" ::: "memory"); \
533#define Z_MAX(a, b) ({ \
535 __typeof__(a) _value_a_ = (a); \
536 __typeof__(b) _value_b_ = (b); \
537 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
545#define Z_MIN(a, b) ({ \
547 __typeof__(a) _value_a_ = (a); \
548 __typeof__(b) _value_b_ = (b); \
549 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
557#define Z_CLAMP(val, low, high) ({ \
559 __typeof__(val) _value_val_ = (val); \
560 __typeof__(low) _value_low_ = (low); \
561 __typeof__(high) _value_high_ = (high); \
562 (_value_val_ < _value_low_) ? _value_low_ : \
563 (_value_val_ > _value_high_) ? _value_high_ : \
574#define Z_POW2_CEIL(x) ((1UL << (63U - __builtin_clzl(x))) < x ? \
575 1UL << (63U - __builtin_clzl(x) + 1U) : \
576 1UL << (63U - __builtin_clzl(x)))
578#define Z_POW2_CEIL(x) ((1UL << (31U - __builtin_clzl(x))) < x ? \
579 1UL << (31U - __builtin_clzl(x) + 1U) : \
580 1UL << (31U - __builtin_clzl(x)))
Common toolchain abstraction.