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