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