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