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.13 2006/02/23 07:37:37 bernie
18 *#* Compile fix for MSVC.
20 *#* Revision 1.12 2006/02/21 16:06:31 bernie
21 *#* Fix ssize_t redefinitions on glibc systems.
23 *#* Revision 1.11 2006/02/20 14:34:58 bernie
24 *#* Use portable type checking.
26 *#* Revision 1.10 2006/02/15 09:12:01 bernie
27 *#* Fixes for ARM/IAR support.
29 *#* Revision 1.9 2006/02/10 12:38:00 bernie
30 *#* Add support for ARM on IAR.
32 *#* Revision 1.8 2006/01/16 03:27:49 bernie
33 *#* Rename sig_t to sigbit_t to avoid clash with POSIX.
35 *#* Revision 1.7 2005/11/27 23:33:05 bernie
36 *#* Drop avr-libc hack for missing stdint.h.
38 *#* Revision 1.6 2005/07/19 07:27:31 bernie
39 *#* Don't use CPU_REG_BITS from cpu.h to avoid circular header dependendy.
41 *#* Revision 1.5 2005/06/27 21:24:37 bernie
42 *#* ticks_t: New typedef.
44 *#* Revision 1.4 2005/06/14 06:15:10 bernie
45 *#* Add X86_64 support.
47 *#* Revision 1.3 2005/04/12 01:37:01 bernie
48 *#* Metrowerks touchups from HeCo.
50 *#* Revision 1.2 2005/04/11 19:10:27 bernie
51 *#* Include top-level headers from cfg/ subdir.
53 *#* Revision 1.1 2005/04/11 19:04:13 bernie
54 *#* Move top-level headers to cfg/ subdir.
56 *#* Revision 1.44 2005/03/29 06:39:59 bernie
57 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
59 *#* Revision 1.43 2005/03/01 23:15:12 bernie
60 *#* Remove compatibility hack.
62 *#* Revision 1.42 2005/02/16 20:28:46 bernie
63 *#* Move PGM macros to mware/pgm.h
65 *#* Revision 1.41 2005/01/22 04:19:22 bernie
66 *#* MTIME_INFINITE: New constant.
68 *#* Revision 1.40 2005/01/20 18:46:04 aleph
69 *#* Add progmem datatypes; PSTR() definition.
71 #ifndef DEVLIB_COMPILER_H
72 #define DEVLIB_COMPILER_H
74 #include <cfg/cpu_detect.h>
77 #if defined __GNUC__ && defined __GNUC_MINOR__
78 #define GNUC_PREREQ(maj, min) \
79 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
81 #define GNUC_PREREQ(maj, min) 0
84 /* Some CW versions do not allow enabling C99 from the settings panel. */
85 #if defined(__MWERKS__)
89 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
90 #define COMPILER_C99 1
92 #define COMPILER_C99 0
96 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
97 #define PP_CAT(x,y) PP_CAT__(x,y)
98 #define PP_CAT__(x,y) x ## y
99 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
100 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
101 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
103 /*! String-ize a token (allowing macros to expand) */
104 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
105 #define PP_STRINGIZE__(x) #x
108 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
110 #pragma language=extended
114 #define COMPILER_VARIADIC_MACROS 1
116 #define INTERRUPT(x) __irq __arm void x (void)
117 #define INLINE static inline
119 /* Include some standard C89/C99 stuff */
126 // IAR has size_t as built-in type, but does not define this symbol.
127 #define _SIZE_T_DEFINED
129 #define INTERRUPT(x) interrupt [x]
130 #define REGISTER shortad
131 #define INLINE /* unsupported */
134 * Imported from <longjmp.h>. Unfortunately, we can't just include
135 * this header because it typedefs jmp_buf to be an array of chars.
136 * This would allow the compiler to place the buffer on an odd address.
137 * The CPU _should_ be able to perform word accesses to
138 * unaligned data, but there are *BUGS* in the 80196KC with
139 * some combinations of opcodes and addressing modes. One of
140 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
141 * provided by the IAR compiler ANSI C library. When ?GR contains
142 * an odd address, surprisingly the CPU will copy the high order
143 * byte of the source operand (SP) in the low order byte of the
144 * destination operand (the memory location pointed to by ?GR).
146 * We also need to replace the library setjmp()/longjmp() with
147 * our own versions because the IAR implementation "forgets" to
148 * save the contents of local registers (?LR).
152 void *sp; /* Stack pointer */
153 void *return_addr; /* Return address */
154 int lr[6]; /* 6 local registers */
157 typedef struct _JMP_BUF jmp_buf[1];
159 int setjmp(jmp_buf env);
160 void longjmp(jmp_buf env, int val);
162 /* Fake bool support */
165 typedef unsigned char bool;
168 #error Unsupported CPU
171 #elif defined(_MSC_VER) /* Win32 emulation support */
173 /* MSVC doesn't provide <stdbool.h>. */
178 #endif /* !__cplusplus */
180 /* These C99 functions are oddly named in MSVCRT32.lib */
181 #define snprintf _snprintf
182 #define vsnprintf _vsnprintf
184 #elif defined(__GNUC__)
186 /* Compiler features */
187 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
188 #define COMPILER_TYPEOF 1
189 #define COMPILER_STATEMENT_EXPRESSIONS 1
192 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
193 #define NORETURN __attribute__((__noreturn__))
194 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
195 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
196 #define USED_VAR(type,name) __attribute__((__used__)) type name
197 #define INLINE static inline __attribute__((__always_inline__))
198 #define LIKELY(x) __builtin_expect(!!(x), 1)
199 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
200 #define PURE_FUNC __attribute__((pure))
201 #define CONST_FUNC __attribute__((const))
202 #define UNUSED_FUNC __attribute__((unused))
203 #define USED_FUNC __attribute__((__used__))
204 #define RESTRICT __restrict__
205 #define MUST_CHECK __attribute__((warn_unused_result))
207 #define DEPRECATED __attribute__((__deprecated__))
211 #define ASSERT_TYPE_EQUAL(var1, var2) \
212 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2)))
213 #define ASSERT_TYPE_IS(var, type) \
214 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type))
217 /* Include some standard C89/C99 stuff */
221 #include <sys/types.h> /* for ssize_t */
225 * Disallow some C++ keywords as identifiers in C programs,
226 * for improved portability.
228 #pragma GCC poison new delete class template typename
229 #pragma GCC poison private protected public operator
230 #pragma GCC poison friend mutable using namespace
231 #pragma GCC poison cin cout cerr clog
234 #elif defined(__MWERKS__)
236 /* Compiler features */
237 #define COMPILER_VARIADIC_MACROS 1
238 #define COMPILER_TYPEOF 1
239 #define COMPILER_STATEMENT_EXPRESSIONS 1
241 #define typeof __typeof__
243 #define UNUSED_ARG(type,arg) type
249 // CodeWarrior has size_t as built-in type, but does not define this symbol.
250 #define _SIZE_T_DEFINED
253 #error unknown compiler
257 /* Defaults for compiler extensions. */
260 * \def COMPILER_VARIADIC_MACROS
261 * Support for macros with variable arguments.
263 #ifndef COMPILER_VARIADIC_MACROS
264 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
268 * \def COMPILER_TYPEOF
269 * Support for dynamic type identification.
271 #ifndef COMPILER_TYPEOF
272 #define COMPILER_TYPEOF 0
276 * \def COMPILER_STATEMENT_EXPRESSIONS
277 * Support for statement expressions.
279 #ifndef COMPILER_STATEMENT_EXPRESSIONS
280 #define COMPILER_STATEMENT_EXPRESSIONS 0
283 /* A few defaults for missing compiler features. */
285 #define INLINE static inline
288 #define NORETURN /* nothing */
291 #define FORMAT(type,fmt,first) /* nothing */
294 #define DEPRECATED /* nothing */
297 #define UNUSED_ARG(type,arg) type arg
299 #define UNUSED UNUSED_ARG /* OBSOLETE */
301 #define UNUSED_VAR(type,name) type name
304 #define USED_VAR(type,name) type name
307 #define REGISTER /* nothing */
310 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
316 #define UNLIKELY(x) x
319 #define PURE_FUNC /* nothing */
322 #define CONST_FUNC /* nothing */
325 #define UNUSED_FUNC /* nothing */
328 #define USED_FUNC /* nothing */
331 #define RESTRICT /* nothing */
334 #define MUST_CHECK /* nothing */
337 /* Misc definitions */
339 #define NULL (void *)0
346 /* Support for hybrid C/C++ applications. */
348 #define EXTERN_C extern "C"
349 #define EXTERN_C_BEGIN extern "C" {
350 #define EXTERN_C_END }
351 #define EXTERN_CONST extern const
353 #define EXTERN_C extern
354 #define EXTERN_C_BEGIN /* nothing */
355 #define EXTERN_C_END /* nothing */
356 #define EXTERN_CONST const
360 #if defined(_MSC_VER) \
361 || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
363 * \name ISO C99 fixed-size types
365 * These should be in <stdint.h>, but a few compilers lack them.
368 typedef signed char int8_t;
369 typedef unsigned char uint8_t;
370 typedef short int int16_t;
371 typedef unsigned short int uint16_t;
372 typedef long int int32_t; /* _WIN64 safe */
373 typedef unsigned long int uint32_t; /* _WIN64 safe */
376 typedef __int64 int64_t;
377 typedef unsigned __int64 uint64_t;
379 typedef long long int64_t;
380 typedef unsigned long long uint64_t;
384 /* This is the standard location. */
390 * The ATmega8 has a very small Flash, so we can't afford
391 * to link in support routines for 32bit integer arithmetic.
393 typedef int16_t ticks_t; /*!< Type for time expressed in ticks. */
394 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
395 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
396 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
397 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
398 #define MTIME_INFINITE 0x7FFFL
400 typedef int32_t ticks_t; /*!< Type for time expressed in ticks. */
401 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
402 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
403 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
404 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
405 #define MTIME_INFINITE 0x7FFFFFFFL
408 /*! Bulk storage large enough for both pointers or integers. */
409 typedef void * iptr_t;
411 /*! Bulk storage large enough for both pointers to constants or integers. */
412 typedef const void * const_iptr_t;
414 typedef unsigned char sigbit_t; /*!< Type for signal bits. */
415 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
416 typedef unsigned char page_t; /*!< Type for banked memory pages. */
420 * \name Standard type definitions.
422 * These should be in <sys/types.h> or <stddef.h>, but many compilers
423 * and C libraries lack them.
425 * We check for some common definitions to avoid redefinitions:
427 * glibc, avr-libc: _SIZE_T_DEFINED, __ssize_t_defined
428 * Darwin libc: _BSD_SIZE_T_DEFINED_, _SIZE_T
433 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
436 /* 32bit or 64bit (32bit for _WIN64). */
437 typedef unsigned long size_t;
443 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
445 /* 32bit or 64bit (32bit for _WIN64). */
446 typedef long ssize_t;
450 /* 16bit (missing in avr-libc's sys/types.h). */
460 * \name Types for hardware registers.
462 * Only use these types for registers whose contents can
463 * be changed asynchronously by external hardware.
468 /* Registers can be accessed only through 16-bit pointers */
469 typedef volatile uint16_t reg16_t;
471 typedef volatile uint8_t reg8_t;
472 typedef volatile uint16_t reg16_t;
473 typedef volatile uint32_t reg32_t;
478 /* Quasi-ANSI macros */
481 * Return the byte offset of the member \a m in struct \a s.
483 * \note This macro should be defined in "stddef.h" and is sometimes
484 * compiler-specific (g++ has a builtin for it).
486 #define offsetof(s,m) (size_t)&(((s *)0)->m)
490 * Count the number of elements in the static array \a a.
492 * \note This macro is non-standard, but implements a very common idiom
494 #define countof(a) (sizeof(a) / sizeof(*(a)))
497 /*! Issue a compilation error if the \a condition is false */
498 #define STATIC_ASSERT(condition) \
499 UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
501 #ifndef ASSERT_TYPE_EQUAL
502 /*! Ensure two variables have the same type. */
503 #define ASSERT_TYPE_EQUAL(var1, var2) \
504 do { (void)(&(var1) == &(var2)); } while(0)
507 #ifndef ASSERT_TYPE_IS
508 /*! Ensure variable is of specified type. */
509 #define ASSERT_TYPE_IS(var, type) \
510 do { (void)(&var == (type *)0); } while(0)
513 #endif /* DEVLIB_COMPILER_H */