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