blob: c3095a802a24af3d1a14ca89e95e21449b52ee32 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -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)
Travis Geiselbrecht84aa16c2010-05-06 14:16:07 -070044#define __GET_FRAME(x) __builtin_frame_address(0)
45
46#define INCBIN(symname, sizename, filename, section) \
47 __asm__ (".section " section "; .align 4; .globl "#symname); \
48 __asm__ (""#symname ":\n.incbin \"" filename "\""); \
49 __asm__ (".section " section "; .align 1;"); \
50 __asm__ (""#symname "_end:"); \
51 __asm__ (".section " section "; .align 4; .globl "#sizename); \
52 __asm__ (""#sizename ": .long "#symname "_end - "#symname " - 1"); \
53 extern unsigned char symname[]; \
54 extern unsigned int sizename
55
56#define INCFILE(symname, sizename, filename) INCBIN(symname, sizename, filename, ".rodata")
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070057
58/* look for gcc 3.0 and above */
59#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 0)
60#define __ALWAYS_INLINE __attribute__((always_inline))
61#else
62#define __ALWAYS_INLINE
63#endif
64
65/* look for gcc 3.1 and above */
66#if !defined(__DEPRECATED) // seems to be built in in some versions of the compiler
67#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
68#define __DEPRECATED __attribute((deprecated))
69#else
70#define __DEPRECATED
71#endif
72#endif
73
74/* look for gcc 3.3 and above */
75#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
76/* the may_alias attribute was introduced in gcc 3.3; before that, there
77 * was no way to specify aliasiang rules on a type-by-type basis */
78#define __MAY_ALIAS __attribute__((may_alias))
79
80/* nonnull was added in gcc 3.3 as well */
81#define __NONNULL(x) __attribute((nonnull x))
82#else
83#define __MAY_ALIAS
84#define __NONNULL(x)
85#endif
86
87/* look for gcc 3.4 and above */
88#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
89#define __WARN_UNUSED_RESULT __attribute((warn_unused_result))
90#else
91#define __WARN_UNUSED_RESULT
92#endif
93
94#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
95#define __EXTERNALLY_VISIBLE __attribute__((externally_visible))
96#else
97#define __EXTERNALLY_VISIBLE
98#endif
99
100#else
101
102#define likely(x) (x)
103#define unlikely(x) (x)
104#define __UNUSED
105#define __PACKED
106#define __ALIGNED(x)
107#define __PRINTFLIKE(__fmt,__varargs)
108#define __SCANFLIKE(__fmt,__varargs)
109#define __SECTION(x)
110#define __PURE
111#define __CONST
112#define __NONNULL(x)
113#define __DEPRECATED
114#define __WARN_UNUSED_RESULT
115#define __ALWAYS_INLINE
116#define __MAY_ALIAS
117#define __NO_RETURN
118#endif
119
120#endif
121
Travis Geiselbrecht84aa16c2010-05-06 14:16:07 -0700122/* TODO: add type check */
123#define countof(a) (sizeof(a) / sizeof((a)[0]))
124
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700125#endif