compiler.h
1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _PICO_PLATFORM_COMPILER_H
8#define _PICO_PLATFORM_COMPILER_H
9
18#include "hardware/platform_defs.h"
19
20#ifndef __ASSEMBLER__
21
22#if defined __GNUC__
23#include <sys/cdefs.h>
24// note LLVM defines __GNUC__
25#ifdef __clang__
26#define PICO_C_COMPILER_IS_CLANG 1
27#else
28#define PICO_C_COMPILER_IS_GNU 1
29#endif
30#elif defined __ICCARM__
31#ifndef __aligned
32#define __aligned(x) __attribute__((__aligned__(x)))
33#endif
34#ifndef __always_inline
35#define __always_inline __attribute__((__always_inline__))
36#endif
37#ifndef __noinline
38#define __noinline __attribute__((__noinline__))
39#endif
40#ifndef __packed
41#define __packed __attribute__((__packed__))
42#endif
43#ifndef __printflike
44#define __printflike(a, b)
45#endif
46#ifndef __unused
47#define __unused __attribute__((__unused__))
48#endif
49#ifndef __used
50#define __used __attribute__((__used__))
51#endif
52#ifndef __CONCAT1
53#define __CONCAT1(a, b) a ## b
54#endif
55#ifndef __CONCAT
56#define __CONCAT(a, b) __CONCAT1(a, b)
57#endif
58#ifndef __STRING
59#define __STRING(a) #a
60#endif
61/* Compatible definitions of GCC builtins */
62
63static inline uint __builtin_ctz(uint x) {
64 extern uint32_t __ctzsi2(uint32_t);
65 return __ctzsi2(x);
66}
67#define __builtin_expect(x, y) (x)
68#define __builtin_isnan(x) __iar_isnan(x)
69#else
70#error Unsupported toolchain
71#endif
72
73#define __weak __attribute__((weak))
74
75#include "pico/types.h"
76
77// GCC_Like_Pragma(x) is a pragma on GNUC compatible compilers
78#ifdef __GNUC__
79#define GCC_Like_Pragma _Pragma
80#else
81#define GCC_Like_Pragma(x)
82#endif
83
84// Clang_Pragma(x) is a pragma on Clang only
85#ifdef __clang__
86#define Clang_Pragma _Pragma
87#else
88#define Clang_Pragma(x)
89#endif
90
91// GCC_Pragma(x) is a pragma on GCC only
92#if PICO_C_COMPILER_IS_GNU
93#define GCC_Pragma _Pragma
94#else
95#define GCC_Pragma(x)
96#endif
97
98#ifdef __cplusplus
99extern "C" {
100#endif
101
109#define __isr
110
111#define __packed_aligned __packed __aligned(4)
112
122#if PICO_C_COMPILER_IS_GNU && (__GNUC__ <= 6 || (__GNUC__ == 7 && (__GNUC_MINOR__ < 3 || !defined(__cplusplus))))
123#define __force_inline inline __always_inline
124#else
125#define __force_inline __always_inline
126#endif
127
131#ifndef count_of
132#define count_of(a) (sizeof(a)/sizeof((a)[0]))
133#endif
134
138#ifndef MAX
139#define MAX(a, b) ((a)>(b)?(a):(b))
140#endif
141
145#ifndef MIN
146#define MIN(a, b) ((b)>(a)?(a):(b))
147#endif
148
149#ifdef __ARM_ARCH_ISA_THUMB
150#define pico_default_asm(...) __asm (".syntax unified\n" __VA_ARGS__)
151#define pico_default_asm_volatile(...) __asm volatile (".syntax unified\n" __VA_ARGS__)
152#define pico_default_asm_goto(...) __asm goto (".syntax unified\n" __VA_ARGS__)
153#else
154#define pico_default_asm(...) __asm (__VA_ARGS__)
155#define pico_default_asm_volatile(...) __asm volatile (__VA_ARGS__)
156#define pico_default_asm_goto(...) __asm goto (__VA_ARGS__)
157#endif
158
172 pico_default_asm_volatile ("" : : : "memory");
173}
174
181#define __check_type_compatible(type_a, type_b) static_assert(__builtin_types_compatible_p(type_a, type_b), __STRING(type_a) " is not compatible with " __STRING(type_b));
182
183#define WRAPPER_FUNC(x) __wrap_ ## x
184#define REAL_FUNC(x) __real_ ## x
185
186#ifdef __cplusplus
187}
188#endif
189
190#else // __ASSEMBLER__
191
192#if defined __GNUC__
193// note LLVM defines __GNUC__
194#ifdef __clang__
195#define PICO_ASSEMBLER_IS_CLANG 1
196#else
197#define PICO_ASSEMBLER_IS_GNU 1
198#endif
199#elif defined __ICCARM__
200#else
201#error Unsupported toolchain
202#endif
203
204#define WRAPPER_FUNC_NAME(x) __wrap_##x
205
206#endif // !__ASSEMBLER__
207
208#endif
#define __force_inline
Attribute to force inlining of a function regardless of optimization level.
Definition: compiler.h:125
static __always_inline void __compiler_memory_barrier(void)
Ensure that the compiler does not move memory access across this method call.
Definition: compiler.h:171