Cleanup documentation
[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.5  2004/07/20 17:08:03  bernie
19  * Cleanup documentation
20  *
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
25  *
26  * Revision 1.3  2004/06/06 18:00:39  bernie
27  * PP_CAT(): New macro.
28  *
29  * Revision 1.2  2004/06/03 11:27:09  bernie
30  * Add dual-license information.
31  *
32  * Revision 1.1  2004/05/23 17:48:35  bernie
33  * Add top-level files.
34  *
35  */
36 #ifndef COMPILER_H
37 #define COMPILER_H
38
39 #include "arch_config.h"
40
41
42 #if defined __GNUC__ && defined __GNUC_MINOR__
43         #define GNUC_PREREQ(maj, min) \
44                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
45 #else
46         #define GNUC_PREREQ(maj, min) 0
47 #endif
48
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
57
58 /* Support for harvard architectures */
59 #define PGM_READ_CHAR(s) (*(s))
60 #define PGM_FUNC(x) x
61 #define PGM_ATTR        /* nothing */
62
63 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
64         #pragma language=extended
65         #undef  INTERRUPT
66         #define INTERRUPT(x)  interrupt [x]
67         #undef  REGISTER
68         #define REGISTER      shortad
69         #undef  INLINE
70         #define INLINE        /* unsupported */
71
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).
83          *
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).
87          */
88         struct _JMP_BUF
89         {
90                 void *  sp;                             /* Stack pointer */
91                 void *  return_addr;    /* Return address */
92                 int             lr[6];                  /* 6 local registers */
93         };
94
95         typedef struct _JMP_BUF jmp_buf[1];
96
97         int setjmp(jmp_buf env);
98         void longjmp(jmp_buf env, int val);
99
100         /* Fake bool support */
101         #define true (1==1)
102         #define false (1!=1)
103         typedef unsigned char bool;
104
105 #elif defined(_MSC_VER) /* Win32 emulation support */
106
107         #include <setjmp.h>
108         #include <time.h> /* for time_t */
109         #define float double
110
111         #ifdef __cplusplus
112                 extern "C" {
113         #endif
114                         void SchedulerIdle(void);
115         #ifdef __cplusplus
116                 }
117         #endif
118
119         /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
120         #define ReleaseSemaphore KReleaseSemaphore
121
122         /* Fake bool support */
123         #ifndef __cplusplus
124                 #define true 1
125                 #define false 0
126                 typedef int bool;
127         #endif /* !__cplusplus */
128
129 #elif defined(__GNUC__)
130
131         /* GCC attributes */
132         #undef  FORMAT
133         #define FORMAT(type,fmt,first)  __attribute__((__format__(type, fmt, first)))
134         #undef  NORETURN
135         #define NORETURN                __attribute__((__noreturn__))
136         #undef  UNUSED
137         #define UNUSED(type,arg)        __attribute__((__unused__)) type arg
138         #undef  INLINE
139         #define INLINE extern inline
140         #if GNUC_PREREQ(3,1)
141                 #undef DEPRECATED
142                 #define DEPRECATED              __attribute__((__deprecated__))
143         #endif
144
145         #if defined(__i386__)
146
147                 /* hack to avoid conflicts with system type */
148                 #define sigset_t system_sigset_t
149                 #include <stddef.h>
150                 #include <setjmp.h>
151                 #include <stdbool.h>
152                 #undef system_sigset_t
153
154                 #ifdef __cplusplus
155                         extern "C" {
156                 #endif
157                                 void SchedulerIdle(void);
158                 #ifdef __cplusplus
159                         }
160                 #endif
161
162         #elif defined(__AVR__)
163
164                 #include <stddef.h>
165                 #include <stdbool.h>
166
167                 /* Missing printf-family functions in avr-libc/stdio.h */
168                 #include <stdarg.h>
169                 #include <avr/pgmspace.h>
170                 #if !GNUC_PREREQ(3,4)
171                         int vsprintf(char *buf, const char *fmt, va_list ap);
172                 #endif
173                 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
174
175                 /* Support for harvard architectures */
176                 #ifdef _PROGMEM
177                         #undef PGM_READ_CHAR
178                         #undef PGM_FUNC
179                         #undef PGM_ATTR
180                         #define PGM_READ_CHAR(s) pgm_read_byte(s)
181                         #define PGM_FUNC(x) x ## _P
182                         #define PGM_ATTR        PROGMEM
183                 #endif
184
185         #endif /* CPU */
186
187 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
188
189         #include <stdint.h>
190         #include <stddef.h>
191         #include <stdbool.h>
192         #include <setjmp.h>
193
194         #define PSTR            /* nothing */
195
196 #else
197         #error unknown compiler
198 #endif
199
200 /* Misc definitions */
201 #ifndef NULL
202 #define NULL    0
203 #endif
204 #ifndef EOF
205 #define EOF             (-1)
206 #endif
207
208
209 /* Quasi-ANSI macros */
210 #ifndef offsetof
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)
213 #endif
214 #ifndef countof
215         /*! Count the number of elements in the static array \a a */
216         #define countof(a) (sizeof(a) / sizeof(*(a)))
217 #endif
218
219
220 /* Simple macros */
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))
224
225 #ifndef BV
226 /*! Convert a bit value to a binary flag */
227 #define BV(x)   (1<<(x))
228 #endif
229
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))
232
233 /*! Calculate a compile-time log2 for a uint8_t */
234 #define UINT8_LOG2(x) \
235         ((x) < 2 ? 0 : \
236          ((x) < 4 ? 1 : \
237           ((x) < 8 ? 2 : \
238            ((x) < 16 ? 3 : \
239             ((x) < 32 ? 4 : \
240              ((x) < 64 ? 5 : \
241               ((x) < 128 ? 6 : 7)))))))
242
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)
246
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)
250
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
254
255
256 /*
257  * Standard type definitions
258  * These should be in <sys/types.h>, but many compilers lack them.
259  */
260 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
261         #define size_t unsigned int
262 #endif
263 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
264         typedef long time_t;
265 #endif /* _TIME_T_DEFINED || __time_t_defined */
266
267 /*! Storage for pointers and integers */
268 #define IPTR void *
269
270 typedef long utime_t;
271 typedef unsigned char sig_t;
272 typedef unsigned char sigset_t;
273 typedef unsigned char page_t;
274
275 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
276         /*
277          * ISO C99 fixed-size types
278          * These should be in <stdint.h>, but many compilers lack them.
279          */
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>
289 #else
290         /* This is the correct location. */
291         #include <stdint.h>
292 #endif
293
294 /*!
295  * \name Types for hardware registers.
296  *
297  * Only use these types for registers whose contents can
298  * be changed asynchronously by external hardware.
299  *
300  * \{
301  */
302 #if (defined(__m56800E__) || defined(__m56800__))
303         /* Registers can be accessed only through 16-bit pointers */
304         typedef volatile uint16_t  reg16_t;
305 #else
306         typedef volatile uint8_t   reg8_t;
307         typedef volatile uint16_t  reg16_t;
308         typedef volatile uint32_t  reg32_t;
309 #endif
310 /*\}*/
311
312 #endif /* COMPILER_H */