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