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.2 2005/04/11 19:10:27 bernie
18 *#* Include top-level headers from cfg/ subdir.
20 *#* Revision 1.1 2005/04/11 19:04:13 bernie
21 *#* Move top-level headers to cfg/ subdir.
23 *#* Revision 1.44 2005/03/29 06:39:59 bernie
24 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
26 *#* Revision 1.43 2005/03/01 23:15:12 bernie
27 *#* Remove compatibility hack.
29 *#* Revision 1.42 2005/02/16 20:28:46 bernie
30 *#* Move PGM macros to mware/pgm.h
32 *#* Revision 1.41 2005/01/22 04:19:22 bernie
33 *#* MTIME_INFINITE: New constant.
35 *#* Revision 1.40 2005/01/20 18:46:04 aleph
36 *#* Add progmem datatypes; PSTR() definition.
38 #ifndef DEVLIB_COMPILER_H
39 #define DEVLIB_COMPILER_H
41 #include <cfg/cpu_detect.h>
44 #if defined __GNUC__ && defined __GNUC_MINOR__
45 #define GNUC_PREREQ(maj, min) \
46 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
48 #define GNUC_PREREQ(maj, min) 0
51 /* Some CW versions do not allow enabling C99 from the settings panel. */
52 #if defined(__MWERKS__)
56 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
57 #define COMPILER_C99 1
59 #define COMPILER_C99 0
63 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
64 #define PP_CAT(x,y) PP_CAT__(x,y)
65 #define PP_CAT__(x,y) x ## y
66 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
67 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
68 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
70 /*! String-ize a token (allowing macros to expand) */
71 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
72 #define PP_STRINGIZE__(x) #x
75 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
76 #pragma language=extended
77 #define INTERRUPT(x) interrupt [x]
78 #define REGISTER shortad
79 #define INLINE /* unsupported */
82 * Imported from <longjmp.h>. Unfortunately, we can't just include
83 * this header because it typedefs jmp_buf to be an array of chars.
84 * This would allow the compiler to place the buffer on an odd address.
85 * The CPU _should_ be able to perform word accesses to
86 * unaligned data, but there are *BUGS* in the 80196KC with
87 * some combinations of opcodes and addressing modes. One of
88 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
89 * provided by the IAR compiler ANSI C library. When ?GR contains
90 * an odd address, surprisingly the CPU will copy the high order
91 * byte of the source operand (SP) in the low order byte of the
92 * destination operand (the memory location pointed to by ?GR).
94 * We also need to replace the library setjmp()/longjmp() with
95 * our own versions because the IAR implementation "forgets" to
96 * save the contents of local registers (?LR).
100 void *sp; /* Stack pointer */
101 void *return_addr; /* Return address */
102 int lr[6]; /* 6 local registers */
105 typedef struct _JMP_BUF jmp_buf[1];
107 int setjmp(jmp_buf env);
108 void longjmp(jmp_buf env, int val);
110 /* Fake bool support */
113 typedef unsigned char bool;
115 #elif defined(_MSC_VER) /* Win32 emulation support */
117 /* MSVC doesn't provide <stdbool.h>. */
122 #endif /* !__cplusplus */
124 /* These C99 functions are oddly named in MSVCRT32.lib */
125 #define snprintf _snprintf
126 #define vsnprintf _vsnprintf
128 #elif defined(__GNUC__)
130 /* Compiler features */
131 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
132 #define COMPILER_TYPEOF 1
133 #define COMPILER_STATEMENT_EXPRESSIONS 1
136 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
137 #define NORETURN __attribute__((__noreturn__))
138 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
139 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
140 #define USED_VAR(type,name) __attribute__((__used__)) type name
141 #define INLINE static inline __attribute__((__always_inline__))
142 #define LIKELY(x) __builtin_expect(!!(x), 1)
143 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
144 #define PURE_FUNC __attribute__((pure))
145 #define CONST_FUNC __attribute__((const))
146 #define UNUSED_FUNC __attribute__((unused))
147 #define USED_FUNC __attribute__((__used__))
148 #define RESTRICT __restrict__
149 #define MUST_CHECK __attribute__((warn_unused_result))
151 #define DEPRECATED __attribute__((__deprecated__))
158 /* Include some standard C89/C99 stuff */
165 * Disallow some C++ keywords as identifiers in C programs,
166 * for improved portability.
168 #pragma GCC poison new delete class template typename
169 #pragma GCC poison private protected public operator
170 #pragma GCC poison friend mutable using namespace
171 #pragma GCC poison cin cout cerr clog
174 #elif defined(__MWERKS__) && CPU_DSP56K
176 /* Compiler features */
177 #define COMPILER_VARIADIC_MACROS 1
178 #define COMPILER_TYPEOF 1
179 #define COMPILER_STATEMENT_EXPRESSIONS 1
181 #define typeof __typeof__
187 // CodeWarrior has size_t as built-in type, but does not define this symbol.
188 #define _SIZE_T_DEFINED
191 #error unknown compiler
195 /* Defaults for compiler extensions. */
198 * \def COMPILER_VARIADIC_MACROS
199 * Support for macros with variable arguments.
201 #ifndef COMPILER_VARIADIC_MACROS
202 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
206 * \def COMPILER_TYPEOF
207 * Support for macros with variable arguments.
209 #ifndef COMPILER_TYPEOF
210 #define COMPILER_TYPEOF 0
214 * \def COMPILER_STATEMENT_EXPRESSIONS
215 * Support for macros with variable arguments.
217 #ifndef COMPILER_STATEMENT_EXPRESSIONS
218 #define COMPILER_STATEMENT_EXPRESSIONS 0
221 /* A few defaults for missing compiler features. */
223 #define INLINE static inline
226 #define NORETURN /* nothing */
229 #define FORMAT(type,fmt,first) /* nothing */
232 #define DEPRECATED /* nothing */
235 #define UNUSED_ARG(type,arg) type arg
237 #define UNUSED UNUSED_ARG /* OBSOLETE */
239 #define UNUSED_VAR(type,name) type name
242 #define USED_VAR(type,name) type name
245 #define REGISTER /* nothing */
248 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
254 #define UNLIKELY(x) x
257 #define PURE_FUNC /* nothing */
260 #define CONST_FUNC /* nothing */
263 #define UNUSED_FUNC /* nothing */
266 #define USED_FUNC /* nothing */
269 #define RESTRICT /* nothing */
272 #define MUST_CHECK /* nothing */
275 /* Misc definitions */
277 #define NULL (void *)0
284 /* Support for hybrid C/C++ applications. */
286 #define EXTERN_C extern "C"
287 #define EXTERN_C_BEGIN extern "C" {
288 #define EXTERN_C_END }
290 #define EXTERN_C extern
291 #define EXTERN_C_BEGIN /* nothing */
292 #define EXTERN_C_END /* nothing */
296 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
298 * \name ISO C99 fixed-size types
300 * These should be in <stdint.h>, but many compilers lack them.
303 typedef signed char int8_t;
304 typedef short int int16_t;
305 typedef long int int32_t;
306 typedef unsigned char uint8_t;
307 typedef unsigned short int uint16_t;
308 typedef unsigned long int uint32_t;
310 #elif defined(__GNUC__) && CPU_AVR
311 /* avr-libc is weird... */
312 #include <inttypes.h>
314 /* This is the correct location. */
320 * The ATmega8 has a very small Flash, so we can't afford
321 * to link in support routines for 32bit integer arithmetic.
323 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
324 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
325 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
326 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
327 #define MTIME_INFINITE 0x7FFFL
329 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
330 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
331 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
332 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
333 #define MTIME_INFINITE 0x7FFFFFFFL
336 /*! Bulk storage large enough for both pointers or integers. */
337 typedef void * iptr_t;
338 typedef const void * const_iptr_t;
340 typedef unsigned char sig_t; /*!< Type for signal bits. */
341 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
342 typedef unsigned char page_t; /*!< Type for banked memory pages. */
346 * \name Standard type definitions.
348 * These should be in <sys/types.h> or <stddef.h>, but many compilers
349 * and C libraries lack them.
351 * We check for some common definitions to avoid redefinitions:
353 * glibc, avr-libc: _SIZE_T_DEFINED
354 * Darwin libc: _BSD_SIZE_T_DEFINED_
358 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_))
359 #if CPU_REG_BITS > 32
361 typedef unsigned long size_t;
362 typedef long ssize_t;
364 /* 32bit or 16bit. */
365 typedef unsigned int size_t;
373 * \name Types for hardware registers.
375 * Only use these types for registers whose contents can
376 * be changed asynchronously by external hardware.
381 /* Registers can be accessed only through 16-bit pointers */
382 typedef volatile uint16_t reg16_t;
384 typedef volatile uint8_t reg8_t;
385 typedef volatile uint16_t reg16_t;
386 typedef volatile uint32_t reg32_t;
391 /* Quasi-ANSI macros */
394 * Return the byte offset of the member \a m in struct \a s.
396 * \note This macro should be defined in "stddef.h" and is sometimes
397 * compiler-specific (g++ has a builtin for it).
399 #define offsetof(s,m) (size_t)&(((s *)0)->m)
403 * Count the number of elements in the static array \a a.
405 * \note This macro is non-standard, but implements a very common idiom
407 #define countof(a) (sizeof(a) / sizeof(*(a)))
410 /*! Issue a compilation error if the \a condition is false */
411 #define STATIC_ASSERT(condition) \
412 UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
414 #endif /* DEVLIB_COMPILER_H */