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.5 2004/07/20 17:08:03 bernie
19 * Cleanup documentation
21 * Revision 1.4 2004/06/27 15:20:26 aleph
22 * Change UNUSED() macro to accept two arguments: type and name;
23 * Add macro GNUC_PREREQ to detect GCC version during build;
24 * Some spacing cleanups and typo fix
26 * Revision 1.3 2004/06/06 18:00:39 bernie
27 * PP_CAT(): New macro.
29 * Revision 1.2 2004/06/03 11:27:09 bernie
30 * Add dual-license information.
32 * Revision 1.1 2004/05/23 17:48:35 bernie
33 * Add top-level files.
39 #include "arch_config.h"
42 #if defined __GNUC__ && defined __GNUC_MINOR__
43 #define GNUC_PREREQ(maj, min) \
44 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
46 #define GNUC_PREREQ(maj, min) 0
49 /* A few defaults for missing compiler features. */
50 #define INLINE static inline
51 #define NORETURN /* nothing */
52 #define FORMAT(type,fmt,first) /* nothing */
53 #define DEPRECATED /* nothing */
54 #define UNUSED(type,arg) type arg
55 #define REGISTER /* nothing */
56 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
58 /* Support for harvard architectures */
59 #define PGM_READ_CHAR(s) (*(s))
61 #define PGM_ATTR /* nothing */
63 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
64 #pragma language=extended
66 #define INTERRUPT(x) interrupt [x]
68 #define REGISTER shortad
70 #define INLINE /* unsupported */
72 /* Imported from <longjmp.h>. Unfortunately, we can't just include
73 * this header because it typedefs jmp_buf to be an array of chars.
74 * This would allow the compiler to place the buffer on an odd address.
75 * The CPU _should_ be able to perform word accesses to
76 * unaligned data, but there are *BUGS* in the 80196KC with
77 * some combinations of opcodes and addressing modes. One of
78 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
79 * provided by the IAR compiler ANSI C library. When ?GR contains
80 * an odd address, surprisingly the CPU will copy the high order
81 * byte of the source operand (SP) in the low order byte of the
82 * destination operand (the memory location pointed to by ?GR).
84 * We also need to replace the library setjmp()/longjmp() with
85 * our own versions because the IAR implementation "forgets" to
86 * save the contents of local registers (?LR).
90 void * sp; /* Stack pointer */
91 void * return_addr; /* Return address */
92 int lr[6]; /* 6 local registers */
95 typedef struct _JMP_BUF jmp_buf[1];
97 int setjmp(jmp_buf env);
98 void longjmp(jmp_buf env, int val);
100 /* Fake bool support */
103 typedef unsigned char bool;
105 #elif defined(_MSC_VER) /* Win32 emulation support */
108 #include <time.h> /* for time_t */
114 void SchedulerIdle(void);
119 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
120 #define ReleaseSemaphore KReleaseSemaphore
122 /* Fake bool support */
127 #endif /* !__cplusplus */
129 #elif defined(__GNUC__)
133 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
135 #define NORETURN __attribute__((__noreturn__))
137 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
139 #define INLINE extern inline
142 #define DEPRECATED __attribute__((__deprecated__))
145 #if defined(__i386__)
147 /* hack to avoid conflicts with system type */
148 #define sigset_t system_sigset_t
152 #undef system_sigset_t
157 void SchedulerIdle(void);
162 #elif defined(__AVR__)
167 /* Missing printf-family functions in avr-libc/stdio.h */
169 #include <avr/pgmspace.h>
170 #if !GNUC_PREREQ(3,4)
171 int vsprintf(char *buf, const char *fmt, va_list ap);
173 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
175 /* Support for harvard architectures */
180 #define PGM_READ_CHAR(s) pgm_read_byte(s)
181 #define PGM_FUNC(x) x ## _P
182 #define PGM_ATTR PROGMEM
187 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
194 #define PSTR /* nothing */
197 #error unknown compiler
200 /* Misc definitions */
209 /* Quasi-ANSI macros */
211 /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
212 #define offsetof(s,m) (size_t)&(((s *)0)->m)
215 /*! Count the number of elements in the static array \a a */
216 #define countof(a) (sizeof(a) / sizeof(*(a)))
221 #define ABS(a) (((a) < 0) ? -(a) : (a))
222 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
223 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
226 /*! Convert a bit value to a binary flag */
227 #define BV(x) (1<<(x))
230 /*! Round up \a x to an even multiple of the 2's power \a pad */
231 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
233 /*! Calculate a compile-time log2 for a uint8_t */
234 #define UINT8_LOG2(x) \
241 ((x) < 128 ? 6 : 7)))))))
243 /*! Calculate a compile-time log2 for a uint16_t */
244 #define UINT16_LOG2(x) \
245 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
247 /*! Calculate a compile-time log2 for a uint32_t */
248 #define UINT32_LOG2(x) \
249 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
251 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
252 #define PP_CAT(x,y) PP_CAT__(x,y)
253 #define PP_CAT__(x,y) x ## y
257 * Standard type definitions
258 * These should be in <sys/types.h>, but many compilers lack them.
260 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
261 #define size_t unsigned int
263 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
265 #endif /* _TIME_T_DEFINED || __time_t_defined */
267 /*! Storage for pointers and integers */
270 typedef long utime_t;
271 typedef unsigned char sig_t;
272 typedef unsigned char sigset_t;
273 typedef unsigned char page_t;
275 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
277 * ISO C99 fixed-size types
278 * These should be in <stdint.h>, but many compilers lack them.
280 typedef signed char int8_t;
281 typedef short int int16_t;
282 typedef long int int32_t;
283 typedef unsigned char uint8_t;
284 typedef unsigned short int uint16_t;
285 typedef unsigned long int uint32_t;
286 #elif defined(__AVR__)
287 /* avr-libc is weird... */
288 #include <inttypes.h>
290 /* This is the correct location. */
295 * \name Types for hardware registers.
297 * Only use these types for registers whose contents can
298 * be changed asynchronously by external hardware.
302 #if (defined(__m56800E__) || defined(__m56800__))
303 /* Registers can be accessed only through 16-bit pointers */
304 typedef volatile uint16_t reg16_t;
306 typedef volatile uint8_t reg8_t;
307 typedef volatile uint16_t reg16_t;
308 typedef volatile uint32_t reg32_t;
312 #endif /* COMPILER_H */