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.6 2004/07/20 23:12:43 bernie
19 * Reverse the logic to provide defaults for undefined macros.
21 * Revision 1.5 2004/07/20 17:08:03 bernie
22 * Cleanup documentation
24 * Revision 1.4 2004/06/27 15:20:26 aleph
25 * Change UNUSED() macro to accept two arguments: type and name;
26 * Add macro GNUC_PREREQ to detect GCC version during build;
27 * Some spacing cleanups and typo fix
29 * Revision 1.3 2004/06/06 18:00:39 bernie
30 * PP_CAT(): New macro.
32 * Revision 1.2 2004/06/03 11:27:09 bernie
33 * Add dual-license information.
35 * Revision 1.1 2004/05/23 17:48:35 bernie
36 * Add top-level files.
42 #include "arch_config.h"
45 #if defined __GNUC__ && defined __GNUC_MINOR__
46 #define GNUC_PREREQ(maj, min) \
47 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
49 #define GNUC_PREREQ(maj, min) 0
52 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
53 #pragma language=extended
54 #define INTERRUPT(x) interrupt [x]
55 #define REGISTER shortad
56 #define INLINE /* unsupported */
58 /* Imported from <longjmp.h>. Unfortunately, we can't just include
59 * this header because it typedefs jmp_buf to be an array of chars.
60 * This would allow the compiler to place the buffer on an odd address.
61 * The CPU _should_ be able to perform word accesses to
62 * unaligned data, but there are *BUGS* in the 80196KC with
63 * some combinations of opcodes and addressing modes. One of
64 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
65 * provided by the IAR compiler ANSI C library. When ?GR contains
66 * an odd address, surprisingly the CPU will copy the high order
67 * byte of the source operand (SP) in the low order byte of the
68 * destination operand (the memory location pointed to by ?GR).
70 * We also need to replace the library setjmp()/longjmp() with
71 * our own versions because the IAR implementation "forgets" to
72 * save the contents of local registers (?LR).
76 void * sp; /* Stack pointer */
77 void * return_addr; /* Return address */
78 int lr[6]; /* 6 local registers */
81 typedef struct _JMP_BUF jmp_buf[1];
83 int setjmp(jmp_buf env);
84 void longjmp(jmp_buf env, int val);
86 /* Fake bool support */
89 typedef unsigned char bool;
91 #elif defined(_MSC_VER) /* Win32 emulation support */
94 #include <time.h> /* for time_t */
97 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
98 #define ReleaseSemaphore KReleaseSemaphore
100 /* Fake bool support */
105 #endif /* !__cplusplus */
107 #elif defined(__GNUC__)
110 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
111 #define NORETURN __attribute__((__noreturn__))
112 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
113 #define INLINE extern inline
115 #define DEPRECATED __attribute__((__deprecated__))
118 #if defined(__i386__)
120 /* hack to avoid conflicts with system type */
121 #define sigset_t system_sigset_t
125 #undef system_sigset_t
127 #elif defined(__AVR__)
132 /* Missing printf-family functions in avr-libc/stdio.h */
134 #include <avr/pgmspace.h>
135 #if !GNUC_PREREQ(3,4)
136 int vsprintf(char *buf, const char *fmt, va_list ap);
138 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
140 /* Support for harvard architectures */
142 #define PGM_READ_CHAR(s) pgm_read_byte(s)
143 #define PGM_FUNC(x) x ## _P
144 #define PGM_ATTR PROGMEM
149 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
157 #error unknown compiler
161 /* A few defaults for missing compiler features. */
163 #define INLINE static inline
166 #define NORETURN /* nothing */
169 #define FORMAT(type,fmt,first) /* nothing */
172 #define DEPRECATED /* nothing */
175 #define UNUSED(type,arg) type arg
178 #define REGISTER /* nothing */
181 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
184 /* Support for harvard architectures */
186 #define PSTR /* nothing */
188 #ifndef PGM_READ_CHAR
189 #define PGM_READ_CHAR(s) (*(s))
192 #define PGM_FUNC(x) x
195 #define PGM_ATTR /* nothing */
199 /* Misc definitions */
208 /* Support for hybrid C/C++ applications. */
210 #define EXTERN_C_BEGIN extern "C" {
211 #define EXTERN_C_END }
213 #define EXTERN_C_BEGIN /* nothing */
214 #define EXTERN_C_END /* nothing */
218 /* Quasi-ANSI macros */
220 /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
221 #define offsetof(s,m) (size_t)&(((s *)0)->m)
224 /*! Count the number of elements in the static array \a a */
225 #define countof(a) (sizeof(a) / sizeof(*(a)))
230 #define ABS(a) (((a) < 0) ? -(a) : (a))
231 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
232 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
235 /*! Convert a bit value to a binary flag */
236 #define BV(x) (1<<(x))
239 /*! Round up \a x to an even multiple of the 2's power \a pad */
240 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
242 /*! Calculate a compile-time log2 for a uint8_t */
243 #define UINT8_LOG2(x) \
250 ((x) < 128 ? 6 : 7)))))))
252 /*! Calculate a compile-time log2 for a uint16_t */
253 #define UINT16_LOG2(x) \
254 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
256 /*! Calculate a compile-time log2 for a uint32_t */
257 #define UINT32_LOG2(x) \
258 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
260 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
261 #define PP_CAT(x,y) PP_CAT__(x,y)
262 #define PP_CAT__(x,y) x ## y
266 * Standard type definitions
267 * These should be in <sys/types.h>, but many compilers lack them.
269 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
270 #define size_t unsigned int
272 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
274 #endif /* _TIME_T_DEFINED || __time_t_defined */
276 /*! Storage for pointers and integers */
279 typedef long utime_t;
280 typedef unsigned char sig_t;
281 typedef unsigned char sigset_t;
282 typedef unsigned char page_t;
284 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
286 * ISO C99 fixed-size types
287 * These should be in <stdint.h>, but many compilers lack them.
289 typedef signed char int8_t;
290 typedef short int int16_t;
291 typedef long int int32_t;
292 typedef unsigned char uint8_t;
293 typedef unsigned short int uint16_t;
294 typedef unsigned long int uint32_t;
295 #elif defined(__AVR__)
296 /* avr-libc is weird... */
297 #include <inttypes.h>
299 /* This is the correct location. */
304 * \name Types for hardware registers.
306 * Only use these types for registers whose contents can
307 * be changed asynchronously by external hardware.
311 #if (defined(__m56800E__) || defined(__m56800__))
312 /* Registers can be accessed only through 16-bit pointers */
313 typedef volatile uint16_t reg16_t;
315 typedef volatile uint8_t reg8_t;
316 typedef volatile uint16_t reg16_t;
317 typedef volatile uint32_t reg32_t;
321 #endif /* COMPILER_H */