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