4 * Copyright 2003, 2004 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 devlib/README for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Additional support macros for compiler independance
18 *#* Revision 1.20 2004/08/29 21:57:58 bernie
19 *#* Move back STATIC_ASSERT() to compiler.h as it's needed in cpu.h;
20 *#* iptr_t, const_iptr_t: Replace IPTR macro with a real typedef.
22 *#* Revision 1.19 2004/08/25 14:12:08 rasky
23 *#* Aggiornato il comment block dei log RCS
25 *#* Revision 1.18 2004/08/24 16:32:37 bernie
26 *#* Document custom types.
28 *#* Revision 1.17 2004/08/24 13:32:14 bernie
29 *#* PP_CAT(), PP_STRINGIZE(): Move back to compiler.h to break circular dependency between cpu.h/compiler.h/macros.h;
30 *#* offsetof(), countof(): Move back to compiler.h to avoid including macros.h almost everywhere;
32 *#* Rename header guards;
33 *#* Don't include arch_config.h in compiler.h as it's not needed there.
35 *#* Revision 1.16 2004/08/14 19:37:57 rasky
36 *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
38 *#* Revision 1.15 2004/08/13 03:23:26 bernie
39 *#* Adjust a few MSVC tweaks from older projects.
41 *#* Revision 1.14 2004/08/10 06:56:29 bernie
42 *#* RESTRICT: New C99-like macro; STATIC_ASSERT: Fix warning for multiple invocation in one file.
44 *#* Revision 1.13 2004/08/02 20:20:29 aleph
45 *#* Merge from project_ks
47 *#* Revision 1.12 2004/08/01 01:21:17 bernie
48 *#* LIKELY(), UNLIKELY(): New compiler-specific macros.
50 *#* Revision 1.11 2004/07/30 14:34:10 rasky
51 *#* Vari fix per documentazione e commenti
52 *#* Aggiunte PP_CATn e STATIC_ASSERT
54 *#* Revision 1.10 2004/07/30 14:15:53 rasky
55 *#* Nuovo supporto unificato per detect della CPU
57 *#* Revision 1.9 2004/07/29 22:57:09 bernie
58 *#* vsprintf(): Remove prototype for backwards compatibility with GCC 3.4; ssize_t: Add definition for inferior compilers.
60 #ifndef DEVLIB_COMPILER_H
61 #define DEVLIB_COMPILER_H
63 #include "cpu_detect.h"
66 #if defined __GNUC__ && defined __GNUC_MINOR__
67 #define GNUC_PREREQ(maj, min) \
68 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
70 #define GNUC_PREREQ(maj, min) 0
73 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
74 #define COMPILER_C99 1
76 #define COMPILER_C99 0
80 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
81 #define PP_CAT(x,y) PP_CAT__(x,y)
82 #define PP_CAT__(x,y) x ## y
83 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
84 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
85 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
87 /*! String-ize a token (allowing macros to expand) */
88 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
89 #define PP_STRINGIZE__(x) #x
92 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
93 #pragma language=extended
94 #define INTERRUPT(x) interrupt [x]
95 #define REGISTER shortad
96 #define INLINE /* unsupported */
98 /* Imported from <longjmp.h>. Unfortunately, we can't just include
99 * this header because it typedefs jmp_buf to be an array of chars.
100 * This would allow the compiler to place the buffer on an odd address.
101 * The CPU _should_ be able to perform word accesses to
102 * unaligned data, but there are *BUGS* in the 80196KC with
103 * some combinations of opcodes and addressing modes. One of
104 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
105 * provided by the IAR compiler ANSI C library. When ?GR contains
106 * an odd address, surprisingly the CPU will copy the high order
107 * byte of the source operand (SP) in the low order byte of the
108 * destination operand (the memory location pointed to by ?GR).
110 * We also need to replace the library setjmp()/longjmp() with
111 * our own versions because the IAR implementation "forgets" to
112 * save the contents of local registers (?LR).
116 void * sp; /* Stack pointer */
117 void * return_addr; /* Return address */
118 int lr[6]; /* 6 local registers */
121 typedef struct _JMP_BUF jmp_buf[1];
123 int setjmp(jmp_buf env);
124 void longjmp(jmp_buf env, int val);
126 /* Fake bool support */
129 typedef unsigned char bool;
131 #elif defined(_MSC_VER) /* Win32 emulation support */
134 #include <time.h> /* for time_t */
136 /* FIXME: I can't remember why exactly this was needed (NdBernie) */
139 /* Fake bool support */
144 #endif /* !__cplusplus */
146 /* These C99 functions are oddly named in MSVCRT32.lib */
147 #define snprintf _snprintf
148 #define vsnprintf _vsnprintf
150 #elif defined(__GNUC__)
153 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
154 #define NORETURN __attribute__((__noreturn__))
155 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
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 RESTRICT __restrict__
161 #define DEPRECATED __attribute__((__deprecated__))
166 /* hack to avoid conflicts with system type */
167 #define sigset_t system_sigset_t
171 #undef system_sigset_t
178 /* Missing printf-family functions in avr-libc/stdio.h */
180 #include <avr/pgmspace.h>
181 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
183 /* Support for harvard architectures */
185 #define PGM_READ_CHAR(s) pgm_read_byte(s)
186 #define PGM_FUNC(x) x ## _P
187 #define PGM_ATTR PROGMEM
192 #elif defined(__MWERKS__) && CPU_DSP56K
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 /* A few defaults for missing compiler features. */
209 #define INLINE static inline
212 #define NORETURN /* nothing */
215 #define FORMAT(type,fmt,first) /* nothing */
218 #define DEPRECATED /* nothing */
221 #define UNUSED(type,arg) type arg
224 #define REGISTER /* nothing */
227 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
233 #define UNLIKELY(x) x
239 /* Support for harvard architectures */
241 #define PSTR /* nothing */
243 #ifndef PGM_READ_CHAR
244 #define PGM_READ_CHAR(s) (*(s))
247 #define PGM_FUNC(x) x
250 #define PGM_ATTR /* nothing */
254 /* Misc definitions */
256 #define NULL (void *)0
263 /* Support for hybrid C/C++ applications. */
265 #define EXTERN_C_BEGIN extern "C" {
266 #define EXTERN_C_END }
268 #define EXTERN_C_BEGIN /* nothing */
269 #define EXTERN_C_END /* nothing */
274 * Standard type definitions.
275 * These should be in <sys/types.h>, but many compilers lack them.
277 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
278 typedef unsigned int size_t;
281 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
283 #endif /* _TIME_T_DEFINED || __time_t_defined */
285 /*! Bulk storage large enough for both pointers or integers */
286 typedef void * iptr_t;
287 typedef const void * const_iptr_t;
288 #define IPTR iptr_t /* OBSOLETE */
290 typedef long utime_t; /*!< Type for time expressed in microseconds */
291 typedef unsigned char sig_t; /*!< Type for signal bits */
292 typedef unsigned char sigset_t; /*!< Type for signal masks */
293 typedef unsigned char page_t; /*!< Type for banked memory pages */
295 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
297 * \name ISO C99 fixed-size types
299 * These should be in <stdint.h>, but many compilers lack them.
302 typedef signed char int8_t;
303 typedef short int int16_t;
304 typedef long int int32_t;
305 typedef unsigned char uint8_t;
306 typedef unsigned short int uint16_t;
307 typedef unsigned long int uint32_t;
309 #elif defined(__GNUC__) && defined(__AVR__)
310 /* avr-libc is weird... */
311 #include <inttypes.h>
313 /* This is the correct location. */
318 * \name Types for hardware registers.
320 * Only use these types for registers whose contents can
321 * be changed asynchronously by external hardware.
326 /* Registers can be accessed only through 16-bit pointers */
327 typedef volatile uint16_t reg16_t;
329 typedef volatile uint8_t reg8_t;
330 typedef volatile uint16_t reg16_t;
331 typedef volatile uint32_t reg32_t;
336 /* Quasi-ANSI macros */
339 * Return the byte offset of the member \a m in struct \a s
341 * \note This macro should be defined in "stddef.h" and is sometimes
342 * compiler-specific (g++ has a builtin for it).
344 #define offsetof(s,m) (size_t)&(((s *)0)->m)
348 * Count the number of elements in the static array \a a
350 * \note This macro is non-standard, but implmenents a very common idiom
352 #define countof(a) (sizeof(a) / sizeof(*(a)))
355 /*! Issue a compilation error if the \a condition is false */
356 #define STATIC_ASSERT(condition) \
357 extern char PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1]
359 #endif /* DEVLIB_COMPILER_H */