1 /*
2  * Copyright (c) 2008-2013 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #ifndef __COMPILER_H
24 #define __COMPILER_H
25 
26 #include <stddef.h>
27 
28 #ifndef __ASSEMBLY__
29 
30 #if __GNUC__
31 #define likely(x)       __builtin_expect(!!(x), 1)
32 #define unlikely(x)     __builtin_expect(!!(x), 0)
33 #define __UNUSED __attribute__((__unused__))
34 #define __PACKED __attribute__((packed))
35 #define __ALIGNED(x) __attribute__((aligned(x)))
36 #define __PRINTFLIKE(__fmt,__varargs) __attribute__((__format__ (__printf__, __fmt, __varargs)))
37 #define __SCANFLIKE(__fmt,__varargs) __attribute__((__format__ (__scanf__, __fmt, __varargs)))
38 #define __SECTION(x) __attribute((section(x)))
39 #define __PURE __attribute((pure))
40 #define __CONST __attribute((const))
41 #define __NO_RETURN __attribute__((noreturn))
42 #define __MALLOC __attribute__((malloc))
43 #define __WEAK __attribute__((__weak__))
44 #define __GNU_INLINE __attribute__((gnu_inline))
45 #define __GET_CALLER(x) __builtin_return_address(0)
46 #define __GET_FRAME(x) __builtin_frame_address(0)
47 #define __NAKED __attribute__((naked))
48 #define __ISCONSTANT(x) __builtin_constant_p(x)
49 #define __NO_INLINE __attribute((noinline))
50 #define __SRAM __NO_INLINE __SECTION(".sram.text")
51 #define __CONSTRUCTOR __attribute__((constructor))
52 #define __DESTRUCTOR __attribute__((destructor))
53 #define __OPTIMIZE(x) __attribute__((optimize(x)))
54 
55 #define INCBIN(symname, sizename, filename, section) \
56     INCBIN_ALIGNED(symname, sizename, filename, section, 4)
57 
58 #define INCBIN_ALIGNED(symname, sizename, filename, section, align)                \
59     __asm__ (".section " section ", \"a\"; .balign " #align "; .globl "#symname);  \
60     __asm__ (""#symname ":\n.incbin \"" filename "\"");                            \
61     __asm__ (".balign 1; "#symname "_end:");                                       \
62     __asm__ (".balign 4; .globl "#sizename);                                       \
63     __asm__ (""#sizename ": .long "#symname "_end - "#symname);                    \
64     __asm__ (".previous");                                                         \
65     extern unsigned char symname[];                                                \
66     extern unsigned int sizename
67 
68 #define INCFILE(symname, sizename, filename) INCBIN(symname, sizename, filename, ".rodata")
69 
70 #define INCFILE_ALIGNED(symname, sizename, filename, align) \
71     INCBIN_ALIGNED(symname, sizename, filename, ".rodata", align)
72 
73 /* look for gcc 3.0 and above */
74 #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 0)
75 #define __ALWAYS_INLINE __attribute__((always_inline))
76 #else
77 #define __ALWAYS_INLINE
78 #endif
79 
80 /* look for gcc 3.1 and above */
81 #if !defined(__DEPRECATED) // seems to be built in in some versions of the compiler
82 #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
83 #define __DEPRECATED __attribute((deprecated))
84 #else
85 #define __DEPRECATED
86 #endif
87 #endif
88 
89 /* look for gcc 3.3 and above */
90 #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
91 /* the may_alias attribute was introduced in gcc 3.3; before that, there
92  * was no way to specify aliasiang rules on a type-by-type basis */
93 #define __MAY_ALIAS __attribute__((may_alias))
94 
95 /* nonnull was added in gcc 3.3 as well */
96 #define __NONNULL(x) __attribute((nonnull x))
97 #else
98 #define __MAY_ALIAS
99 #define __NONNULL(x)
100 #endif
101 
102 /* look for gcc 3.4 and above */
103 #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || defined(__clang__)
104 #define __WARN_UNUSED_RESULT __attribute((warn_unused_result))
105 #else
106 #define __WARN_UNUSED_RESULT
107 #endif
108 
109 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && !defined(__clang__))
110 #define __EXTERNALLY_VISIBLE __attribute__((externally_visible))
111 #else
112 #define __EXTERNALLY_VISIBLE
113 #endif
114 
115 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) || defined(__clang__)
116 #define __UNREACHABLE __builtin_unreachable()
117 #else
118 #define __UNREACHABLE
119 #endif
120 
121 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__)
122 #ifdef __cplusplus
123 #define STATIC_ASSERT(e) static_assert(e, #e)
124 #else
125 #define STATIC_ASSERT(e) _Static_assert(e, #e)
126 #endif
127 #else
128 #define STATIC_ASSERT(e) extern char (*ct_assert(void)) [sizeof(char[1 - 2*!(e)])]
129 #endif
130 
131 /* compiler fence */
132 #define CF do { __asm__ volatile("" ::: "memory"); } while(0)
133 
134 #define __WEAK_ALIAS(x) __attribute__((__weak__, alias(x)))
135 #define __ALIAS(x) __attribute__((alias(x)))
136 
137 #define __EXPORT __attribute__ ((visibility("default")))
138 #define __LOCAL  __attribute__ ((visibility("hidden")))
139 
140 #define __THREAD __thread
141 
142 #define __offsetof(type, field) __builtin_offsetof(type, field)
143 
144 #else
145 
146 #define likely(x)       (x)
147 #define unlikely(x)     (x)
148 #define __UNUSED
149 #define __PACKED
150 #define __ALIGNED(x)
151 #define __PRINTFLIKE(__fmt,__varargs)
152 #define __SCANFLIKE(__fmt,__varargs)
153 #define __SECTION(x)
154 #define __PURE
155 #define __CONST
156 #define __NONNULL(x)
157 #define __DEPRECATED
158 #define __WARN_UNUSED_RESULT
159 #define __ALWAYS_INLINE
160 #define __MAY_ALIAS
161 #define __NO_RETURN
162 #endif
163 
164 #endif
165 
166 #ifdef __cplusplus
167 extern "C++" {
168 template <typename T, size_t N>
countof(const T (& array)[N])169 constexpr inline size_t countof(const T (&array)[N]) { return N; }
170 }
171 #else
172 /* TODO: add type check */
173 #define countof(a) (sizeof(a) / sizeof((a)[0]))
174 #endif
175 
176 #ifndef __has_attribute
177 #define __has_attribute(x) 0
178 #endif
179 
180 #if __has_attribute(fallthrough)
181 #define __FALLTHROUGH __attribute__((fallthrough))
182 #else
183 #define __FALLTHROUGH
184 #endif
185 
186 #ifndef __has_feature
187 #define __has_feature(x) 0
188 #endif
189 
190 #if __has_feature(shadow_call_stack)
191 #define __NO_SHADOW_CALL_STACK  __attribute__((no_sanitize("shadow-call-stack")))
192 #else
193 #define __NO_SHADOW_CALL_STACK
194 #endif
195 
196 /* CPP header guards */
197 #ifdef __cplusplus
198 #define __BEGIN_CDECLS  extern "C" {
199 #define __END_CDECLS    }
200 #else
201 #define __BEGIN_CDECLS
202 #define __END_CDECLS
203 #endif
204 
205 /*
206  * Macros to prevent merging or refetching of reads and writes. Don't use with
207  * struct types if you don't want compiler to generate calls to memcpy().
208  */
209 #define READ_ONCE(x) (*((volatile __typeof__(x) *) &(x)))
210 #define WRITE_ONCE(x, val) (*((volatile __typeof__(val) *) &(x)) = (val))
211 
212 #endif
213