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