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