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.4 2005/06/14 06:15:10 bernie
18 *#* Add X86_64 support.
20 *#* Revision 1.3 2005/04/12 01:37:01 bernie
21 *#* Metrowerks touchups from HeCo.
23 *#* Revision 1.2 2005/04/11 19:10:27 bernie
24 *#* Include top-level headers from cfg/ subdir.
26 *#* Revision 1.1 2005/04/11 19:04:13 bernie
27 *#* Move top-level headers to cfg/ subdir.
29 *#* Revision 1.44 2005/03/29 06:39:59 bernie
30 *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
32 *#* Revision 1.43 2005/03/01 23:15:12 bernie
33 *#* Remove compatibility hack.
35 *#* Revision 1.42 2005/02/16 20:28:46 bernie
36 *#* Move PGM macros to mware/pgm.h
38 *#* Revision 1.41 2005/01/22 04:19:22 bernie
39 *#* MTIME_INFINITE: New constant.
41 *#* Revision 1.40 2005/01/20 18:46:04 aleph
42 *#* Add progmem datatypes; PSTR() definition.
44 #ifndef DEVLIB_COMPILER_H
45 #define DEVLIB_COMPILER_H
47 #include <cfg/cpu_detect.h>
50 #if defined __GNUC__ && defined __GNUC_MINOR__
51 #define GNUC_PREREQ(maj, min) \
52 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
54 #define GNUC_PREREQ(maj, min) 0
57 /* Some CW versions do not allow enabling C99 from the settings panel. */
58 #if defined(__MWERKS__)
62 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
63 #define COMPILER_C99 1
65 #define COMPILER_C99 0
69 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
70 #define PP_CAT(x,y) PP_CAT__(x,y)
71 #define PP_CAT__(x,y) x ## y
72 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
73 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
74 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
76 /*! String-ize a token (allowing macros to expand) */
77 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
78 #define PP_STRINGIZE__(x) #x
81 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
82 #pragma language=extended
83 #define INTERRUPT(x) interrupt [x]
84 #define REGISTER shortad
85 #define INLINE /* unsupported */
88 * Imported from <longjmp.h>. Unfortunately, we can't just include
89 * this header because it typedefs jmp_buf to be an array of chars.
90 * This would allow the compiler to place the buffer on an odd address.
91 * The CPU _should_ be able to perform word accesses to
92 * unaligned data, but there are *BUGS* in the 80196KC with
93 * some combinations of opcodes and addressing modes. One of
94 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
95 * provided by the IAR compiler ANSI C library. When ?GR contains
96 * an odd address, surprisingly the CPU will copy the high order
97 * byte of the source operand (SP) in the low order byte of the
98 * destination operand (the memory location pointed to by ?GR).
100 * We also need to replace the library setjmp()/longjmp() with
101 * our own versions because the IAR implementation "forgets" to
102 * save the contents of local registers (?LR).
106 void *sp; /* Stack pointer */
107 void *return_addr; /* Return address */
108 int lr[6]; /* 6 local registers */
111 typedef struct _JMP_BUF jmp_buf[1];
113 int setjmp(jmp_buf env);
114 void longjmp(jmp_buf env, int val);
116 /* Fake bool support */
119 typedef unsigned char bool;
121 #elif defined(_MSC_VER) /* Win32 emulation support */
123 /* MSVC doesn't provide <stdbool.h>. */
128 #endif /* !__cplusplus */
130 /* These C99 functions are oddly named in MSVCRT32.lib */
131 #define snprintf _snprintf
132 #define vsnprintf _vsnprintf
134 #elif defined(__GNUC__)
136 /* Compiler features */
137 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
138 #define COMPILER_TYPEOF 1
139 #define COMPILER_STATEMENT_EXPRESSIONS 1
142 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
143 #define NORETURN __attribute__((__noreturn__))
144 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
145 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
146 #define USED_VAR(type,name) __attribute__((__used__)) type name
147 #define INLINE static inline __attribute__((__always_inline__))
148 #define LIKELY(x) __builtin_expect(!!(x), 1)
149 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
150 #define PURE_FUNC __attribute__((pure))
151 #define CONST_FUNC __attribute__((const))
152 #define UNUSED_FUNC __attribute__((unused))
153 #define USED_FUNC __attribute__((__used__))
154 #define RESTRICT __restrict__
155 #define MUST_CHECK __attribute__((warn_unused_result))
157 #define DEPRECATED __attribute__((__deprecated__))
164 /* Include some standard C89/C99 stuff */
171 * Disallow some C++ keywords as identifiers in C programs,
172 * for improved portability.
174 #pragma GCC poison new delete class template typename
175 #pragma GCC poison private protected public operator
176 #pragma GCC poison friend mutable using namespace
177 #pragma GCC poison cin cout cerr clog
180 #elif defined(__MWERKS__)
182 /* Compiler features */
183 #define COMPILER_VARIADIC_MACROS 1
184 #define COMPILER_TYPEOF 1
185 #define COMPILER_STATEMENT_EXPRESSIONS 1
187 #define typeof __typeof__
189 #define UNUSED_ARG(type,arg) type
195 // CodeWarrior has size_t as built-in type, but does not define this symbol.
196 #define _SIZE_T_DEFINED
199 #error unknown compiler
203 /* Defaults for compiler extensions. */
206 * \def COMPILER_VARIADIC_MACROS
207 * Support for macros with variable arguments.
209 #ifndef COMPILER_VARIADIC_MACROS
210 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
214 * \def COMPILER_TYPEOF
215 * Support for macros with variable arguments.
217 #ifndef COMPILER_TYPEOF
218 #define COMPILER_TYPEOF 0
222 * \def COMPILER_STATEMENT_EXPRESSIONS
223 * Support for macros with variable arguments.
225 #ifndef COMPILER_STATEMENT_EXPRESSIONS
226 #define COMPILER_STATEMENT_EXPRESSIONS 0
229 /* A few defaults for missing compiler features. */
231 #define INLINE static inline
234 #define NORETURN /* nothing */
237 #define FORMAT(type,fmt,first) /* nothing */
240 #define DEPRECATED /* nothing */
243 #define UNUSED_ARG(type,arg) type arg
245 #define UNUSED UNUSED_ARG /* OBSOLETE */
247 #define UNUSED_VAR(type,name) type name
250 #define USED_VAR(type,name) type name
253 #define REGISTER /* nothing */
256 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
262 #define UNLIKELY(x) x
265 #define PURE_FUNC /* nothing */
268 #define CONST_FUNC /* nothing */
271 #define UNUSED_FUNC /* nothing */
274 #define USED_FUNC /* nothing */
277 #define RESTRICT /* nothing */
280 #define MUST_CHECK /* nothing */
283 /* Misc definitions */
285 #define NULL (void *)0
292 /* Support for hybrid C/C++ applications. */
294 #define EXTERN_C extern "C"
295 #define EXTERN_C_BEGIN extern "C" {
296 #define EXTERN_C_END }
298 #define EXTERN_C extern
299 #define EXTERN_C_BEGIN /* nothing */
300 #define EXTERN_C_END /* nothing */
304 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
306 * \name ISO C99 fixed-size types
308 * These should be in <stdint.h>, but many compilers lack them.
311 typedef signed char int8_t;
312 typedef unsigned char uint8_t;
313 typedef short int int16_t;
314 typedef unsigned short int uint16_t;
315 typedef long int int32_t; /* _WIN64 safe */
316 typedef unsigned long int uint32_t; /* _WIN64 safe */
319 typedef __int64 int64_t;
320 typedef unsigned __int64 uint64_t;
322 typedef long long int64_t;
323 typedef unsigned long long uint64_t;
326 #elif defined(__GNUC__) && CPU_AVR
327 /* avr-libc is weird... (Fixed in avr-libc-1.2, hack to be removed soon) */
328 #include <inttypes.h>
330 /* This is the correct location. */
336 * The ATmega8 has a very small Flash, so we can't afford
337 * to link in support routines for 32bit integer arithmetic.
339 typedef int16_t mtime_t; /*!< Type for time expressed in milliseconds. */
340 typedef int16_t utime_t; /*!< Type for time expressed in microseconds. */
341 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
342 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
343 #define MTIME_INFINITE 0x7FFFL
345 typedef int32_t mtime_t; /*!< Type for time expressed in milliseconds. */
346 typedef int32_t utime_t; /*!< Type for time expressed in microseconds. */
347 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
348 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
349 #define MTIME_INFINITE 0x7FFFFFFFL
352 /*! Bulk storage large enough for both pointers or integers. */
353 typedef void * iptr_t;
354 typedef const void * const_iptr_t;
356 typedef unsigned char sig_t; /*!< Type for signal bits. */
357 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
358 typedef unsigned char page_t; /*!< Type for banked memory pages. */
362 * \name Standard type definitions.
364 * These should be in <sys/types.h> or <stddef.h>, but many compilers
365 * and C libraries lack them.
367 * We check for some common definitions to avoid redefinitions:
369 * glibc, avr-libc: _SIZE_T_DEFINED
370 * Darwin libc: _BSD_SIZE_T_DEFINED_
374 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_))
375 #if CPU_REG_BITS > 32
377 typedef unsigned long size_t;
379 /* 32bit or 16bit. */
380 typedef unsigned int size_t;
384 #if !(defined(ssize_t) || defined(__ssize_t_defined))
385 #if CPU_REG_BITS > 32
386 /* 64bit (32bit for _WIN64). */
387 typedef long ssize_t;
389 /* 32bit or 16bit. */
397 * \name Types for hardware registers.
399 * Only use these types for registers whose contents can
400 * be changed asynchronously by external hardware.
405 /* Registers can be accessed only through 16-bit pointers */
406 typedef volatile uint16_t reg16_t;
408 typedef volatile uint8_t reg8_t;
409 typedef volatile uint16_t reg16_t;
410 typedef volatile uint32_t reg32_t;
415 /* Quasi-ANSI macros */
418 * Return the byte offset of the member \a m in struct \a s.
420 * \note This macro should be defined in "stddef.h" and is sometimes
421 * compiler-specific (g++ has a builtin for it).
423 #define offsetof(s,m) (size_t)&(((s *)0)->m)
427 * Count the number of elements in the static array \a a.
429 * \note This macro is non-standard, but implements a very common idiom
431 #define countof(a) (sizeof(a) / sizeof(*(a)))
434 /*! Issue a compilation error if the \a condition is false */
435 #define STATIC_ASSERT(condition) \
436 UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
438 #endif /* DEVLIB_COMPILER_H */