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.3 2004/06/06 18:00:39 bernie
19 * PP_CAT(): New macro.
21 * Revision 1.2 2004/06/03 11:27:09 bernie
22 * Add dual-license information.
24 * Revision 1.1 2004/05/23 17:48:35 bernie
25 * Add top-level files.
31 #include "arch_config.h"
33 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
34 #pragma language=extended
35 #define INTERRUPT(x) interrupt [x]
36 #define REGISTER shortad
37 #define ATOMIC monitor
40 #define FORMAT(type,fmt,first) /* nothing */
41 #define NORETURN /* nothing */
42 #define UNUSED(arg) arg
44 /* Imported from <longjmp.h>. Unfortunately, we can't just include
45 * this header because it typedefs jmp_buf to be an array of chars.
46 * This would allow the compiler to place the buffer on an odd address.
47 * The CPU _should_ be able to perform word accesses to
48 * unaligned data, but there are *BUGS* in the 80196KC with
49 * some combinations of opcodes and addressing modes. One of
50 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
51 * provided by the IAR compiler ANSI C library. When ?GR contains
52 * an odd address, surprisingly the CPU will copy the high order
53 * byte of the source operand (SP) in the low order byte of the
54 * destination operand (the memory location pointed to by ?GR).
56 * We also need to replace the library setjmp()/longjmp() with
57 * our own versions because the IAR implementation "forgets" to
58 * save the contents of local registers (?LR).
62 void * sp; /* Stack pointer */
63 void * return_addr; /* Return address */
64 int lr[6]; /* 6 local registers */
67 typedef struct _JMP_BUF jmp_buf[1];
69 int setjmp(jmp_buf env);
70 void longjmp(jmp_buf env, int val);
75 typedef unsigned char bool;
76 #endif /* !__cplusplus */
78 #elif defined(_MSC_VER) /* Win32 emulation support */
81 #include <time.h> /* for time_t */
84 #define REGISTER /* nothing */
85 #define INTERRUPT(x) /* nothing */
88 #define FORMAT(type,fmt,first) /* nothing */
89 #define NORETURN /* nothing */
90 #define UNUSED(arg) arg
91 #define INLINE static inline
96 void SchedulerIdle(void);
101 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
102 #define ReleaseSemaphore KReleaseSemaphore
108 #endif /* !__cplusplus */
110 #elif defined(__GNUC__)
113 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
114 #define NORETURN __attribute__((__noreturn__))
115 #define UNUSED(arg) __attribute__((__unused__)) arg
116 //FIXME #define INLINE static inline
117 #define INLINE extern inline
119 #if defined(__i386__)
121 /* hack to avoid conflicts with system type */
122 #define sigset_t system_sigset_t
126 #undef system_sigset_t
128 #define REGISTER /* nothing */
129 #define INTERRUPT(x) /* nothing */
134 void SchedulerIdle(void);
139 #elif defined(__AVR__)
143 #define FLASH __attribute__((progmem))
144 #define REGISTER /* nothing */
146 /* Missing printf-family functions in avr-libc/stdio.h */
148 #include <avr/pgmspace.h>
149 int vsprintf(char *buf, const char *fmt, va_list ap);
150 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
152 /* Support for hardvard architectures */
154 #define PGM_READ_CHAR(s) pgm_read_byte(s)
155 #define PGM_FUNC(x) x ## _P
156 #define PGM_ATTR PROGMEM
158 #define PGM_READ_CHAR(s) (*(s))
159 #define PGM_FUNC(x) x
160 #define PGM_ATTR /*nop*/
165 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
172 #define FLASH /* nothing */
173 #define REGISTER /* nothing */
174 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
175 #define SCHEDULER_IDLE /* nothing */
177 #define INLINE static inline
180 #define FORMAT(type,fmt,first) /* nothing */
181 #define NORETURN /* nothing */
182 #define UNUSED(arg) arg
184 /* Support for hardvard architectures */
185 #define PGM_READ_CHAR(s) (*(s))
186 #define PGM_FUNC /* nothing */
187 #define PGM_ATTR /* nothing */
188 #define PSTR /* nothing */
191 #error unknown compiler
194 /* Misc definitions */
205 * offsetof(s,m) - Give the byte offset of the member <m> in struct <s>
206 * countof(a) - Count the number of elements in the static array <a>
209 #define offsetof(s,m) (size_t)&(((s *)0)->m)
213 #define countof(a) (sizeof(a) / sizeof(*(a)))
217 #define ABS(a) (((a) < 0) ? -(a) : (a))
218 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
219 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
221 /*! Convert a bit value to a binary flag */
223 #define BV(x) (1<<(x))
226 /*! Round up \a x to an even multiple of the 2's power \a pad */
227 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
229 /*! Calculate a compile-time log2 for a uint8_t */
230 #define UINT8_LOG2(x) \
237 ((x) < 128 ? 6 : 7)))))))
239 /*! Calculate a compile-time log2 for a uint16_t */
240 #define UINT16_LOG2(x) \
241 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
243 /*! Calculate a compile-time log2 for a uint32_t */
244 #define UINT32_LOG2(x) \
245 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
247 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
248 #define PP_CAT(x,y) PP_CAT__(x,y)
249 #define PP_CAT__(x,y) x ## y
251 /* Type definitions - should go in <sys/types.h> */
252 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
253 #define size_t unsigned int
255 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
257 #endif /* _TIME_T_DEFINED || __time_t_defined */
259 /*! Storage for pointers and integers */
262 typedef long utime_t;
263 typedef unsigned char sig_t;
264 typedef unsigned char sigset_t;
265 typedef unsigned char page_t;
267 /* ISO C99 fixed-size types */
268 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
269 typedef signed char int8_t;
270 typedef short int int16_t;
271 typedef long int int32_t;
272 typedef unsigned char uint8_t;
273 typedef unsigned short int uint16_t;
274 typedef unsigned long int uint32_t;
275 #elif defined(__AVR__)
276 # include <inttypes.h>
281 #if (defined(__m56800E__) || defined(__m56800__))
282 /* Registers can be accessed only through 16-bit pointers */
283 typedef volatile uint16_t reg16_t;
285 typedef volatile uint8_t reg8_t;
286 typedef volatile uint16_t reg16_t;
287 typedef volatile uint32_t reg32_t;
290 #endif /* COMPILER_H */