blob: 44e258725866a63b381d418e62256934463632e0 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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#ifndef __ASSEMBLY__
27
28#if __GNUC__
29#define likely(x) __builtin_expect(!!(x), 1)
30#define unlikely(x) __builtin_expect(!!(x), 0)
31#define __UNUSED __attribute__((__unused__))
32#define __PACKED __attribute__((packed))
33#define __ALIGNED(x) __attribute__((aligned(x)))
34#define __PRINTFLIKE(__fmt,__varargs) __attribute__((__format__ (__printf__, __fmt, __varargs)))
35#define __SCANFLIKE(__fmt,__varargs) __attribute__((__format__ (__scanf__, __fmt, __varargs)))
36#define __SECTION(x) __attribute((section(x)))
37#define __PURE __attribute((pure))
38#define __CONST __attribute((const))
39#define __NO_RETURN __attribute__((noreturn))
40#define __MALLOC __attribute__((malloc))
41#define __WEAK __attribute__((weak))
42#define __GNU_INLINE __attribute__((gnu_inline))
43#define __GET_CALLER(x) __builtin_return_address(0)
44#define __GET_FRAME(x) __builtin_frame_address(0)
45#define __NAKED __attribute__((naked))
46#define __ISCONSTANT(x) __builtin_constant_p(x)
47#define __NO_INLINE __attribute((noinline))
48#define __SRAM __NO_INLINE __SECTION(".sram.text")
49#define __CONSTRUCTOR __attribute__((constructor))
50#define __DESTRUCTOR __attribute__((destructor))
51#define __OPTIMIZE(x) __attribute__((optimize(x)))
52
53#define INCBIN(symname, sizename, filename, section) \
54 __asm__ (".section " section "; .align 4; .globl "#symname); \
55 __asm__ (""#symname ":\n.incbin \"" filename "\""); \
56 __asm__ (".section " section "; .align 1;"); \
57 __asm__ (""#symname "_end:"); \
58 __asm__ (".section " section "; .align 4; .globl "#sizename); \
59 __asm__ (""#sizename ": .long "#symname "_end - "#symname " - 1"); \
60 extern unsigned char symname[]; \
61 extern unsigned int sizename
62
63#define INCFILE(symname, sizename, filename) INCBIN(symname, sizename, filename, ".rodata")
64
65/* look for gcc 3.0 and above */
66#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 0)
67#define __ALWAYS_INLINE __attribute__((always_inline))
68#else
69#define __ALWAYS_INLINE
70#endif
71
72/* look for gcc 3.1 and above */
73#if !defined(__DEPRECATED) // seems to be built in in some versions of the compiler
74#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
75#define __DEPRECATED __attribute((deprecated))
76#else
77#define __DEPRECATED
78#endif
79#endif
80
81/* look for gcc 3.3 and above */
82#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
83/* the may_alias attribute was introduced in gcc 3.3; before that, there
84 * was no way to specify aliasiang rules on a type-by-type basis */
85#define __MAY_ALIAS __attribute__((may_alias))
86
87/* nonnull was added in gcc 3.3 as well */
88#define __NONNULL(x) __attribute((nonnull x))
89#else
90#define __MAY_ALIAS
91#define __NONNULL(x)
92#endif
93
94/* look for gcc 3.4 and above */
95#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
96#define __WARN_UNUSED_RESULT __attribute((warn_unused_result))
97#else
98#define __WARN_UNUSED_RESULT
99#endif
100
101#if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && !defined(__clang__))
102#define __EXTERNALLY_VISIBLE __attribute__((externally_visible))
103#else
104#define __EXTERNALLY_VISIBLE
105#endif
106
107#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) || defined(__clang__)
108#define __UNREACHABLE __builtin_unreachable()
109#else
110#define __UNREACHABLE
111#endif
112
113#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
114#ifdef __cplusplus
115#define STATIC_ASSERT(e) static_assert(e, #e)
116#else
117#define STATIC_ASSERT(e) _Static_assert(e, #e)
118#endif
119#else
120#define STATIC_ASSERT(e) extern char (*ct_assert(void)) [sizeof(char[1 - 2*!(e)])]
121#endif
122
123/* compiler fence */
124#define CF do { __asm__ volatile("" ::: "memory"); } while(0)
125
126#define __WEAK_ALIAS(x) __attribute__((weak, alias(x)))
127#define __ALIAS(x) __attribute__((alias(x)))
128
129#define __EXPORT __attribute__ ((visibility("default")))
130#define __LOCAL __attribute__ ((visibility("hidden")))
131
132#define __THREAD __thread
133
134#define __offsetof(type, field) __builtin_offsetof(type, field)
135
136#else
137
138#define likely(x) (x)
139#define unlikely(x) (x)
140#define __UNUSED
141#define __PACKED
142#define __ALIGNED(x)
143#define __PRINTFLIKE(__fmt,__varargs)
144#define __SCANFLIKE(__fmt,__varargs)
145#define __SECTION(x)
146#define __PURE
147#define __CONST
148#define __NONNULL(x)
149#define __DEPRECATED
150#define __WARN_UNUSED_RESULT
151#define __ALWAYS_INLINE
152#define __MAY_ALIAS
153#define __NO_RETURN
154#endif
155
156#endif
157
158/* TODO: add type check */
159#define countof(a) (sizeof(a) / sizeof((a)[0]))
160
161/* macro-expanding concat */
162#define concat(a, b) __ex_concat(a, b)
163#define __ex_concat(a, b) a ## b
164
165/* CPP header guards */
166#ifdef __cplusplus
167#define __BEGIN_CDECLS extern "C" {
168#define __END_CDECLS }
169#else
170#define __BEGIN_CDECLS
171#define __END_CDECLS
172#endif
173
174#endif