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