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