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