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