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