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