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