4 * Copyright 2003, 2004 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 devlib/README for information.
9 * \brief Additional support macros for compiler independance
12 * \author Bernardo Innocenti <bernie@develer.com>
17 *#* Revision 1.41 2005/01/22 04:19:22 bernie
18 *#* MTIME_INFINITE: New constant.
20 *#* Revision 1.40 2005/01/20 18:46:04 aleph
21 *#* Add progmem datatypes; PSTR() definition.
23 *#* Revision 1.39 2005/01/08 08:49:39 bernie
24 *#* Define PROGMEM on AVR only when not including pgmspace.h.
26 *#* Revision 1.38 2004/12/31 16:41:52 bernie
27 *#* PROGMEM: Define to nothing for non-Harvard processors.
29 *#* Revision 1.37 2004/12/08 09:43:21 bernie
30 *#* Metrowerks supports variadic macros.
32 *#* Revision 1.36 2004/12/08 08:55:54 bernie
33 *#* Rename sigset_t to sigmask_t and time_t to mtime_t, to avoid conflicts with POSIX definitions.
35 *#* Revision 1.35 2004/12/08 07:35:51 bernie
36 *#* Typo in macro name.
38 *#* Revision 1.34 2004/11/28 23:21:33 bernie
39 *#* Use mtime_t instead of overloading ANSI time_t with new semantics.
41 *#* Revision 1.33 2004/11/16 23:09:40 bernie
42 *#* size_t: Add 64bit definitions; time_t: Add 16bit hack for tiny CPUs.
44 *#* Revision 1.32 2004/11/16 22:42:44 bernie
47 *#* Revision 1.31 2004/11/16 22:37:28 bernie
48 *#* IPTR: Remove obsolete definition.
50 *#* Revision 1.30 2004/11/16 22:30:19 bernie
51 *#* Declare fixed-size types before other types.
53 *#* Revision 1.29 2004/11/16 20:34:40 bernie
54 *#* UNUSED_VAR, USED_VAR, USED_FUNC: New macros; UNUSED_ARG: Rename from UNUSED.
56 *#* Revision 1.28 2004/10/21 11:03:52 bernie
59 *#* Revision 1.27 2004/10/21 10:09:40 bernie
60 *#* Remove spurious token in preprocessor directive.
62 *#* Revision 1.26 2004/10/19 08:55:14 bernie
63 *#* UNUSED_FUNC: New function attribute.
65 *#* Revision 1.25 2004/10/19 07:14:20 bernie
66 *#* Add macros to test for specific compiler features.
68 *#* Revision 1.24 2004/10/03 18:35:13 bernie
69 *#* Poison C++ keywords in C programs for better portability.
71 *#* Revision 1.23 2004/09/20 03:30:27 bernie
72 *#* Remove vsprintf_P() proto, no longer needed with avr-libc 1.0.4.
74 *#* Revision 1.22 2004/09/14 21:03:04 bernie
75 *#* PURE_FUNC, CONST_FUNC, MUST_CHECK: New function attributes; LIKELY()/UNLIKELY(): Fix for non-integral expressions.
77 *#* Revision 1.21 2004/09/06 21:38:31 bernie
78 *#* Misc documentation and style fixes.
80 *#* Revision 1.20 2004/08/29 21:57:58 bernie
81 *#* Move back STATIC_ASSERT() to compiler.h as it's needed in cpu.h;
82 *#* iptr_t, const_iptr_t: Replace IPTR macro with a real typedef.
84 *#* Revision 1.19 2004/08/25 14:12:08 rasky
85 *#* Aggiornato il comment block dei log RCS
87 *#* Revision 1.18 2004/08/24 16:32:37 bernie
88 *#* Document custom types.
90 *#* Revision 1.17 2004/08/24 13:32:14 bernie
91 *#* PP_CAT(), PP_STRINGIZE(): Move back to compiler.h to break circular dependency between cpu.h/compiler.h/macros.h;
92 *#* offsetof(), countof(): Move back to compiler.h to avoid including macros.h almost everywhere;
94 *#* Rename header guards;
95 *#* Don't include arch_config.h in compiler.h as it's not needed there.
97 *#* Revision 1.16 2004/08/14 19:37:57 rasky
98 *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
100 *#* Revision 1.15 2004/08/13 03:23:26 bernie
101 *#* Adjust a few MSVC tweaks from older projects.
103 *#* Revision 1.14 2004/08/10 06:56:29 bernie
104 *#* RESTRICT: New C99-like macro; STATIC_ASSERT: Fix warning for multiple invocation in one file.
106 *#* Revision 1.13 2004/08/02 20:20:29 aleph
107 *#* Merge from project_ks
109 *#* Revision 1.12 2004/08/01 01:21:17 bernie
110 *#* LIKELY(), UNLIKELY(): New compiler-specific macros.
112 *#* Revision 1.11 2004/07/30 14:34:10 rasky
113 *#* Vari fix per documentazione e commenti
114 *#* Aggiunte PP_CATn e STATIC_ASSERT
116 *#* Revision 1.10 2004/07/30 14:15:53 rasky
117 *#* Nuovo supporto unificato per detect della CPU
119 *#* Revision 1.9 2004/07/29 22:57:09 bernie
120 *#* vsprintf(): Remove prototype for backwards compatibility with GCC 3.4; ssize_t: Add definition for inferior compilers.
122 #ifndef DEVLIB_COMPILER_H
123 #define DEVLIB_COMPILER_H
125 #include "cpu_detect.h"
128 #if defined __GNUC__ && defined __GNUC_MINOR__
129 #define GNUC_PREREQ(maj, min) \
130 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
132 #define GNUC_PREREQ(maj, min) 0
135 /* Some CW versions do not allow enabling C99 from the settings panel. */
136 #if defined(__MWERKS__)
140 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
141 #define COMPILER_C99 1
143 #define COMPILER_C99 0
147 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
148 #define PP_CAT(x,y) PP_CAT__(x,y)
149 #define PP_CAT__(x,y) x ## y
150 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
151 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
152 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
154 /*! String-ize a token (allowing macros to expand) */
155 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
156 #define PP_STRINGIZE__(x) #x
159 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
160 #pragma language=extended
161 #define INTERRUPT(x) interrupt [x]
162 #define REGISTER shortad
163 #define INLINE /* unsupported */
166 * Imported from <longjmp.h>. Unfortunately, we can't just include
167 * this header because it typedefs jmp_buf to be an array of chars.
168 * This would allow the compiler to place the buffer on an odd address.
169 * The CPU _should_ be able to perform word accesses to
170 * unaligned data, but there are *BUGS* in the 80196KC with
171 * some combinations of opcodes and addressing modes. One of
172 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
173 * provided by the IAR compiler ANSI C library. When ?GR contains
174 * an odd address, surprisingly the CPU will copy the high order
175 * byte of the source operand (SP) in the low order byte of the
176 * destination operand (the memory location pointed to by ?GR).
178 * We also need to replace the library setjmp()/longjmp() with
179 * our own versions because the IAR implementation "forgets" to
180 * save the contents of local registers (?LR).
184 void *sp; /* Stack pointer */
185 void *return_addr; /* Return address */
186 int lr[6]; /* 6 local registers */
189 typedef struct _JMP_BUF jmp_buf[1];
191 int setjmp(jmp_buf env);
192 void longjmp(jmp_buf env, int val);
194 /* Fake bool support */
197 typedef unsigned char bool;
199 #elif defined(_MSC_VER) /* Win32 emulation support */
203 /* FIXME: I can't remember why exactly this was needed (NdBernie) */
206 /* MSVC doesn't provide <stdbool.h>. */
211 #endif /* !__cplusplus */
213 /* These C99 functions are oddly named in MSVCRT32.lib */
214 #define snprintf _snprintf
215 #define vsnprintf _vsnprintf
217 #elif defined(__GNUC__)
219 /* Compiler features */
220 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
221 #define COMPILER_TYPEOF 1
222 #define COMPILER_STATEMENT_EXPRESSIONS 1
225 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
226 #define NORETURN __attribute__((__noreturn__))
227 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
228 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
229 #define USED_VAR(type,name) __attribute__((__used__)) type name
230 #define INLINE static inline __attribute__((__always_inline__))
231 #define LIKELY(x) __builtin_expect(!!(x), 1)
232 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
233 #define PURE_FUNC __attribute__((pure))
234 #define CONST_FUNC __attribute__((const))
235 #define UNUSED_FUNC __attribute__((unused))
236 #define USED_FUNC __attribute__((__used__))
237 #define RESTRICT __restrict__
238 #define MUST_CHECK __attribute__((warn_unused_result))
240 #define DEPRECATED __attribute__((__deprecated__))
254 /* Support for harvard architectures */
256 #include <avr/pgmspace.h>
257 #define PGM_READ_CHAR(s) pgm_read_byte(s)
258 #define PGM_FUNC(x) x ## _P
259 #define PGM_ATTR PROGMEM
261 /* We still need this for prototypes */
262 #define PROGMEM __attribute__((__progmem__))
263 #define PSTR(s) ({static char __c[] PROGMEM = (s); __c;})
270 * Disallow some C++ keywords as identifiers in C programs,
271 * for improved portability.
273 #pragma GCC poison new delete class template typename
274 #pragma GCC poison private protected public operator
275 #pragma GCC poison friend mutable using namespace
276 #pragma GCC poison cin cout cerr clog
279 #elif defined(__MWERKS__) && CPU_DSP56K
281 /* Compiler features */
282 #define COMPILER_VARIADIC_MACROS 1
283 #define COMPILER_TYPEOF 1
284 #define COMPILER_STATEMENT_EXPRESSIONS 1
286 #define typeof __typeof__
293 // CodeWarrior has size_t as built-in type, but does not define this symbol.
294 #define _SIZE_T_DEFINED
297 #error unknown compiler
301 /* Defaults for compiler extensions. */
304 * \def COMPILER_VARIADIC_MACROS
305 * Support for macros with variable arguments.
307 #ifndef COMPILER_VARIADIC_MACROS
308 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
312 * \def COMPILER_TYPEOF
313 * Support for macros with variable arguments.
315 #ifndef COMPILER_TYPEOF
316 #define COMPILER_TYPEOF 0
320 * \def COMPILER_STATEMENT_EXPRESSIONS
321 * Support for macros with variable arguments.
323 #ifndef COMPILER_STATEMENT_EXPRESSIONS
324 #define COMPILER_STATEMENT_EXPRESSIONS 0
327 /* A few defaults for missing compiler features. */
329 #define INLINE static inline
332 #define NORETURN /* nothing */
335 #define FORMAT(type,fmt,first) /* nothing */
338 #define DEPRECATED /* nothing */
341 #define UNUSED_ARG(type,arg) type arg
343 #define UNUSED UNUSED_ARG /* OBSOLETE */
345 #define UNUSED_VAR(type,name) type name
348 #define USED_VAR(type,name) type name
351 #define REGISTER /* nothing */
354 #define PROGMEM /* nothing */
357 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
363 #define UNLIKELY(x) x
366 #define PURE_FUNC /* nothing */
369 #define CONST_FUNC /* nothing */
372 #define UNUSED_FUNC /* nothing */
375 #define USED_FUNC /* nothing */
378 #define RESTRICT /* nothing */
381 #define MUST_CHECK /* nothing */
384 /* Support for harvard architectures */
386 #define PSTR /* nothing */
388 #ifndef PGM_READ_CHAR
389 #define PGM_READ_CHAR(s) (*(s))
392 #define PGM_FUNC(x) x
395 #define PGM_ATTR /* nothing */
398 /* Misc definitions */
400 #define NULL (void *)0
407 /* Support for hybrid C/C++ applications. */
409 #define EXTERN_C extern "C"
410 #define EXTERN_C_BEGIN extern "C" {
411 #define EXTERN_C_END }
413 #define EXTERN_C extern
414 #define EXTERN_C_BEGIN /* nothing */
415 #define EXTERN_C_END /* nothing */
419 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
421 * \name ISO C99 fixed-size types
423 * These should be in <stdint.h>, but many compilers lack them.
426 typedef signed char int8_t;
427 typedef short int int16_t;
428 typedef long int int32_t;
429 typedef unsigned char uint8_t;
430 typedef unsigned short int uint16_t;
431 typedef unsigned long int uint32_t;
433 #elif defined(__GNUC__) && CPU_AVR
434 /* avr-libc is weird... */
435 #include <inttypes.h>
437 /* This is the correct location. */
442 * \name Types for variables stored in program memory (harvard processors).
445 typedef PROGMEM int8_t pgm_int8_t;
446 typedef PROGMEM uint8_t pgm_uint8_t;
447 typedef PROGMEM int16_t pgm_int16_t;
448 typedef PROGMEM uint16_t pgm_uint16_t;
449 typedef PROGMEM int32_t pgm_int32_t;
450 typedef PROGMEM uint32_t pgm_uint32_t;
455 * The ATmega8 has a very small Flash, so we can't afford
456 * to link in support routines for 32bit integer arithmetic.
458 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
459 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
460 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
461 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
462 #define MTIME_INFINITE 0x7FFFL
464 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
465 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
466 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
467 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
468 #define MTIME_INFINITE 0x7FFFFFFFL
471 /*! Bulk storage large enough for both pointers or integers. */
472 typedef void * iptr_t;
473 typedef const void * const_iptr_t;
475 typedef unsigned char sig_t; /*!< Type for signal bits. */
476 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
477 typedef unsigned char page_t; /*!< Type for banked memory pages. */
481 * \name Standard type definitions.
483 * These should be in <sys/types.h>, but many compilers lack them.
487 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
488 #if CPU_REG_BITS > 32
490 typedef unsigned long size_t;
491 typedef long ssize_t;
493 /* 32bit or 16bit. */
494 typedef unsigned int size_t;
499 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined) || defined(_EMUL))
500 /*! For backwards compatibility. Use mtime_t in new code. */
501 #define time_t mtime_t
502 #define SIZEOF_TIME_T SIZEOF_MTIME_T
504 /* Just a guess, but quite safe. */
505 #define SIZEOF_TIME_T SIZEOF_LONG
506 #endif /* _TIME_T_DEFINED || __time_t_defined */
511 * \name Types for hardware registers.
513 * Only use these types for registers whose contents can
514 * be changed asynchronously by external hardware.
519 /* Registers can be accessed only through 16-bit pointers */
520 typedef volatile uint16_t reg16_t;
522 typedef volatile uint8_t reg8_t;
523 typedef volatile uint16_t reg16_t;
524 typedef volatile uint32_t reg32_t;
529 /* Quasi-ANSI macros */
532 * Return the byte offset of the member \a m in struct \a s.
534 * \note This macro should be defined in "stddef.h" and is sometimes
535 * compiler-specific (g++ has a builtin for it).
537 #define offsetof(s,m) (size_t)&(((s *)0)->m)
541 * Count the number of elements in the static array \a a.
543 * \note This macro is non-standard, but implements a very common idiom
545 #define countof(a) (sizeof(a) / sizeof(*(a)))
548 /*! Issue a compilation error if the \a condition is false */
549 #define STATIC_ASSERT(condition) \
550 UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
552 #endif /* DEVLIB_COMPILER_H */