Update to Doxygen 1.4.1.
[bertos.git] / compiler.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 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 devlib/README 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.41  2005/01/22 04:19:22  bernie
18  *#* MTIME_INFINITE: New constant.
19  *#*
20  *#* Revision 1.40  2005/01/20 18:46:04  aleph
21  *#* Add progmem datatypes; PSTR() definition.
22  *#*
23  *#* Revision 1.39  2005/01/08 08:49:39  bernie
24  *#* Define PROGMEM on AVR only when not including pgmspace.h.
25  *#*
26  *#* Revision 1.38  2004/12/31 16:41:52  bernie
27  *#* PROGMEM: Define to nothing for non-Harvard processors.
28  *#*
29  *#* Revision 1.37  2004/12/08 09:43:21  bernie
30  *#* Metrowerks supports variadic macros.
31  *#*
32  *#* Revision 1.36  2004/12/08 08:55:54  bernie
33  *#* Rename sigset_t to sigmask_t and time_t to mtime_t, to avoid conflicts with POSIX definitions.
34  *#*
35  *#* Revision 1.35  2004/12/08 07:35:51  bernie
36  *#* Typo in macro name.
37  *#*
38  *#* Revision 1.34  2004/11/28 23:21:33  bernie
39  *#* Use mtime_t instead of overloading ANSI time_t with new semantics.
40  *#*
41  *#* Revision 1.33  2004/11/16 23:09:40  bernie
42  *#* size_t: Add 64bit definitions; time_t: Add 16bit hack for tiny CPUs.
43  *#*
44  *#* Revision 1.32  2004/11/16 22:42:44  bernie
45  *#* Doxygen fixes.
46  *#*
47  *#* Revision 1.31  2004/11/16 22:37:28  bernie
48  *#* IPTR: Remove obsolete definition.
49  *#*
50  *#* Revision 1.30  2004/11/16 22:30:19  bernie
51  *#* Declare fixed-size types before other types.
52  *#*
53  *#* Revision 1.29  2004/11/16 20:34:40  bernie
54  *#* UNUSED_VAR, USED_VAR, USED_FUNC: New macros; UNUSED_ARG: Rename from UNUSED.
55  *#*
56  *#* Revision 1.28  2004/10/21 11:03:52  bernie
57  *#* Typo.
58  *#*
59  *#* Revision 1.27  2004/10/21 10:09:40  bernie
60  *#* Remove spurious token in preprocessor directive.
61  *#*
62  *#* Revision 1.26  2004/10/19 08:55:14  bernie
63  *#* UNUSED_FUNC: New function attribute.
64  *#*
65  *#* Revision 1.25  2004/10/19 07:14:20  bernie
66  *#* Add macros to test for specific compiler features.
67  *#*
68  *#* Revision 1.24  2004/10/03 18:35:13  bernie
69  *#* Poison C++ keywords in C programs for better portability.
70  *#*
71  *#* Revision 1.23  2004/09/20 03:30:27  bernie
72  *#* Remove vsprintf_P() proto, no longer needed with avr-libc 1.0.4.
73  *#*
74  *#* Revision 1.22  2004/09/14 21:03:04  bernie
75  *#* PURE_FUNC, CONST_FUNC, MUST_CHECK: New function attributes; LIKELY()/UNLIKELY(): Fix for non-integral expressions.
76  *#*
77  *#* Revision 1.21  2004/09/06 21:38:31  bernie
78  *#* Misc documentation and style fixes.
79  *#*
80  *#* Revision 1.20  2004/08/29 21:57:58  bernie
81  *#* Move back STATIC_ASSERT() to compiler.h as it's needed in cpu.h;
82  *#* iptr_t, const_iptr_t: Replace IPTR macro with a real typedef.
83  *#*
84  *#* Revision 1.19  2004/08/25 14:12:08  rasky
85  *#* Aggiornato il comment block dei log RCS
86  *#*
87  *#* Revision 1.18  2004/08/24 16:32:37  bernie
88  *#* Document custom types.
89  *#*
90  *#* Revision 1.17  2004/08/24 13:32:14  bernie
91  *#* PP_CAT(), PP_STRINGIZE(): Move back to compiler.h to break circular dependency between cpu.h/compiler.h/macros.h;
92  *#* offsetof(), countof(): Move back to compiler.h to avoid including macros.h almost everywhere;
93  *#* Trim CVS log;
94  *#* Rename header guards;
95  *#* Don't include arch_config.h in compiler.h as it's not needed there.
96  *#*
97  *#* Revision 1.16  2004/08/14 19:37:57  rasky
98  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
99  *#*
100  *#* Revision 1.15  2004/08/13 03:23:26  bernie
101  *#* Adjust a few MSVC tweaks from older projects.
102  *#*
103  *#* Revision 1.14  2004/08/10 06:56:29  bernie
104  *#* RESTRICT: New C99-like macro; STATIC_ASSERT: Fix warning for multiple invocation in one file.
105  *#*
106  *#* Revision 1.13  2004/08/02 20:20:29  aleph
107  *#* Merge from project_ks
108  *#*
109  *#* Revision 1.12  2004/08/01 01:21:17  bernie
110  *#* LIKELY(), UNLIKELY(): New compiler-specific macros.
111  *#*
112  *#* Revision 1.11  2004/07/30 14:34:10  rasky
113  *#* Vari fix per documentazione e commenti
114  *#* Aggiunte PP_CATn e STATIC_ASSERT
115  *#*
116  *#* Revision 1.10  2004/07/30 14:15:53  rasky
117  *#* Nuovo supporto unificato per detect della CPU
118  *#*
119  *#* Revision 1.9  2004/07/29 22:57:09  bernie
120  *#* vsprintf(): Remove prototype for backwards compatibility with GCC 3.4; ssize_t: Add definition for inferior compilers.
121  *#*/
122 #ifndef DEVLIB_COMPILER_H
123 #define DEVLIB_COMPILER_H
124
125 #include "cpu_detect.h"
126
127
128 #if defined __GNUC__ && defined __GNUC_MINOR__
129         #define GNUC_PREREQ(maj, min) \
130                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
131 #else
132         #define GNUC_PREREQ(maj, min) 0
133 #endif
134
135 /* Some CW versions do not allow enabling C99 from the settings panel. */
136 #if defined(__MWERKS__)
137         #pragma c99 on
138 #endif
139
140 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
141         #define COMPILER_C99      1
142 #else
143         #define COMPILER_C99      0
144 #endif
145
146
147 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
148 #define PP_CAT(x,y)         PP_CAT__(x,y)
149 #define PP_CAT__(x,y)       x ## y
150 #define PP_CAT3(x,y,z)      PP_CAT(PP_CAT(x,y),z)
151 #define PP_CAT4(x,y,z,w)    PP_CAT(PP_CAT3(x,y,z),w)
152 #define PP_CAT5(x,y,z,w,j)  PP_CAT(PP_CAT4(x,y,z,w),j)
153
154 /*! String-ize a token (allowing macros to expand) */
155 #define PP_STRINGIZE(x)     PP_STRINGIZE__(x)
156 #define PP_STRINGIZE__(x)   #x
157
158
159 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
160         #pragma language=extended
161         #define INTERRUPT(x)  interrupt [x]
162         #define REGISTER      shortad
163         #define INLINE        /* unsupported */
164
165         /*
166          * Imported from <longjmp.h>. Unfortunately, we can't just include
167          * this header because it typedefs jmp_buf to be an array of chars.
168          * This would allow the compiler to place the buffer on an odd address.
169          * The CPU _should_ be able to perform word accesses to
170          * unaligned data, but there are *BUGS* in the 80196KC with
171          * some combinations of opcodes and addressing modes. One of
172          * these, "ST SP,[?GR]+" is used in the longjmp() implementation
173          * provided by the IAR compiler ANSI C library. When ?GR contains
174          * an odd address, surprisingly the CPU will copy the high order
175          * byte of the source operand (SP) in the low order byte of the
176          * destination operand (the memory location pointed to by ?GR).
177          *
178          * We also need to replace the library setjmp()/longjmp() with
179          * our own versions because the IAR implementation "forgets" to
180          * save the contents of local registers (?LR).
181          */
182         struct _JMP_BUF
183         {
184                 void *sp;           /* Stack pointer */
185                 void *return_addr;  /* Return address */
186                 int lr[6];          /* 6 local registers */
187         };
188
189         typedef struct _JMP_BUF jmp_buf[1];
190
191         int setjmp(jmp_buf env);
192         void longjmp(jmp_buf env, int val);
193
194         /* Fake bool support */
195         #define true (1==1)
196         #define false (1!=1)
197         typedef unsigned char bool;
198
199 #elif defined(_MSC_VER) /* Win32 emulation support */
200
201         #include <setjmp.h>
202
203         /* FIXME: I can't remember why exactly this was needed (NdBernie) */
204         #define float double
205
206         /* MSVC doesn't provide <stdbool.h>. */
207         #ifndef __cplusplus
208                 #define true (1==1)
209                 #define false (1!=1)
210                 typedef int bool;
211         #endif /* !__cplusplus */
212
213         /* These C99 functions are oddly named in MSVCRT32.lib */
214         #define snprintf _snprintf
215         #define vsnprintf _vsnprintf
216
217 #elif defined(__GNUC__)
218
219         /* Compiler features */
220         #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
221         #define COMPILER_TYPEOF 1
222         #define COMPILER_STATEMENT_EXPRESSIONS 1
223
224         /* GCC attributes */
225         #define FORMAT(type,fmt,first)  __attribute__((__format__(type, fmt, first)))
226         #define NORETURN                __attribute__((__noreturn__))
227         #define UNUSED_ARG(type,arg)    __attribute__((__unused__)) type arg
228         #define UNUSED_VAR(type,name)   __attribute__((__unused__)) type name
229         #define USED_VAR(type,name)     __attribute__((__used__)) type name
230         #define INLINE                  static inline __attribute__((__always_inline__))
231         #define LIKELY(x)               __builtin_expect(!!(x), 1)
232         #define UNLIKELY(x)             __builtin_expect(!!(x), 0)
233         #define PURE_FUNC               __attribute__((pure))
234         #define CONST_FUNC              __attribute__((const))
235         #define UNUSED_FUNC             __attribute__((unused))
236         #define USED_FUNC               __attribute__((__used__))
237         #define RESTRICT                __restrict__
238         #define MUST_CHECK              __attribute__((warn_unused_result))
239         #if GNUC_PREREQ(3,1)
240                 #define DEPRECATED  __attribute__((__deprecated__))
241         #endif
242
243         #if CPU_X86
244
245                 #include <stddef.h>
246                 #include <setjmp.h>
247                 #include <stdbool.h>
248
249         #elif CPU_AVR
250
251                 #include <stddef.h>
252                 #include <stdbool.h>
253
254                 /* Support for harvard architectures */
255                 #ifdef _PROGMEM
256                         #include <avr/pgmspace.h>
257                         #define PGM_READ_CHAR(s) pgm_read_byte(s)
258                         #define PGM_FUNC(x) x ## _P
259                         #define PGM_ATTR  PROGMEM
260                 #else
261                         /* We still need this for prototypes */
262                         #define PROGMEM  __attribute__((__progmem__))
263                         #define PSTR(s) ({static char __c[] PROGMEM = (s); __c;})
264                 #endif
265
266         #endif
267
268         #ifndef __cplusplus
269                 /*
270                  * Disallow some C++ keywords as identifiers in C programs,
271                  * for improved portability.
272                  */
273                 #pragma GCC poison new delete class template typename
274                 #pragma GCC poison private protected public operator
275                 #pragma GCC poison friend mutable using namespace
276                 #pragma GCC poison cin cout cerr clog
277         #endif
278
279 #elif defined(__MWERKS__) && CPU_DSP56K
280
281         /* Compiler features */
282         #define COMPILER_VARIADIC_MACROS 1
283         #define COMPILER_TYPEOF 1
284         #define COMPILER_STATEMENT_EXPRESSIONS 1
285
286         #define typeof __typeof__
287
288         #include <stdint.h>
289         #include <stddef.h>
290         #include <stdbool.h>
291         #include <setjmp.h>
292
293         // CodeWarrior has size_t as built-in type, but does not define this symbol.
294         #define _SIZE_T_DEFINED
295
296 #else
297         #error unknown compiler
298 #endif
299
300
301 /* Defaults for compiler extensions. */
302
303 /*!
304  * \def COMPILER_VARIADIC_MACROS
305  * Support for macros with variable arguments.
306  */
307 #ifndef COMPILER_VARIADIC_MACROS
308 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
309 #endif
310
311 /*!
312  * \def COMPILER_TYPEOF
313  * Support for macros with variable arguments.
314  */
315 #ifndef COMPILER_TYPEOF
316 #define COMPILER_TYPEOF 0
317 #endif
318
319 /*!
320  * \def COMPILER_STATEMENT_EXPRESSIONS
321  * Support for macros with variable arguments.
322  */
323 #ifndef COMPILER_STATEMENT_EXPRESSIONS
324 #define COMPILER_STATEMENT_EXPRESSIONS 0
325 #endif
326
327 /* A few defaults for missing compiler features. */
328 #ifndef INLINE
329 #define INLINE                 static inline
330 #endif
331 #ifndef NORETURN
332 #define NORETURN               /* nothing */
333 #endif
334 #ifndef FORMAT
335 #define FORMAT(type,fmt,first) /* nothing */
336 #endif
337 #ifndef DEPRECATED
338 #define DEPRECATED             /* nothing */
339 #endif
340 #ifndef UNUSED_ARG
341 #define UNUSED_ARG(type,arg)   type arg
342 #endif
343 #define UNUSED                 UNUSED_ARG /* OBSOLETE */
344 #ifndef UNUSED_VAR
345 #define UNUSED_VAR(type,name)  type name
346 #endif
347 #ifndef USED_VAR
348 #define USED_VAR(type,name)    type name
349 #endif
350 #ifndef REGISTER
351 #define REGISTER               /* nothing */
352 #endif
353 #ifndef PROGMEM
354 #define PROGMEM                /* nothing */
355 #endif
356 #ifndef INTERRUPT
357 #define INTERRUPT(x)           ERROR_NOT_IMPLEMENTED
358 #endif
359 #ifndef LIKELY
360 #define LIKELY(x)              x
361 #endif
362 #ifndef UNLIKELY
363 #define UNLIKELY(x)            x
364 #endif
365 #ifndef PURE_FUNC
366 #define PURE_FUNC              /* nothing */
367 #endif
368 #ifndef CONST_FUNC
369 #define CONST_FUNC             /* nothing */
370 #endif
371 #ifndef UNUSED_FUNC
372 #define UNUSED_FUNC            /* nothing */
373 #endif
374 #ifndef USED_FUNC
375 #define USED_FUNC              /* nothing */
376 #endif
377 #ifndef RESTRICT
378 #define RESTRICT               /* nothing */
379 #endif
380 #ifndef MUST_CHECK
381 #define MUST_CHECK             /* nothing */
382 #endif
383
384 /* Support for harvard architectures */
385 #ifndef PSTR
386 #define PSTR            /* nothing */
387 #endif
388 #ifndef PGM_READ_CHAR
389 #define PGM_READ_CHAR(s) (*(s))
390 #endif
391 #ifndef PGM_FUNC
392 #define PGM_FUNC(x) x
393 #endif
394 #ifndef PGM_ATTR
395 #define PGM_ATTR        /* nothing */
396 #endif
397
398 /* Misc definitions */
399 #ifndef NULL
400 #define NULL  (void *)0
401 #endif
402 #ifndef EOF
403 #define EOF   (-1)
404 #endif
405
406
407 /* Support for hybrid C/C++ applications. */
408 #ifdef __cplusplus
409         #define EXTERN_C        extern "C"
410         #define EXTERN_C_BEGIN  extern "C" {
411         #define EXTERN_C_END    }
412 #else
413         #define EXTERN_C        extern
414         #define EXTERN_C_BEGIN  /* nothing */
415         #define EXTERN_C_END    /* nothing */
416 #endif
417
418
419 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
420         /*!
421          * \name ISO C99 fixed-size types
422          *
423          * These should be in <stdint.h>, but many compilers lack them.
424          * \{
425          */
426         typedef signed char         int8_t;
427         typedef short int           int16_t;
428         typedef long int            int32_t;
429         typedef unsigned char       uint8_t;
430         typedef unsigned short int  uint16_t;
431         typedef unsigned long int   uint32_t;
432         /* \} */
433 #elif defined(__GNUC__) && CPU_AVR
434         /* avr-libc is weird... */
435         #include <inttypes.h>
436 #else
437         /* This is the correct location. */
438         #include <stdint.h>
439 #endif
440
441 /*!
442  * \name Types for variables stored in program memory (harvard processors).
443  * \{
444  */
445 typedef PROGMEM int8_t pgm_int8_t;
446 typedef PROGMEM uint8_t pgm_uint8_t;
447 typedef PROGMEM int16_t pgm_int16_t;
448 typedef PROGMEM uint16_t pgm_uint16_t;
449 typedef PROGMEM int32_t pgm_int32_t;
450 typedef PROGMEM uint32_t pgm_uint32_t;
451 /*\}*/
452
453 #if CPU_AVR_ATMEGA8
454         /*
455          * The ATmega8 has a very small Flash, so we can't afford
456          * to link in support routines for 32bit integer arithmetic.
457          */
458         typedef int16_t mtime_t;  /*!< Type for time expressed in milliseconds. */
459         typedef int16_t utime_t;  /*!< Type for time expressed in microseconds. */
460         #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
461         #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
462         #define MTIME_INFINITE 0x7FFFL
463 #else
464         typedef int32_t mtime_t;  /*!< Type for time expressed in milliseconds. */
465         typedef int32_t utime_t;  /*!< Type for time expressed in microseconds. */
466         #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
467         #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
468         #define MTIME_INFINITE 0x7FFFFFFFL
469 #endif
470
471 /*! Bulk storage large enough for both pointers or integers. */
472 typedef void * iptr_t;
473 typedef const void * const_iptr_t;
474
475 typedef unsigned char sig_t;     /*!< Type for signal bits. */
476 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
477 typedef unsigned char page_t;    /*!< Type for banked memory pages. */
478
479
480 /*!
481  * \name Standard type definitions.
482  *
483  * These should be in <sys/types.h>, but many compilers lack them.
484  *
485  * \{
486  */
487 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
488         #if CPU_REG_BITS > 32
489                 /* 64bit. */
490                 typedef unsigned long size_t;
491                 typedef long ssize_t;
492         #else
493                 /* 32bit or 16bit. */
494                 typedef unsigned int size_t;
495                 typedef int ssize_t;
496         #endif
497 #endif
498
499 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined) || defined(_EMUL))
500         /*! For backwards compatibility.  Use mtime_t in new code. */
501         #define time_t mtime_t
502         #define SIZEOF_TIME_T SIZEOF_MTIME_T
503 #else
504         /* Just a guess, but quite safe. */
505         #define SIZEOF_TIME_T SIZEOF_LONG
506 #endif /* _TIME_T_DEFINED || __time_t_defined */
507 /*\}*/
508
509
510 /*!
511  * \name Types for hardware registers.
512  *
513  * Only use these types for registers whose contents can
514  * be changed asynchronously by external hardware.
515  *
516  * \{
517  */
518 #if CPU_DSP56K
519         /* Registers can be accessed only through 16-bit pointers */
520         typedef volatile uint16_t  reg16_t;
521 #else
522         typedef volatile uint8_t   reg8_t;
523         typedef volatile uint16_t  reg16_t;
524         typedef volatile uint32_t  reg32_t;
525 #endif
526 /*\}*/
527
528
529 /* Quasi-ANSI macros */
530 #ifndef offsetof
531         /*!
532          * Return the byte offset of the member \a m in struct \a s.
533          *
534          * \note This macro should be defined in "stddef.h" and is sometimes
535          *       compiler-specific (g++ has a builtin for it).
536          */
537         #define offsetof(s,m)  (size_t)&(((s *)0)->m)
538 #endif
539 #ifndef countof
540         /*!
541          * Count the number of elements in the static array \a a.
542          *
543          * \note This macro is non-standard, but implements a very common idiom
544          */
545         #define countof(a)  (sizeof(a) / sizeof(*(a)))
546 #endif
547
548 /*! Issue a compilation error if the \a condition is false */
549 #define STATIC_ASSERT(condition)  \
550         UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
551
552 #endif /* DEVLIB_COMPILER_H */