Add dual-license information.
[bertos.git] / compiler.h
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief Additional support macros for compiler independance
14  */
15
16 /*
17  * $Log$
18  * Revision 1.2  2004/06/03 11:27:09  bernie
19  * Add dual-license information.
20  *
21  * Revision 1.1  2004/05/23 17:48:35  bernie
22  * Add top-level files.
23  *
24  */
25 #ifndef COMPILER_H
26 #define COMPILER_H
27
28 #include "arch_config.h"
29
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
35
36         /* GCC attributes */
37         #define FORMAT(type,fmt,first) /* nothing */
38         #define NORETURN               /* nothing */
39         #define UNUSED(arg)            arg
40
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).
52          *
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).
56          */
57         struct _JMP_BUF
58         {
59                 void *  sp;                             /* Stack pointer */
60                 void *  return_addr;    /* Return address */
61                 int             lr[6];                  /* 6 local registers */
62         };
63
64         typedef struct _JMP_BUF jmp_buf[1];
65
66         int setjmp(jmp_buf env);
67         void longjmp(jmp_buf env, int val);
68
69         #ifndef __cplusplus
70                 #define true 1
71                 #define false 0
72                 typedef unsigned char bool;
73         #endif /* !__cplusplus */
74
75 #elif defined(_MSC_VER) /* Win32 emulation support */
76
77         #include <setjmp.h>
78         #include <time.h> /* for time_t */
79         #define float double
80
81         #define REGISTER                /* nothing */
82         #define INTERRUPT(x)    /* nothing */
83
84         /* GCC attributes */
85         #define FORMAT(type,fmt,first) /* nothing */
86         #define NORETURN               /* nothing */
87         #define UNUSED(arg)            arg
88         #define INLINE static inline
89
90         #ifdef __cplusplus
91                 extern "C" {
92         #endif
93                         void SchedulerIdle(void);
94         #ifdef __cplusplus
95                 }
96         #endif
97
98         /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
99         #define ReleaseSemaphore KReleaseSemaphore
100
101         #ifndef __cplusplus
102                 #define true 1
103                 #define false 0
104                 typedef int bool;
105         #endif /* !__cplusplus */
106
107 #elif defined(__GNUC__)
108
109         /* GCC attributes */
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
115
116         #if defined(__i386__)
117
118                 /* hack to avoid conflicts with system type */
119                 #define sigset_t system_sigset_t
120                 #include <stddef.h>
121                 #include <setjmp.h>
122                 #include <stdbool.h>
123                 #undef system_sigset_t
124
125                 #define REGISTER                /* nothing */
126                 #define INTERRUPT(x)    /* nothing */
127
128                 #ifdef __cplusplus
129                         extern "C" {
130                 #endif
131                                 void SchedulerIdle(void);
132                 #ifdef __cplusplus
133                         }
134                 #endif
135
136         #elif defined(__AVR__)
137
138                 #include <stddef.h>
139                 #include <stdbool.h>
140                 #define FLASH           __attribute__((progmem))
141                 #define REGISTER                /* nothing */
142
143                 /* Missing printf-family functions in avr-libc/stdio.h */
144                 #include <stdarg.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);
148
149                 /* Support for hardvard architectures */
150                 #ifdef _PROGMEM
151                         #define PGM_READ_CHAR(s) pgm_read_byte(s)
152                         #define PGM_FUNC(x) x ## _P
153                         #define PGM_ATTR        PROGMEM
154                 #else
155                         #define PGM_READ_CHAR(s) (*(s))
156                         #define PGM_FUNC(x) x
157                         #define PGM_ATTR        /*nop*/
158                 #endif
159
160         #endif /* CPU */
161
162 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
163
164         #include <stdint.h>
165         #include <stddef.h>
166         #include <stdbool.h>
167         #include <setjmp.h>
168
169         #define FLASH           /* nothing */
170         #define REGISTER                /* nothing */
171         #define INTERRUPT(x)    ERROR_NOT_IMPLEMENTED
172         #define SCHEDULER_IDLE  /* nothing */
173         
174         #define INLINE          static inline
175
176         /* GCC attributes */
177         #define FORMAT(type,fmt,first)  /* nothing */
178         #define NORETURN                /* nothing */
179         #define UNUSED(arg)             arg
180
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 */
186
187 #else
188         #error unknown compiler
189 #endif
190
191 /* Misc definitions */
192 #ifndef NULL
193 #define NULL    0
194 #endif
195 #ifndef EOF
196 #define EOF             (-1)
197 #endif
198
199
200 /* Quasi-ANSI macros
201  *
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>
204  */
205 #ifndef offsetof
206 #define offsetof(s,m)   (size_t)&(((s *)0)->m)
207 #endif
208
209 #ifndef countof
210 #define countof(a) (sizeof(a) / sizeof(*(a)))
211 #endif
212
213 /* Simple macros */
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))
217
218 /*! Convert a bit value to a binary flag */
219 #ifndef BV
220 #define BV(x)   (1<<(x))
221 #endif
222
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))
225
226 /*! Calculate a compile-time log2 for a uint8_t */
227 #define UINT8_LOG2(x) \
228         ((x) < 2 ? 0 : \
229          ((x) < 4 ? 1 : \
230           ((x) < 8 ? 2 : \
231            ((x) < 16 ? 3 : \
232             ((x) < 32 ? 4 : \
233              ((x) < 64 ? 5 : \
234               ((x) < 128 ? 6 : 7)))))))
235
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)
239
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)
243
244 /* Type definitions - should go in <sys/types.h> */
245 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
246         #define size_t unsigned int
247 #endif
248 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
249         typedef long time_t;
250 #endif /* _TIME_T_DEFINED || __time_t_defined */
251
252 /*! Storage for pointers and integers */
253 #define IPTR void *
254
255 typedef long utime_t;
256 typedef unsigned char sig_t;
257 typedef unsigned char sigset_t;
258 typedef unsigned char page_t;
259
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>
270 #else
271 #       include <stdint.h>
272 #endif
273
274 #if (defined(__m56800E__) || defined(__m56800__))
275         /* Registers can be accessed only through 16-bit pointers */
276         typedef volatile uint16_t  reg16_t;
277 #else
278         typedef volatile uint8_t   reg8_t;
279         typedef volatile uint16_t  reg16_t;
280         typedef volatile uint32_t  reg32_t;
281 #endif
282
283 #endif /* COMPILER_H */