Change UNUSED() macro to accept two arguments: type and name;
[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.4  2004/06/27 15:20:26  aleph
19  * Change UNUSED() macro to accept two arguments: type and name;
20  * Add macro GNUC_PREREQ to detect GCC version during build;
21  * Some spacing cleanups and typo fix
22  *
23  * Revision 1.3  2004/06/06 18:00:39  bernie
24  * PP_CAT(): New macro.
25  *
26  * Revision 1.2  2004/06/03 11:27:09  bernie
27  * Add dual-license information.
28  *
29  * Revision 1.1  2004/05/23 17:48:35  bernie
30  * Add top-level files.
31  *
32  */
33 #ifndef COMPILER_H
34 #define COMPILER_H
35
36 #include "arch_config.h"
37
38
39 #if defined __GNUC__ && defined __GNUC_MINOR__
40         #define GNUC_PREREQ(maj, min) \
41                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
42 #else
43         #define GNUC_PREREQ(maj, min) 0
44 #endif
45
46
47 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
48         #pragma language=extended
49         #define INTERRUPT(x)    interrupt [x]
50         #define REGISTER                shortad
51         #define ATOMIC                  monitor
52
53         /* GCC attributes */
54         #define FORMAT(type,fmt,first) /* nothing */
55         #define NORETURN               /* nothing */
56         #define UNUSED(type,arg)       type arg
57
58         /* Imported from <longjmp.h>. Unfortunately, we can't just include
59          * this header because it typedefs jmp_buf to be an array of chars.
60          * This would allow the compiler to place the buffer on an odd address.
61          * The CPU _should_ be able to perform word accesses to
62          * unaligned data, but there are *BUGS* in the 80196KC with
63          * some combinations of opcodes and addressing modes. One of
64          * these, "ST SP,[?GR]+" is used in the longjmp() implementation
65          * provided by the IAR compiler ANSI C library. When ?GR contains
66          * an odd address, surprisingly the CPU will copy the high order
67          * byte of the source operand (SP) in the low order byte of the
68          * destination operand (the memory location pointed to by ?GR).
69          *
70          * We also need to replace the library setjmp()/longjmp() with
71          * our own versions because the IAR implementation "forgets" to
72          * save the contents of local registers (?LR).
73          */
74         struct _JMP_BUF
75         {
76                 void *  sp;                             /* Stack pointer */
77                 void *  return_addr;    /* Return address */
78                 int             lr[6];                  /* 6 local registers */
79         };
80
81         typedef struct _JMP_BUF jmp_buf[1];
82
83         int setjmp(jmp_buf env);
84         void longjmp(jmp_buf env, int val);
85
86         #ifndef __cplusplus
87                 #define true 1
88                 #define false 0
89                 typedef unsigned char bool;
90         #endif /* !__cplusplus */
91
92 #elif defined(_MSC_VER) /* Win32 emulation support */
93
94         #include <setjmp.h>
95         #include <time.h> /* for time_t */
96         #define float double
97
98         #define REGISTER                /* nothing */
99         #define INTERRUPT(x)    /* nothing */
100
101         /* GCC attributes */
102         #define FORMAT(type,fmt,first) /* nothing */
103         #define NORETURN               /* nothing */
104         #define UNUSED(type,arg)       type arg
105         #define INLINE static inline
106
107         #ifdef __cplusplus
108                 extern "C" {
109         #endif
110                         void SchedulerIdle(void);
111         #ifdef __cplusplus
112                 }
113         #endif
114
115         /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
116         #define ReleaseSemaphore KReleaseSemaphore
117
118         #ifndef __cplusplus
119                 #define true 1
120                 #define false 0
121                 typedef int bool;
122         #endif /* !__cplusplus */
123
124 #elif defined(__GNUC__)
125
126         /* GCC attributes */
127         #define FORMAT(type,fmt,first)  __attribute__((__format__(type, fmt, first)))
128         #define NORETURN                __attribute__((__noreturn__))
129         #define UNUSED(type,arg)        __attribute__((__unused__)) type arg
130         //FIXME #define INLINE static inline
131         #define INLINE extern inline
132
133         #if defined(__i386__)
134
135                 /* hack to avoid conflicts with system type */
136                 #define sigset_t system_sigset_t
137                 #include <stddef.h>
138                 #include <setjmp.h>
139                 #include <stdbool.h>
140                 #undef system_sigset_t
141
142                 #define REGISTER                /* nothing */
143                 #define INTERRUPT(x)    /* nothing */
144
145                 #ifdef __cplusplus
146                         extern "C" {
147                 #endif
148                                 void SchedulerIdle(void);
149                 #ifdef __cplusplus
150                         }
151                 #endif
152
153         #elif defined(__AVR__)
154
155                 #include <stddef.h>
156                 #include <stdbool.h>
157                 #define FLASH           __attribute__((progmem))
158                 #define REGISTER    /* nothing */
159
160                 /* Missing printf-family functions in avr-libc/stdio.h */
161                 #include <stdarg.h>
162                 #include <avr/pgmspace.h>
163                 #if !GNUC_PREREQ(3,4)
164                         int vsprintf(char *buf, const char *fmt, va_list ap);
165                 #endif
166                 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
167
168                 /* Support for harvard architectures */
169                 #ifdef _PROGMEM
170                         #define PGM_READ_CHAR(s) pgm_read_byte(s)
171                         #define PGM_FUNC(x) x ## _P
172                         #define PGM_ATTR        PROGMEM
173                 #else
174                         #define PGM_READ_CHAR(s) (*(s))
175                         #define PGM_FUNC(x) x
176                         #define PGM_ATTR        /*nop*/
177                 #endif
178
179         #endif /* CPU */
180
181 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
182
183         #include <stdint.h>
184         #include <stddef.h>
185         #include <stdbool.h>
186         #include <setjmp.h>
187
188         #define FLASH           /* nothing */
189         #define REGISTER                /* nothing */
190         #define INTERRUPT(x)    ERROR_NOT_IMPLEMENTED
191         #define SCHEDULER_IDLE  /* nothing */
192         
193         #define INLINE          static inline
194
195         /* GCC attributes */
196         #define FORMAT(type,fmt,first)  /* nothing */
197         #define NORETURN                /* nothing */
198         #define UNUSED(type,arg)        type arg
199
200         /* Support for harvard architectures */
201         #define PGM_READ_CHAR(s) (*(s))
202         #define PGM_FUNC                /* nothing */
203         #define PGM_ATTR                /* nothing */
204         #define PSTR            /* nothing */
205
206 #else
207         #error unknown compiler
208 #endif
209
210 /* Misc definitions */
211 #ifndef NULL
212 #define NULL    0
213 #endif
214 #ifndef EOF
215 #define EOF             (-1)
216 #endif
217
218
219 /* Quasi-ANSI macros
220  *
221  * offsetof(s,m) - Give the byte offset of the member <m> in struct <s>
222  * countof(a) - Count the number of elements in the static array <a>
223  */
224 #ifndef offsetof
225 #define offsetof(s,m)   (size_t)&(((s *)0)->m)
226 #endif
227
228 #ifndef countof
229 #define countof(a) (sizeof(a) / sizeof(*(a)))
230 #endif
231
232 /* Simple macros */
233 #define ABS(a)          (((a) < 0) ? -(a) : (a))
234 #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
235 #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
236
237 /*! Convert a bit value to a binary flag */
238 #ifndef BV
239 #define BV(x)   (1<<(x))
240 #endif
241
242 /*! Round up \a x to an even multiple of the 2's power \a pad */
243 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
244
245 /*! Calculate a compile-time log2 for a uint8_t */
246 #define UINT8_LOG2(x) \
247         ((x) < 2 ? 0 : \
248          ((x) < 4 ? 1 : \
249           ((x) < 8 ? 2 : \
250            ((x) < 16 ? 3 : \
251             ((x) < 32 ? 4 : \
252              ((x) < 64 ? 5 : \
253               ((x) < 128 ? 6 : 7)))))))
254
255 /*! Calculate a compile-time log2 for a uint16_t */
256 #define UINT16_LOG2(x) \
257         ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
258
259 /*! Calculate a compile-time log2 for a uint32_t */
260 #define UINT32_LOG2(x) \
261         ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
262
263 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
264 #define PP_CAT(x,y)                       PP_CAT__(x,y)
265 #define PP_CAT__(x,y)                     x ## y
266
267 /* Type definitions - should go in <sys/types.h> */
268 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
269         #define size_t unsigned int
270 #endif
271 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
272         typedef long time_t;
273 #endif /* _TIME_T_DEFINED || __time_t_defined */
274
275 /*! Storage for pointers and integers */
276 #define IPTR void *
277
278 typedef long utime_t;
279 typedef unsigned char sig_t;
280 typedef unsigned char sigset_t;
281 typedef unsigned char page_t;
282
283 /* ISO C99 fixed-size types */
284 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
285         typedef signed char         int8_t;
286         typedef short int           int16_t;
287         typedef long int            int32_t;
288         typedef unsigned char       uint8_t;
289         typedef unsigned short int  uint16_t;
290         typedef unsigned long int   uint32_t;
291 #elif defined(__AVR__)
292 #       include <inttypes.h>
293 #else
294 #       include <stdint.h>
295 #endif
296
297 #if (defined(__m56800E__) || defined(__m56800__))
298         /* Registers can be accessed only through 16-bit pointers */
299         typedef volatile uint16_t  reg16_t;
300 #else
301         typedef volatile uint8_t   reg8_t;
302         typedef volatile uint16_t  reg16_t;
303         typedef volatile uint32_t  reg32_t;
304 #endif
305
306 #endif /* COMPILER_H */