Add X86_64 support.
[bertos.git] / cfg / compiler.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 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 README.devlib for information.
7  * -->
8  *
9  * \brief Additional support macros for compiler independance
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.4  2005/06/14 06:15:10  bernie
18  *#* Add X86_64 support.
19  *#*
20  *#* Revision 1.3  2005/04/12 01:37:01  bernie
21  *#* Metrowerks touchups from HeCo.
22  *#*
23  *#* Revision 1.2  2005/04/11 19:10:27  bernie
24  *#* Include top-level headers from cfg/ subdir.
25  *#*
26  *#* Revision 1.1  2005/04/11 19:04:13  bernie
27  *#* Move top-level headers to cfg/ subdir.
28  *#*
29  *#* Revision 1.44  2005/03/29 06:39:59  bernie
30  *#* setjmp.h, time_t: Remove ancient retrocompatibility; Remove MSVC double to float hack.
31  *#*
32  *#* Revision 1.43  2005/03/01 23:15:12  bernie
33  *#* Remove compatibility hack.
34  *#*
35  *#* Revision 1.42  2005/02/16 20:28:46  bernie
36  *#* Move PGM macros to mware/pgm.h
37  *#*
38  *#* Revision 1.41  2005/01/22 04:19:22  bernie
39  *#* MTIME_INFINITE: New constant.
40  *#*
41  *#* Revision 1.40  2005/01/20 18:46:04  aleph
42  *#* Add progmem datatypes; PSTR() definition.
43  *#*/
44 #ifndef DEVLIB_COMPILER_H
45 #define DEVLIB_COMPILER_H
46
47 #include <cfg/cpu_detect.h>
48
49
50 #if defined __GNUC__ && defined __GNUC_MINOR__
51         #define GNUC_PREREQ(maj, min) \
52                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
53 #else
54         #define GNUC_PREREQ(maj, min) 0
55 #endif
56
57 /* Some CW versions do not allow enabling C99 from the settings panel. */
58 #if defined(__MWERKS__)
59         #pragma c99 on
60 #endif
61
62 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
63         #define COMPILER_C99      1
64 #else
65         #define COMPILER_C99      0
66 #endif
67
68
69 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
70 #define PP_CAT(x,y)         PP_CAT__(x,y)
71 #define PP_CAT__(x,y)       x ## y
72 #define PP_CAT3(x,y,z)      PP_CAT(PP_CAT(x,y),z)
73 #define PP_CAT4(x,y,z,w)    PP_CAT(PP_CAT3(x,y,z),w)
74 #define PP_CAT5(x,y,z,w,j)  PP_CAT(PP_CAT4(x,y,z,w),j)
75
76 /*! String-ize a token (allowing macros to expand) */
77 #define PP_STRINGIZE(x)     PP_STRINGIZE__(x)
78 #define PP_STRINGIZE__(x)   #x
79
80
81 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
82         #pragma language=extended
83         #define INTERRUPT(x)  interrupt [x]
84         #define REGISTER      shortad
85         #define INLINE        /* unsupported */
86
87         /*
88          * Imported from <longjmp.h>. Unfortunately, we can't just include
89          * this header because it typedefs jmp_buf to be an array of chars.
90          * This would allow the compiler to place the buffer on an odd address.
91          * The CPU _should_ be able to perform word accesses to
92          * unaligned data, but there are *BUGS* in the 80196KC with
93          * some combinations of opcodes and addressing modes. One of
94          * these, "ST SP,[?GR]+" is used in the longjmp() implementation
95          * provided by the IAR compiler ANSI C library. When ?GR contains
96          * an odd address, surprisingly the CPU will copy the high order
97          * byte of the source operand (SP) in the low order byte of the
98          * destination operand (the memory location pointed to by ?GR).
99          *
100          * We also need to replace the library setjmp()/longjmp() with
101          * our own versions because the IAR implementation "forgets" to
102          * save the contents of local registers (?LR).
103          */
104         struct _JMP_BUF
105         {
106                 void *sp;           /* Stack pointer */
107                 void *return_addr;  /* Return address */
108                 int lr[6];          /* 6 local registers */
109         };
110
111         typedef struct _JMP_BUF jmp_buf[1];
112
113         int setjmp(jmp_buf env);
114         void longjmp(jmp_buf env, int val);
115
116         /* Fake bool support */
117         #define true (1==1)
118         #define false (1!=1)
119         typedef unsigned char bool;
120
121 #elif defined(_MSC_VER) /* Win32 emulation support */
122
123         /* MSVC doesn't provide <stdbool.h>. */
124         #ifndef __cplusplus
125                 #define true (1==1)
126                 #define false (1!=1)
127                 typedef int bool;
128         #endif /* !__cplusplus */
129
130         /* These C99 functions are oddly named in MSVCRT32.lib */
131         #define snprintf _snprintf
132         #define vsnprintf _vsnprintf
133
134 #elif defined(__GNUC__)
135
136         /* Compiler features */
137         #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
138         #define COMPILER_TYPEOF 1
139         #define COMPILER_STATEMENT_EXPRESSIONS 1
140
141         /* GCC attributes */
142         #define FORMAT(type,fmt,first)  __attribute__((__format__(type, fmt, first)))
143         #define NORETURN                __attribute__((__noreturn__))
144         #define UNUSED_ARG(type,arg)    __attribute__((__unused__)) type arg
145         #define UNUSED_VAR(type,name)   __attribute__((__unused__)) type name
146         #define USED_VAR(type,name)     __attribute__((__used__)) type name
147         #define INLINE                  static inline __attribute__((__always_inline__))
148         #define LIKELY(x)               __builtin_expect(!!(x), 1)
149         #define UNLIKELY(x)             __builtin_expect(!!(x), 0)
150         #define PURE_FUNC               __attribute__((pure))
151         #define CONST_FUNC              __attribute__((const))
152         #define UNUSED_FUNC             __attribute__((unused))
153         #define USED_FUNC               __attribute__((__used__))
154         #define RESTRICT                __restrict__
155         #define MUST_CHECK              __attribute__((warn_unused_result))
156         #if GNUC_PREREQ(3,1)
157                 #define DEPRECATED  __attribute__((__deprecated__))
158         #endif
159
160         #if CPU_AVR
161                 #include <stddef.h>
162                 #include <stdbool.h>
163         #else
164                 /* Include some standard C89/C99 stuff */
165                 #include <stddef.h>
166                 #include <stdbool.h>
167         #endif
168
169         #ifndef __cplusplus
170                 /*
171                  * Disallow some C++ keywords as identifiers in C programs,
172                  * for improved portability.
173                  */
174                 #pragma GCC poison new delete class template typename
175                 #pragma GCC poison private protected public operator
176                 #pragma GCC poison friend mutable using namespace
177                 #pragma GCC poison cin cout cerr clog
178         #endif
179
180 #elif defined(__MWERKS__)
181
182         /* Compiler features */
183         #define COMPILER_VARIADIC_MACROS 1
184         #define COMPILER_TYPEOF 1
185         #define COMPILER_STATEMENT_EXPRESSIONS 1
186
187         #define typeof __typeof__
188
189         #define UNUSED_ARG(type,arg)    type
190
191         #include <stdint.h>
192         #include <stddef.h>
193         #include <stdbool.h>
194
195         // CodeWarrior has size_t as built-in type, but does not define this symbol.
196         #define _SIZE_T_DEFINED
197
198 #else
199         #error unknown compiler
200 #endif
201
202
203 /* Defaults for compiler extensions. */
204
205 /*!
206  * \def COMPILER_VARIADIC_MACROS
207  * Support for macros with variable arguments.
208  */
209 #ifndef COMPILER_VARIADIC_MACROS
210 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
211 #endif
212
213 /*!
214  * \def COMPILER_TYPEOF
215  * Support for macros with variable arguments.
216  */
217 #ifndef COMPILER_TYPEOF
218 #define COMPILER_TYPEOF 0
219 #endif
220
221 /*!
222  * \def COMPILER_STATEMENT_EXPRESSIONS
223  * Support for macros with variable arguments.
224  */
225 #ifndef COMPILER_STATEMENT_EXPRESSIONS
226 #define COMPILER_STATEMENT_EXPRESSIONS 0
227 #endif
228
229 /* A few defaults for missing compiler features. */
230 #ifndef INLINE
231 #define INLINE                 static inline
232 #endif
233 #ifndef NORETURN
234 #define NORETURN               /* nothing */
235 #endif
236 #ifndef FORMAT
237 #define FORMAT(type,fmt,first) /* nothing */
238 #endif
239 #ifndef DEPRECATED
240 #define DEPRECATED             /* nothing */
241 #endif
242 #ifndef UNUSED_ARG
243 #define UNUSED_ARG(type,arg)   type arg
244 #endif
245 #define UNUSED                 UNUSED_ARG /* OBSOLETE */
246 #ifndef UNUSED_VAR
247 #define UNUSED_VAR(type,name)  type name
248 #endif
249 #ifndef USED_VAR
250 #define USED_VAR(type,name)    type name
251 #endif
252 #ifndef REGISTER
253 #define REGISTER               /* nothing */
254 #endif
255 #ifndef INTERRUPT
256 #define INTERRUPT(x)           ERROR_NOT_IMPLEMENTED
257 #endif
258 #ifndef LIKELY
259 #define LIKELY(x)              x
260 #endif
261 #ifndef UNLIKELY
262 #define UNLIKELY(x)            x
263 #endif
264 #ifndef PURE_FUNC
265 #define PURE_FUNC              /* nothing */
266 #endif
267 #ifndef CONST_FUNC
268 #define CONST_FUNC             /* nothing */
269 #endif
270 #ifndef UNUSED_FUNC
271 #define UNUSED_FUNC            /* nothing */
272 #endif
273 #ifndef USED_FUNC
274 #define USED_FUNC              /* nothing */
275 #endif
276 #ifndef RESTRICT
277 #define RESTRICT               /* nothing */
278 #endif
279 #ifndef MUST_CHECK
280 #define MUST_CHECK             /* nothing */
281 #endif
282
283 /* Misc definitions */
284 #ifndef NULL
285 #define NULL  (void *)0
286 #endif
287 #ifndef EOF
288 #define EOF   (-1)
289 #endif
290
291
292 /* Support for hybrid C/C++ applications. */
293 #ifdef __cplusplus
294         #define EXTERN_C        extern "C"
295         #define EXTERN_C_BEGIN  extern "C" {
296         #define EXTERN_C_END    }
297 #else
298         #define EXTERN_C        extern
299         #define EXTERN_C_BEGIN  /* nothing */
300         #define EXTERN_C_END    /* nothing */
301 #endif
302
303
304 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
305         /*!
306          * \name ISO C99 fixed-size types
307          *
308          * These should be in <stdint.h>, but many compilers lack them.
309          * \{
310          */
311         typedef signed char         int8_t;
312         typedef unsigned char       uint8_t;
313         typedef short int           int16_t;
314         typedef unsigned short int  uint16_t;
315         typedef long int            int32_t; /* _WIN64 safe */
316         typedef unsigned long int   uint32_t; /* _WIN64 safe */
317
318         #ifdef _MSC_VER
319                 typedef __int64              int64_t;
320                 typedef unsigned __int64     uint64_t;
321         #else
322                 typedef long long            int64_t;
323                 typedef unsigned long long   uint64_t;
324         #endif
325         /* \} */
326 #elif defined(__GNUC__) && CPU_AVR
327         /* avr-libc is weird... (Fixed in avr-libc-1.2, hack to be removed soon) */
328         #include <inttypes.h>
329 #else
330         /* This is the correct location. */
331         #include <stdint.h>
332 #endif
333
334 #if CPU_AVR_ATMEGA8
335         /*
336          * The ATmega8 has a very small Flash, so we can't afford
337          * to link in support routines for 32bit integer arithmetic.
338          */
339         typedef int16_t mtime_t;  /*!< Type for time expressed in milliseconds. */
340         typedef int16_t utime_t;  /*!< Type for time expressed in microseconds. */
341         #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
342         #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
343         #define MTIME_INFINITE 0x7FFFL
344 #else
345         typedef int32_t mtime_t;  /*!< Type for time expressed in milliseconds. */
346         typedef int32_t utime_t;  /*!< Type for time expressed in microseconds. */
347         #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
348         #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
349         #define MTIME_INFINITE 0x7FFFFFFFL
350 #endif
351
352 /*! Bulk storage large enough for both pointers or integers. */
353 typedef void * iptr_t;
354 typedef const void * const_iptr_t;
355
356 typedef unsigned char sig_t;     /*!< Type for signal bits. */
357 typedef unsigned char sigmask_t; /*!< Type for signal masks. */
358 typedef unsigned char page_t;    /*!< Type for banked memory pages. */
359
360
361 /*!
362  * \name Standard type definitions.
363  *
364  * These should be in <sys/types.h> or <stddef.h>, but many compilers
365  * and C libraries lack them.
366  *
367  * We check for some common definitions to avoid redefinitions:
368  *
369  *    glibc, avr-libc: _SIZE_T_DEFINED
370  *    Darwin libc:     _BSD_SIZE_T_DEFINED_
371  *
372  * \{
373  */
374 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_))
375         #if CPU_REG_BITS > 32
376                 /* 64bit. */
377                 typedef unsigned long size_t;
378         #else
379                 /* 32bit or 16bit. */
380                 typedef unsigned int size_t;
381         #endif
382 #endif
383
384 #if !(defined(ssize_t) || defined(__ssize_t_defined))
385         #if CPU_REG_BITS > 32
386                 /* 64bit (32bit for _WIN64). */
387                 typedef long ssize_t;
388         #else
389                 /* 32bit or 16bit. */
390                 typedef int ssize_t;
391         #endif
392 #endif
393 /*\}*/
394
395
396 /*!
397  * \name Types for hardware registers.
398  *
399  * Only use these types for registers whose contents can
400  * be changed asynchronously by external hardware.
401  *
402  * \{
403  */
404 #if CPU_DSP56K
405         /* Registers can be accessed only through 16-bit pointers */
406         typedef volatile uint16_t  reg16_t;
407 #else
408         typedef volatile uint8_t   reg8_t;
409         typedef volatile uint16_t  reg16_t;
410         typedef volatile uint32_t  reg32_t;
411 #endif
412 /*\}*/
413
414
415 /* Quasi-ANSI macros */
416 #ifndef offsetof
417         /*!
418          * Return the byte offset of the member \a m in struct \a s.
419          *
420          * \note This macro should be defined in "stddef.h" and is sometimes
421          *       compiler-specific (g++ has a builtin for it).
422          */
423         #define offsetof(s,m)  (size_t)&(((s *)0)->m)
424 #endif
425 #ifndef countof
426         /*!
427          * Count the number of elements in the static array \a a.
428          *
429          * \note This macro is non-standard, but implements a very common idiom
430          */
431         #define countof(a)  (sizeof(a) / sizeof(*(a)))
432 #endif
433
434 /*! Issue a compilation error if the \a condition is false */
435 #define STATIC_ASSERT(condition)  \
436         UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1])
437
438 #endif /* DEVLIB_COMPILER_H */