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.8 2004/07/20 23:43:39 bernie
19 * Use attribute((always_inline)) to force inlining. This fixes the much
20 * hated need of redundant prototypes for inline functions.
22 * Revision 1.7 2004/07/20 23:26:48 bernie
23 * Fix two errors introduced by previous commit.
25 * Revision 1.6 2004/07/20 23:12:43 bernie
26 * *** empty log message ***
28 * Revision 1.5 2004/07/20 17:08:03 bernie
29 * Cleanup documentation
31 * Revision 1.4 2004/06/27 15:20:26 aleph
32 * Change UNUSED() macro to accept two arguments: type and name;
33 * Add macro GNUC_PREREQ to detect GCC version during build;
34 * Some spacing cleanups and typo fix
36 * Revision 1.3 2004/06/06 18:00:39 bernie
37 * PP_CAT(): New macro.
39 * Revision 1.2 2004/06/03 11:27:09 bernie
40 * Add dual-license information.
42 * Revision 1.1 2004/05/23 17:48:35 bernie
43 * Add top-level files.
49 #include "arch_config.h"
52 #if defined __GNUC__ && defined __GNUC_MINOR__
53 #define GNUC_PREREQ(maj, min) \
54 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
56 #define GNUC_PREREQ(maj, min) 0
59 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
60 #pragma language=extended
61 #define INTERRUPT(x) interrupt [x]
62 #define REGISTER shortad
63 #define INLINE /* unsupported */
65 /* Imported from <longjmp.h>. Unfortunately, we can't just include
66 * this header because it typedefs jmp_buf to be an array of chars.
67 * This would allow the compiler to place the buffer on an odd address.
68 * The CPU _should_ be able to perform word accesses to
69 * unaligned data, but there are *BUGS* in the 80196KC with
70 * some combinations of opcodes and addressing modes. One of
71 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
72 * provided by the IAR compiler ANSI C library. When ?GR contains
73 * an odd address, surprisingly the CPU will copy the high order
74 * byte of the source operand (SP) in the low order byte of the
75 * destination operand (the memory location pointed to by ?GR).
77 * We also need to replace the library setjmp()/longjmp() with
78 * our own versions because the IAR implementation "forgets" to
79 * save the contents of local registers (?LR).
83 void * sp; /* Stack pointer */
84 void * return_addr; /* Return address */
85 int lr[6]; /* 6 local registers */
88 typedef struct _JMP_BUF jmp_buf[1];
90 int setjmp(jmp_buf env);
91 void longjmp(jmp_buf env, int val);
93 /* Fake bool support */
96 typedef unsigned char bool;
98 #elif defined(_MSC_VER) /* Win32 emulation support */
101 #include <time.h> /* for time_t */
104 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
105 #define ReleaseSemaphore KReleaseSemaphore
107 /* Fake bool support */
112 #endif /* !__cplusplus */
114 #elif defined(__GNUC__)
117 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
118 #define NORETURN __attribute__((__noreturn__))
119 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
120 #define INLINE static inline __attribute__((__always_inline__))
122 #define DEPRECATED __attribute__((__deprecated__))
125 #if defined(__i386__)
127 /* hack to avoid conflicts with system type */
128 #define sigset_t system_sigset_t
132 #undef system_sigset_t
134 #elif defined(__AVR__)
139 /* Missing printf-family functions in avr-libc/stdio.h */
141 #include <avr/pgmspace.h>
142 #if !GNUC_PREREQ(3,4)
143 int vsprintf(char *buf, const char *fmt, va_list ap);
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 #define size_t unsigned int
279 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
281 #endif /* _TIME_T_DEFINED || __time_t_defined */
283 /*! Storage for pointers and integers */
286 typedef long utime_t;
287 typedef unsigned char sig_t;
288 typedef unsigned char sigset_t;
289 typedef unsigned char page_t;
291 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
293 * ISO C99 fixed-size types
294 * These should be in <stdint.h>, but many compilers lack them.
296 typedef signed char int8_t;
297 typedef short int int16_t;
298 typedef long int int32_t;
299 typedef unsigned char uint8_t;
300 typedef unsigned short int uint16_t;
301 typedef unsigned long int uint32_t;
302 #elif defined(__AVR__)
303 /* avr-libc is weird... */
304 #include <inttypes.h>
306 /* This is the correct location. */
311 * \name Types for hardware registers.
313 * Only use these types for registers whose contents can
314 * be changed asynchronously by external hardware.
318 #if (defined(__m56800E__) || defined(__m56800__))
319 /* Registers can be accessed only through 16-bit pointers */
320 typedef volatile uint16_t reg16_t;
322 typedef volatile uint8_t reg8_t;
323 typedef volatile uint16_t reg16_t;
324 typedef volatile uint32_t reg32_t;
328 #endif /* COMPILER_H */