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