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