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