123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- #ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
- #define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
- #ifndef _LINKER
- #ifndef __ORDER_BIG_ENDIAN__
- #define __ORDER_BIG_ENDIAN__ (1)
- #endif
- #ifndef __ORDER_LITTLE_ENDIAN__
- #define __ORDER_LITTLE_ENDIAN__ (2)
- #endif
- #ifndef __BYTE_ORDER__
- #if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
- defined(__THUMBEB__) || defined(__AARCH64EB__) || \
- defined(__MIPSEB__) || defined(__TC32EB__)
- #define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
- #elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
- defined(__THUMBEL__) || defined(__AARCH64EL__) || \
- defined(__MIPSEL__) || defined(__TC32EL__)
- #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
- #else
- #error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
- #endif
- #endif
- #ifdef __cplusplus
- #define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
- #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
- (__STDC_VERSION__) >= 201100
- #define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
- #else
- #define BUILD_ASSERT(EXPR, MSG...)
- #endif
- #include <toolchain/common.h>
- #include <stdbool.h>
- #define ALIAS_OF(of) __attribute__((alias(#of)))
- #define FUNC_ALIAS(real_func, new_alias, return_type) \
- return_type new_alias() ALIAS_OF(real_func)
- #if defined(CONFIG_ARCH_POSIX)
- #include <arch/posix/posix_trace.h>
- #define CODE_UNREACHABLE \
- {\
- posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
- __FILE__, __LINE__)
- __builtin_unreachable()
- }
- #else
- #define CODE_UNREACHABLE __builtin_unreachable()
- #endif
- #define FUNC_NORETURN __attribute__((__noreturn__))
- #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
- #define _NODATA_SECTION(segment) __attribute__((section(#segment)))
- #else
- #define _NODATA_SECTION(segment) \
- __attribute__((section(#segment ",\"wa\",@nobits#")))
- #endif
- #define UNALIGNED_GET(p) \
- __extension__ ({ \
- struct __attribute__((__packed__)) { \
- __typeof__(*(p)) __v
- } *__p = (__typeof__(__p)) (p)
- __p->__v
- })
- #if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
- #define UNALIGNED_PUT(v, p) \
- do { \
- struct __attribute__((__packed__)) { \
- __typeof__(*p) __v
- } *__p = (__typeof__(__p)) (p)
- __p->__v = (v)
- compiler_barrier()
- } while (false)
- #else
- #define UNALIGNED_PUT(v, p) \
- do { \
- struct __attribute__((__packed__)) { \
- __typeof__(*p) __v
- } *__p = (__typeof__(__p)) (p)
- __p->__v = (v)
- } while (false)
- #endif
- #define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
- #define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
- #define __GENERIC_DOT_SECTION(segment) \
- __attribute__((section("." STRINGIFY(segment))))
- #define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
- #define ___in_section(a, b, c) \
- __attribute__((section("." Z_STRINGIFY(a) \
- "." Z_STRINGIFY(b) \
- "." Z_STRINGIFY(c))))
- #define __in_section(a, b, c) ___in_section(a, b, c)
- #define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
- #define __in_section_unique_named(seg, name) \
- ___in_section(seg, __FILE__, name)
- #if !defined(CONFIG_XIP)
- #define __ramfunc
- #elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
- #define __ramfunc __attribute__((noinline)) \
- __attribute__((long_call, section(".ramfunc")))
- #endif
- #ifndef __fallthrough
- #if __GNUC__ >= 7
- #define __fallthrough __attribute__((fallthrough))
- #else
- #define __fallthrough
- #endif
- #endif
- #ifndef __packed
- #define __packed __attribute__((__packed__))
- #endif
- #ifndef __aligned
- #define __aligned(x) __attribute__((__aligned__(x)))
- #endif
- #define __may_alias __attribute__((__may_alias__))
- #ifndef __printf_like
- #define __printf_like(f, a) __attribute__((format (printf, f, a)))
- #endif
- #define __used __attribute__((__used__))
- #ifndef __deprecated
- #define __deprecated __attribute__((deprecated))
- #endif
- #ifndef __attribute_const__
- #define __attribute_const__ __attribute__((__const__))
- #endif
- #ifndef __must_check
- #define __must_check __attribute__((warn_unused_result))
- #endif
- #define ARG_UNUSED(x) (void)(x)
- #define likely(x) __builtin_expect((bool)!!(x), true)
- #define unlikely(x) __builtin_expect((bool)!!(x), false)
- #define popcount(x) __builtin_popcount(x)
- #ifndef __no_optimization
- #define __no_optimization __attribute__((optimize("-O0")))
- #endif
- #ifndef __weak
- #define __weak __attribute__((__weak__))
- #endif
- #define __unused __attribute__((__unused__))
- #if __GNUC__ >= 5
- #define HAS_BUILTIN___builtin_add_overflow 1
- #define HAS_BUILTIN___builtin_sub_overflow 1
- #define HAS_BUILTIN___builtin_mul_overflow 1
- #define HAS_BUILTIN___builtin_div_overflow 1
- #endif
- #if __GNUC__ >= 4
- #define HAS_BUILTIN___builtin_clz 1
- #define HAS_BUILTIN___builtin_clzl 1
- #define HAS_BUILTIN___builtin_clzll 1
- #define HAS_BUILTIN___builtin_ctz 1
- #define HAS_BUILTIN___builtin_ctzl 1
- #define HAS_BUILTIN___builtin_ctzll 1
- #endif
- #define __WARN(msg) __WARN1(GCC warning msg)
- #define __WARN1(s) _Pragma(#s)
- #ifndef __DEPRECATED_MACRO
- #define __DEPRECATED_MACRO __WARN("Macro is deprecated")
- #endif
- #if defined(_ASMLANGUAGE)
- #if defined(CONFIG_ARM)
- #if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
- #define FUNC_CODE() .thumb;
- #define FUNC_INSTR(a)
- #else
- #define FUNC_CODE() .code 32
- #define FUNC_INSTR(a)
- #endif
- #else
- #define FUNC_CODE()
- #define FUNC_INSTR(a)
- #endif
- #endif
- #if defined(_ASMLANGUAGE)
- #if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
- || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64)
- #define GTEXT(sym) .global sym; .type sym, %function
- #define GDATA(sym) .global sym; .type sym, %object
- #define WTEXT(sym) .weak sym; .type sym, %function
- #define WDATA(sym) .weak sym; .type sym, %object
- #elif defined(CONFIG_ARC)
- .macro glbl_text symbol
- .globl \symbol
- .type \symbol, %function
- .endm
- .macro glbl_data symbol
- .globl \symbol
- .type \symbol, %object
- .endm
- .macro weak_data symbol
- .weak \symbol
- .type \symbol, %object
- .endm
- #define GTEXT(sym) glbl_text sym
- #define GDATA(sym) glbl_data sym
- #define WDATA(sym) weak_data sym
- #else
- #define GTEXT(sym) .globl sym; .type sym, @function
- #define GDATA(sym) .globl sym; .type sym, @object
- #endif
- #if defined(CONFIG_ARC)
- .macro section_var section, symbol
- .section .\section\().\symbol
- \symbol :
- .endm
- .macro section_func section, symbol
- .section .\section\().\symbol, "ax"
- FUNC_CODE()
- PERFOPT_ALIGN
- \symbol :
- FUNC_INSTR(\symbol)
- .endm
- .macro section_subsec_func section, subsection, symbol
- .section .\section\().\subsection, "ax"
- PERFOPT_ALIGN
- \symbol :
- .endm
- #define SECTION_VAR(sect, sym) section_var sect, sym
- #define SECTION_FUNC(sect, sym) section_func sect, sym
- #define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
- section_subsec_func sect, subsec, sym
- #else
- #define SECTION_VAR(sect, sym) .section .sect.sym; sym:
- #define SECTION_FUNC(sect, sym) \
- .section .sect.sym, "ax"
- FUNC_CODE() \
- PERFOPT_ALIGN
- FUNC_INSTR(sym)
- #define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
- .section .sect.subsec, "ax"
- #endif
- #endif
- #if defined(_ASMLANGUAGE)
- #if defined(CONFIG_ARM)
- #if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
- #define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
- #else
- #define _ASM_FILE_PROLOGUE .text; .code 32
- #endif
- #elif defined(CONFIG_ARM64)
- #define _ASM_FILE_PROLOGUE .text
- #endif
- #endif
- #define GEN_OFFSET_EXTERN(name) extern const char name[]
- #define GEN_ABS_SYM_BEGIN(name) \
- EXTERN_C void name(void)
- void name(void) \
- {
- #define GEN_ABS_SYM_END }
- #if defined(CONFIG_ARM)
- #define GEN_ABSOLUTE_SYM(name, value) \
- __asm__(".globl\t" #name "\n\t.equ\t" #name \
- ",%B0" \
- "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
- #define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
- __asm__(".globl\t" #name \
- "\n\t.equ\t" #name "," #value \
- "\n\t.type\t" #name ",%object")
- #elif defined(CONFIG_X86)
- #define GEN_ABSOLUTE_SYM(name, value) \
- __asm__(".globl\t" #name "\n\t.equ\t" #name \
- ",%c0" \
- "\n\t.type\t" #name ",@object" : : "n"(value))
- #define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
- __asm__(".globl\t" #name \
- "\n\t.equ\t" #name "," #value \
- "\n\t.type\t" #name ",@object")
- #elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
- #define GEN_ABSOLUTE_SYM(name, value) \
- __asm__(".globl\t" #name "\n\t.equ\t" #name \
- ",%c0" \
- "\n\t.type\t" #name ",@object" : : "n"(value))
- #define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
- __asm__(".globl\t" #name \
- "\n\t.equ\t" #name "," #value \
- "\n\t.type\t" #name ",@object")
- #elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || defined(CONFIG_XTENSA)
- #define GEN_ABSOLUTE_SYM(name, value) \
- __asm__(".globl\t" #name "\n\t.equ\t" #name \
- ",%0" \
- "\n\t.type\t" #name ",%%object" : : "n"(value))
- #define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
- __asm__(".globl\t" #name \
- "\n\t.equ\t" #name "," #value \
- "\n\t.type\t" #name ",%object")
- #elif defined(CONFIG_ARCH_POSIX)
- #define GEN_ABSOLUTE_SYM(name, value) \
- __asm__(".globl\t" #name "\n\t.equ\t" #name \
- ",%c0" \
- "\n\t.type\t" #name ",@object" : : "n"(value))
- #define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
- __asm__(".globl\t" #name \
- "\n\t.equ\t" #name "," #value \
- "\n\t.type\t" #name ",@object")
- #elif defined(CONFIG_SPARC)
- #define GEN_ABSOLUTE_SYM(name, value) \
- __asm__(".global\t" #name "\n\t.equ\t" #name \
- ",%0" \
- "\n\t.type\t" #name ",#object" : : "n"(value))
- #define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
- __asm__(".globl\t" #name \
- "\n\t.equ\t" #name "," #value \
- "\n\t.type\t" #name ",#object")
- #else
- #error processor architecture not supported
- #endif
- #define compiler_barrier() do { \
- __asm__ __volatile__ ("" ::: "memory")
- } while (false)
- #define Z_MAX(a, b) ({ \
- \
- __typeof__(a) _value_a_ = (a)
- __typeof__(b) _value_b_ = (b)
- _value_a_ > _value_b_ ? _value_a_ : _value_b_
- })
- #define Z_MIN(a, b) ({ \
- \
- __typeof__(a) _value_a_ = (a)
- __typeof__(b) _value_b_ = (b)
- _value_a_ < _value_b_ ? _value_a_ : _value_b_
- })
- #define Z_CLAMP(val, low, high) ({ \
- \
- __typeof__(val) _value_val_ = (val)
- __typeof__(low) _value_low_ = (low)
- __typeof__(high) _value_high_ = (high)
- (_value_val_ < _value_low_) ? _value_low_ : \
- (_value_val_ > _value_high_) ? _value_high_ : \
- _value_val_
- })
- #ifdef CONFIG_64BIT
- #define Z_POW2_CEIL(x) ((1UL << (63U - __builtin_clzl(x))) < x ? \
- 1UL << (63U - __builtin_clzl(x) + 1U) : \
- 1UL << (63U - __builtin_clzl(x)))
- #else
- #define Z_POW2_CEIL(x) ((1UL << (31U - __builtin_clzl(x))) < x ? \
- 1UL << (31U - __builtin_clzl(x) + 1U) : \
- 1UL << (31U - __builtin_clzl(x)))
- #endif
- #endif
- #endif
|