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.5 2005/06/27 21:24:37 bernie
18 *#* ticks_t: New typedef.
20 *#* Revision 1.4 2005/06/14 06:15:10 bernie
21 *#* Add X86_64 support.
23 *#* Revision 1.3 2005/04/12 01:37:01 bernie
24 *#* Metrowerks touchups from HeCo.
26 *#* Revision 1.2 2005/04/11 19:10:27 bernie
27 *#* Include top-level headers from cfg/ subdir.
29 *#* Revision 1.1 2005/04/11 19:04:13 bernie
30 *#* Move top-level headers to cfg/ subdir.
32 *#* Revision 1.44 2005/03/29 06:39:59 bernie
33 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
35 *#* Revision 1.43 2005/03/01 23:15:12 bernie
36 *#* Remove compatibility hack.
38 *#* Revision 1.42 2005/02/16 20:28:46 bernie
39 *#* Move PGM macros to mware/pgm.h
41 *#* Revision 1.41 2005/01/22 04:19:22 bernie
42 *#* MTIME_INFINITE: New constant.
44 *#* Revision 1.40 2005/01/20 18:46:04 aleph
45 *#* Add progmem datatypes; PSTR() definition.
47 #ifndef DEVLIB_COMPILER_H
48 #define DEVLIB_COMPILER_H
50 #include <cfg/cpu_detect.h>
53 #if defined __GNUC__ && defined __GNUC_MINOR__
54 #define GNUC_PREREQ(maj, min) \
55 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
57 #define GNUC_PREREQ(maj, min) 0
60 /* Some CW versions do not allow enabling C99 from the settings panel. */
61 #if defined(__MWERKS__)
65 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
66 #define COMPILER_C99 1
68 #define COMPILER_C99 0
72 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
73 #define PP_CAT(x,y) PP_CAT__(x,y)
74 #define PP_CAT__(x,y) x ## y
75 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
76 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
77 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
79 /*! String-ize a token (allowing macros to expand) */
80 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
81 #define PP_STRINGIZE__(x) #x
84 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
85 #pragma language=extended
86 #define INTERRUPT(x) interrupt [x]
87 #define REGISTER shortad
88 #define INLINE /* unsupported */
91 * Imported from <longjmp.h>. Unfortunately, we can't just include
92 * this header because it typedefs jmp_buf to be an array of chars.
93 * This would allow the compiler to place the buffer on an odd address.
94 * The CPU _should_ be able to perform word accesses to
95 * unaligned data, but there are *BUGS* in the 80196KC with
96 * some combinations of opcodes and addressing modes. One of
97 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
98 * provided by the IAR compiler ANSI C library. When ?GR contains
99 * an odd address, surprisingly the CPU will copy the high order
100 * byte of the source operand (SP) in the low order byte of the
101 * destination operand (the memory location pointed to by ?GR).
103 * We also need to replace the library setjmp()/longjmp() with
104 * our own versions because the IAR implementation "forgets" to
105 * save the contents of local registers (?LR).
109 void *sp; /* Stack pointer */
110 void *return_addr; /* Return address */
111 int lr[6]; /* 6 local registers */
114 typedef struct _JMP_BUF jmp_buf[1];
116 int setjmp(jmp_buf env);
117 void longjmp(jmp_buf env, int val);
119 /* Fake bool support */
122 typedef unsigned char bool;
124 #elif defined(_MSC_VER) /* Win32 emulation support */
126 /* MSVC doesn't provide <stdbool.h>. */
131 #endif /* !__cplusplus */
133 /* These C99 functions are oddly named in MSVCRT32.lib */
134 #define snprintf _snprintf
135 #define vsnprintf _vsnprintf
137 #elif defined(__GNUC__)
139 /* Compiler features */
140 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
141 #define COMPILER_TYPEOF 1
142 #define COMPILER_STATEMENT_EXPRESSIONS 1
145 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
146 #define NORETURN __attribute__((__noreturn__))
147 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
148 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
149 #define USED_VAR(type,name) __attribute__((__used__)) type name
150 #define INLINE static inline __attribute__((__always_inline__))
151 #define LIKELY(x) __builtin_expect(!!(x), 1)
152 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
153 #define PURE_FUNC __attribute__((pure))
154 #define CONST_FUNC __attribute__((const))
155 #define UNUSED_FUNC __attribute__((unused))
156 #define USED_FUNC __attribute__((__used__))
157 #define RESTRICT __restrict__
158 #define MUST_CHECK __attribute__((warn_unused_result))
160 #define DEPRECATED __attribute__((__deprecated__))
167 /* Include some standard C89/C99 stuff */
174 * Disallow some C++ keywords as identifiers in C programs,
175 * for improved portability.
177 #pragma GCC poison new delete class template typename
178 #pragma GCC poison private protected public operator
179 #pragma GCC poison friend mutable using namespace
180 #pragma GCC poison cin cout cerr clog
183 #elif defined(__MWERKS__)
185 /* Compiler features */
186 #define COMPILER_VARIADIC_MACROS 1
187 #define COMPILER_TYPEOF 1
188 #define COMPILER_STATEMENT_EXPRESSIONS 1
190 #define typeof __typeof__
192 #define UNUSED_ARG(type,arg) type
198 // CodeWarrior has size_t as built-in type, but does not define this symbol.
199 #define _SIZE_T_DEFINED
202 #error unknown compiler
206 /* Defaults for compiler extensions. */
209 * \def COMPILER_VARIADIC_MACROS
210 * Support for macros with variable arguments.
212 #ifndef COMPILER_VARIADIC_MACROS
213 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
217 * \def COMPILER_TYPEOF
218 * Support for macros with variable arguments.
220 #ifndef COMPILER_TYPEOF
221 #define COMPILER_TYPEOF 0
225 * \def COMPILER_STATEMENT_EXPRESSIONS
226 * Support for macros with variable arguments.
228 #ifndef COMPILER_STATEMENT_EXPRESSIONS
229 #define COMPILER_STATEMENT_EXPRESSIONS 0
232 /* A few defaults for missing compiler features. */
234 #define INLINE static inline
237 #define NORETURN /* nothing */
240 #define FORMAT(type,fmt,first) /* nothing */
243 #define DEPRECATED /* nothing */
246 #define UNUSED_ARG(type,arg) type arg
248 #define UNUSED UNUSED_ARG /* OBSOLETE */
250 #define UNUSED_VAR(type,name) type name
253 #define USED_VAR(type,name) type name
256 #define REGISTER /* nothing */
259 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
265 #define UNLIKELY(x) x
268 #define PURE_FUNC /* nothing */
271 #define CONST_FUNC /* nothing */
274 #define UNUSED_FUNC /* nothing */
277 #define USED_FUNC /* nothing */
280 #define RESTRICT /* nothing */
283 #define MUST_CHECK /* nothing */
286 /* Misc definitions */
288 #define NULL (void *)0
295 /* Support for hybrid C/C++ applications. */
297 #define EXTERN_C extern "C"
298 #define EXTERN_C_BEGIN extern "C" {
299 #define EXTERN_C_END }
301 #define EXTERN_C extern
302 #define EXTERN_C_BEGIN /* nothing */
303 #define EXTERN_C_END /* nothing */
307 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
309 * \name ISO C99 fixed-size types
311 * These should be in <stdint.h>, but many compilers lack them.
314 typedef signed char int8_t;
315 typedef unsigned char uint8_t;
316 typedef short int int16_t;
317 typedef unsigned short int uint16_t;
318 typedef long int int32_t; /* _WIN64 safe */
319 typedef unsigned long int uint32_t; /* _WIN64 safe */
322 typedef __int64 int64_t;
323 typedef unsigned __int64 uint64_t;
325 typedef long long int64_t;
326 typedef unsigned long long uint64_t;
329 #elif defined(__GNUC__) && CPU_AVR
330 /* avr-libc is weird... (Fixed in avr-libc-1.2, hack to be removed soon) */
331 #include <inttypes.h>
333 /* This is the correct location. */
339 * The ATmega8 has a very small Flash, so we can't afford
340 * to link in support routines for 32bit integer arithmetic.
342 typedef int16_t ticks_t; /*!< Type for time expressed in ticks. */
343 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
344 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
345 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
346 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
347 #define MTIME_INFINITE 0x7FFFL
349 typedef int32_t ticks_t; /*!< Type for time expressed in ticks. */
350 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
351 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
352 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
353 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
354 #define MTIME_INFINITE 0x7FFFFFFFL
357 /*! Bulk storage large enough for both pointers or integers. */
358 typedef void * iptr_t;
360 /*! Bulk storage large enough for both pointers to constants or integers. */
361 typedef const void * const_iptr_t;
363 typedef unsigned char sig_t; /*!< Type for signal bits. */
364 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
365 typedef unsigned char page_t; /*!< Type for banked memory pages. */
369 * \name Standard type definitions.
371 * These should be in <sys/types.h> or <stddef.h>, but many compilers
372 * and C libraries lack them.
374 * We check for some common definitions to avoid redefinitions:
376 * glibc, avr-libc: _SIZE_T_DEFINED
377 * Darwin libc: _BSD_SIZE_T_DEFINED_
381 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_))
382 #if CPU_REG_BITS > 32
384 typedef unsigned long size_t;
386 /* 32bit or 16bit. */
387 typedef unsigned int size_t;
391 #if !(defined(ssize_t) || defined(__ssize_t_defined))
392 #if CPU_REG_BITS > 32
393 /* 64bit (32bit for _WIN64). */
394 typedef long ssize_t;
396 /* 32bit or 16bit. */
404 * \name Types for hardware registers.
406 * Only use these types for registers whose contents can
407 * be changed asynchronously by external hardware.
412 /* Registers can be accessed only through 16-bit pointers */
413 typedef volatile uint16_t reg16_t;
415 typedef volatile uint8_t reg8_t;
416 typedef volatile uint16_t reg16_t;
417 typedef volatile uint32_t reg32_t;
422 /* Quasi-ANSI macros */
425 * Return the byte offset of the member \a m in struct \a s.
427 * \note This macro should be defined in "stddef.h" and is sometimes
428 * compiler-specific (g++ has a builtin for it).
430 #define offsetof(s,m) (size_t)&(((s *)0)->m)
434 * Count the number of elements in the static array \a a.
436 * \note This macro is non-standard, but implements a very common idiom
438 #define countof(a) (sizeof(a) / sizeof(*(a)))
441 /*! Issue a compilation error if the \a condition is false */
442 #define STATIC_ASSERT(condition) \
443 UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
445 #endif /* DEVLIB_COMPILER_H */