VERS_TAG(): Rename for consistency; APP_COPYRIGHT: Canonoicalize.
[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.20  2004/08/29 21:57:58  bernie
19  *#* Move back STATIC_ASSERT() to compiler.h as it's needed in cpu.h;
20  *#* iptr_t, const_iptr_t: Replace IPTR macro with a real typedef.
21  *#*
22  *#* Revision 1.19  2004/08/25 14:12:08  rasky
23  *#* Aggiornato il comment block dei log RCS
24  *#*
25  *#* Revision 1.18  2004/08/24 16:32:37  bernie
26  *#* Document custom types.
27  *#*
28  *#* Revision 1.17  2004/08/24 13:32:14  bernie
29  *#* PP_CAT(), PP_STRINGIZE(): Move back to compiler.h to break circular dependency between cpu.h/compiler.h/macros.h;
30  *#* offsetof(), countof(): Move back to compiler.h to avoid including macros.h almost everywhere;
31  *#* Trim CVS log;
32  *#* Rename header guards;
33  *#* Don't include arch_config.h in compiler.h as it's not needed there.
34  *#*
35  *#* Revision 1.16  2004/08/14 19:37:57  rasky
36  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
37  *#*
38  *#* Revision 1.15  2004/08/13 03:23:26  bernie
39  *#* Adjust a few MSVC tweaks from older projects.
40  *#*
41  *#* Revision 1.14  2004/08/10 06:56:29  bernie
42  *#* RESTRICT: New C99-like macro; STATIC_ASSERT: Fix warning for multiple invocation in one file.
43  *#*
44  *#* Revision 1.13  2004/08/02 20:20:29  aleph
45  *#* Merge from project_ks
46  *#*
47  *#* Revision 1.12  2004/08/01 01:21:17  bernie
48  *#* LIKELY(), UNLIKELY(): New compiler-specific macros.
49  *#*
50  *#* Revision 1.11  2004/07/30 14:34:10  rasky
51  *#* Vari fix per documentazione e commenti
52  *#* Aggiunte PP_CATn e STATIC_ASSERT
53  *#*
54  *#* Revision 1.10  2004/07/30 14:15:53  rasky
55  *#* Nuovo supporto unificato per detect della CPU
56  *#*
57  *#* Revision 1.9  2004/07/29 22:57:09  bernie
58  *#* vsprintf(): Remove prototype for backwards compatibility with GCC 3.4; ssize_t: Add definition for inferior compilers.
59  *#*/
60 #ifndef DEVLIB_COMPILER_H
61 #define DEVLIB_COMPILER_H
62
63 #include "cpu_detect.h"
64
65
66 #if defined __GNUC__ && defined __GNUC_MINOR__
67         #define GNUC_PREREQ(maj, min) \
68                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
69 #else
70         #define GNUC_PREREQ(maj, min) 0
71 #endif
72
73 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
74         #define COMPILER_C99      1
75 #else
76         #define COMPILER_C99      0
77 #endif
78
79
80 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
81 #define PP_CAT(x,y)         PP_CAT__(x,y)
82 #define PP_CAT__(x,y)       x ## y
83 #define PP_CAT3(x,y,z)      PP_CAT(PP_CAT(x,y),z)
84 #define PP_CAT4(x,y,z,w)    PP_CAT(PP_CAT3(x,y,z),w)
85 #define PP_CAT5(x,y,z,w,j)  PP_CAT(PP_CAT4(x,y,z,w),j)
86
87 /*! String-ize a token (allowing macros to expand) */
88 #define PP_STRINGIZE(x)     PP_STRINGIZE__(x)
89 #define PP_STRINGIZE__(x)   #x
90
91
92 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
93         #pragma language=extended
94         #define INTERRUPT(x)  interrupt [x]
95         #define REGISTER      shortad
96         #define INLINE        /* unsupported */
97
98         /* Imported from <longjmp.h>. Unfortunately, we can't just include
99          * this header because it typedefs jmp_buf to be an array of chars.
100          * This would allow the compiler to place the buffer on an odd address.
101          * The CPU _should_ be able to perform word accesses to
102          * unaligned data, but there are *BUGS* in the 80196KC with
103          * some combinations of opcodes and addressing modes. One of
104          * these, "ST SP,[?GR]+" is used in the longjmp() implementation
105          * provided by the IAR compiler ANSI C library. When ?GR contains
106          * an odd address, surprisingly the CPU will copy the high order
107          * byte of the source operand (SP) in the low order byte of the
108          * destination operand (the memory location pointed to by ?GR).
109          *
110          * We also need to replace the library setjmp()/longjmp() with
111          * our own versions because the IAR implementation "forgets" to
112          * save the contents of local registers (?LR).
113          */
114         struct _JMP_BUF
115         {
116                 void *  sp;                             /* Stack pointer */
117                 void *  return_addr;    /* Return address */
118                 int             lr[6];                  /* 6 local registers */
119         };
120
121         typedef struct _JMP_BUF jmp_buf[1];
122
123         int setjmp(jmp_buf env);
124         void longjmp(jmp_buf env, int val);
125
126         /* Fake bool support */
127         #define true (1==1)
128         #define false (1!=1)
129         typedef unsigned char bool;
130
131 #elif defined(_MSC_VER) /* Win32 emulation support */
132
133         #include <setjmp.h>
134         #include <time.h> /* for time_t */
135
136         /* FIXME: I can't remember why exactly this was needed (NdBernie) */
137         #define float double
138
139         /* Fake bool support */
140         #ifndef __cplusplus
141                 #define true 1
142                 #define false 0
143                 typedef int bool;
144         #endif /* !__cplusplus */
145
146         /* These C99 functions are oddly named in MSVCRT32.lib */
147         #define snprintf _snprintf
148         #define vsnprintf _vsnprintf
149
150 #elif defined(__GNUC__)
151
152         /* GCC attributes */
153         #define FORMAT(type,fmt,first)  __attribute__((__format__(type, fmt, first)))
154         #define NORETURN                __attribute__((__noreturn__))
155         #define UNUSED(type,arg)        __attribute__((__unused__)) type arg
156         #define INLINE                  static inline __attribute__((__always_inline__))
157         #define LIKELY(x)               __builtin_expect((x), 1)
158         #define UNLIKELY(x)             __builtin_expect((x), 0)
159         #define RESTRICT                __restrict__
160         #if GNUC_PREREQ(3,1)
161                 #define DEPRECATED      __attribute__((__deprecated__))
162         #endif
163
164         #if CPU_X86
165
166                 /* hack to avoid conflicts with system type */
167                 #define sigset_t system_sigset_t
168                 #include <stddef.h>
169                 #include <setjmp.h>
170                 #include <stdbool.h>
171                 #undef system_sigset_t
172
173         #elif CPU_AVR
174
175                 #include <stddef.h>
176                 #include <stdbool.h>
177
178                 /* Missing printf-family functions in avr-libc/stdio.h */
179                 #include <stdarg.h>
180                 #include <avr/pgmspace.h>
181                 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
182
183                 /* Support for harvard architectures */
184                 #ifdef _PROGMEM
185                         #define PGM_READ_CHAR(s) pgm_read_byte(s)
186                         #define PGM_FUNC(x) x ## _P
187                         #define PGM_ATTR        PROGMEM
188                 #endif
189
190         #endif
191
192 #elif defined(__MWERKS__) && CPU_DSP56K
193
194         #include <stdint.h>
195         #include <stddef.h>
196         #include <stdbool.h>
197         #include <setjmp.h>
198
199         // CodeWarrior has size_t as built-in type, but does not define this symbol.
200         #define _SIZE_T_DEFINED
201
202 #else
203         #error unknown compiler
204 #endif
205
206
207 /* A few defaults for missing compiler features. */
208 #ifndef INLINE
209 #define INLINE                 static inline
210 #endif
211 #ifndef NORETURN
212 #define NORETURN               /* nothing */
213 #endif
214 #ifndef FORMAT
215 #define FORMAT(type,fmt,first) /* nothing */
216 #endif
217 #ifndef DEPRECATED
218 #define DEPRECATED             /* nothing */
219 #endif
220 #ifndef UNUSED
221 #define UNUSED(type,arg)       type arg
222 #endif
223 #ifndef REGISTER
224 #define REGISTER               /* nothing */
225 #endif
226 #ifndef INTERRUPT
227 #define INTERRUPT(x)           ERROR_NOT_IMPLEMENTED
228 #endif
229 #ifndef LIKELY
230 #define LIKELY(x)              x
231 #endif
232 #ifndef UNLIKELY
233 #define UNLIKELY(x)            x
234 #endif
235 #ifndef RESTRICT
236 #define RESTRICT
237 #endif
238
239 /* Support for harvard architectures */
240 #ifndef PSTR
241 #define PSTR            /* nothing */
242 #endif
243 #ifndef PGM_READ_CHAR
244 #define PGM_READ_CHAR(s) (*(s))
245 #endif
246 #ifndef PGM_FUNC
247 #define PGM_FUNC(x) x
248 #endif
249 #ifndef PGM_ATTR
250 #define PGM_ATTR        /* nothing */
251 #endif
252
253
254 /* Misc definitions */
255 #ifndef NULL
256 #define NULL  (void *)0
257 #endif
258 #ifndef EOF
259 #define EOF   (-1)
260 #endif
261
262
263 /* Support for hybrid C/C++ applications. */
264 #ifdef __cplusplus
265         #define EXTERN_C_BEGIN  extern "C" {
266         #define EXTERN_C_END    }
267 #else
268         #define EXTERN_C_BEGIN  /* nothing */
269         #define EXTERN_C_END    /* nothing */
270 #endif
271
272
273 /*
274  * Standard type definitions.
275  * These should be in <sys/types.h>, but many compilers lack them.
276  */
277 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
278         typedef unsigned int size_t;
279         typedef int ssize_t;
280 #endif
281 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
282         typedef long time_t;
283 #endif /* _TIME_T_DEFINED || __time_t_defined */
284
285 /*! Bulk storage large enough for both pointers or integers */
286 typedef void * iptr_t;
287 typedef const void * const_iptr_t;
288 #define IPTR iptr_t  /* OBSOLETE */
289
290 typedef long utime_t;            /*!< Type for time expressed in microseconds */
291 typedef unsigned char sig_t;     /*!< Type for signal bits */
292 typedef unsigned char sigset_t;  /*!< Type for signal masks */
293 typedef unsigned char page_t;    /*!< Type for banked memory pages */
294
295 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
296         /*!
297          * \name ISO C99 fixed-size types
298          *
299          * These should be in <stdint.h>, but many compilers lack them.
300          * \{
301          */
302         typedef signed char         int8_t;
303         typedef short int           int16_t;
304         typedef long int            int32_t;
305         typedef unsigned char       uint8_t;
306         typedef unsigned short int  uint16_t;
307         typedef unsigned long int   uint32_t;
308         /* \} */
309 #elif defined(__GNUC__) && defined(__AVR__)
310         /* avr-libc is weird... */
311         #include <inttypes.h>
312 #else
313         /* This is the correct location. */
314         #include <stdint.h>
315 #endif
316
317 /*!
318  * \name Types for hardware registers.
319  *
320  * Only use these types for registers whose contents can
321  * be changed asynchronously by external hardware.
322  *
323  * \{
324  */
325 #if CPU_DSP56K
326         /* Registers can be accessed only through 16-bit pointers */
327         typedef volatile uint16_t  reg16_t;
328 #else
329         typedef volatile uint8_t   reg8_t;
330         typedef volatile uint16_t  reg16_t;
331         typedef volatile uint32_t  reg32_t;
332 #endif
333 /*\}*/
334
335
336 /* Quasi-ANSI macros */
337 #ifndef offsetof
338         /*!
339          * Return the byte offset of the member \a m in struct \a s
340          *
341          * \note This macro should be defined in "stddef.h" and is sometimes
342          *       compiler-specific (g++ has a builtin for it).
343          */
344         #define offsetof(s,m)  (size_t)&(((s *)0)->m)
345 #endif
346 #ifndef countof
347         /*!
348          * Count the number of elements in the static array \a a
349          *
350          * \note This macro is non-standard, but implmenents a very common idiom
351          */
352         #define countof(a)  (sizeof(a) / sizeof(*(a)))
353 #endif
354
355 /*! Issue a compilation error if the \a condition is false */
356 #define STATIC_ASSERT(condition)  \
357         extern char PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1]
358
359 #endif /* DEVLIB_COMPILER_H */