blob: cc96002c90e8d56392bad10ee005738760d2d11d [file] [log] [blame]
Dennis Huang6d037712014-04-22 19:20:59 -07001/*
2 * Copyright (c) 2008 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
48#define INCBIN(symname, sizename, filename, section) \
49 __asm__ (".section " section "; .align 4; .globl "#symname); \
50 __asm__ (""#symname ":\n.incbin \"" filename "\""); \
51 __asm__ (".section " section "; .align 1;"); \
52 __asm__ (""#symname "_end:"); \
53 __asm__ (".section " section "; .align 4; .globl "#sizename); \
54 __asm__ (""#sizename ": .long "#symname "_end - "#symname " - 1"); \
55 extern unsigned char symname[]; \
56 extern unsigned int sizename
57
58#define INCFILE(symname, sizename, filename) INCBIN(symname, sizename, filename, ".rodata")
59
60/* look for gcc 3.0 and above */
61#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 0)
62#define __ALWAYS_INLINE __attribute__((always_inline))
63#else
64#define __ALWAYS_INLINE
65#endif
66
67/* look for gcc 3.1 and above */
68#if !defined(__DEPRECATED) // seems to be built in in some versions of the compiler
69#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
70#define __DEPRECATED __attribute((deprecated))
71#else
72#define __DEPRECATED
73#endif
74#endif
75
76/* look for gcc 3.3 and above */
77#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
78/* the may_alias attribute was introduced in gcc 3.3; before that, there
79 * was no way to specify aliasiang rules on a type-by-type basis */
80#define __MAY_ALIAS __attribute__((may_alias))
81
82/* nonnull was added in gcc 3.3 as well */
83#define __NONNULL(x) __attribute((nonnull x))
84#else
85#define __MAY_ALIAS
86#define __NONNULL(x)
87#endif
88
89/* look for gcc 3.4 and above */
90#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
91#define __WARN_UNUSED_RESULT __attribute((warn_unused_result))
92#else
93#define __WARN_UNUSED_RESULT
94#endif
95
96#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
97#define __EXTERNALLY_VISIBLE __attribute__((externally_visible))
98#else
99#define __EXTERNALLY_VISIBLE
100#endif
101
102#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
103#define __UNREACHABLE __builtin_unreachable()
104#else
105#define __UNREACHABLE
106#endif
107
108/* compiler fence */
109#define CF do { __asm__ volatile("" ::: "memory"); } while(0)
110
111#define __WEAK_ALIAS(x) __attribute__((weak, alias(x)))
112#define __ALIAS(x) __attribute__((alias(x)))
113
114#define __EXPORT __attribute__ ((visibility("default")))
115#define __LOCAL __attribute__ ((visibility("hidden")))
116
117#define __THREAD __thread
118
119#define __offsetof(type, field) __builtin_offsetof(type, field)
120
121#else
122
123#define likely(x) (x)
124#define unlikely(x) (x)
125#define __UNUSED
126#define __PACKED
127#define __ALIGNED(x)
128#define __PRINTFLIKE(__fmt,__varargs)
129#define __SCANFLIKE(__fmt,__varargs)
130#define __SECTION(x)
131#define __PURE
132#define __CONST
133#define __NONNULL(x)
134#define __DEPRECATED
135#define __WARN_UNUSED_RESULT
136#define __ALWAYS_INLINE
137#define __MAY_ALIAS
138#define __NO_RETURN
139#endif
140
141#endif
142
143/* TODO: add type check */
144#define countof(a) (sizeof(a) / sizeof((a)[0]))
145
146/* CPP header guards */
147#ifdef __cplusplus
148#define __BEGIN_CDECLS extern "C" {
149#define __END_CDECLS }
150#else
151#define __BEGIN_CDECLS
152#define __END_CDECLS
153#endif
154
155#endif