Zephyr Project API  3.1.0
A Scalable Open Source RTOS
gcc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2014,2017 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
9
17#define GCC_VERSION \
18 ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
19
20/* GCC supports #pragma diagnostics since 4.6.0 */
21#if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (GCC_VERSION >= 40600)
22#define TOOLCHAIN_HAS_PRAGMA_DIAG 1
23#endif
24
25#if !defined(TOOLCHAIN_HAS_C_GENERIC) && (GCC_VERSION >= 40900)
26#define TOOLCHAIN_HAS_C_GENERIC 1
27#endif
28
29#if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (GCC_VERSION >= 40900)
30#define TOOLCHAIN_HAS_C_AUTO_TYPE 1
31#endif
32
33/*
34 * Older versions of GCC do not define __BYTE_ORDER__, so it must be manually
35 * detected and defined using arch-specific definitions.
36 */
37
38#ifndef _LINKER
39
40#ifndef __ORDER_BIG_ENDIAN__
41#define __ORDER_BIG_ENDIAN__ (1)
42#endif
43
44#ifndef __ORDER_LITTLE_ENDIAN__
45#define __ORDER_LITTLE_ENDIAN__ (2)
46#endif
47
48#ifndef __BYTE_ORDER__
49#if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
50 defined(__THUMBEB__) || defined(__AARCH64EB__) || \
51 defined(__MIPSEB__) || defined(__TC32EB__)
52
53#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
54
55#elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
56 defined(__THUMBEL__) || defined(__AARCH64EL__) || \
57 defined(__MIPSEL__) || defined(__TC32EL__)
58
59#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
60
61#else
62#error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
63#endif
64#endif
65
66
67/* C++11 has static_assert built in */
68#if defined(__cplusplus) && (__cplusplus >= 201103L)
69#define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
70
71/*
72 * GCC 4.6 and higher have the C11 _Static_assert built in, and its
73 * output is easier to understand than the common BUILD_ASSERT macros.
74 */
75#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
76 (__STDC_VERSION__) >= 201100
77#define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
78#else
79#define BUILD_ASSERT(EXPR, MSG...)
80#endif
81
82#ifdef __cplusplus
83#define ZRESTRICT __restrict
84#else
85#define ZRESTRICT restrict
86#endif
87
89#include <stdbool.h>
90
91#define ALIAS_OF(of) __attribute__((alias(#of)))
92
93#define FUNC_ALIAS(real_func, new_alias, return_type) \
94 return_type new_alias() ALIAS_OF(real_func)
95
96#if defined(CONFIG_ARCH_POSIX)
98
99/*let's not segfault if this were to happen for some reason*/
100#define CODE_UNREACHABLE \
101{\
102 posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
103 __FILE__, __LINE__);\
104 __builtin_unreachable(); \
105}
106#else
107#define CODE_UNREACHABLE __builtin_unreachable()
108#endif
109#define FUNC_NORETURN __attribute__((__noreturn__))
110
111/* The GNU assembler for Cortex-M3 uses # for immediate values, not
112 * comments, so the @nobits# trick does not work.
113 */
114#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
115#define _NODATA_SECTION(segment) __attribute__((section(#segment)))
116#else
117#define _NODATA_SECTION(segment) \
118 __attribute__((section(#segment ",\"wa\",@nobits#")))
119#endif
120
121/* Unaligned access */
122#define UNALIGNED_GET(p) \
123__extension__ ({ \
124 struct __attribute__((__packed__)) { \
125 __typeof__(*(p)) __v; \
126 } *__p = (__typeof__(__p)) (p); \
127 __p->__v; \
128})
129
130
131#if __GNUC__ >= 7 && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
132
133/* Version of UNALIGNED_PUT() which issues a compiler_barrier() after
134 * the store. It is required to workaround an apparent optimization
135 * bug in GCC for ARM Cortex-M3 and higher targets, when multiple
136 * byte, half-word and word stores (strb, strh, str instructions),
137 * which support unaligned access, can be coalesced into store double
138 * (strd) instruction, which doesn't support unaligned access (the
139 * compilers in question do this optimization ignoring __packed__
140 * attribute).
141 */
142#define UNALIGNED_PUT(v, p) \
143do { \
144 struct __attribute__((__packed__)) { \
145 __typeof__(*p) __v; \
146 } *__p = (__typeof__(__p)) (p); \
147 __p->__v = (v); \
148 compiler_barrier(); \
149} while (false)
150
151#else
152
153#define UNALIGNED_PUT(v, p) \
154do { \
155 struct __attribute__((__packed__)) { \
156 __typeof__(*p) __v; \
157 } *__p = (__typeof__(__p)) (p); \
158 __p->__v = (v); \
159} while (false)
160
161#endif
162
163/* Double indirection to ensure section names are expanded before
164 * stringification
165 */
166#define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
167#define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
168
169#define __GENERIC_DOT_SECTION(segment) \
170 __attribute__((section("." STRINGIFY(segment))))
171#define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
172
173#define ___in_section(a, b, c) \
174 __attribute__((section("." Z_STRINGIFY(a) \
175 "." Z_STRINGIFY(b) \
176 "." Z_STRINGIFY(c))))
177#define __in_section(a, b, c) ___in_section(a, b, c)
178
179#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
180
181#define __in_section_unique_named(seg, name) \
182 ___in_section(seg, __FILE__, name)
183
184/* When using XIP, using '__ramfunc' places a function into RAM instead
185 * of FLASH. Make sure '__ramfunc' is defined only when
186 * CONFIG_ARCH_HAS_RAMFUNC_SUPPORT is defined, so that the compiler can
187 * report an error if '__ramfunc' is used but the architecture does not
188 * support it.
189 */
190#if !defined(CONFIG_XIP)
191#define __ramfunc
192#elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
193#define __ramfunc __attribute__((noinline)) \
194 __attribute__((long_call, section(".ramfunc")))
195#endif /* !CONFIG_XIP */
196
197#ifndef __fallthrough
198#if __GNUC__ >= 7
199#define __fallthrough __attribute__((fallthrough))
200#else
201#define __fallthrough
202#endif /* __GNUC__ >= 7 */
203#endif
204
205#ifndef __packed
206#define __packed __attribute__((__packed__))
207#endif
208#ifndef __aligned
209#define __aligned(x) __attribute__((__aligned__(x)))
210#endif
211#define __may_alias __attribute__((__may_alias__))
212#ifndef __printf_like
213#define __printf_like(f, a) __attribute__((format (printf, f, a)))
214#endif
215#define __used __attribute__((__used__))
216#ifndef __deprecated
217#define __deprecated __attribute__((deprecated))
218#endif
219#ifndef __attribute_const__
220#define __attribute_const__ __attribute__((__const__))
221#endif
222#ifndef __must_check
223#define __must_check __attribute__((warn_unused_result))
224#endif
225#define ARG_UNUSED(x) (void)(x)
226
227#define likely(x) __builtin_expect((bool)!!(x), true)
228#define unlikely(x) __builtin_expect((bool)!!(x), false)
229
230#define popcount(x) __builtin_popcount(x)
231
232#ifndef __no_optimization
233#define __no_optimization __attribute__((optimize("-O0")))
234#endif
235
236#ifndef __weak
237#define __weak __attribute__((__weak__))
238#endif
239#define __unused __attribute__((__unused__))
240
241/* Builtins with availability that depend on the compiler version. */
242#if __GNUC__ >= 5
243#define HAS_BUILTIN___builtin_add_overflow 1
244#define HAS_BUILTIN___builtin_sub_overflow 1
245#define HAS_BUILTIN___builtin_mul_overflow 1
246#define HAS_BUILTIN___builtin_div_overflow 1
247#endif
248#if __GNUC__ >= 4
249#define HAS_BUILTIN___builtin_clz 1
250#define HAS_BUILTIN___builtin_clzl 1
251#define HAS_BUILTIN___builtin_clzll 1
252#define HAS_BUILTIN___builtin_ctz 1
253#define HAS_BUILTIN___builtin_ctzl 1
254#define HAS_BUILTIN___builtin_ctzll 1
255#endif
256
257/*
258 * Be *very* careful with these. You cannot filter out __DEPRECATED_MACRO with
259 * -wno-deprecated, which has implications for -Werror.
260 */
261
262/*
263 * Expands to nothing and generates a warning. Used like
264 *
265 * #define FOO __WARN("Please use BAR instead") ...
266 *
267 * The warning points to the location where the macro is expanded.
268 */
269#define __WARN(msg) __WARN1(GCC warning msg)
270#define __WARN1(s) _Pragma(#s)
271
272/* Generic message */
273#ifndef __DEPRECATED_MACRO
274#define __DEPRECATED_MACRO __WARN("Macro is deprecated")
275#endif
276
277/* These macros allow having ARM asm functions callable from thumb */
278
279#if defined(_ASMLANGUAGE)
280
281#if defined(CONFIG_ARM)
282
283#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
284
285#define FUNC_CODE() .thumb;
286#define FUNC_INSTR(a)
287
288#else
289
290#define FUNC_CODE() .code 32
291#define FUNC_INSTR(a)
292
293#endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
294
295#else
296
297#define FUNC_CODE()
298#define FUNC_INSTR(a)
299
300#endif /* CONFIG_ARM */
301
302#endif /* _ASMLANGUAGE */
303
304/*
305 * These macros are used to declare assembly language symbols that need
306 * to be typed properly(func or data) to be visible to the OMF tool.
307 * So that the build tool could mark them as an entry point to be linked
308 * correctly. This is an elfism. Use #if 0 for a.out.
309 */
310
311#if defined(_ASMLANGUAGE)
312
313#if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
314 || defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
315 || defined(CONFIG_MIPS)
316#define GTEXT(sym) .global sym; .type sym, %function
317#define GDATA(sym) .global sym; .type sym, %object
318#define WTEXT(sym) .weak sym; .type sym, %function
319#define WDATA(sym) .weak sym; .type sym, %object
320#elif defined(CONFIG_ARC)
321/*
322 * Need to use assembly macros because ';' is interpreted as the start of
323 * a single line comment in the ARC assembler.
324 */
325
326.macro glbl_text symbol
327 .globl \symbol
328 .type \symbol, %function
329.endm
330
331.macro glbl_data symbol
332 .globl \symbol
333 .type \symbol, %object
334.endm
335
336.macro weak_data symbol
337 .weak \symbol
338 .type \symbol, %object
339.endm
340
341#define GTEXT(sym) glbl_text sym
342#define GDATA(sym) glbl_data sym
343#define WDATA(sym) weak_data sym
344
345#else /* !CONFIG_ARM && !CONFIG_ARC */
346#define GTEXT(sym) .globl sym; .type sym, @function
347#define GDATA(sym) .globl sym; .type sym, @object
348#endif
349
350/*
351 * These macros specify the section in which a given function or variable
352 * resides.
353 *
354 * - SECTION_FUNC allows only one function to reside in a sub-section
355 * - SECTION_SUBSEC_FUNC allows multiple functions to reside in a sub-section
356 * This ensures that garbage collection only discards the section
357 * if all functions in the sub-section are not referenced.
358 */
359
360#if defined(CONFIG_ARC)
361/*
362 * Need to use assembly macros because ';' is interpreted as the start of
363 * a single line comment in the ARC assembler.
364 *
365 * Also, '\‍()' is needed in the .section directive of these macros for
366 * correct substitution of the 'section' variable.
367 */
368
369.macro section_var section, symbol
370 .section .\section\‍().\symbol
371 \symbol :
372.endm
373
374.macro section_func section, symbol
375 .section .\section\().\symbol, "ax"
376 FUNC_CODE()
377 PERFOPT_ALIGN
378 \symbol :
379 FUNC_INSTR(\symbol)
380.endm
381
382.macro section_subsec_func section, subsection, symbol
383 .section .\section\().\subsection, "ax"
384 PERFOPT_ALIGN
385 \symbol :
386.endm
387
388#define SECTION_VAR(sect, sym) section_var sect, sym
389#define SECTION_FUNC(sect, sym) section_func sect, sym
390#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
391 section_subsec_func sect, subsec, sym
392#else /* !CONFIG_ARC */
393
394#define SECTION_VAR(sect, sym) .section .sect.sym; sym:
395#define SECTION_FUNC(sect, sym) \
396 .section .sect.sym, "ax"; \
397 FUNC_CODE() \
398 PERFOPT_ALIGN; sym : \
399 FUNC_INSTR(sym)
400#define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
401 .section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
402
403#endif /* CONFIG_ARC */
404
405#endif /* _ASMLANGUAGE */
406
407#if defined(_ASMLANGUAGE)
408#if defined(CONFIG_ARM)
409#if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
410/* '.syntax unified' is a gcc-ism used in thumb-2 asm files */
411#define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
412#else
413#define _ASM_FILE_PROLOGUE .text; .code 32
414#endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
415#elif defined(CONFIG_ARM64)
416#define _ASM_FILE_PROLOGUE .text
417#endif /* CONFIG_ARM64 || CONFIG_ARM */
418#endif /* _ASMLANGUAGE */
419
420/*
421 * These macros generate absolute symbols for GCC
422 */
423
424/* create an extern reference to the absolute symbol */
425
426#define GEN_OFFSET_EXTERN(name) extern const char name[]
427
428#define GEN_ABS_SYM_BEGIN(name) \
429 EXTERN_C void name(void); \
430 void name(void) \
431 {
432
433#define GEN_ABS_SYM_END }
434
435/*
436 * Note that GEN_ABSOLUTE_SYM(), depending on the architecture
437 * and toolchain, may restrict the range of values permitted
438 * for assignment to the named symbol.
439 *
440 * For example, on x86, "value" is interpreted as signed
441 * 32-bit integer. Passing in an unsigned 32-bit integer
442 * with MSB set would result in a negative integer.
443 * Moreover, GCC would error out if an integer larger
444 * than 2^32-1 is passed as "value".
445 */
446
447/*
448 * GEN_ABSOLUTE_SYM_KCONFIG() is outputted by the build system
449 * to generate named symbol/value pairs for kconfigs.
450 */
451
452#if defined(CONFIG_ARM)
453
454/*
455 * GNU/ARM backend does not have a proper operand modifier which does not
456 * produces prefix # followed by value, such as %0 for PowerPC, Intel, and
457 * MIPS. The workaround performed here is using %B0 which converts
458 * the value to ~(value). Thus "n"(~(value)) is set in operand constraint
459 * to output (value) in the ARM specific GEN_OFFSET macro.
460 */
461
462#define GEN_ABSOLUTE_SYM(name, value) \
463 __asm__(".globl\t" #name "\n\t.equ\t" #name \
464 ",%B0" \
465 "\n\t.type\t" #name ",%%object" : : "n"(~(value)))
466
467#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
468 __asm__(".globl\t" #name \
469 "\n\t.equ\t" #name "," #value \
470 "\n\t.type\t" #name ",%object")
471
472#elif defined(CONFIG_X86)
473
474#define GEN_ABSOLUTE_SYM(name, value) \
475 __asm__(".globl\t" #name "\n\t.equ\t" #name \
476 ",%c0" \
477 "\n\t.type\t" #name ",@object" : : "n"(value))
478
479#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
480 __asm__(".globl\t" #name \
481 "\n\t.equ\t" #name "," #value \
482 "\n\t.type\t" #name ",@object")
483
484#elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
485
486#define GEN_ABSOLUTE_SYM(name, value) \
487 __asm__(".globl\t" #name "\n\t.equ\t" #name \
488 ",%c0" \
489 "\n\t.type\t" #name ",@object" : : "n"(value))
490
491#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
492 __asm__(".globl\t" #name \
493 "\n\t.equ\t" #name "," #value \
494 "\n\t.type\t" #name ",@object")
495
496#elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
497 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
498
499/* No special prefixes necessary for constants in this arch AFAICT */
500#define GEN_ABSOLUTE_SYM(name, value) \
501 __asm__(".globl\t" #name "\n\t.equ\t" #name \
502 ",%0" \
503 "\n\t.type\t" #name ",%%object" : : "n"(value))
504
505#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
506 __asm__(".globl\t" #name \
507 "\n\t.equ\t" #name "," #value \
508 "\n\t.type\t" #name ",%object")
509
510#elif defined(CONFIG_ARCH_POSIX)
511#define GEN_ABSOLUTE_SYM(name, value) \
512 __asm__(".globl\t" #name "\n\t.equ\t" #name \
513 ",%c0" \
514 "\n\t.type\t" #name ",@object" : : "n"(value))
515
516#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
517 __asm__(".globl\t" #name \
518 "\n\t.equ\t" #name "," #value \
519 "\n\t.type\t" #name ",@object")
520
521#elif defined(CONFIG_SPARC)
522#define GEN_ABSOLUTE_SYM(name, value) \
523 __asm__(".global\t" #name "\n\t.equ\t" #name \
524 ",%0" \
525 "\n\t.type\t" #name ",#object" : : "n"(value))
526
527#define GEN_ABSOLUTE_SYM_KCONFIG(name, value) \
528 __asm__(".globl\t" #name \
529 "\n\t.equ\t" #name "," #value \
530 "\n\t.type\t" #name ",#object")
531
532#else
533#error processor architecture not supported
534#endif
535
536#define compiler_barrier() do { \
537 __asm__ __volatile__ ("" ::: "memory"); \
538} while (false)
539
549#define Z_MAX(a, b) ({ \
550 /* random suffix to avoid naming conflict */ \
551 __typeof__(a) _value_a_ = (a); \
552 __typeof__(b) _value_b_ = (b); \
553 _value_a_ > _value_b_ ? _value_a_ : _value_b_; \
554 })
555
561#define Z_MIN(a, b) ({ \
562 /* random suffix to avoid naming conflict */ \
563 __typeof__(a) _value_a_ = (a); \
564 __typeof__(b) _value_b_ = (b); \
565 _value_a_ < _value_b_ ? _value_a_ : _value_b_; \
566 })
567
573#define Z_CLAMP(val, low, high) ({ \
574 /* random suffix to avoid naming conflict */ \
575 __typeof__(val) _value_val_ = (val); \
576 __typeof__(low) _value_low_ = (low); \
577 __typeof__(high) _value_high_ = (high); \
578 (_value_val_ < _value_low_) ? _value_low_ : \
579 (_value_val_ > _value_high_) ? _value_high_ : \
580 _value_val_; \
581 })
582
589#ifdef CONFIG_64BIT
590#define Z_POW2_CEIL(x) ((1UL << (63U - __builtin_clzl(x))) < x ? \
591 1UL << (63U - __builtin_clzl(x) + 1U) : \
592 1UL << (63U - __builtin_clzl(x)))
593#else
594#define Z_POW2_CEIL(x) ((1UL << (31U - __builtin_clz(x))) < x ? \
595 1UL << (31U - __builtin_clz(x) + 1U) : \
596 1UL << (31U - __builtin_clz(x)))
597#endif
598
605#define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
606
607#endif /* !_LINKER */
608#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */
Common toolchain abstraction.