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