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.3 2005/04/12 01:37:01 bernie
18 *#* Metrowerks touchups from HeCo.
20 *#* Revision 1.2 2005/04/11 19:10:27 bernie
21 *#* Include top-level headers from cfg/ subdir.
23 *#* Revision 1.1 2005/04/11 19:04:13 bernie
24 *#* Move top-level headers to cfg/ subdir.
26 *#* Revision 1.44 2005/03/29 06:39:59 bernie
27 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
29 *#* Revision 1.43 2005/03/01 23:15:12 bernie
30 *#* Remove compatibility hack.
32 *#* Revision 1.42 2005/02/16 20:28:46 bernie
33 *#* Move PGM macros to mware/pgm.h
35 *#* Revision 1.41 2005/01/22 04:19:22 bernie
36 *#* MTIME_INFINITE: New constant.
38 *#* Revision 1.40 2005/01/20 18:46:04 aleph
39 *#* Add progmem datatypes; PSTR() definition.
41 #ifndef DEVLIB_COMPILER_H
42 #define DEVLIB_COMPILER_H
44 #include <cfg/cpu_detect.h>
47 #if defined __GNUC__ && defined __GNUC_MINOR__
48 #define GNUC_PREREQ(maj, min) \
49 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
51 #define GNUC_PREREQ(maj, min) 0
54 /* Some CW versions do not allow enabling C99 from the settings panel. */
55 #if defined(__MWERKS__)
59 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
60 #define COMPILER_C99 1
62 #define COMPILER_C99 0
66 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
67 #define PP_CAT(x,y) PP_CAT__(x,y)
68 #define PP_CAT__(x,y) x ## y
69 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
70 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
71 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
73 /*! String-ize a token (allowing macros to expand) */
74 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
75 #define PP_STRINGIZE__(x) #x
78 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
79 #pragma language=extended
80 #define INTERRUPT(x) interrupt [x]
81 #define REGISTER shortad
82 #define INLINE /* unsupported */
85 * Imported from <longjmp.h>. Unfortunately, we can't just include
86 * this header because it typedefs jmp_buf to be an array of chars.
87 * This would allow the compiler to place the buffer on an odd address.
88 * The CPU _should_ be able to perform word accesses to
89 * unaligned data, but there are *BUGS* in the 80196KC with
90 * some combinations of opcodes and addressing modes. One of
91 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
92 * provided by the IAR compiler ANSI C library. When ?GR contains
93 * an odd address, surprisingly the CPU will copy the high order
94 * byte of the source operand (SP) in the low order byte of the
95 * destination operand (the memory location pointed to by ?GR).
97 * We also need to replace the library setjmp()/longjmp() with
98 * our own versions because the IAR implementation "forgets" to
99 * save the contents of local registers (?LR).
103 void *sp; /* Stack pointer */
104 void *return_addr; /* Return address */
105 int lr[6]; /* 6 local registers */
108 typedef struct _JMP_BUF jmp_buf[1];
110 int setjmp(jmp_buf env);
111 void longjmp(jmp_buf env, int val);
113 /* Fake bool support */
116 typedef unsigned char bool;
118 #elif defined(_MSC_VER) /* Win32 emulation support */
120 /* MSVC doesn't provide <stdbool.h>. */
125 #endif /* !__cplusplus */
127 /* These C99 functions are oddly named in MSVCRT32.lib */
128 #define snprintf _snprintf
129 #define vsnprintf _vsnprintf
131 #elif defined(__GNUC__)
133 /* Compiler features */
134 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
135 #define COMPILER_TYPEOF 1
136 #define COMPILER_STATEMENT_EXPRESSIONS 1
139 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
140 #define NORETURN __attribute__((__noreturn__))
141 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
142 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
143 #define USED_VAR(type,name) __attribute__((__used__)) type name
144 #define INLINE static inline __attribute__((__always_inline__))
145 #define LIKELY(x) __builtin_expect(!!(x), 1)
146 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
147 #define PURE_FUNC __attribute__((pure))
148 #define CONST_FUNC __attribute__((const))
149 #define UNUSED_FUNC __attribute__((unused))
150 #define USED_FUNC __attribute__((__used__))
151 #define RESTRICT __restrict__
152 #define MUST_CHECK __attribute__((warn_unused_result))
154 #define DEPRECATED __attribute__((__deprecated__))
161 /* Include some standard C89/C99 stuff */
168 * Disallow some C++ keywords as identifiers in C programs,
169 * for improved portability.
171 #pragma GCC poison new delete class template typename
172 #pragma GCC poison private protected public operator
173 #pragma GCC poison friend mutable using namespace
174 #pragma GCC poison cin cout cerr clog
177 #elif defined(__MWERKS__)
179 /* Compiler features */
180 #define COMPILER_VARIADIC_MACROS 1
181 #define COMPILER_TYPEOF 1
182 #define COMPILER_STATEMENT_EXPRESSIONS 1
184 #define typeof __typeof__
186 #define UNUSED_ARG(type,arg) type
192 // CodeWarrior has size_t as built-in type, but does not define this symbol.
193 #define _SIZE_T_DEFINED
196 #error unknown compiler
200 /* Defaults for compiler extensions. */
203 * \def COMPILER_VARIADIC_MACROS
204 * Support for macros with variable arguments.
206 #ifndef COMPILER_VARIADIC_MACROS
207 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
211 * \def COMPILER_TYPEOF
212 * Support for macros with variable arguments.
214 #ifndef COMPILER_TYPEOF
215 #define COMPILER_TYPEOF 0
219 * \def COMPILER_STATEMENT_EXPRESSIONS
220 * Support for macros with variable arguments.
222 #ifndef COMPILER_STATEMENT_EXPRESSIONS
223 #define COMPILER_STATEMENT_EXPRESSIONS 0
226 /* A few defaults for missing compiler features. */
228 #define INLINE static inline
231 #define NORETURN /* nothing */
234 #define FORMAT(type,fmt,first) /* nothing */
237 #define DEPRECATED /* nothing */
240 #define UNUSED_ARG(type,arg) type arg
242 #define UNUSED UNUSED_ARG /* OBSOLETE */
244 #define UNUSED_VAR(type,name) type name
247 #define USED_VAR(type,name) type name
250 #define REGISTER /* nothing */
253 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
259 #define UNLIKELY(x) x
262 #define PURE_FUNC /* nothing */
265 #define CONST_FUNC /* nothing */
268 #define UNUSED_FUNC /* nothing */
271 #define USED_FUNC /* nothing */
274 #define RESTRICT /* nothing */
277 #define MUST_CHECK /* nothing */
280 /* Misc definitions */
282 #define NULL (void *)0
289 /* Support for hybrid C/C++ applications. */
291 #define EXTERN_C extern "C"
292 #define EXTERN_C_BEGIN extern "C" {
293 #define EXTERN_C_END }
295 #define EXTERN_C extern
296 #define EXTERN_C_BEGIN /* nothing */
297 #define EXTERN_C_END /* nothing */
301 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
303 * \name ISO C99 fixed-size types
305 * These should be in <stdint.h>, but many compilers lack them.
308 typedef signed char int8_t;
309 typedef short int int16_t;
310 typedef long int int32_t;
311 typedef unsigned char uint8_t;
312 typedef unsigned short int uint16_t;
313 typedef unsigned long int uint32_t;
315 #elif defined(__GNUC__) && CPU_AVR
316 /* avr-libc is weird... */
317 #include <inttypes.h>
319 /* This is the correct location. */
325 * The ATmega8 has a very small Flash, so we can't afford
326 * to link in support routines for 32bit integer arithmetic.
328 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
329 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
330 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
331 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
332 #define MTIME_INFINITE 0x7FFFL
334 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
335 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
336 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
337 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
338 #define MTIME_INFINITE 0x7FFFFFFFL
341 /*! Bulk storage large enough for both pointers or integers. */
342 typedef void * iptr_t;
343 typedef const void * const_iptr_t;
345 typedef unsigned char sig_t; /*!< Type for signal bits. */
346 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
347 typedef unsigned char page_t; /*!< Type for banked memory pages. */
351 * \name Standard type definitions.
353 * These should be in <sys/types.h> or <stddef.h>, but many compilers
354 * and C libraries lack them.
356 * We check for some common definitions to avoid redefinitions:
358 * glibc, avr-libc: _SIZE_T_DEFINED
359 * Darwin libc: _BSD_SIZE_T_DEFINED_
363 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_))
364 #if CPU_REG_BITS > 32
366 typedef unsigned long size_t;
367 typedef long ssize_t;
369 /* 32bit or 16bit. */
370 typedef unsigned int size_t;
378 * \name Types for hardware registers.
380 * Only use these types for registers whose contents can
381 * be changed asynchronously by external hardware.
386 /* Registers can be accessed only through 16-bit pointers */
387 typedef volatile uint16_t reg16_t;
389 typedef volatile uint8_t reg8_t;
390 typedef volatile uint16_t reg16_t;
391 typedef volatile uint32_t reg32_t;
396 /* Quasi-ANSI macros */
399 * Return the byte offset of the member \a m in struct \a s.
401 * \note This macro should be defined in "stddef.h" and is sometimes
402 * compiler-specific (g++ has a builtin for it).
404 #define offsetof(s,m) (size_t)&(((s *)0)->m)
408 * Count the number of elements in the static array \a a.
410 * \note This macro is non-standard, but implements a very common idiom
412 #define countof(a) (sizeof(a) / sizeof(*(a)))
415 /*! Issue a compilation error if the \a condition is false */
416 #define STATIC_ASSERT(condition) \
417 UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
419 #endif /* DEVLIB_COMPILER_H */