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