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.23 2006/09/13 18:28:38 bernie
20 *#* Revision 1.22 2006/07/19 12:56:25 bernie
21 *#* Convert to new Doxygen style.
23 *#* Revision 1.21 2006/07/19 12:53:43 bernie
26 *#* Revision 1.20 2006/06/10 05:36:15 bernie
27 *#* Convert to new Doxygen comments.
29 *#* Revision 1.19 2006/04/11 00:07:33 bernie
30 *#* Implemenent MF_SAVESEL flag.
32 *#* Revision 1.18 2006/03/22 13:34:34 bernie
35 *#* Revision 1.17 2006/03/22 09:48:35 bernie
38 *#* Revision 1.16 2006/03/20 17:48:09 bernie
39 *#* Fix for avr-libc; INTERRUPT(): Remove macro.
41 *#* Revision 1.15 2006/03/13 02:06:25 bernie
42 *#* containerof(): New macro.
44 *#* Revision 1.14 2006/02/23 08:36:33 bernie
45 *#* Emulate __func__ on MSVC.
47 *#* Revision 1.13 2006/02/23 07:37:37 bernie
48 *#* Compile fix for MSVC.
50 *#* Revision 1.12 2006/02/21 16:06:31 bernie
51 *#* Fix ssize_t redefinitions on glibc systems.
53 *#* Revision 1.11 2006/02/20 14:34:58 bernie
54 *#* Use portable type checking.
56 *#* Revision 1.10 2006/02/15 09:12:01 bernie
57 *#* Fixes for ARM/IAR support.
59 *#* Revision 1.9 2006/02/10 12:38:00 bernie
60 *#* Add support for ARM on IAR.
62 *#* Revision 1.8 2006/01/16 03:27:49 bernie
63 *#* Rename sig_t to sigbit_t to avoid clash with POSIX.
65 *#* Revision 1.7 2005/11/27 23:33:05 bernie
66 *#* Drop avr-libc hack for missing stdint.h.
68 *#* Revision 1.6 2005/07/19 07:27:31 bernie
69 *#* Don't use CPU_REG_BITS from cpu.h to avoid circular header dependendy.
71 *#* Revision 1.5 2005/06/27 21:24:37 bernie
72 *#* ticks_t: New typedef.
74 *#* Revision 1.4 2005/06/14 06:15:10 bernie
75 *#* Add X86_64 support.
77 *#* Revision 1.3 2005/04/12 01:37:01 bernie
78 *#* Metrowerks touchups from HeCo.
80 *#* Revision 1.2 2005/04/11 19:10:27 bernie
81 *#* Include top-level headers from cfg/ subdir.
83 *#* Revision 1.1 2005/04/11 19:04:13 bernie
84 *#* Move top-level headers to cfg/ subdir.
86 #ifndef DEVLIB_COMPILER_H
87 #define DEVLIB_COMPILER_H
89 #include <cfg/cpu_detect.h>
92 #if defined __GNUC__ && defined __GNUC_MINOR__
93 #define GNUC_PREREQ(maj, min) \
94 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
96 #define GNUC_PREREQ(maj, min) 0
99 /* Some CW versions do not allow enabling C99 from the settings panel. */
100 #if defined(__MWERKS__)
104 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
105 #define COMPILER_C99 1
107 #define COMPILER_C99 0
111 /** Concatenate two different preprocessor tokens (allowing macros to expand) */
112 #define PP_CAT(x,y) PP_CAT__(x,y)
113 #define PP_CAT__(x,y) x ## y
114 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
115 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
116 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
118 /** String-ize a token (allowing macros to expand) */
119 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
120 #define PP_STRINGIZE__(x) #x
123 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
125 #pragma language=extended
129 #define COMPILER_VARIADIC_MACROS 1
131 #define INTERRUPT(x) __irq __arm void x (void)
132 #define INLINE static inline
134 /* Include some standard C89/C99 stuff */
141 // IAR has size_t as built-in type, but does not define this symbol.
142 #define _SIZE_T_DEFINED
144 #define INTERRUPT(x) interrupt [x]
145 #define REGISTER shortad
146 #define INLINE /* unsupported */
149 * Imported from <longjmp.h>. Unfortunately, we can't just include
150 * this header because it typedefs jmp_buf to be an array of chars.
151 * This would allow the compiler to place the buffer on an odd address.
152 * The CPU _should_ be able to perform word accesses to
153 * unaligned data, but there are *BUGS* in the 80196KC with
154 * some combinations of opcodes and addressing modes. One of
155 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
156 * provided by the IAR compiler ANSI C library. When ?GR contains
157 * an odd address, surprisingly the CPU will copy the high order
158 * byte of the source operand (SP) in the low order byte of the
159 * destination operand (the memory location pointed to by ?GR).
161 * We also need to replace the library setjmp()/longjmp() with
162 * our own versions because the IAR implementation "forgets" to
163 * save the contents of local registers (?LR).
167 void *sp; /* Stack pointer */
168 void *return_addr; /* Return address */
169 int lr[6]; /* 6 local registers */
172 typedef struct _JMP_BUF jmp_buf[1];
174 int setjmp(jmp_buf env);
175 void longjmp(jmp_buf env, int val);
177 /* Fake bool support */
180 typedef unsigned char bool;
183 #error Unsupported CPU
186 #elif defined(_MSC_VER) /* Win32 emulation support */
188 /* MSVC doesn't provide <stdbool.h>. */
193 #endif /* !__cplusplus */
195 /* These C99 functions are oddly named in MSVCRT32.lib */
196 #define snprintf _snprintf
197 #define vsnprintf _vsnprintf
199 /* MSVC doesn't support C99's __func__, but has a similar extension. */
200 #define __func__ __FUNCTION__
202 /* MSVC doesn't support C99's inline keyword */
204 #define INLINE __inline
207 #elif defined(__GNUC__)
209 /* Compiler features */
210 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
211 #define COMPILER_TYPEOF 1
212 #define COMPILER_STATEMENT_EXPRESSIONS 1
215 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
216 #define NORETURN __attribute__((__noreturn__))
217 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
218 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
219 #define USED_VAR(type,name) __attribute__((__used__)) type name
220 #define INLINE static inline __attribute__((__always_inline__))
221 #define LIKELY(x) __builtin_expect(!!(x), 1)
222 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
223 #define PURE_FUNC __attribute__((pure))
224 #define CONST_FUNC __attribute__((const))
225 #define UNUSED_FUNC __attribute__((unused))
226 #define USED_FUNC __attribute__((__used__))
227 #define RESTRICT __restrict__
228 #define MUST_CHECK __attribute__((warn_unused_result))
230 #define DEPRECATED __attribute__((__deprecated__))
234 #define ASSERT_TYPE_EQUAL(var1, var2) \
235 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2)))
236 #define ASSERT_TYPE_IS(var, type) \
237 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type))
240 /* Include some standard C89/C99 stuff */
245 #include <sys/types.h> /* for ssize_t */
250 * Disallow some C++ keywords as identifiers in C programs,
251 * for improved portability.
253 #pragma GCC poison new delete class template typename
254 #pragma GCC poison private protected public operator
255 #pragma GCC poison friend mutable using namespace
256 #pragma GCC poison cin cout cerr clog
259 #elif defined(__MWERKS__)
261 /* Compiler features */
262 #define COMPILER_VARIADIC_MACROS 1
263 #define COMPILER_TYPEOF 1
264 #define COMPILER_STATEMENT_EXPRESSIONS 1
266 #define typeof __typeof__
268 #define UNUSED_ARG(type,arg) type
274 // CodeWarrior has size_t as built-in type, but does not define this symbol.
275 #define _SIZE_T_DEFINED
278 #error unknown compiler
282 /* Defaults for compiler extensions. */
285 * \def COMPILER_VARIADIC_MACROS
286 * Support for macros with variable arguments.
288 #ifndef COMPILER_VARIADIC_MACROS
289 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
293 * \def COMPILER_TYPEOF
294 * Support for dynamic type identification.
296 #ifndef COMPILER_TYPEOF
297 #define COMPILER_TYPEOF 0
301 * \def COMPILER_STATEMENT_EXPRESSIONS
302 * Support for statement expressions.
304 #ifndef COMPILER_STATEMENT_EXPRESSIONS
305 #define COMPILER_STATEMENT_EXPRESSIONS 0
308 /* A few defaults for missing compiler features. */
310 #define INLINE static inline
313 #define NORETURN /* nothing */
316 #define FORMAT(type,fmt,first) /* nothing */
319 #define DEPRECATED /* nothing */
322 #define UNUSED_ARG(type,arg) type arg
324 #define UNUSED UNUSED_ARG /* OBSOLETE */
326 #define UNUSED_VAR(type,name) type name
329 #define USED_VAR(type,name) type name
332 #define REGISTER /* nothing */
338 #define UNLIKELY(x) x
341 #define PURE_FUNC /* nothing */
344 #define CONST_FUNC /* nothing */
347 #define UNUSED_FUNC /* nothing */
350 #define USED_FUNC /* nothing */
353 #define RESTRICT /* nothing */
356 #define MUST_CHECK /* nothing */
359 /* Misc definitions */
361 #define NULL (void *)0
368 /* Support for hybrid C/C++ applications. */
370 #define EXTERN_C extern "C"
371 #define EXTERN_C_BEGIN extern "C" {
372 #define EXTERN_C_END }
373 #define EXTERN_CONST extern const
374 #define CONST_CAST(TYPE,EXPR) (const_cast<TYPE>(EXPR))
376 #define EXTERN_C extern
377 #define EXTERN_C_BEGIN /* nothing */
378 #define EXTERN_C_END /* nothing */
379 #define EXTERN_CONST const
380 #define CONST_CAST(TYPE,EXPR) ((TYPE)(EXPR)) /* FIXME: How can we suppress the warning in C? */
384 #if defined(_MSC_VER) \
385 || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
387 * \name ISO C99 fixed-size types
389 * These should be in <stdint.h>, but a few compilers lack them.
392 typedef signed char int8_t;
393 typedef unsigned char uint8_t;
394 typedef short int int16_t;
395 typedef unsigned short int uint16_t;
396 typedef long int int32_t; /* _WIN64 safe */
397 typedef unsigned long int uint32_t; /* _WIN64 safe */
400 typedef __int64 int64_t;
401 typedef unsigned __int64 uint64_t;
403 typedef long long int64_t;
404 typedef unsigned long long uint64_t;
408 /* This is the standard location. */
414 * The ATmega8 has a very small Flash, so we can't afford
415 * to link in support routines for 32bit integer arithmetic.
417 typedef int16_t ticks_t; /**< Type for time expressed in ticks. */
418 typedef int16_t mtime_t; /**< Type for time expressed in milliseconds. */
419 typedef int16_t utime_t; /**< Type for time expressed in microseconds. */
420 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
421 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
422 #define MTIME_INFINITE 0x7FFFL
424 typedef int32_t ticks_t; /**< Type for time expressed in ticks. */
425 typedef int32_t mtime_t; /**< Type for time expressed in milliseconds. */
426 typedef int32_t utime_t; /**< Type for time expressed in microseconds. */
427 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
428 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
429 #define MTIME_INFINITE 0x7FFFFFFFL
432 /** Bulk storage large enough for both pointers or integers. */
433 typedef void * iptr_t;
435 /** Bulk storage large enough for both pointers to constants or integers. */
436 typedef const void * const_iptr_t;
438 typedef unsigned char sigbit_t; /**< Type for signal bits. */
439 typedef unsigned char sigmask_t; /**< Type for signal masks. */
440 typedef unsigned char page_t; /**< Type for banked memory pages. */
444 * \name Standard type definitions.
446 * These should be in <sys/types.h> or <stddef.h>, but many compilers
447 * and C libraries lack them.
449 * We check for some common definitions to avoid redefinitions:
451 * glibc, avr-libc: _SIZE_T_DEFINED, __ssize_t_defined
452 * Darwin libc: _BSD_SIZE_T_DEFINED_, _SIZE_T
457 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
460 /* 32bit or 64bit (32bit for _WIN64). */
461 typedef unsigned long size_t;
467 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
469 /* 32bit or 64bit (32bit for _WIN64). */
470 typedef long ssize_t;
474 /* 16bit (missing in avr-libc's sys/types.h). */
484 * \name Types for hardware registers.
486 * Only use these types for registers whose contents can
487 * be changed asynchronously by external hardware.
492 /* Registers can be accessed only through 16-bit pointers */
493 typedef volatile uint16_t reg16_t;
495 typedef volatile uint8_t reg8_t;
496 typedef volatile uint16_t reg16_t;
497 typedef volatile uint32_t reg32_t;
502 /* Quasi-ANSI macros */
505 * Return the byte offset of the member \a m in struct \a s.
507 * \note This macro should be defined in "stddef.h" and is sometimes
508 * compiler-specific (g++ has a builtin for it).
510 #define offsetof(s,m) (size_t)&(((s *)0)->m)
514 * Count the number of elements in the static array \a a.
516 * \note This macro is non-standard, but implements a very common idiom
518 #define countof(a) (sizeof(a) / sizeof(*(a)))
522 * Cast a member of a structure out to the containing structure.
524 * \param ptr the pointer to the member.
525 * \param type the type of the container struct this is embedded in.
526 * \param member the name of the member within the struct.
528 #if COMPILER_TYPEOF && COMPILER_STATEMENT_EXPRESSIONS
529 #define containerof(ptr, type, member) ({ \
530 const typeof( ((type *)0)->member ) *_mptr = (ptr); /* type check */ \
531 (type *)((char *)_mptr - offsetof(type, member)); \
534 #define containerof(ptr, type, member) \
535 ( (type *)((char *)(ptr) - offsetof(type, member)) )
538 /** Issue a compilation error if the \a condition is false */
539 #define STATIC_ASSERT(condition) \
540 UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
542 #ifndef ASSERT_TYPE_EQUAL
543 /** Ensure two variables have the same type. */
544 #define ASSERT_TYPE_EQUAL(var1, var2) \
545 do { (void)(&(var1) == &(var2)); } while(0)
548 #ifndef ASSERT_TYPE_IS
549 /** Ensure variable is of specified type. */
550 #define ASSERT_TYPE_IS(var, type) \
551 do { (void)(&(var) == (type *)0); } while(0)
554 #endif /* DEVLIB_COMPILER_H */