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