]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4sys/include/compiler.h
update
[l4.git] / l4 / pkg / l4sys / include / compiler.h
1 /*****************************************************************************/
2 /**
3  * \file
4  * \brief   L4 compiler related defines.
5  * \ingroup l4_api
6  */
7 /*
8  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
9  *               Alexander Warg <warg@os.inf.tu-dresden.de>,
10  *               Frank Mehnert <fm3@os.inf.tu-dresden.de>,
11  *               Jork Löser <jork@os.inf.tu-dresden.de>,
12  *               Ronald Aigner <ra3@os.inf.tu-dresden.de>
13  *     economic rights: Technische Universität Dresden (Germany)
14  *
15  * This file is part of TUD:OS and distributed under the terms of the
16  * GNU General Public License 2.
17  * Please see the COPYING-GPL-2 file for details.
18  *
19  * As a special exception, you may use this file as part of a free software
20  * library without restriction.  Specifically, if other files instantiate
21  * templates or use macros or inline functions from this file, or you compile
22  * this file and link it with other files to produce an executable, this
23  * file does not by itself cause the resulting executable to be covered by
24  * the GNU General Public License.  This exception does not however
25  * invalidate any other reasons why the executable file might be covered by
26  * the GNU General Public License.
27  */
28 /*****************************************************************************/
29 #ifndef __L4_COMPILER_H__
30 #define __L4_COMPILER_H__
31
32 #if !defined(__ASSEMBLY__) && !defined(__ASSEMBLER__)
33
34 /**
35  * \addtogroup l4sys_defines
36  *
37  * <c>\#include <l4/sys/compiler.h></c>
38  */
39 /*@{*/
40
41 /**
42  * L4 Inline function attribute.
43  * \hideinitializer
44  */
45 #ifndef L4_INLINE
46 #ifndef __cplusplus
47 #  ifdef __OPTIMIZE__
48 #    define L4_INLINE_STATIC static __inline__
49 #    define L4_INLINE_EXTERN extern __inline__
50      /* gcc-4.3 implements c99 inline behaviour, i.e. we use the
51       * 'extern  inline' there, 4.2 and below use 'static inline' */
52 #    if (__GNUC__ == 4 && __GNUC_MINOR__ <= 2) || __GNUC__ <= 3
53 #      define L4_INLINE L4_INLINE_STATIC
54 #    else
55 #      ifdef __GNUC_STDC_INLINE__
56 #        define L4_INLINE L4_INLINE_STATIC
57 #      else
58 #        define L4_INLINE L4_INLINE_EXTERN
59 #      endif
60 #    endif
61 #  else /* ! __OPTIMIZE__ */
62 #    define L4_INLINE static
63 #  endif /* ! __OPTIMIZE__ */
64 #else /* __cplusplus */
65 #  define L4_INLINE inline
66 #endif  /* __cplusplus */
67 #endif  /* L4_INLINE */
68
69
70 /**
71  * \brief Handcoded version of __attribute__((constructor(xx))).
72  * \param func function declaration (prototype)
73  * \param prio the prio must be 65535 - \a gcc_prio
74  */
75 #ifdef __ARM_EABI__
76 #  define L4_DECLARE_CONSTRUCTOR(func, prio) \
77     static void (* func ## _ctor__)(void) __attribute__((used,section(".init_array." L4_stringify(prio)))) = &func;
78 #else
79 #  define L4_DECLARE_CONSTRUCTOR(func, prio) \
80     static void (* func ## _ctor__)(void) __attribute__((used,section(".ctors." L4_stringify(prio)))) = &func;
81 #endif
82
83
84
85 /**
86  * Start section with C types and functions.
87  * \def     __BEGIN_DECLS
88  * \hideinitializer
89  */
90 /**
91  * End section with C types and functions.
92  * \def     __END_DECLS
93  * \hideinitializer
94  */
95 /**
96  * Start section with C types and functions.
97  * \def     EXTERN_C_BEGIN
98  * \hideinitializer
99  */
100 /**
101  * End section with C types and functions.
102  * \def     EXTERN_C_END
103  * \hideinitializer
104  */
105 /**
106  * Mark C types and functions.
107  * \def     EXTERN_C
108  * \hideinitializer
109  */
110
111 /**
112  * \def L4_NOTHROW
113  * \hideinitializer
114  * \brief Mark a function declaration and definition as never
115  *        throwing an exception. (Also for C code).
116  *
117  * This macro shall be used to mark C and C++ functions that never
118  * throw any exception.  Note that also C functions may throw exceptions
119  * according to the compilers ABI and shall be marke with L4_NOTHROW
120  * if they never do.  In C++ this is equvalent to \c throw().
121  *
122  * \code
123  * int foo() L4_NOTHROW;
124  * ...
125  * int foo() L4_NOTHROW
126  * {
127  *   ...
128  *   return result;
129  * }
130  * \endcode
131  *
132  */
133
134 /**
135  * \def L4_EXPORT
136  * \hideinitializer
137  * \brief Attribute to mark functions, variables, and data types as being
138  *        exported from a library.
139  *
140  * All data types, functions, and global variables that shall be exported
141  * from a library shall be marked with this attribute.  The default may become
142  * to hide everything that is not marked as L4_EXPORT from the users of a
143  * library and provide the possibility for aggressive optimization of all
144  * those internal functionality of a library.
145  *
146  * Usage:
147  * \code
148  * class L4_EXPORT My_class
149  * {
150  *   ...
151  * };
152  *
153  * int L4_EXPORT function(void);
154  *
155  * int L4_EXPORT global_data; // global data is not recommended
156  * \endcode
157  *
158  */
159
160 /**
161  * \def L4_HIDDEN
162  * \hideinitializer
163  * \brief Attribute to mark functions, variables, and data types as being
164  *        explicitly hidden from users of a library.
165  *
166  * This attribute is intended for functions, data, and data types that
167  * shall never be visible outside of a library.  In particular, for shared
168  * libraries this may result in much faster code within the library and short
169  * linking times.
170  *
171  * \code
172  * class L4_HIDDEN My_class
173  * {
174  *   ...
175  * };
176  *
177  * int L4_HIDDEN function(void);
178  *
179  * int L4_HIDDEN global_data; // global data is not recommended
180  * \endcode
181  */
182 #ifndef __cplusplus
183 #  define L4_NOTHROW__A       __attribute__((nothrow))
184 #  define L4_NOTHROW
185 #  define EXTERN_C_BEGIN
186 #  define EXTERN_C_END
187 #  define EXTERN_C
188 #  ifndef __BEGIN_DECLS
189 #    define __BEGIN_DECLS
190 #  endif
191 #  ifndef __END_DECLS
192 #    define __END_DECLS
193 #  endif
194 #  define L4_DEFAULT_PARAM(x)
195 #else /* __cplusplus */
196 #  define L4_NOTHROW throw()
197 #  define EXTERN_C_BEGIN extern "C" {
198 #  define EXTERN_C_END }
199 #  define EXTERN_C extern "C"
200 #  ifndef __BEGIN_DECLS
201 #    define __BEGIN_DECLS extern "C" {
202 #  endif
203 #  ifndef __END_DECLS
204 #    define __END_DECLS }
205 #  endif
206 #  define L4_DEFAULT_PARAM(x) = x
207 #endif /* __cplusplus */
208
209 /**
210  * Noreturn function attribute.
211  * \hideinitializer
212  */
213 #define L4_NORETURN __attribute__((noreturn))
214
215 /**
216  * No instrumentation function attribute.
217  * \hideinitializer
218  */
219 #define L4_NOINSTRUMENT __attribute__((no_instrument_function))
220 #ifndef L4_HIDDEN
221 #  define L4_HIDDEN __attribute__((visibility("hidden")))
222 #endif
223 #ifndef L4_EXPORT
224 #  define L4_EXPORT __attribute__((visibility("default")))
225 #endif
226 #define L4_STRONG_ALIAS(name, aliasname) L4__STRONG_ALIAS(name, aliasname)
227 #define L4__STRONG_ALIAS(name, aliasname) \
228   extern __typeof (name) aliasname __attribute__ ((alias (#name)));
229
230
231 #endif /* !__ASSEMBLY__ */
232
233 #include <l4/sys/linkage.h>
234
235 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
236 #define __builtin_expect(x, expected_value) (x)
237 #endif
238
239 #define EXPECT_TRUE(x)  __builtin_expect((x),1)   ///< Expression is likely to execute. \hideinitializer
240 #define EXPECT_FALSE(x) __builtin_expect((x),0)   ///< Expression is unlikely to execute. \hideinitializer
241
242 #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || __GNUC__ >= 4
243 /* Make sure that the function is not removed by optimization. Without the
244  * "used" attribute, unreferenced static functions are removed. */
245 #define L4_STICKY(x)    __attribute__((used)) x         ///< Mark symbol sticky (even not there) \hideinitializer
246 /* The deprecated attribute is available with 3.1 and higher (3.3 as here
247  * is ok for us */
248 #define L4_DEPRECATED   __attribute__((deprecated))     ///< Mark symbol deprecated. \hideinitializer
249 #else
250 /* The "used" attribute is not available with older gcc versions so simply
251  * make sure that gcc doesn't warn about unused functions. */
252 #define L4_STICKY(x)    __attribute__((unused)) x       ///< Mark symbol sticky (even not there).
253 #define L4_DEPRECATED                                   ///< Mark symbol deprecated
254 #endif
255
256 #ifndef __GXX_EXPERIMENTAL_CXX0X__
257 #ifndef static_assert
258 #define static_assert(x, y) \
259   do { (void)sizeof(char[-(!(x))]); } while (0)
260 #endif
261 #endif
262
263 #define L4_stringify_helper(x) #x                       ///< stringify helper. \hideinitializer
264 #define L4_stringify(x)        L4_stringify_helper(x)   ///< stringify. \hideinitializer
265
266 #ifndef __ASSEMBLER__
267 /**
268  * \brief Memory barrier.
269  */
270 L4_INLINE void l4_mb(void);
271
272 /**
273  * \brief Write memory barrier.
274  */
275 L4_INLINE void l4_wmb(void);
276
277
278 /* Implementations */
279 L4_INLINE void l4_mb(void)
280 {
281   __asm__ __volatile__ ("" : : : "memory");
282 }
283
284 L4_INLINE void l4_wmb(void)
285 {
286   __asm__ __volatile__ ("" : : : "memory");
287 }
288 #endif
289
290 /*@}*/
291
292 #endif /* !__L4_COMPILER_H__ */