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.4 2004/06/27 15:20:26 aleph
19 * Change UNUSED() macro to accept two arguments: type and name;
20 * Add macro GNUC_PREREQ to detect GCC version during build;
21 * Some spacing cleanups and typo fix
23 * Revision 1.3 2004/06/06 18:00:39 bernie
24 * PP_CAT(): New macro.
26 * Revision 1.2 2004/06/03 11:27:09 bernie
27 * Add dual-license information.
29 * Revision 1.1 2004/05/23 17:48:35 bernie
30 * Add top-level files.
36 #include "arch_config.h"
39 #if defined __GNUC__ && defined __GNUC_MINOR__
40 #define GNUC_PREREQ(maj, min) \
41 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
43 #define GNUC_PREREQ(maj, min) 0
47 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
48 #pragma language=extended
49 #define INTERRUPT(x) interrupt [x]
50 #define REGISTER shortad
51 #define ATOMIC monitor
54 #define FORMAT(type,fmt,first) /* nothing */
55 #define NORETURN /* nothing */
56 #define UNUSED(type,arg) type arg
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);
89 typedef unsigned char bool;
90 #endif /* !__cplusplus */
92 #elif defined(_MSC_VER) /* Win32 emulation support */
95 #include <time.h> /* for time_t */
98 #define REGISTER /* nothing */
99 #define INTERRUPT(x) /* nothing */
102 #define FORMAT(type,fmt,first) /* nothing */
103 #define NORETURN /* nothing */
104 #define UNUSED(type,arg) type arg
105 #define INLINE static inline
110 void SchedulerIdle(void);
115 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
116 #define ReleaseSemaphore KReleaseSemaphore
122 #endif /* !__cplusplus */
124 #elif defined(__GNUC__)
127 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
128 #define NORETURN __attribute__((__noreturn__))
129 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
130 //FIXME #define INLINE static inline
131 #define INLINE extern inline
133 #if defined(__i386__)
135 /* hack to avoid conflicts with system type */
136 #define sigset_t system_sigset_t
140 #undef system_sigset_t
142 #define REGISTER /* nothing */
143 #define INTERRUPT(x) /* nothing */
148 void SchedulerIdle(void);
153 #elif defined(__AVR__)
157 #define FLASH __attribute__((progmem))
158 #define REGISTER /* nothing */
160 /* Missing printf-family functions in avr-libc/stdio.h */
162 #include <avr/pgmspace.h>
163 #if !GNUC_PREREQ(3,4)
164 int vsprintf(char *buf, const char *fmt, va_list ap);
166 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
168 /* Support for harvard architectures */
170 #define PGM_READ_CHAR(s) pgm_read_byte(s)
171 #define PGM_FUNC(x) x ## _P
172 #define PGM_ATTR PROGMEM
174 #define PGM_READ_CHAR(s) (*(s))
175 #define PGM_FUNC(x) x
176 #define PGM_ATTR /*nop*/
181 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
188 #define FLASH /* nothing */
189 #define REGISTER /* nothing */
190 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
191 #define SCHEDULER_IDLE /* nothing */
193 #define INLINE static inline
196 #define FORMAT(type,fmt,first) /* nothing */
197 #define NORETURN /* nothing */
198 #define UNUSED(type,arg) type arg
200 /* Support for harvard architectures */
201 #define PGM_READ_CHAR(s) (*(s))
202 #define PGM_FUNC /* nothing */
203 #define PGM_ATTR /* nothing */
204 #define PSTR /* nothing */
207 #error unknown compiler
210 /* Misc definitions */
221 * offsetof(s,m) - Give the byte offset of the member <m> in struct <s>
222 * countof(a) - Count the number of elements in the static array <a>
225 #define offsetof(s,m) (size_t)&(((s *)0)->m)
229 #define countof(a) (sizeof(a) / sizeof(*(a)))
233 #define ABS(a) (((a) < 0) ? -(a) : (a))
234 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
235 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
237 /*! Convert a bit value to a binary flag */
239 #define BV(x) (1<<(x))
242 /*! Round up \a x to an even multiple of the 2's power \a pad */
243 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
245 /*! Calculate a compile-time log2 for a uint8_t */
246 #define UINT8_LOG2(x) \
253 ((x) < 128 ? 6 : 7)))))))
255 /*! Calculate a compile-time log2 for a uint16_t */
256 #define UINT16_LOG2(x) \
257 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
259 /*! Calculate a compile-time log2 for a uint32_t */
260 #define UINT32_LOG2(x) \
261 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
263 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
264 #define PP_CAT(x,y) PP_CAT__(x,y)
265 #define PP_CAT__(x,y) x ## y
267 /* Type definitions - should go in <sys/types.h> */
268 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
269 #define size_t unsigned int
271 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
273 #endif /* _TIME_T_DEFINED || __time_t_defined */
275 /*! Storage for pointers and integers */
278 typedef long utime_t;
279 typedef unsigned char sig_t;
280 typedef unsigned char sigset_t;
281 typedef unsigned char page_t;
283 /* ISO C99 fixed-size types */
284 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
285 typedef signed char int8_t;
286 typedef short int int16_t;
287 typedef long int int32_t;
288 typedef unsigned char uint8_t;
289 typedef unsigned short int uint16_t;
290 typedef unsigned long int uint32_t;
291 #elif defined(__AVR__)
292 # include <inttypes.h>
297 #if (defined(__m56800E__) || defined(__m56800__))
298 /* Registers can be accessed only through 16-bit pointers */
299 typedef volatile uint16_t reg16_t;
301 typedef volatile uint8_t reg8_t;
302 typedef volatile uint16_t reg16_t;
303 typedef volatile uint32_t reg32_t;
306 #endif /* COMPILER_H */