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.9 2004/07/29 22:57:09 bernie
19 * vsprintf(): Remove prototype for backwards compatibility with GCC 3.4; ssize_t: Add definition for inferior compilers.
21 * Revision 1.8 2004/07/20 23:43:39 bernie
22 * Use attribute((always_inline)) to force inlining. This fixes the much
23 * hated need of redundant prototypes for inline functions.
25 * Revision 1.7 2004/07/20 23:26:48 bernie
26 * Fix two errors introduced by previous commit.
28 * Revision 1.6 2004/07/20 23:12:43 bernie
29 * *** empty log message ***
31 * Revision 1.5 2004/07/20 17:08:03 bernie
32 * Cleanup documentation
34 * Revision 1.4 2004/06/27 15:20:26 aleph
35 * Change UNUSED() macro to accept two arguments: type and name;
36 * Add macro GNUC_PREREQ to detect GCC version during build;
37 * Some spacing cleanups and typo fix
39 * Revision 1.3 2004/06/06 18:00:39 bernie
40 * PP_CAT(): New macro.
42 * Revision 1.2 2004/06/03 11:27:09 bernie
43 * Add dual-license information.
45 * Revision 1.1 2004/05/23 17:48:35 bernie
46 * Add top-level files.
52 #include "arch_config.h"
55 #if defined __GNUC__ && defined __GNUC_MINOR__
56 #define GNUC_PREREQ(maj, min) \
57 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
59 #define GNUC_PREREQ(maj, min) 0
62 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
63 #pragma language=extended
64 #define INTERRUPT(x) interrupt [x]
65 #define REGISTER shortad
66 #define INLINE /* unsupported */
68 /* Imported from <longjmp.h>. Unfortunately, we can't just include
69 * this header because it typedefs jmp_buf to be an array of chars.
70 * This would allow the compiler to place the buffer on an odd address.
71 * The CPU _should_ be able to perform word accesses to
72 * unaligned data, but there are *BUGS* in the 80196KC with
73 * some combinations of opcodes and addressing modes. One of
74 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
75 * provided by the IAR compiler ANSI C library. When ?GR contains
76 * an odd address, surprisingly the CPU will copy the high order
77 * byte of the source operand (SP) in the low order byte of the
78 * destination operand (the memory location pointed to by ?GR).
80 * We also need to replace the library setjmp()/longjmp() with
81 * our own versions because the IAR implementation "forgets" to
82 * save the contents of local registers (?LR).
86 void * sp; /* Stack pointer */
87 void * return_addr; /* Return address */
88 int lr[6]; /* 6 local registers */
91 typedef struct _JMP_BUF jmp_buf[1];
93 int setjmp(jmp_buf env);
94 void longjmp(jmp_buf env, int val);
96 /* Fake bool support */
99 typedef unsigned char bool;
101 #elif defined(_MSC_VER) /* Win32 emulation support */
104 #include <time.h> /* for time_t */
107 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
108 #define ReleaseSemaphore KReleaseSemaphore
110 /* Fake bool support */
115 #endif /* !__cplusplus */
117 #elif defined(__GNUC__)
120 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
121 #define NORETURN __attribute__((__noreturn__))
122 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
123 #define INLINE static inline __attribute__((__always_inline__))
125 #define DEPRECATED __attribute__((__deprecated__))
128 #if defined(__i386__)
130 /* hack to avoid conflicts with system type */
131 #define sigset_t system_sigset_t
135 #undef system_sigset_t
137 #elif defined(__AVR__)
142 /* Missing printf-family functions in avr-libc/stdio.h */
144 #include <avr/pgmspace.h>
145 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
147 /* Support for harvard architectures */
149 #define PGM_READ_CHAR(s) pgm_read_byte(s)
150 #define PGM_FUNC(x) x ## _P
151 #define PGM_ATTR PROGMEM
156 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
164 #error unknown compiler
168 /* A few defaults for missing compiler features. */
170 #define INLINE static inline
173 #define NORETURN /* nothing */
176 #define FORMAT(type,fmt,first) /* nothing */
179 #define DEPRECATED /* nothing */
182 #define UNUSED(type,arg) type arg
185 #define REGISTER /* nothing */
188 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
191 /* Support for harvard architectures */
193 #define PSTR /* nothing */
195 #ifndef PGM_READ_CHAR
196 #define PGM_READ_CHAR(s) (*(s))
199 #define PGM_FUNC(x) x
202 #define PGM_ATTR /* nothing */
206 /* Misc definitions */
215 /* Support for hybrid C/C++ applications. */
217 #define EXTERN_C_BEGIN extern "C" {
218 #define EXTERN_C_END }
220 #define EXTERN_C_BEGIN /* nothing */
221 #define EXTERN_C_END /* nothing */
225 /* Quasi-ANSI macros */
227 /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
228 #define offsetof(s,m) (size_t)&(((s *)0)->m)
231 /*! Count the number of elements in the static array \a a */
232 #define countof(a) (sizeof(a) / sizeof(*(a)))
237 #define ABS(a) (((a) < 0) ? -(a) : (a))
238 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
239 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
242 /*! Convert a bit value to a binary flag */
243 #define BV(x) (1<<(x))
246 /*! Round up \a x to an even multiple of the 2's power \a pad */
247 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
249 /*! Calculate a compile-time log2 for a uint8_t */
250 #define UINT8_LOG2(x) \
257 ((x) < 128 ? 6 : 7)))))))
259 /*! Calculate a compile-time log2 for a uint16_t */
260 #define UINT16_LOG2(x) \
261 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
263 /*! Calculate a compile-time log2 for a uint32_t */
264 #define UINT32_LOG2(x) \
265 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
267 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
268 #define PP_CAT(x,y) PP_CAT__(x,y)
269 #define PP_CAT__(x,y) x ## y
273 * Standard type definitions
274 * These should be in <sys/types.h>, but many compilers lack them.
276 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
277 typedef unsigned int size_t;
280 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
282 #endif /* _TIME_T_DEFINED || __time_t_defined */
284 /*! Storage for pointers and integers */
287 typedef long utime_t;
288 typedef unsigned char sig_t;
289 typedef unsigned char sigset_t;
290 typedef unsigned char page_t;
292 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
294 * ISO C99 fixed-size types
295 * These should be in <stdint.h>, but many compilers lack them.
297 typedef signed char int8_t;
298 typedef short int int16_t;
299 typedef long int int32_t;
300 typedef unsigned char uint8_t;
301 typedef unsigned short int uint16_t;
302 typedef unsigned long int uint32_t;
303 #elif defined(__AVR__)
304 /* avr-libc is weird... */
305 #include <inttypes.h>
307 /* This is the correct location. */
312 * \name Types for hardware registers.
314 * Only use these types for registers whose contents can
315 * be changed asynchronously by external hardware.
319 #if (defined(__m56800E__) || defined(__m56800__))
320 /* Registers can be accessed only through 16-bit pointers */
321 typedef volatile uint16_t reg16_t;
323 typedef volatile uint8_t reg8_t;
324 typedef volatile uint16_t reg16_t;
325 typedef volatile uint32_t reg32_t;
329 #endif /* COMPILER_H */