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