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.7 2005/11/27 23:33:05 bernie
18 *#* Drop avr-libc hack for missing stdint.h.
20 *#* Revision 1.6 2005/07/19 07:27:31 bernie
21 *#* Don't use CPU_REG_BITS from cpu.h to avoid circular header dependendy.
23 *#* Revision 1.5 2005/06/27 21:24:37 bernie
24 *#* ticks_t: New typedef.
26 *#* Revision 1.4 2005/06/14 06:15:10 bernie
27 *#* Add X86_64 support.
29 *#* Revision 1.3 2005/04/12 01:37:01 bernie
30 *#* Metrowerks touchups from HeCo.
32 *#* Revision 1.2 2005/04/11 19:10:27 bernie
33 *#* Include top-level headers from cfg/ subdir.
35 *#* Revision 1.1 2005/04/11 19:04:13 bernie
36 *#* Move top-level headers to cfg/ subdir.
38 *#* Revision 1.44 2005/03/29 06:39:59 bernie
39 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
41 *#* Revision 1.43 2005/03/01 23:15:12 bernie
42 *#* Remove compatibility hack.
44 *#* Revision 1.42 2005/02/16 20:28:46 bernie
45 *#* Move PGM macros to mware/pgm.h
47 *#* Revision 1.41 2005/01/22 04:19:22 bernie
48 *#* MTIME_INFINITE: New constant.
50 *#* Revision 1.40 2005/01/20 18:46:04 aleph
51 *#* Add progmem datatypes; PSTR() definition.
53 #ifndef DEVLIB_COMPILER_H
54 #define DEVLIB_COMPILER_H
56 #include <cfg/cpu_detect.h>
59 #if defined __GNUC__ && defined __GNUC_MINOR__
60 #define GNUC_PREREQ(maj, min) \
61 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
63 #define GNUC_PREREQ(maj, min) 0
66 /* Some CW versions do not allow enabling C99 from the settings panel. */
67 #if defined(__MWERKS__)
71 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
72 #define COMPILER_C99 1
74 #define COMPILER_C99 0
78 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
79 #define PP_CAT(x,y) PP_CAT__(x,y)
80 #define PP_CAT__(x,y) x ## y
81 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
82 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
83 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
85 /*! String-ize a token (allowing macros to expand) */
86 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
87 #define PP_STRINGIZE__(x) #x
90 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
91 #pragma language=extended
92 #define INTERRUPT(x) interrupt [x]
93 #define REGISTER shortad
94 #define INLINE /* unsupported */
97 * Imported from <longjmp.h>. Unfortunately, we can't just include
98 * this header because it typedefs jmp_buf to be an array of chars.
99 * This would allow the compiler to place the buffer on an odd address.
100 * The CPU _should_ be able to perform word accesses to
101 * unaligned data, but there are *BUGS* in the 80196KC with
102 * some combinations of opcodes and addressing modes. One of
103 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
104 * provided by the IAR compiler ANSI C library. When ?GR contains
105 * an odd address, surprisingly the CPU will copy the high order
106 * byte of the source operand (SP) in the low order byte of the
107 * destination operand (the memory location pointed to by ?GR).
109 * We also need to replace the library setjmp()/longjmp() with
110 * our own versions because the IAR implementation "forgets" to
111 * save the contents of local registers (?LR).
115 void *sp; /* Stack pointer */
116 void *return_addr; /* Return address */
117 int lr[6]; /* 6 local registers */
120 typedef struct _JMP_BUF jmp_buf[1];
122 int setjmp(jmp_buf env);
123 void longjmp(jmp_buf env, int val);
125 /* Fake bool support */
128 typedef unsigned char bool;
130 #elif defined(_MSC_VER) /* Win32 emulation support */
132 /* MSVC doesn't provide <stdbool.h>. */
137 #endif /* !__cplusplus */
139 /* These C99 functions are oddly named in MSVCRT32.lib */
140 #define snprintf _snprintf
141 #define vsnprintf _vsnprintf
143 #elif defined(__GNUC__)
145 /* Compiler features */
146 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
147 #define COMPILER_TYPEOF 1
148 #define COMPILER_STATEMENT_EXPRESSIONS 1
151 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
152 #define NORETURN __attribute__((__noreturn__))
153 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
154 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
155 #define USED_VAR(type,name) __attribute__((__used__)) type name
156 #define INLINE static inline __attribute__((__always_inline__))
157 #define LIKELY(x) __builtin_expect(!!(x), 1)
158 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
159 #define PURE_FUNC __attribute__((pure))
160 #define CONST_FUNC __attribute__((const))
161 #define UNUSED_FUNC __attribute__((unused))
162 #define USED_FUNC __attribute__((__used__))
163 #define RESTRICT __restrict__
164 #define MUST_CHECK __attribute__((warn_unused_result))
166 #define DEPRECATED __attribute__((__deprecated__))
169 /* Include some standard C89/C99 stuff */
175 * Disallow some C++ keywords as identifiers in C programs,
176 * for improved portability.
178 #pragma GCC poison new delete class template typename
179 #pragma GCC poison private protected public operator
180 #pragma GCC poison friend mutable using namespace
181 #pragma GCC poison cin cout cerr clog
184 #elif defined(__MWERKS__)
186 /* Compiler features */
187 #define COMPILER_VARIADIC_MACROS 1
188 #define COMPILER_TYPEOF 1
189 #define COMPILER_STATEMENT_EXPRESSIONS 1
191 #define typeof __typeof__
193 #define UNUSED_ARG(type,arg) type
199 // CodeWarrior has size_t as built-in type, but does not define this symbol.
200 #define _SIZE_T_DEFINED
203 #error unknown compiler
207 /* Defaults for compiler extensions. */
210 * \def COMPILER_VARIADIC_MACROS
211 * Support for macros with variable arguments.
213 #ifndef COMPILER_VARIADIC_MACROS
214 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
218 * \def COMPILER_TYPEOF
219 * Support for macros with variable arguments.
221 #ifndef COMPILER_TYPEOF
222 #define COMPILER_TYPEOF 0
226 * \def COMPILER_STATEMENT_EXPRESSIONS
227 * Support for macros with variable arguments.
229 #ifndef COMPILER_STATEMENT_EXPRESSIONS
230 #define COMPILER_STATEMENT_EXPRESSIONS 0
233 /* A few defaults for missing compiler features. */
235 #define INLINE static inline
238 #define NORETURN /* nothing */
241 #define FORMAT(type,fmt,first) /* nothing */
244 #define DEPRECATED /* nothing */
247 #define UNUSED_ARG(type,arg) type arg
249 #define UNUSED UNUSED_ARG /* OBSOLETE */
251 #define UNUSED_VAR(type,name) type name
254 #define USED_VAR(type,name) type name
257 #define REGISTER /* nothing */
260 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
266 #define UNLIKELY(x) x
269 #define PURE_FUNC /* nothing */
272 #define CONST_FUNC /* nothing */
275 #define UNUSED_FUNC /* nothing */
278 #define USED_FUNC /* nothing */
281 #define RESTRICT /* nothing */
284 #define MUST_CHECK /* nothing */
287 /* Misc definitions */
289 #define NULL (void *)0
296 /* Support for hybrid C/C++ applications. */
298 #define EXTERN_C extern "C"
299 #define EXTERN_C_BEGIN extern "C" {
300 #define EXTERN_C_END }
302 #define EXTERN_C extern
303 #define EXTERN_C_BEGIN /* nothing */
304 #define EXTERN_C_END /* nothing */
308 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
310 * \name ISO C99 fixed-size types
312 * These should be in <stdint.h>, but many compilers lack them.
315 typedef signed char int8_t;
316 typedef unsigned char uint8_t;
317 typedef short int int16_t;
318 typedef unsigned short int uint16_t;
319 typedef long int int32_t; /* _WIN64 safe */
320 typedef unsigned long int uint32_t; /* _WIN64 safe */
323 typedef __int64 int64_t;
324 typedef unsigned __int64 uint64_t;
326 typedef long long int64_t;
327 typedef unsigned long long uint64_t;
331 /* This is the standard location. */
337 * The ATmega8 has a very small Flash, so we can't afford
338 * to link in support routines for 32bit integer arithmetic.
340 typedef int16_t ticks_t; /*!< Type for time expressed in ticks. */
341 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
342 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
343 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
344 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
345 #define MTIME_INFINITE 0x7FFFL
347 typedef int32_t ticks_t; /*!< Type for time expressed in ticks. */
348 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
349 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
350 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
351 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
352 #define MTIME_INFINITE 0x7FFFFFFFL
355 /*! Bulk storage large enough for both pointers or integers. */
356 typedef void * iptr_t;
358 /*! Bulk storage large enough for both pointers to constants or integers. */
359 typedef const void * const_iptr_t;
361 typedef unsigned char sig_t; /*!< Type for signal bits. */
362 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
363 typedef unsigned char page_t; /*!< Type for banked memory pages. */
367 * \name Standard type definitions.
369 * These should be in <sys/types.h> or <stddef.h>, but many compilers
370 * and C libraries lack them.
372 * We check for some common definitions to avoid redefinitions:
374 * glibc, avr-libc: _SIZE_T_DEFINED
375 * Darwin libc: _BSD_SIZE_T_DEFINED_
379 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_))
381 /* 32bit or 64bit (32bit for _WIN64). */
382 typedef unsigned long size_t;
388 #if !(defined(ssize_t) || defined(__ssize_t_defined))
390 /* 32bit or 64bit (32bit for _WIN64). */
391 typedef long ssize_t;
393 /* 16bit (missing in avr-libc's sys/types.h). */
403 * \name Types for hardware registers.
405 * Only use these types for registers whose contents can
406 * be changed asynchronously by external hardware.
411 /* Registers can be accessed only through 16-bit pointers */
412 typedef volatile uint16_t reg16_t;
414 typedef volatile uint8_t reg8_t;
415 typedef volatile uint16_t reg16_t;
416 typedef volatile uint32_t reg32_t;
421 /* Quasi-ANSI macros */
424 * Return the byte offset of the member \a m in struct \a s.
426 * \note This macro should be defined in "stddef.h" and is sometimes
427 * compiler-specific (g++ has a builtin for it).
429 #define offsetof(s,m) (size_t)&(((s *)0)->m)
433 * Count the number of elements in the static array \a a.
435 * \note This macro is non-standard, but implements a very common idiom
437 #define countof(a) (sizeof(a) / sizeof(*(a)))
440 /*! Issue a compilation error if the \a condition is false */
441 #define STATIC_ASSERT(condition) \
442 UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
444 #endif /* DEVLIB_COMPILER_H */