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.8 2006/01/16 03:27:49 bernie
18 *#* Rename sig_t to sigbit_t to avoid clash with POSIX.
20 *#* Revision 1.7 2005/11/27 23:33:05 bernie
21 *#* Drop avr-libc hack for missing stdint.h.
23 *#* Revision 1.6 2005/07/19 07:27:31 bernie
24 *#* Don't use CPU_REG_BITS from cpu.h to avoid circular header dependendy.
26 *#* Revision 1.5 2005/06/27 21:24:37 bernie
27 *#* ticks_t: New typedef.
29 *#* Revision 1.4 2005/06/14 06:15:10 bernie
30 *#* Add X86_64 support.
32 *#* Revision 1.3 2005/04/12 01:37:01 bernie
33 *#* Metrowerks touchups from HeCo.
35 *#* Revision 1.2 2005/04/11 19:10:27 bernie
36 *#* Include top-level headers from cfg/ subdir.
38 *#* Revision 1.1 2005/04/11 19:04:13 bernie
39 *#* Move top-level headers to cfg/ subdir.
41 *#* Revision 1.44 2005/03/29 06:39:59 bernie
42 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
44 *#* Revision 1.43 2005/03/01 23:15:12 bernie
45 *#* Remove compatibility hack.
47 *#* Revision 1.42 2005/02/16 20:28:46 bernie
48 *#* Move PGM macros to mware/pgm.h
50 *#* Revision 1.41 2005/01/22 04:19:22 bernie
51 *#* MTIME_INFINITE: New constant.
53 *#* Revision 1.40 2005/01/20 18:46:04 aleph
54 *#* Add progmem datatypes; PSTR() definition.
56 #ifndef DEVLIB_COMPILER_H
57 #define DEVLIB_COMPILER_H
59 #include <cfg/cpu_detect.h>
62 #if defined __GNUC__ && defined __GNUC_MINOR__
63 #define GNUC_PREREQ(maj, min) \
64 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
66 #define GNUC_PREREQ(maj, min) 0
69 /* Some CW versions do not allow enabling C99 from the settings panel. */
70 #if defined(__MWERKS__)
74 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
75 #define COMPILER_C99 1
77 #define COMPILER_C99 0
81 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
82 #define PP_CAT(x,y) PP_CAT__(x,y)
83 #define PP_CAT__(x,y) x ## y
84 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
85 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
86 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
88 /*! String-ize a token (allowing macros to expand) */
89 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
90 #define PP_STRINGIZE__(x) #x
93 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
94 #pragma language=extended
95 #define INTERRUPT(x) interrupt [x]
96 #define REGISTER shortad
97 #define INLINE /* unsupported */
100 * Imported from <longjmp.h>. Unfortunately, we can't just include
101 * this header because it typedefs jmp_buf to be an array of chars.
102 * This would allow the compiler to place the buffer on an odd address.
103 * The CPU _should_ be able to perform word accesses to
104 * unaligned data, but there are *BUGS* in the 80196KC with
105 * some combinations of opcodes and addressing modes. One of
106 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
107 * provided by the IAR compiler ANSI C library. When ?GR contains
108 * an odd address, surprisingly the CPU will copy the high order
109 * byte of the source operand (SP) in the low order byte of the
110 * destination operand (the memory location pointed to by ?GR).
112 * We also need to replace the library setjmp()/longjmp() with
113 * our own versions because the IAR implementation "forgets" to
114 * save the contents of local registers (?LR).
118 void *sp; /* Stack pointer */
119 void *return_addr; /* Return address */
120 int lr[6]; /* 6 local registers */
123 typedef struct _JMP_BUF jmp_buf[1];
125 int setjmp(jmp_buf env);
126 void longjmp(jmp_buf env, int val);
128 /* Fake bool support */
131 typedef unsigned char bool;
133 #elif defined(_MSC_VER) /* Win32 emulation support */
135 /* MSVC doesn't provide <stdbool.h>. */
140 #endif /* !__cplusplus */
142 /* These C99 functions are oddly named in MSVCRT32.lib */
143 #define snprintf _snprintf
144 #define vsnprintf _vsnprintf
146 #elif defined(__GNUC__)
148 /* Compiler features */
149 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
150 #define COMPILER_TYPEOF 1
151 #define COMPILER_STATEMENT_EXPRESSIONS 1
154 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
155 #define NORETURN __attribute__((__noreturn__))
156 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
157 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
158 #define USED_VAR(type,name) __attribute__((__used__)) type name
159 #define INLINE static inline __attribute__((__always_inline__))
160 #define LIKELY(x) __builtin_expect(!!(x), 1)
161 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
162 #define PURE_FUNC __attribute__((pure))
163 #define CONST_FUNC __attribute__((const))
164 #define UNUSED_FUNC __attribute__((unused))
165 #define USED_FUNC __attribute__((__used__))
166 #define RESTRICT __restrict__
167 #define MUST_CHECK __attribute__((warn_unused_result))
169 #define DEPRECATED __attribute__((__deprecated__))
172 /* Include some standard C89/C99 stuff */
178 * Disallow some C++ keywords as identifiers in C programs,
179 * for improved portability.
181 #pragma GCC poison new delete class template typename
182 #pragma GCC poison private protected public operator
183 #pragma GCC poison friend mutable using namespace
184 #pragma GCC poison cin cout cerr clog
187 #elif defined(__MWERKS__)
189 /* Compiler features */
190 #define COMPILER_VARIADIC_MACROS 1
191 #define COMPILER_TYPEOF 1
192 #define COMPILER_STATEMENT_EXPRESSIONS 1
194 #define typeof __typeof__
196 #define UNUSED_ARG(type,arg) type
202 // CodeWarrior has size_t as built-in type, but does not define this symbol.
203 #define _SIZE_T_DEFINED
206 #error unknown compiler
210 /* Defaults for compiler extensions. */
213 * \def COMPILER_VARIADIC_MACROS
214 * Support for macros with variable arguments.
216 #ifndef COMPILER_VARIADIC_MACROS
217 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
221 * \def COMPILER_TYPEOF
222 * Support for macros with variable arguments.
224 #ifndef COMPILER_TYPEOF
225 #define COMPILER_TYPEOF 0
229 * \def COMPILER_STATEMENT_EXPRESSIONS
230 * Support for macros with variable arguments.
232 #ifndef COMPILER_STATEMENT_EXPRESSIONS
233 #define COMPILER_STATEMENT_EXPRESSIONS 0
236 /* A few defaults for missing compiler features. */
238 #define INLINE static inline
241 #define NORETURN /* nothing */
244 #define FORMAT(type,fmt,first) /* nothing */
247 #define DEPRECATED /* nothing */
250 #define UNUSED_ARG(type,arg) type arg
252 #define UNUSED UNUSED_ARG /* OBSOLETE */
254 #define UNUSED_VAR(type,name) type name
257 #define USED_VAR(type,name) type name
260 #define REGISTER /* nothing */
263 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
269 #define UNLIKELY(x) x
272 #define PURE_FUNC /* nothing */
275 #define CONST_FUNC /* nothing */
278 #define UNUSED_FUNC /* nothing */
281 #define USED_FUNC /* nothing */
284 #define RESTRICT /* nothing */
287 #define MUST_CHECK /* nothing */
290 /* Misc definitions */
292 #define NULL (void *)0
299 /* Support for hybrid C/C++ applications. */
301 #define EXTERN_C extern "C"
302 #define EXTERN_C_BEGIN extern "C" {
303 #define EXTERN_C_END }
305 #define EXTERN_C extern
306 #define EXTERN_C_BEGIN /* nothing */
307 #define EXTERN_C_END /* nothing */
311 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
313 * \name ISO C99 fixed-size types
315 * These should be in <stdint.h>, but many compilers lack them.
318 typedef signed char int8_t;
319 typedef unsigned char uint8_t;
320 typedef short int int16_t;
321 typedef unsigned short int uint16_t;
322 typedef long int int32_t; /* _WIN64 safe */
323 typedef unsigned long int uint32_t; /* _WIN64 safe */
326 typedef __int64 int64_t;
327 typedef unsigned __int64 uint64_t;
329 typedef long long int64_t;
330 typedef unsigned long long uint64_t;
334 /* This is the standard location. */
340 * The ATmega8 has a very small Flash, so we can't afford
341 * to link in support routines for 32bit integer arithmetic.
343 typedef int16_t ticks_t; /*!< Type for time expressed in ticks. */
344 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
345 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
346 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
347 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
348 #define MTIME_INFINITE 0x7FFFL
350 typedef int32_t ticks_t; /*!< Type for time expressed in ticks. */
351 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
352 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
353 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
354 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
355 #define MTIME_INFINITE 0x7FFFFFFFL
358 /*! Bulk storage large enough for both pointers or integers. */
359 typedef void * iptr_t;
361 /*! Bulk storage large enough for both pointers to constants or integers. */
362 typedef const void * const_iptr_t;
364 typedef unsigned char sigbit_t; /*!< Type for signal bits. */
365 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
366 typedef unsigned char page_t; /*!< Type for banked memory pages. */
370 * \name Standard type definitions.
372 * These should be in <sys/types.h> or <stddef.h>, but many compilers
373 * and C libraries lack them.
375 * We check for some common definitions to avoid redefinitions:
377 * glibc, avr-libc: _SIZE_T_DEFINED
378 * Darwin libc: _BSD_SIZE_T_DEFINED_
382 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_))
384 /* 32bit or 64bit (32bit for _WIN64). */
385 typedef unsigned long size_t;
391 #if !(defined(ssize_t) || defined(__ssize_t_defined))
393 /* 32bit or 64bit (32bit for _WIN64). */
394 typedef long ssize_t;
396 /* 16bit (missing in avr-libc's sys/types.h). */
406 * \name Types for hardware registers.
408 * Only use these types for registers whose contents can
409 * be changed asynchronously by external hardware.
414 /* Registers can be accessed only through 16-bit pointers */
415 typedef volatile uint16_t reg16_t;
417 typedef volatile uint8_t reg8_t;
418 typedef volatile uint16_t reg16_t;
419 typedef volatile uint32_t reg32_t;
424 /* Quasi-ANSI macros */
427 * Return the byte offset of the member \a m in struct \a s.
429 * \note This macro should be defined in "stddef.h" and is sometimes
430 * compiler-specific (g++ has a builtin for it).
432 #define offsetof(s,m) (size_t)&(((s *)0)->m)
436 * Count the number of elements in the static array \a a.
438 * \note This macro is non-standard, but implements a very common idiom
440 #define countof(a) (sizeof(a) / sizeof(*(a)))
443 /*! Issue a compilation error if the \a condition is false */
444 #define STATIC_ASSERT(condition) \
445 UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
447 #endif /* DEVLIB_COMPILER_H */