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