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.2 2004/06/03 11:27:09 bernie
19 * Add dual-license information.
21 * Revision 1.1 2004/05/23 17:48:35 bernie
22 * Add top-level files.
28 #include "arch_config.h"
30 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
31 #pragma language=extended
32 #define INTERRUPT(x) interrupt [x]
33 #define REGISTER shortad
34 #define ATOMIC monitor
37 #define FORMAT(type,fmt,first) /* nothing */
38 #define NORETURN /* nothing */
39 #define UNUSED(arg) arg
41 /* Imported from <longjmp.h>. Unfortunately, we can't just include
42 * this header because it typedefs jmp_buf to be an array of chars.
43 * This would allow the compiler to place the buffer on an odd address.
44 * The CPU _should_ be able to perform word accesses to
45 * unaligned data, but there are *BUGS* in the 80196KC with
46 * some combinations of opcodes and addressing modes. One of
47 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
48 * provided by the IAR compiler ANSI C library. When ?GR contains
49 * an odd address, surprisingly the CPU will copy the high order
50 * byte of the source operand (SP) in the low order byte of the
51 * destination operand (the memory location pointed to by ?GR).
53 * We also need to replace the library setjmp()/longjmp() with
54 * our own versions because the IAR implementation "forgets" to
55 * save the contents of local registers (?LR).
59 void * sp; /* Stack pointer */
60 void * return_addr; /* Return address */
61 int lr[6]; /* 6 local registers */
64 typedef struct _JMP_BUF jmp_buf[1];
66 int setjmp(jmp_buf env);
67 void longjmp(jmp_buf env, int val);
72 typedef unsigned char bool;
73 #endif /* !__cplusplus */
75 #elif defined(_MSC_VER) /* Win32 emulation support */
78 #include <time.h> /* for time_t */
81 #define REGISTER /* nothing */
82 #define INTERRUPT(x) /* nothing */
85 #define FORMAT(type,fmt,first) /* nothing */
86 #define NORETURN /* nothing */
87 #define UNUSED(arg) arg
88 #define INLINE static inline
93 void SchedulerIdle(void);
98 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
99 #define ReleaseSemaphore KReleaseSemaphore
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(arg) __attribute__((__unused__)) arg
113 //FIXME #define INLINE static inline
114 #define INLINE extern inline
116 #if defined(__i386__)
118 /* hack to avoid conflicts with system type */
119 #define sigset_t system_sigset_t
123 #undef system_sigset_t
125 #define REGISTER /* nothing */
126 #define INTERRUPT(x) /* nothing */
131 void SchedulerIdle(void);
136 #elif defined(__AVR__)
140 #define FLASH __attribute__((progmem))
141 #define REGISTER /* nothing */
143 /* Missing printf-family functions in avr-libc/stdio.h */
145 #include <avr/pgmspace.h>
146 int vsprintf(char *buf, const char *fmt, va_list ap);
147 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
149 /* Support for hardvard architectures */
151 #define PGM_READ_CHAR(s) pgm_read_byte(s)
152 #define PGM_FUNC(x) x ## _P
153 #define PGM_ATTR PROGMEM
155 #define PGM_READ_CHAR(s) (*(s))
156 #define PGM_FUNC(x) x
157 #define PGM_ATTR /*nop*/
162 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
169 #define FLASH /* nothing */
170 #define REGISTER /* nothing */
171 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
172 #define SCHEDULER_IDLE /* nothing */
174 #define INLINE static inline
177 #define FORMAT(type,fmt,first) /* nothing */
178 #define NORETURN /* nothing */
179 #define UNUSED(arg) arg
181 /* Support for hardvard architectures */
182 #define PGM_READ_CHAR(s) (*(s))
183 #define PGM_FUNC /* nothing */
184 #define PGM_ATTR /* nothing */
185 #define PSTR /* nothing */
188 #error unknown compiler
191 /* Misc definitions */
202 * offsetof(s,m) - Give the byte offset of the member <m> in struct <s>
203 * countof(a) - Count the number of elements in the static array <a>
206 #define offsetof(s,m) (size_t)&(((s *)0)->m)
210 #define countof(a) (sizeof(a) / sizeof(*(a)))
214 #define ABS(a) (((a) < 0) ? -(a) : (a))
215 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
216 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
218 /*! Convert a bit value to a binary flag */
220 #define BV(x) (1<<(x))
223 /*! Round up \a x to an even multiple of the 2's power \a pad */
224 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
226 /*! Calculate a compile-time log2 for a uint8_t */
227 #define UINT8_LOG2(x) \
234 ((x) < 128 ? 6 : 7)))))))
236 /*! Calculate a compile-time log2 for a uint16_t */
237 #define UINT16_LOG2(x) \
238 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
240 /*! Calculate a compile-time log2 for a uint32_t */
241 #define UINT32_LOG2(x) \
242 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
244 /* Type definitions - should go in <sys/types.h> */
245 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
246 #define size_t unsigned int
248 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
250 #endif /* _TIME_T_DEFINED || __time_t_defined */
252 /*! Storage for pointers and integers */
255 typedef long utime_t;
256 typedef unsigned char sig_t;
257 typedef unsigned char sigset_t;
258 typedef unsigned char page_t;
260 /* ISO C99 fixed-size types */
261 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
262 typedef signed char int8_t;
263 typedef short int int16_t;
264 typedef long int int32_t;
265 typedef unsigned char uint8_t;
266 typedef unsigned short int uint16_t;
267 typedef unsigned long int uint32_t;
268 #elif defined(__AVR__)
269 # include <inttypes.h>
274 #if (defined(__m56800E__) || defined(__m56800__))
275 /* Registers can be accessed only through 16-bit pointers */
276 typedef volatile uint16_t reg16_t;
278 typedef volatile uint8_t reg8_t;
279 typedef volatile uint16_t reg16_t;
280 typedef volatile uint32_t reg32_t;
283 #endif /* COMPILER_H */