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