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.11 2004/07/30 14:34:10 rasky
19 * Vari fix per documentazione e commenti
20 * Aggiunte PP_CATn e STATIC_ASSERT
22 * Revision 1.10 2004/07/30 14:15:53 rasky
23 * Nuovo supporto unificato per detect della CPU
25 * Revision 1.9 2004/07/29 22:57:09 bernie
26 * vsprintf(): Remove prototype for backwards compatibility with GCC 3.4; ssize_t: Add definition for inferior compilers.
28 * Revision 1.8 2004/07/20 23:43:39 bernie
29 * Use attribute((always_inline)) to force inlining. This fixes the much
30 * hated need of redundant prototypes for inline functions.
32 * Revision 1.7 2004/07/20 23:26:48 bernie
33 * Fix two errors introduced by previous commit.
35 * Revision 1.6 2004/07/20 23:12:43 bernie
36 * *** empty log message ***
38 * Revision 1.5 2004/07/20 17:08:03 bernie
39 * Cleanup documentation
41 * Revision 1.4 2004/06/27 15:20:26 aleph
42 * Change UNUSED() macro to accept two arguments: type and name;
43 * Add macro GNUC_PREREQ to detect GCC version during build;
44 * Some spacing cleanups and typo fix
46 * Revision 1.3 2004/06/06 18:00:39 bernie
47 * PP_CAT(): New macro.
49 * Revision 1.2 2004/06/03 11:27:09 bernie
50 * Add dual-license information.
52 * Revision 1.1 2004/05/23 17:48:35 bernie
53 * Add top-level files.
59 #include "arch_config.h"
60 #include "cpu_detect.h"
63 #if defined __GNUC__ && defined __GNUC_MINOR__
64 #define GNUC_PREREQ(maj, min) \
65 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
67 #define GNUC_PREREQ(maj, min) 0
70 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
71 #pragma language=extended
72 #define INTERRUPT(x) interrupt [x]
73 #define REGISTER shortad
74 #define INLINE /* unsupported */
76 /* Imported from <longjmp.h>. Unfortunately, we can't just include
77 * this header because it typedefs jmp_buf to be an array of chars.
78 * This would allow the compiler to place the buffer on an odd address.
79 * The CPU _should_ be able to perform word accesses to
80 * unaligned data, but there are *BUGS* in the 80196KC with
81 * some combinations of opcodes and addressing modes. One of
82 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
83 * provided by the IAR compiler ANSI C library. When ?GR contains
84 * an odd address, surprisingly the CPU will copy the high order
85 * byte of the source operand (SP) in the low order byte of the
86 * destination operand (the memory location pointed to by ?GR).
88 * We also need to replace the library setjmp()/longjmp() with
89 * our own versions because the IAR implementation "forgets" to
90 * save the contents of local registers (?LR).
94 void * sp; /* Stack pointer */
95 void * return_addr; /* Return address */
96 int lr[6]; /* 6 local registers */
99 typedef struct _JMP_BUF jmp_buf[1];
101 int setjmp(jmp_buf env);
102 void longjmp(jmp_buf env, int val);
104 /* Fake bool support */
107 typedef unsigned char bool;
109 #elif defined(_MSC_VER) /* Win32 emulation support */
112 #include <time.h> /* for time_t */
115 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
116 #define ReleaseSemaphore KReleaseSemaphore
118 /* Fake bool support */
123 #endif /* !__cplusplus */
125 #elif defined(__GNUC__)
128 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
129 #define NORETURN __attribute__((__noreturn__))
130 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
131 #define INLINE static inline __attribute__((__always_inline__))
133 #define DEPRECATED __attribute__((__deprecated__))
138 /* hack to avoid conflicts with system type */
139 #define sigset_t system_sigset_t
143 #undef system_sigset_t
150 /* Missing printf-family functions in avr-libc/stdio.h */
152 #include <avr/pgmspace.h>
153 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
155 /* Support for harvard architectures */
157 #define PGM_READ_CHAR(s) pgm_read_byte(s)
158 #define PGM_FUNC(x) x ## _P
159 #define PGM_ATTR PROGMEM
164 #elif defined(__MWERKS__) && CPU_DSP56K
171 // CodeWarrior has size_t as built-in type, but does not define this symbol.
172 #define _SIZE_T_DEFINED
175 #error unknown compiler
179 /* A few defaults for missing compiler features. */
181 #define INLINE static inline
184 #define NORETURN /* nothing */
187 #define FORMAT(type,fmt,first) /* nothing */
190 #define DEPRECATED /* nothing */
193 #define UNUSED(type,arg) type arg
196 #define REGISTER /* nothing */
199 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
202 /* Support for harvard architectures */
204 #define PSTR /* nothing */
206 #ifndef PGM_READ_CHAR
207 #define PGM_READ_CHAR(s) (*(s))
210 #define PGM_FUNC(x) x
213 #define PGM_ATTR /* nothing */
217 /* Misc definitions */
226 /* Support for hybrid C/C++ applications. */
228 #define EXTERN_C_BEGIN extern "C" {
229 #define EXTERN_C_END }
231 #define EXTERN_C_BEGIN /* nothing */
232 #define EXTERN_C_END /* nothing */
236 /* Quasi-ANSI macros */
238 /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
239 #define offsetof(s,m) (size_t)&(((s *)0)->m)
242 /*! Count the number of elements in the static array \a a */
243 #define countof(a) (sizeof(a) / sizeof(*(a)))
248 #define ABS(a) (((a) < 0) ? -(a) : (a))
249 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
250 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
253 /*! Convert a bit value to a binary flag */
254 #define BV(x) (1<<(x))
257 /*! Round up \a x to an even multiple of the 2's power \a pad */
258 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
260 /*! Calculate a compile-time log2 for a uint8_t */
261 #define UINT8_LOG2(x) \
268 ((x) < 128 ? 6 : 7)))))))
270 /*! Calculate a compile-time log2 for a uint16_t */
271 #define UINT16_LOG2(x) \
272 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
274 /*! Calculate a compile-time log2 for a uint32_t */
275 #define UINT32_LOG2(x) \
276 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
278 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
279 #define PP_CAT(x,y) PP_CAT__(x,y)
280 #define PP_CAT__(x,y) x ## y
281 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
282 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
283 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
285 /*! String-ize a token (allowing macros to expand) */
286 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
287 #define PP_STRINGIZE__(x) #x
289 /*! Issue a compilation error if the \a condition is false */
290 #define STATIC_ASSERT(condition) \
291 extern char CT_ASSERT___[(condition) ? 1 : -1]
294 * Standard type definitions
295 * These should be in <sys/types.h>, but many compilers lack them.
297 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
298 typedef unsigned int size_t;
301 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
303 #endif /* _TIME_T_DEFINED || __time_t_defined */
305 /*! Storage for pointers and integers */
308 typedef long utime_t;
309 typedef unsigned char sig_t;
310 typedef unsigned char sigset_t;
311 typedef unsigned char page_t;
313 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
315 * ISO C99 fixed-size types
316 * These should be in <stdint.h>, but many compilers lack them.
318 typedef signed char int8_t;
319 typedef short int int16_t;
320 typedef long int int32_t;
321 typedef unsigned char uint8_t;
322 typedef unsigned short int uint16_t;
323 typedef unsigned long int uint32_t;
324 #elif defined(__AVR__)
325 /* TODO: should this detect GCC+AVR combo, or just CPU_AVR? */
326 /* avr-libc is weird... */
327 #include <inttypes.h>
329 /* This is the correct location. */
334 * \name Types for hardware registers.
336 * Only use these types for registers whose contents can
337 * be changed asynchronously by external hardware.
342 /* Registers can be accessed only through 16-bit pointers */
343 typedef volatile uint16_t reg16_t;
345 typedef volatile uint8_t reg8_t;
346 typedef volatile uint16_t reg16_t;
347 typedef volatile uint32_t reg32_t;
351 #endif /* COMPILER_H */