Fix doxygen documentation.
[bertos.git] / bertos / cfg / compiler.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2001, 2002, 2003 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief Additional support macros for compiler independance
35  *
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39 #ifndef BERTOS_COMPILER_H
40 #define BERTOS_COMPILER_H
41
42 #include <cpu/detect.h>
43
44
45 #if defined __GNUC__ && defined __GNUC_MINOR__
46         #define GNUC_PREREQ(maj, min) \
47                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
48 #else
49         #define GNUC_PREREQ(maj, min) 0
50 #endif
51
52 /* Some CW versions do not allow enabling C99 from the settings panel. */
53 #if defined(__MWERKS__)
54         #pragma c99 on
55 #endif
56
57 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
58         #define COMPILER_C99      1
59 #else
60         #define COMPILER_C99      0
61 #endif
62
63
64 /** Concatenate two different preprocessor tokens (allowing macros to expand) */
65 #define PP_CAT(x,y)         PP_CAT__(x,y)
66 #define PP_CAT__(x,y)       x ## y
67 #define PP_CAT3(x,y,z)      PP_CAT(PP_CAT(x,y),z)
68 #define PP_CAT4(x,y,z,w)    PP_CAT(PP_CAT3(x,y,z),w)
69 #define PP_CAT5(x,y,z,w,j)  PP_CAT(PP_CAT4(x,y,z,w),j)
70
71 /** String-ize a token (allowing macros to expand) */
72 #define PP_STRINGIZE(x)     PP_STRINGIZE__(x)
73 #define PP_STRINGIZE__(x)   #x
74
75
76 /**
77  */
78 #if COMPILER_C99
79         #define COUNT_PARMS2(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _, ...) _
80         #define COUNT_PARMS(...) \
81                         COUNT_PARMS2(11 , ## __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
82
83         /**
84          * usage:
85          * \code
86          * #define foo_init(...) PP_CAT(foo_init_, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
87          * \endcode
88          */
89
90 #else
91         #define COUNT_PARMS2(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _, ...) _
92         #define COUNT_PARMS(args...) \
93                         COUNT_PARMS2(11 , ## args, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
94
95         /**
96          * usage:
97          * \code
98          * #define foo_init(args...) PP_CAT(foo_init_, COUNT_PARMS(args)) (args)
99          * \endcode
100          */
101
102 #endif
103
104 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
105
106         #pragma language=extended
107
108         #if CPU_ARM
109
110                 #define COMPILER_VARIADIC_MACROS 1
111
112                 #define INTERRUPT(x)  __irq __arm void x (void)
113                 #define INLINE        static inline
114
115                 /* Include some standard C89/C99 stuff */
116                 #include <stddef.h>
117                 #include <stdint.h>
118                 #include <stdbool.h>
119
120         #elif CPU_I196
121
122                 // IAR has size_t as built-in type, but does not define this symbol.
123                 #define _SIZE_T_DEFINED
124
125                 #define INTERRUPT(x)  interrupt [x]
126                 #define REGISTER      shortad
127                 #define INLINE        /* unsupported */
128
129                 /*
130                  * Imported from <longjmp.h>. Unfortunately, we can't just include
131                  * this header because it typedefs jmp_buf to be an array of chars.
132                  * This would allow the compiler to place the buffer on an odd address.
133                  * The CPU _should_ be able to perform word accesses to
134                  * unaligned data, but there are *BUGS* in the 80196KC with
135                  * some combinations of opcodes and addressing modes. One of
136                  * these, "ST SP,[?GR]+" is used in the longjmp() implementation
137                  * provided by the IAR compiler ANSI C library. When ?GR contains
138                  * an odd address, surprisingly the CPU will copy the high order
139                  * byte of the source operand (SP) in the low order byte of the
140                  * destination operand (the memory location pointed to by ?GR).
141                  *
142                  * We also need to replace the library setjmp()/longjmp() with
143                  * our own versions because the IAR implementation "forgets" to
144                  * save the contents of local registers (?LR).
145                  */
146                 struct _JMP_BUF
147                 {
148                         void *sp;           /* Stack pointer */
149                         void *return_addr;  /* Return address */
150                         int lr[6];          /* 6 local registers */
151                 };
152
153                 typedef struct _JMP_BUF jmp_buf[1];
154
155                 int setjmp(jmp_buf env);
156                 void longjmp(jmp_buf env, int val);
157
158                 /* Fake bool support */
159                 #define true (1==1)
160                 #define false (1!=1)
161                 typedef unsigned char bool;
162
163         #else
164                 #error Unsupported CPU
165         #endif
166
167 #elif defined(_MSC_VER) /* Win32 emulation support */
168
169         /* MSVC doesn't provide <stdbool.h>. */
170         #ifndef __cplusplus
171                 #define true (1==1)
172                 #define false (1!=1)
173                 typedef int bool;
174         #endif /* !__cplusplus */
175
176         /* These C99 functions are oddly named in MSVCRT32.lib */
177         #define snprintf _snprintf
178         #define vsnprintf _vsnprintf
179
180         /* MSVC doesn't support C99's __func__, but has a similar extension. */
181         #define __func__ __FUNCTION__
182
183         /* MSVC doesn't support C99's inline keyword */
184         #ifndef __cplusplus
185                 #define INLINE __inline
186         #endif
187
188 #elif defined(__GNUC__)
189
190         /* Compiler features */
191         #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
192         #define COMPILER_TYPEOF 1
193         #define COMPILER_STATEMENT_EXPRESSIONS 1
194
195         /* GCC attributes */
196         #define FORMAT(type,fmt,first)  __attribute__((__format__(type, fmt, first)))
197         #define NORETURN                __attribute__((__noreturn__))
198         #define UNUSED_ARG(type,arg)    __attribute__((__unused__)) type arg
199         #define UNUSED_VAR(type,name)   __attribute__((__unused__)) type name
200         #define USED_VAR(type,name)     __attribute__((__used__)) type name
201         #define INLINE                  static inline __attribute__((__always_inline__))
202         #define NOINLINE                __attribute__((noinline))
203         #define LIKELY(x)               __builtin_expect(!!(x), 1)
204         #define UNLIKELY(x)             __builtin_expect(!!(x), 0)
205         #define PURE_FUNC               __attribute__((pure))
206         #define CONST_FUNC              __attribute__((const))
207         #define UNUSED_FUNC             __attribute__((unused))
208         #define USED_FUNC               __attribute__((__used__))
209         #define RESTRICT                __restrict__
210         #define MUST_CHECK              __attribute__((warn_unused_result))
211         #define PACKED                  __attribute__((packed))
212         #if CPU_ARM | CPU_CM3
213                 #define NAKED           __attribute__((naked))
214         #else
215                 #define NAKED
216         #endif
217
218         /**
219          * Force compiler to realod context variable.
220          */
221         #define MEMORY_BARRIER           asm volatile ("" : : : "memory")
222
223         #if GNUC_PREREQ(3,1)
224                 #define DEPRECATED  __attribute__((__deprecated__))
225         #endif
226
227         #if GNUC_PREREQ(4,5)
228                 #define UNREACHABLE() __builtin_unreachable()
229         #endif
230
231         #ifndef __cplusplus
232                 #define ASSERT_TYPE_EQUAL(var1, var2) \
233                         STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2)))
234                 #define ASSERT_TYPE_IS(var, type) \
235                         STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type))
236         #endif
237
238         /* Include some standard C89/C99 stuff */
239         #include <stddef.h>
240         #include <stdint.h>
241         #include <stdbool.h>
242         #if !CPU_AVR
243         #include <sys/types.h> /* for ssize_t */
244         #endif
245
246         #ifndef __cplusplus
247                 /*
248                  * Disallow some C++ keywords as identifiers in C programs,
249                  * for improved portability.
250                  */
251                 #pragma GCC poison new delete class template typename
252                 #pragma GCC poison private protected public operator
253                 #pragma GCC poison friend mutable using namespace
254                 #pragma GCC poison cin cout cerr clog
255         #endif
256
257
258
259 #elif defined(__MWERKS__)
260
261         /* Compiler features */
262         #define COMPILER_VARIADIC_MACROS 1
263         #define COMPILER_TYPEOF 1
264         #define COMPILER_STATEMENT_EXPRESSIONS 1
265
266         #define typeof __typeof__
267
268         #define UNUSED_ARG(type,arg)    type
269
270         #include <stddef.h>
271         #include <stdint.h>
272         #include <stdbool.h>
273
274         // CodeWarrior has size_t as built-in type, but does not define this symbol.
275         #define _SIZE_T_DEFINED
276
277 #else
278         #error unknown compiler
279 #endif
280
281
282 /* Defaults for compiler extensions. */
283
284 /**
285  * \def COMPILER_VARIADIC_MACROS
286  * Support for macros with variable arguments.
287  */
288 #ifndef COMPILER_VARIADIC_MACROS
289 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
290 #endif
291
292 /**
293  * \def COMPILER_TYPEOF
294  * Support for dynamic type identification.
295  */
296 #ifndef COMPILER_TYPEOF
297 #define COMPILER_TYPEOF 0
298 #endif
299
300 /**
301  * \def COMPILER_STATEMENT_EXPRESSIONS
302  * Support for statement expressions.
303  */
304 #ifndef COMPILER_STATEMENT_EXPRESSIONS
305 #define COMPILER_STATEMENT_EXPRESSIONS 0
306 #endif
307
308 /* A few defaults for missing compiler features. */
309 #ifndef INLINE
310 #define INLINE                 static inline
311 #endif
312 #ifndef NOINLINE
313 #define NOINLINE               /* nothing */
314 #endif
315 #ifndef NORETURN
316 #define NORETURN               /* nothing */
317 #endif
318 #ifndef FORMAT
319 #define FORMAT(type,fmt,first) /* nothing */
320 #endif
321 #ifndef DEPRECATED
322 #define DEPRECATED             /* nothing */
323 #endif
324 #ifndef UNUSED_ARG
325 #define UNUSED_ARG(type,arg)   type arg
326 #endif
327 #ifndef UNUSED_VAR
328 #define UNUSED_VAR(type,name)  type name
329 #endif
330 #ifndef USED_VAR
331 #define USED_VAR(type,name)    type name
332 #endif
333 #ifndef REGISTER
334 #define REGISTER               /* nothing */
335 #endif
336 #ifndef LIKELY
337 #define LIKELY(x)              x
338 #endif
339 #ifndef UNLIKELY
340 #define UNLIKELY(x)            x
341 #endif
342 #ifndef PURE_FUNC
343 #define PURE_FUNC              /* nothing */
344 #endif
345 #ifndef CONST_FUNC
346 #define CONST_FUNC             /* nothing */
347 #endif
348 #ifndef UNUSED_FUNC
349 #define UNUSED_FUNC            /* nothing */
350 #endif
351 #ifndef USED_FUNC
352 #define USED_FUNC              /* nothing */
353 #endif
354 #ifndef RESTRICT
355 #define RESTRICT               /* nothing */
356 #endif
357 #ifndef MUST_CHECK
358 #define MUST_CHECK             /* nothing */
359 #endif
360 #ifndef PACKED
361 #define PACKED                 /* nothing */
362 #endif
363 #ifndef MEMORY_BARRIER
364 #define MEMORY_BARRIER         /* nothing */
365 #warning No memory barrier defined for select compiler. If you use the kernel check it.
366 #endif
367 #ifndef UNREACHABLE
368 #define UNREACHABLE() for (;;)
369 #endif
370
371
372 /* Misc definitions */
373 #ifndef NULL
374 #define NULL  (void *)0
375 #endif
376 #ifndef EOF
377 #define EOF   (-1)
378 #endif
379
380 /* Support for hybrid C/C++ applications. */
381 #ifdef __cplusplus
382         #define EXTERN_C        extern "C"
383         #define EXTERN_C_BEGIN  extern "C" {
384         #define EXTERN_C_END    }
385         #define EXTERN_CONST    extern const
386         #define CONST_CAST(TYPE,EXPR)   (const_cast<TYPE>(EXPR))
387 #else
388         #define EXTERN_C        extern
389         #define EXTERN_C_BEGIN  /* nothing */
390         #define EXTERN_C_END    /* nothing */
391         #define EXTERN_CONST    const
392         #define CONST_CAST(TYPE,EXPR)   ((TYPE)(EXPR)) /* FIXME: How can we suppress the warning in C? */
393 #endif
394
395
396 #if defined(_MSC_VER) \
397         || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
398         /**
399          * \name ISO C99 fixed-size types
400          *
401          * These should be in <stdint.h>, but a few compilers lack them.
402          * \{
403          */
404         typedef signed char         int8_t;
405         typedef unsigned char       uint8_t;
406         typedef short int           int16_t;
407         typedef unsigned short int  uint16_t;
408         typedef long int            int32_t; /* _WIN64 safe */
409         typedef unsigned long int   uint32_t; /* _WIN64 safe */
410
411         #ifdef _MSC_VER
412                 typedef __int64              int64_t;
413                 typedef unsigned __int64     uint64_t;
414         #else
415                 typedef long long            int64_t;
416                 typedef unsigned long long   uint64_t;
417         #endif
418         /* \} */
419 #else
420         /* This is the standard location. */
421         #include <stdint.h>
422 #endif
423
424 #if CPU_AVR_ATMEGA8
425         /*
426          * The ATmega8 has a very small Flash, so we can't afford
427          * to link in support routines for 32bit integer arithmetic.
428          */
429         typedef int16_t ticks_t;  /**< Type for time expressed in ticks. */
430         typedef int16_t mtime_t;  /**< Type for time expressed in milliseconds. */
431         typedef int16_t utime_t;  /**< Type for time expressed in microseconds. */
432         #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
433         #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
434         #define MTIME_INFINITE 0x7FFFL
435 #else
436         typedef int32_t ticks_t;  /**< Type for time expressed in ticks. */
437
438         typedef int32_t utime_t;  /**< Type for time expressed in microseconds. */
439         #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
440
441         #ifndef DEVLIB_MTIME_DEFINED
442                 #define DEVLIB_MTIME_DEFINED 1 /* Resolve conflict with <os/hptime.h> */
443                 typedef int32_t mtime_t;  /**< Type for time expressed in milliseconds. */
444                 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
445                 #define MTIME_INFINITE 0x7FFFFFFFL
446         #endif
447 #endif
448
449 /** Bulk storage large enough for both pointers or integers. */
450 typedef void * iptr_t;
451
452 /** Bulk storage large enough for both pointers to constants or integers. */
453 typedef const void * const_iptr_t;
454
455 typedef unsigned char sigbit_t;  /**< Type for signal bits. */
456 typedef unsigned char sigmask_t; /**< Type for signal masks. */
457
458
459 /**
460  * \name Standard type definitions.
461  *
462  * These should be in <sys/types.h> or <stddef.h>, but many compilers
463  * and C libraries lack them.
464  *
465  * We check for some common definitions to avoid redefinitions:
466  *
467  *    glibc, avr-libc: _SIZE_T_DEFINED, __ssize_t_defined
468  *    Darwin libc:     _BSD_SIZE_T_DEFINED_, _SIZE_T
469  *    IAR ARM:         _SIZE_T
470  *
471  * \{
472  */
473 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
474         || defined(_SIZE_T))
475         #if CPU_X86
476                 /* 32bit or 64bit (32bit for _WIN64). */
477                 typedef unsigned long size_t;
478         #else
479                 #error Unknown CPU
480         #endif
481 #endif
482
483 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
484         #if CPU_X86
485                 /* 32bit or 64bit (32bit for _WIN64). */
486                 typedef long ssize_t;
487         #elif CPU_ARM || CPU_CM3
488                 typedef int ssize_t;
489         #elif CPU_AVR
490                 /* 16bit (missing in avr-libc's sys/types.h). */
491                 typedef int ssize_t;
492         #else
493                 #error Unknown CPU
494         #endif
495 #endif
496 /*\}*/
497
498
499 /**
500  * \name Types for hardware registers.
501  *
502  * Only use these types for registers whose contents can
503  * be changed asynchronously by external hardware.
504  *
505  * \{
506  */
507 #if CPU_DSP56K
508         /* Registers can be accessed only through 16-bit pointers */
509         typedef volatile uint16_t  reg16_t;
510 #else
511         typedef volatile uint8_t   reg8_t;
512         typedef volatile uint16_t  reg16_t;
513         typedef volatile uint32_t  reg32_t;
514 #endif
515 /*\}*/
516
517
518 /* Quasi-ANSI macros */
519 #ifndef offsetof
520         /**
521          * Return the byte offset of the member \a m in struct \a s.
522          *
523          * \note This macro should be defined in "stddef.h" and is sometimes
524          *       compiler-specific (g++ has a builtin for it).
525          */
526         #define offsetof(s,m)  (size_t)&(((s *)0)->m)
527 #endif
528 #ifndef countof
529         /**
530          * Count the number of elements in the static array \a a.
531          *
532          * \note This macro is non-standard, but implements a very common idiom
533          */
534         #define countof(a)  (sizeof(a) / sizeof(*(a)))
535 #endif
536 #ifndef alignof
537         /**
538          * Return the alignment in memory of a generic data type.
539          *
540          * \note We need to worry about alignment when allocating memory that
541          * will be used later by unknown objects (e.g., malloc()) or, more
542          * generally, whenever creating generic container types.
543          */
544         #define alignof(type) offsetof(struct { char c; type member; }, member)
545 #endif
546
547 /**
548  * Cast a member of a structure out to the containing structure.
549  *
550  * \param ptr     the pointer to the member.
551  * \param type    the type of the container struct this is embedded in.
552  * \param member  the name of the member within the struct.
553  */
554 #if COMPILER_TYPEOF && COMPILER_STATEMENT_EXPRESSIONS
555         #define containerof(ptr, type, member) ({ \
556                 typeof( ((type *)0)->member ) *_mptr = (ptr); /* type check */ \
557                 (type *)(void *)((char *)_mptr - offsetof(type, member)); \
558         })
559 #else
560         #define containerof(ptr, type, member) \
561                 ( (type *)(void *)((char *)(ptr) - offsetof(type, member)) )
562 #endif
563
564 /** Issue a compilation error if the \a condition is false */
565 #define STATIC_ASSERT(condition)  \
566         UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
567
568 #ifndef ASSERT_TYPE_EQUAL
569         /** Ensure two variables have the same type. */
570         #define ASSERT_TYPE_EQUAL(var1, var2)  \
571                         do { (void)(&(var1) == &(var2)); } while(0)
572 #endif
573
574 #ifndef ASSERT_TYPE_IS
575         /** Ensure variable is of specified type. */
576         #define ASSERT_TYPE_IS(var, type)  \
577                         do { (void)(&(var) == (type *)0); } while(0)
578 #endif
579
580 #endif /* BERTOS_COMPILER_H */