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