Reverse the logic to provide defaults for undefined macros.
[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.6  2004/07/20 23:12:43  bernie
19  * Reverse the logic to provide defaults for undefined macros.
20  *
21  * Revision 1.5  2004/07/20 17:08:03  bernie
22  * Cleanup documentation
23  *
24  * Revision 1.4  2004/06/27 15:20:26  aleph
25  * Change UNUSED() macro to accept two arguments: type and name;
26  * Add macro GNUC_PREREQ to detect GCC version during build;
27  * Some spacing cleanups and typo fix
28  *
29  * Revision 1.3  2004/06/06 18:00:39  bernie
30  * PP_CAT(): New macro.
31  *
32  * Revision 1.2  2004/06/03 11:27:09  bernie
33  * Add dual-license information.
34  *
35  * Revision 1.1  2004/05/23 17:48:35  bernie
36  * Add top-level files.
37  *
38  */
39 #ifndef COMPILER_H
40 #define COMPILER_H
41
42 #include "arch_config.h"
43
44
45 #if defined __GNUC__ && defined __GNUC_MINOR__
46         #define GNUC_PREREQ(maj, min) \
47                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
48 #else
49         #define GNUC_PREREQ(maj, min) 0
50 #endif
51
52 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
53         #pragma language=extended
54         #define INTERRUPT(x)  interrupt [x]
55         #define REGISTER      shortad
56         #define INLINE        /* unsupported */
57
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).
69          *
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).
73          */
74         struct _JMP_BUF
75         {
76                 void *  sp;                             /* Stack pointer */
77                 void *  return_addr;    /* Return address */
78                 int             lr[6];                  /* 6 local registers */
79         };
80
81         typedef struct _JMP_BUF jmp_buf[1];
82
83         int setjmp(jmp_buf env);
84         void longjmp(jmp_buf env, int val);
85
86         /* Fake bool support */
87         #define true (1==1)
88         #define false (1!=1)
89         typedef unsigned char bool;
90
91 #elif defined(_MSC_VER) /* Win32 emulation support */
92
93         #include <setjmp.h>
94         #include <time.h> /* for time_t */
95         #define float double
96
97         /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
98         #define ReleaseSemaphore KReleaseSemaphore
99
100         /* Fake bool support */
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(type,arg)        __attribute__((__unused__)) type arg
113         #define INLINE extern inline
114         #if GNUC_PREREQ(3,1)
115                 #define DEPRECATED              __attribute__((__deprecated__))
116         #endif
117
118         #if defined(__i386__)
119
120                 /* hack to avoid conflicts with system type */
121                 #define sigset_t system_sigset_t
122                 #include <stddef.h>
123                 #include <setjmp.h>
124                 #include <stdbool.h>
125                 #undef system_sigset_t
126
127         #elif defined(__AVR__)
128
129                 #include <stddef.h>
130                 #include <stdbool.h>
131
132                 /* Missing printf-family functions in avr-libc/stdio.h */
133                 #include <stdarg.h>
134                 #include <avr/pgmspace.h>
135                 #if !GNUC_PREREQ(3,4)
136                         int vsprintf(char *buf, const char *fmt, va_list ap);
137                 #endif
138                 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
139
140                 /* Support for harvard architectures */
141                 #ifdef _PROGMEM
142                         #define PGM_READ_CHAR(s) pgm_read_byte(s)
143                         #define PGM_FUNC(x) x ## _P
144                         #define PGM_ATTR        PROGMEM
145                 #endif
146
147         #endif /* CPU */
148
149 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
150
151         #include <stdint.h>
152         #include <stddef.h>
153         #include <stdbool.h>
154         #include <setjmp.h>
155
156 #else
157         #error unknown compiler
158 #endif
159
160
161 /* A few defaults for missing compiler features. */
162 #ifndef INLINE
163 #define INLINE                 static inline
164 #endif
165 #ifndef NORETURN
166 #define NORETURN               /* nothing */
167 #endif
168 #ifndef FORMAT
169 #define FORMAT(type,fmt,first) /* nothing */
170 #endif
171 #ifndef
172 #define DEPRECATED             /* nothing */
173 #endif
174 #ifndef
175 #define UNUSED(type,arg)       type arg
176 #endif
177 #ifndef REGISTER
178 #define REGISTER               /* nothing */
179 #endif
180 #ifndef INTERRUPT
181 #define INTERRUPT(x)           ERROR_NOT_IMPLEMENTED
182 #endif
183
184 /* Support for harvard architectures */
185 #ifndef PSTR
186 #define PSTR            /* nothing */
187 #endif
188 #ifndef PGM_READ_CHAR
189 #define PGM_READ_CHAR(s) (*(s))
190 #endif
191 #ifndef PGM_FUNC
192 #define PGM_FUNC(x) x
193 #endif
194 #ifndef PGM_ATTR
195 #define PGM_ATTR        /* nothing */
196 #endif
197
198
199 /* Misc definitions */
200 #ifndef NULL
201 #define NULL  0
202 #endif
203 #ifndef EOF
204 #define EOF   (-1)
205 #endif
206
207
208 /* Support for hybrid C/C++ applications. */
209 #ifdef __cplusplus
210         #define EXTERN_C_BEGIN  extern "C" {
211         #define EXTERN_C_END    }
212 #else
213         #define EXTERN_C_BEGIN  /* nothing */
214         #define EXTERN_C_END    /* nothing */
215 #endif
216
217
218 /* Quasi-ANSI macros */
219 #ifndef offsetof
220         /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
221         #define offsetof(s,m)   (size_t)&(((s *)0)->m)
222 #endif
223 #ifndef countof
224         /*! Count the number of elements in the static array \a a */
225         #define countof(a) (sizeof(a) / sizeof(*(a)))
226 #endif
227
228
229 /* Simple macros */
230 #define ABS(a)          (((a) < 0) ? -(a) : (a))
231 #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
232 #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
233
234 #ifndef BV
235 /*! Convert a bit value to a binary flag */
236 #define BV(x)   (1<<(x))
237 #endif
238
239 /*! Round up \a x to an even multiple of the 2's power \a pad */
240 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
241
242 /*! Calculate a compile-time log2 for a uint8_t */
243 #define UINT8_LOG2(x) \
244         ((x) < 2 ? 0 : \
245          ((x) < 4 ? 1 : \
246           ((x) < 8 ? 2 : \
247            ((x) < 16 ? 3 : \
248             ((x) < 32 ? 4 : \
249              ((x) < 64 ? 5 : \
250               ((x) < 128 ? 6 : 7)))))))
251
252 /*! Calculate a compile-time log2 for a uint16_t */
253 #define UINT16_LOG2(x) \
254         ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
255
256 /*! Calculate a compile-time log2 for a uint32_t */
257 #define UINT32_LOG2(x) \
258         ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
259
260 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
261 #define PP_CAT(x,y)      PP_CAT__(x,y)
262 #define PP_CAT__(x,y)    x ## y
263
264
265 /*
266  * Standard type definitions
267  * These should be in <sys/types.h>, but many compilers lack them.
268  */
269 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
270         #define size_t unsigned int
271 #endif
272 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
273         typedef long time_t;
274 #endif /* _TIME_T_DEFINED || __time_t_defined */
275
276 /*! Storage for pointers and integers */
277 #define IPTR void *
278
279 typedef long utime_t;
280 typedef unsigned char sig_t;
281 typedef unsigned char sigset_t;
282 typedef unsigned char page_t;
283
284 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
285         /*
286          * ISO C99 fixed-size types
287          * These should be in <stdint.h>, but many compilers lack them.
288          */
289         typedef signed char         int8_t;
290         typedef short int           int16_t;
291         typedef long int            int32_t;
292         typedef unsigned char       uint8_t;
293         typedef unsigned short int  uint16_t;
294         typedef unsigned long int   uint32_t;
295 #elif defined(__AVR__)
296         /* avr-libc is weird... */
297         #include <inttypes.h>
298 #else
299         /* This is the correct location. */
300         #include <stdint.h>
301 #endif
302
303 /*!
304  * \name Types for hardware registers.
305  *
306  * Only use these types for registers whose contents can
307  * be changed asynchronously by external hardware.
308  *
309  * \{
310  */
311 #if (defined(__m56800E__) || defined(__m56800__))
312         /* Registers can be accessed only through 16-bit pointers */
313         typedef volatile uint16_t  reg16_t;
314 #else
315         typedef volatile uint8_t   reg8_t;
316         typedef volatile uint16_t  reg16_t;
317         typedef volatile uint32_t  reg32_t;
318 #endif
319 /*\}*/
320
321 #endif /* COMPILER_H */