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.
9 * \brief Additional support macros for compiler independance
12 * \author Bernardo Innocenti <bernie@develer.com>
17 *#* Revision 1.17 2006/03/22 09:48:35 bernie
20 *#* Revision 1.16 2006/03/20 17:48:09 bernie
21 *#* Fix for avr-libc; INTERRUPT(): Remove macro.
23 *#* Revision 1.15 2006/03/13 02:06:25 bernie
24 *#* containerof(): New macro.
26 *#* Revision 1.14 2006/02/23 08:36:33 bernie
27 *#* Emulate __func__ on MSVC.
29 *#* Revision 1.13 2006/02/23 07:37:37 bernie
30 *#* Compile fix for MSVC.
32 *#* Revision 1.12 2006/02/21 16:06:31 bernie
33 *#* Fix ssize_t redefinitions on glibc systems.
35 *#* Revision 1.11 2006/02/20 14:34:58 bernie
36 *#* Use portable type checking.
38 *#* Revision 1.10 2006/02/15 09:12:01 bernie
39 *#* Fixes for ARM/IAR support.
41 *#* Revision 1.9 2006/02/10 12:38:00 bernie
42 *#* Add support for ARM on IAR.
44 *#* Revision 1.8 2006/01/16 03:27:49 bernie
45 *#* Rename sig_t to sigbit_t to avoid clash with POSIX.
47 *#* Revision 1.7 2005/11/27 23:33:05 bernie
48 *#* Drop avr-libc hack for missing stdint.h.
50 *#* Revision 1.6 2005/07/19 07:27:31 bernie
51 *#* Don't use CPU_REG_BITS from cpu.h to avoid circular header dependendy.
53 *#* Revision 1.5 2005/06/27 21:24:37 bernie
54 *#* ticks_t: New typedef.
56 *#* Revision 1.4 2005/06/14 06:15:10 bernie
57 *#* Add X86_64 support.
59 *#* Revision 1.3 2005/04/12 01:37:01 bernie
60 *#* Metrowerks touchups from HeCo.
62 *#* Revision 1.2 2005/04/11 19:10:27 bernie
63 *#* Include top-level headers from cfg/ subdir.
65 *#* Revision 1.1 2005/04/11 19:04:13 bernie
66 *#* Move top-level headers to cfg/ subdir.
68 *#* Revision 1.44 2005/03/29 06:39:59 bernie
69 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
71 *#* Revision 1.43 2005/03/01 23:15:12 bernie
72 *#* Remove compatibility hack.
74 *#* Revision 1.42 2005/02/16 20:28:46 bernie
75 *#* Move PGM macros to mware/pgm.h
77 *#* Revision 1.41 2005/01/22 04:19:22 bernie
78 *#* MTIME_INFINITE: New constant.
80 *#* Revision 1.40 2005/01/20 18:46:04 aleph
81 *#* Add progmem datatypes; PSTR() definition.
83 #ifndef DEVLIB_COMPILER_H
84 #define DEVLIB_COMPILER_H
86 #include <cfg/cpu_detect.h>
89 #if defined __GNUC__ && defined __GNUC_MINOR__
90 #define GNUC_PREREQ(maj, min) \
91 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
93 #define GNUC_PREREQ(maj, min) 0
96 /* Some CW versions do not allow enabling C99 from the settings panel. */
97 #if defined(__MWERKS__)
101 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
102 #define COMPILER_C99 1
104 #define COMPILER_C99 0
108 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
109 #define PP_CAT(x,y) PP_CAT__(x,y)
110 #define PP_CAT__(x,y) x ## y
111 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
112 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
113 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
115 /*! String-ize a token (allowing macros to expand) */
116 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
117 #define PP_STRINGIZE__(x) #x
120 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
122 #pragma language=extended
126 #define COMPILER_VARIADIC_MACROS 1
128 #define INTERRUPT(x) __irq __arm void x (void)
129 #define INLINE static inline
131 /* Include some standard C89/C99 stuff */
138 // IAR has size_t as built-in type, but does not define this symbol.
139 #define _SIZE_T_DEFINED
141 #define INTERRUPT(x) interrupt [x]
142 #define REGISTER shortad
143 #define INLINE /* unsupported */
146 * Imported from <longjmp.h>. Unfortunately, we can't just include
147 * this header because it typedefs jmp_buf to be an array of chars.
148 * This would allow the compiler to place the buffer on an odd address.
149 * The CPU _should_ be able to perform word accesses to
150 * unaligned data, but there are *BUGS* in the 80196KC with
151 * some combinations of opcodes and addressing modes. One of
152 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
153 * provided by the IAR compiler ANSI C library. When ?GR contains
154 * an odd address, surprisingly the CPU will copy the high order
155 * byte of the source operand (SP) in the low order byte of the
156 * destination operand (the memory location pointed to by ?GR).
158 * We also need to replace the library setjmp()/longjmp() with
159 * our own versions because the IAR implementation "forgets" to
160 * save the contents of local registers (?LR).
164 void *sp; /* Stack pointer */
165 void *return_addr; /* Return address */
166 int lr[6]; /* 6 local registers */
169 typedef struct _JMP_BUF jmp_buf[1];
171 int setjmp(jmp_buf env);
172 void longjmp(jmp_buf env, int val);
174 /* Fake bool support */
177 typedef unsigned char bool;
180 #error Unsupported CPU
183 #elif defined(_MSC_VER) /* Win32 emulation support */
185 /* MSVC doesn't provide <stdbool.h>. */
190 #endif /* !__cplusplus */
192 /* These C99 functions are oddly named in MSVCRT32.lib */
193 #define snprintf _snprintf
194 #define vsnprintf _vsnprintf
196 /* MSVC doesn't support C99's __func__, but has a similar extension. */
197 #define __func__ __FUNCTION__
199 #elif defined(__GNUC__)
201 /* Compiler features */
202 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
203 #define COMPILER_TYPEOF 1
204 #define COMPILER_STATEMENT_EXPRESSIONS 1
207 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
208 #define NORETURN __attribute__((__noreturn__))
209 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
210 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
211 #define USED_VAR(type,name) __attribute__((__used__)) type name
212 #define INLINE static inline __attribute__((__always_inline__))
213 #define LIKELY(x) __builtin_expect(!!(x), 1)
214 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
215 #define PURE_FUNC __attribute__((pure))
216 #define CONST_FUNC __attribute__((const))
217 #define UNUSED_FUNC __attribute__((unused))
218 #define USED_FUNC __attribute__((__used__))
219 #define RESTRICT __restrict__
220 #define MUST_CHECK __attribute__((warn_unused_result))
222 #define DEPRECATED __attribute__((__deprecated__))
226 #define ASSERT_TYPE_EQUAL(var1, var2) \
227 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2)))
228 #define ASSERT_TYPE_IS(var, type) \
229 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type))
232 /* Include some standard C89/C99 stuff */
237 #include <sys/types.h> /* for ssize_t */
242 * Disallow some C++ keywords as identifiers in C programs,
243 * for improved portability.
245 #pragma GCC poison new delete class template typename
246 #pragma GCC poison private protected public operator
247 #pragma GCC poison friend mutable using namespace
248 #pragma GCC poison cin cout cerr clog
251 #elif defined(__MWERKS__)
253 /* Compiler features */
254 #define COMPILER_VARIADIC_MACROS 1
255 #define COMPILER_TYPEOF 1
256 #define COMPILER_STATEMENT_EXPRESSIONS 1
258 #define typeof __typeof__
260 #define UNUSED_ARG(type,arg) type
266 // CodeWarrior has size_t as built-in type, but does not define this symbol.
267 #define _SIZE_T_DEFINED
270 #error unknown compiler
274 /* Defaults for compiler extensions. */
277 * \def COMPILER_VARIADIC_MACROS
278 * Support for macros with variable arguments.
280 #ifndef COMPILER_VARIADIC_MACROS
281 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
285 * \def COMPILER_TYPEOF
286 * Support for dynamic type identification.
288 #ifndef COMPILER_TYPEOF
289 #define COMPILER_TYPEOF 0
293 * \def COMPILER_STATEMENT_EXPRESSIONS
294 * Support for statement expressions.
296 #ifndef COMPILER_STATEMENT_EXPRESSIONS
297 #define COMPILER_STATEMENT_EXPRESSIONS 0
300 /* A few defaults for missing compiler features. */
302 #define INLINE static inline
305 #define NORETURN /* nothing */
308 #define FORMAT(type,fmt,first) /* nothing */
311 #define DEPRECATED /* nothing */
314 #define UNUSED_ARG(type,arg) type arg
316 #define UNUSED UNUSED_ARG /* OBSOLETE */
318 #define UNUSED_VAR(type,name) type name
321 #define USED_VAR(type,name) type name
324 #define REGISTER /* nothing */
330 #define UNLIKELY(x) x
333 #define PURE_FUNC /* nothing */
336 #define CONST_FUNC /* nothing */
339 #define UNUSED_FUNC /* nothing */
342 #define USED_FUNC /* nothing */
345 #define RESTRICT /* nothing */
348 #define MUST_CHECK /* nothing */
351 /* Misc definitions */
353 #define NULL (void *)0
360 /* Support for hybrid C/C++ applications. */
362 #define EXTERN_C extern "C"
363 #define EXTERN_C_BEGIN extern "C" {
364 #define EXTERN_C_END }
365 #define EXTERN_CONST extern const
367 #define EXTERN_C extern
368 #define EXTERN_C_BEGIN /* nothing */
369 #define EXTERN_C_END /* nothing */
370 #define EXTERN_CONST const
374 #if defined(_MSC_VER) \
375 || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
377 * \name ISO C99 fixed-size types
379 * These should be in <stdint.h>, but a few compilers lack them.
382 typedef signed char int8_t;
383 typedef unsigned char uint8_t;
384 typedef short int int16_t;
385 typedef unsigned short int uint16_t;
386 typedef long int int32_t; /* _WIN64 safe */
387 typedef unsigned long int uint32_t; /* _WIN64 safe */
390 typedef __int64 int64_t;
391 typedef unsigned __int64 uint64_t;
393 typedef long long int64_t;
394 typedef unsigned long long uint64_t;
398 /* This is the standard location. */
404 * The ATmega8 has a very small Flash, so we can't afford
405 * to link in support routines for 32bit integer arithmetic.
407 typedef int16_t ticks_t; /*!< Type for time expressed in ticks. */
408 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
409 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
410 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
411 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
412 #define MTIME_INFINITE 0x7FFFL
414 typedef int32_t ticks_t; /*!< Type for time expressed in ticks. */
415 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
416 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
417 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
418 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
419 #define MTIME_INFINITE 0x7FFFFFFFL
422 /*! Bulk storage large enough for both pointers or integers. */
423 typedef void * iptr_t;
425 /*! Bulk storage large enough for both pointers to constants or integers. */
426 typedef const void * const_iptr_t;
428 typedef unsigned char sigbit_t; /*!< Type for signal bits. */
429 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
430 typedef unsigned char page_t; /*!< Type for banked memory pages. */
434 * \name Standard type definitions.
436 * These should be in <sys/types.h> or <stddef.h>, but many compilers
437 * and C libraries lack them.
439 * We check for some common definitions to avoid redefinitions:
441 * glibc, avr-libc: _SIZE_T_DEFINED, __ssize_t_defined
442 * Darwin libc: _BSD_SIZE_T_DEFINED_, _SIZE_T
447 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
450 /* 32bit or 64bit (32bit for _WIN64). */
451 typedef unsigned long size_t;
457 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
459 /* 32bit or 64bit (32bit for _WIN64). */
460 typedef long ssize_t;
464 /* 16bit (missing in avr-libc's sys/types.h). */
474 * \name Types for hardware registers.
476 * Only use these types for registers whose contents can
477 * be changed asynchronously by external hardware.
482 /* Registers can be accessed only through 16-bit pointers */
483 typedef volatile uint16_t reg16_t;
485 typedef volatile uint8_t reg8_t;
486 typedef volatile uint16_t reg16_t;
487 typedef volatile uint32_t reg32_t;
492 /* Quasi-ANSI macros */
495 * Return the byte offset of the member \a m in struct \a s.
497 * \note This macro should be defined in "stddef.h" and is sometimes
498 * compiler-specific (g++ has a builtin for it).
500 #define offsetof(s,m) (size_t)&(((s *)0)->m)
504 * Count the number of elements in the static array \a a.
506 * \note This macro is non-standard, but implements a very common idiom
508 #define countof(a) (sizeof(a) / sizeof(*(a)))
512 * Cast a member of a structure out to the containing structure.
514 * \param ptr the pointer to the member.
515 * \param type the type of the container struct this is embedded in.
516 * \param member the name of the member within the struct.
518 #if COMPILER_TYPEOF && COMPILER_STATEMENT_EXPRESSIONS
519 #define containerof(ptr, type, member) ({ \
520 const typeof( ((type *)0)->member ) *_mptr = (ptr); /* type check */ \
521 (type *)((char *)_mptr - offsetof(type, member)); \
524 #define containerof(ptr, type, member) \
525 ( (type *)((char *)(ptr) - offsetof(type, member)) )
528 /*! Issue a compilation error if the \a condition is false */
529 #define STATIC_ASSERT(condition) \
530 UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
532 #ifndef ASSERT_TYPE_EQUAL
533 /*! Ensure two variables have the same type. */
534 #define ASSERT_TYPE_EQUAL(var1, var2) \
535 do { (void)(&(var1) == &(var2)); } while(0)
538 #ifndef ASSERT_TYPE_IS
539 /** Ensure variable is of specified type. */
540 #define ASSERT_TYPE_IS(var, type) \
541 do { (void)(&(var) == (type *)0); } while(0)
544 #endif /* DEVLIB_COMPILER_H */