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