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