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