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 * @file 11 * @brief Common toolchain abstraction 12 * 13 * Macros to abstract compiler capabilities (common to all toolchains). 14 */ 15 16 /* Abstract use of extern keyword for compatibility between C and C++ */ 17 #ifdef __cplusplus 18 #define EXTERN_C extern "C" 19 #else 20 #define EXTERN_C extern 21 #endif 22 23 /* Use TASK_ENTRY_CPP to tag task entry points defined in C++ files. */ 24 25 #ifdef __cplusplus 26 #define TASK_ENTRY_CPP extern "C" 27 #endif 28 29 /* 30 * Generate a reference to an external symbol. 31 * The reference indicates to the linker that the symbol is required 32 * by the module containing the reference and should be included 33 * in the image if the module is in the image. 34 * 35 * The assembler directive ".set" is used to define a local symbol. 36 * No memory is allocated, and the local symbol does not appear in 37 * the symbol table. 38 */ 39 40 #ifdef _ASMLANGUAGE 41 #define REQUIRES(sym) .set sym ## _Requires, sym 42 #else 43 #define REQUIRES(sym) __asm__ (".set " # sym "_Requires, " # sym "\n\t"); 44 #endif 45 46 #ifdef _ASMLANGUAGE 47 #define SECTION .section 48 #endif 49 50 /* 51 * If the project is being built for speed (i.e. not for minimum size) then 52 * align functions and branches in executable sections to improve performance. 53 */ 54 55 #ifdef _ASMLANGUAGE 56 57 #if defined(CONFIG_X86) 58 59 #ifdef PERF_OPT 60 #define PERFOPT_ALIGN .balign 16 61 #else 62 #define PERFOPT_ALIGN .balign 1 63 #endif 64 65 #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64) 66 67 #define PERFOPT_ALIGN .balign 4 68 69 #elif defined(CONFIG_ARC) 70 71 /* .align assembler directive is supposed by all ARC toolchains and it is 72 * implemented in a same way across ARC toolchains. 73 */ 74 #define PERFOPT_ALIGN .align 4 75 76 #elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \ 77 defined(CONFIG_XTENSA) 78 #define PERFOPT_ALIGN .balign 4 79 80 #elif defined(CONFIG_ARCH_POSIX) 81 82 #elif defined(CONFIG_SPARC) 83 84 #define PERFOPT_ALIGN .align 4 85 86 #else 87 88 #error Architecture unsupported 89 90 #endif 91 92 #define GC_SECTION(sym) SECTION .text.##sym, "ax" 93 94 #endif /* _ASMLANGUAGE */ 95 96 /* force inlining a function */ 97 98 #if !defined(_ASMLANGUAGE) 99 #ifdef CONFIG_COVERAGE 100 /* 101 * The always_inline attribute forces a function to be inlined, 102 * even ignoring -fno-inline. So for code coverage, do not 103 * force inlining of these functions to keep their bodies around 104 * so their number of executions can be counted. 105 * 106 * Note that "inline" is kept here for kobject_hash.c and 107 * priv_stacks_hash.c. These are built without compiler flags 108 * used for coverage. ALWAYS_INLINE cannot be empty as compiler 109 * would complain about unused functions. Attaching unused 110 * attribute would result in their text sections balloon more than 111 * 10 times in size, as those functions are kept in text section. 112 * So just keep "inline" here. 113 */ 114 #define ALWAYS_INLINE inline 115 #else 116 #define ALWAYS_INLINE inline __attribute__((always_inline)) 117 #endif 118 #endif 119 120 #define Z_STRINGIFY(x) #x 121 #define STRINGIFY(s) Z_STRINGIFY(s) 122 123 /* concatenate the values of the arguments into one */ 124 #define _DO_CONCAT(x, y) x ## y 125 #define _CONCAT(x, y) _DO_CONCAT(x, y) 126 127 /* Additionally used as a sentinel by gen_syscalls.py to identify what 128 * functions are system calls 129 * 130 * Note POSIX unit tests don't still generate the system call stubs, so 131 * until https://github.com/zephyrproject-rtos/zephyr/issues/5006 is 132 * fixed via possibly #4174, we introduce this hack -- which will 133 * disallow us to test system calls in POSIX unit testing (currently 134 * not used). 135 */ 136 #ifndef ZTEST_UNITTEST 137 #define __syscall static inline 138 #else 139 #define __syscall 140 #endif /* ZTEST_UNITTEST */ 141 142 /* Definitions for struct declaration tags. These are sentinel values used by 143 * parse_syscalls.py to gather a list of names of struct declarations that 144 * have these tags applied for them. 145 */ 146 147 /* Indicates this is a driver subsystem */ 148 #define __subsystem 149 150 /* Indicates this is a network socket object */ 151 #define __net_socket 152 153 #ifndef BUILD_ASSERT 154 /* Compile-time assertion that makes the build to fail. 155 * Common implementation swallows the message. 156 */ 157 #define BUILD_ASSERT(EXPR, MSG...) \ 158 enum _CONCAT(__build_assert_enum, __COUNTER__) { \ 159 _CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \ 160 } 161 #endif 162 163 /* 164 * This is meant to be used in conjunction with __in_section() and similar 165 * where scattered structure instances are concatenated together by the linker 166 * and walked by the code at run time just like a contiguous array of such 167 * structures. 168 * 169 * Assemblers and linkers may insert alignment padding by default whose 170 * size is larger than the natural alignment for those structures when 171 * gathering various section segments together, messing up the array walk. 172 * To prevent this, we need to provide an explicit alignment not to rely 173 * on the default that might just work by luck. 174 * 175 * Alignment statements in linker scripts are not sufficient as 176 * the assembler may add padding by itself to each segment when switching 177 * between sections within the same file even if it merges many such segments 178 * into a single section in the end. 179 */ 180 #define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type 181 182 /** 183 * @brief Iterable Sections APIs 184 * @defgroup iterable_section_apis Iterable Sections APIs 185 * @{ 186 */ 187 188 /** 189 * @brief Defines a new iterable section. 190 * 191 * @details 192 * Convenience helper combining __in_section() and Z_DECL_ALIGN(). 193 * The section name is the struct type prepended with an underscore. 194 * The subsection is "static" and the subsubsection is the variable name. 195 * 196 * In the linker script, create output sections for these using 197 * ITERABLE_SECTION_ROM() or ITERABLE_SECTION_RAM(). 198 */ 199 #define STRUCT_SECTION_ITERABLE(struct_type, name) \ 200 Z_DECL_ALIGN(struct struct_type) name \ 201 __in_section(_##struct_type, static, name) __used 202 203 #define Z_STRUCT_SECTION_ITERABLE(struct_type, name) \ 204 __DEPRECATED_MACRO \ 205 STRUCT_SECTION_ITERABLE(struct_type, name) 206 207 /** 208 * @brief Defines an alternate data type iterable section. 209 * 210 * @details 211 * Special variant of STRUCT_SECTION_ITERABLE(), for placing alternate 212 * data types within the iterable section of a specific data type. The 213 * data type sizes and semantics must be equivalent! 214 */ 215 #define STRUCT_SECTION_ITERABLE_ALTERNATE(out_type, struct_type, name) \ 216 Z_DECL_ALIGN(struct struct_type) name \ 217 __in_section(_##out_type, static, name) __used 218 219 #define Z_STRUCT_SECTION_ITERABLE_ALTERNATE(out_type, struct_type, name) \ 220 __DEPRECATED_MACRO \ 221 STRUCT_SECTION_ITERABLE_ALTERNATE(out_type, struct_type, name) 222 223 /** 224 * @brief Iterate over a specified iterable section. 225 * 226 * @details 227 * Iterator for structure instances gathered by STRUCT_SECTION_ITERABLE(). 228 * The linker must provide a _<struct_type>_list_start symbol and a 229 * _<struct_type>_list_end symbol to mark the start and the end of the 230 * list of struct objects to iterate over. This is normally done using 231 * ITERABLE_SECTION_ROM() or ITERABLE_SECTION_RAM() in the linker script. 232 */ 233 #define STRUCT_SECTION_FOREACH(struct_type, iterator) \ 234 extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \ 235 extern struct struct_type _CONCAT(_##struct_type, _list_end)[]; \ 236 for (struct struct_type *iterator = \ 237 _CONCAT(_##struct_type, _list_start); \ 238 ({ __ASSERT(iterator <= _CONCAT(_##struct_type, _list_end), \ 239 "unexpected list end location"); \ 240 iterator < _CONCAT(_##struct_type, _list_end); }); \ 241 iterator++) 242 243 #define Z_STRUCT_SECTION_FOREACH(struct_type, iterator) \ 244 __DEPRECATED_MACRO \ 245 STRUCT_SECTION_FOREACH(struct_type, iterator) 246 247 /** 248 * @} 249 */ /* end of struct_section_apis */ 250 251 #define LOG2CEIL(x) \ 252 ((((x) <= 4) ? 2 : (((x) <= 8) ? 3 : (((x) <= 16) ? \ 253 4 : (((x) <= 32) ? 5 : (((x) <= 64) ? 6 : (((x) <= 128) ? \ 254 7 : (((x) <= 256) ? 8 : (((x) <= 512) ? 9 : (((x) <= 1024) ? \ 255 10 : (((x) <= 2048) ? 11 : (((x) <= 4096) ? 12 : (((x) <= 8192) ? \ 256 13 : (((x) <= 16384) ? 14 : (((x) <= 32768) ? 15:(((x) <= 65536) ? \ 257 16 : (((x) <= 131072) ? 17 : (((x) <= 262144) ? 18:(((x) <= 524288) ? \ 258 19 : (((x) <= 1048576) ? 20 : (((x) <= 2097152) ? \ 259 21 : (((x) <= 4194304) ? 22 : (((x) <= 8388608) ? \ 260 23 : (((x) <= 16777216) ? 24 : (((x) <= 33554432) ? \ 261 25 : (((x) <= 67108864) ? 26 : (((x) <= 134217728) ? \ 262 27 : (((x) <= 268435456) ? 28 : (((x) <= 536870912) ? \ 263 29 : (((x) <= 1073741824) ? 30 : (((x) <= 2147483648) ? \ 264 31 : 32))))))))))))))))))))))))))))))) 265 266 #endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */ 267