Line data Source code
1 1 : /* 2 : * Copyright (c) 2010-2014 Wind River Systems, Inc. 3 : * 4 : * SPDX-License-Identifier: Apache-2.0 5 : */ 6 : 7 : #ifndef ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ 8 : #define ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ 9 : 10 : #ifndef ZEPHYR_INCLUDE_TOOLCHAIN_H_ 11 : #error Please do not include toolchain-specific headers directly, use <zephyr/toolchain.h> instead 12 : #endif 13 : 14 : /** 15 : * @file 16 : * @brief Common toolchain abstraction 17 : * 18 : * Macros to abstract compiler capabilities (common to all toolchains). 19 : */ 20 : 21 : /* Abstract use of extern keyword for compatibility between C and C++ */ 22 : #ifdef __cplusplus 23 : #define EXTERN_C extern "C" 24 : #else 25 0 : #define EXTERN_C extern 26 : #endif 27 : 28 : /* Use TASK_ENTRY_CPP to tag task entry points defined in C++ files. */ 29 : 30 : #ifdef __cplusplus 31 : #define TASK_ENTRY_CPP extern "C" 32 : #endif 33 : 34 : #ifndef ZRESTRICT 35 : #ifndef __cplusplus 36 0 : #define ZRESTRICT restrict 37 : #else 38 : #define ZRESTRICT 39 : #endif 40 : #endif 41 : 42 : /* 43 : * Generate a reference to an external symbol. 44 : * The reference indicates to the linker that the symbol is required 45 : * by the module containing the reference and should be included 46 : * in the image if the module is in the image. 47 : * 48 : * The assembler directive ".set" is used to define a local symbol. 49 : * No memory is allocated, and the local symbol does not appear in 50 : * the symbol table. 51 : */ 52 : 53 : #ifdef _ASMLANGUAGE 54 : #define REQUIRES(sym) .set sym ## _Requires, sym 55 : #else 56 0 : #define REQUIRES(sym) __asm__ (".set " # sym "_Requires, " # sym "\n\t"); 57 : #endif 58 : 59 : #ifdef _ASMLANGUAGE 60 : #define SECTION .section 61 : #endif 62 : 63 : /* 64 : * If the project is being built for speed (i.e. not for minimum size) then 65 : * align functions and branches in executable sections to improve performance. 66 : */ 67 : 68 : #ifdef _ASMLANGUAGE 69 : 70 : #if defined(CONFIG_X86) 71 : 72 : #ifdef PERF_OPT 73 : #define PERFOPT_ALIGN .balign 16 74 : #else 75 : #define PERFOPT_ALIGN .balign 1 76 : #endif 77 : 78 : #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64) 79 : 80 : #define PERFOPT_ALIGN .balign 4 81 : 82 : #elif defined(CONFIG_ARC) 83 : 84 : /* .align assembler directive is supposed by all ARC toolchains and it is 85 : * implemented in a same way across ARC toolchains. 86 : */ 87 : #define PERFOPT_ALIGN .align 4 88 : 89 : #elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \ 90 : defined(CONFIG_XTENSA) || defined(CONFIG_MIPS) 91 : #define PERFOPT_ALIGN .balign 4 92 : 93 : #elif defined(CONFIG_ARCH_POSIX) 94 : 95 : #elif defined(CONFIG_SPARC) 96 : 97 : #define PERFOPT_ALIGN .align 4 98 : 99 : #else 100 : 101 : #error Architecture unsupported 102 : 103 : #endif 104 : 105 : #define GC_SECTION(sym) SECTION .text.##sym, "ax" 106 : 107 : #endif /* _ASMLANGUAGE */ 108 : 109 : /* force inlining a function */ 110 : 111 : #if !defined(_ASMLANGUAGE) 112 : #ifdef CONFIG_COVERAGE 113 : /* 114 : * The always_inline attribute forces a function to be inlined, 115 : * even ignoring -fno-inline. So for code coverage, do not 116 : * force inlining of these functions to keep their bodies around 117 : * so their number of executions can be counted. 118 : * 119 : * Note that "inline" is kept here for kobject_hash.c and 120 : * priv_stacks_hash.c. These are built without compiler flags 121 : * used for coverage. ALWAYS_INLINE cannot be empty as compiler 122 : * would complain about unused functions. Attaching unused 123 : * attribute would result in their text sections balloon more than 124 : * 10 times in size, as those functions are kept in text section. 125 : * So just keep "inline" here. 126 : */ 127 : #define ALWAYS_INLINE inline 128 : #else 129 0 : #define ALWAYS_INLINE inline __attribute__((always_inline)) 130 : #endif 131 : #endif 132 : 133 : #define Z_STRINGIFY(x) #x 134 0 : #define STRINGIFY(s) Z_STRINGIFY(s) 135 : 136 : /* concatenate the values of the arguments into one */ 137 : #define _DO_CONCAT(x, y) x ## y 138 : #define _CONCAT(x, y) _DO_CONCAT(x, y) 139 : 140 : /* Additionally used as a sentinel by gen_syscalls.py to identify what 141 : * functions are system calls 142 : * 143 : * Note POSIX unit tests don't still generate the system call stubs, so 144 : * until https://github.com/zephyrproject-rtos/zephyr/issues/5006 is 145 : * fixed via possibly #4174, we introduce this hack -- which will 146 : * disallow us to test system calls in POSIX unit testing (currently 147 : * not used). 148 : */ 149 : #ifndef ZTEST_UNITTEST 150 : #define __syscall static inline 151 : #define __syscall_always_inline static inline __attribute__((always_inline)) 152 : #else 153 : #define __syscall 154 : #define __syscall_always_inline 155 : #endif /* ZTEST_UNITTEST */ 156 : 157 : /* Definitions for struct declaration tags. These are sentinel values used by 158 : * parse_syscalls.py to gather a list of names of struct declarations that 159 : * have these tags applied for them. 160 : */ 161 : 162 : /* Indicates this is a driver subsystem */ 163 : #define __subsystem 164 : 165 : /* Indicates this is a network socket object */ 166 : #define __net_socket 167 : 168 : #ifndef BUILD_ASSERT 169 : /* Compile-time assertion that makes the build to fail. 170 : * Common implementation swallows the message. 171 : */ 172 : #define BUILD_ASSERT(EXPR, MSG...) \ 173 : enum _CONCAT(__build_assert_enum, __COUNTER__) { \ 174 : _CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \ 175 : } 176 : #endif 177 : 178 : /* 179 : * This is meant to be used in conjunction with __in_section() and similar 180 : * where scattered structure instances are concatenated together by the linker 181 : * and walked by the code at run time just like a contiguous array of such 182 : * structures. 183 : * 184 : * Assemblers and linkers may insert alignment padding by default whose 185 : * size is larger than the natural alignment for those structures when 186 : * gathering various section segments together, messing up the array walk. 187 : * To prevent this, we need to provide an explicit alignment not to rely 188 : * on the default that might just work by luck. 189 : * 190 : * Alignment statements in linker scripts are not sufficient as 191 : * the assembler may add padding by itself to each segment when switching 192 : * between sections within the same file even if it merges many such segments 193 : * into a single section in the end. 194 : */ 195 : #define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type 196 : 197 : /* Check if a pointer is aligned for against a specific byte boundary */ 198 0 : #define IS_PTR_ALIGNED_BYTES(ptr, bytes) ((((uintptr_t)ptr) % bytes) == 0) 199 : 200 : /* Check if a pointer is aligned enough for a particular data type. */ 201 0 : #define IS_PTR_ALIGNED(ptr, type) IS_PTR_ALIGNED_BYTES(ptr, __alignof(type)) 202 : 203 : /** @brief Tag a symbol (e.g. function) to be kept in the binary even though it is not used. 204 : * 205 : * It prevents symbol from being removed by the linker garbage collector. It 206 : * is achieved by adding a pointer to that symbol to the kept memory section. 207 : * 208 : * @param symbol Symbol to keep. 209 : */ 210 1 : #define LINKER_KEEP(symbol) \ 211 : static const void * const symbol##_ptr __used \ 212 : __attribute__((__section__(".symbol_to_keep"))) = (void *)&symbol 213 : 214 : #endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */