4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2001, 2002, 2003 Bernie Innocenti <bernie@codewiz.org>
34 * \brief Additional support macros for compiler independance
37 * \author Bernie Innocenti <bernie@codewiz.org>
40 #ifndef BERTOS_COMPILER_H
41 #define BERTOS_COMPILER_H
43 #include <cpu/detect.h>
46 #if defined __GNUC__ && defined __GNUC_MINOR__
47 #define GNUC_PREREQ(maj, min) \
48 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
50 #define GNUC_PREREQ(maj, min) 0
53 /* Some CW versions do not allow enabling C99 from the settings panel. */
54 #if defined(__MWERKS__)
58 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
59 #define COMPILER_C99 1
61 #define COMPILER_C99 0
65 /** Concatenate two different preprocessor tokens (allowing macros to expand) */
66 #define PP_CAT(x,y) PP_CAT__(x,y)
67 #define PP_CAT__(x,y) x ## y
68 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
69 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
70 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
72 /** String-ize a token (allowing macros to expand) */
73 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
74 #define PP_STRINGIZE__(x) #x
77 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
79 #pragma language=extended
83 #define COMPILER_VARIADIC_MACROS 1
85 #define INTERRUPT(x) __irq __arm void x (void)
86 #define INLINE static inline
88 /* Include some standard C89/C99 stuff */
95 // IAR has size_t as built-in type, but does not define this symbol.
96 #define _SIZE_T_DEFINED
98 #define INTERRUPT(x) interrupt [x]
99 #define REGISTER shortad
100 #define INLINE /* unsupported */
103 * Imported from <longjmp.h>. Unfortunately, we can't just include
104 * this header because it typedefs jmp_buf to be an array of chars.
105 * This would allow the compiler to place the buffer on an odd address.
106 * The CPU _should_ be able to perform word accesses to
107 * unaligned data, but there are *BUGS* in the 80196KC with
108 * some combinations of opcodes and addressing modes. One of
109 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
110 * provided by the IAR compiler ANSI C library. When ?GR contains
111 * an odd address, surprisingly the CPU will copy the high order
112 * byte of the source operand (SP) in the low order byte of the
113 * destination operand (the memory location pointed to by ?GR).
115 * We also need to replace the library setjmp()/longjmp() with
116 * our own versions because the IAR implementation "forgets" to
117 * save the contents of local registers (?LR).
121 void *sp; /* Stack pointer */
122 void *return_addr; /* Return address */
123 int lr[6]; /* 6 local registers */
126 typedef struct _JMP_BUF jmp_buf[1];
128 int setjmp(jmp_buf env);
129 void longjmp(jmp_buf env, int val);
131 /* Fake bool support */
134 typedef unsigned char bool;
137 #error Unsupported CPU
140 #elif defined(_MSC_VER) /* Win32 emulation support */
142 /* MSVC doesn't provide <stdbool.h>. */
147 #endif /* !__cplusplus */
149 /* These C99 functions are oddly named in MSVCRT32.lib */
150 #define snprintf _snprintf
151 #define vsnprintf _vsnprintf
153 /* MSVC doesn't support C99's __func__, but has a similar extension. */
154 #define __func__ __FUNCTION__
156 /* MSVC doesn't support C99's inline keyword */
158 #define INLINE __inline
161 #elif defined(__GNUC__)
163 /* Compiler features */
164 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
165 #define COMPILER_TYPEOF 1
166 #define COMPILER_STATEMENT_EXPRESSIONS 1
169 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
170 #define NORETURN __attribute__((__noreturn__))
171 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
172 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
173 #define USED_VAR(type,name) __attribute__((__used__)) type name
174 #define INLINE static inline __attribute__((__always_inline__))
175 #define LIKELY(x) __builtin_expect(!!(x), 1)
176 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
177 #define PURE_FUNC __attribute__((pure))
178 #define CONST_FUNC __attribute__((const))
179 #define UNUSED_FUNC __attribute__((unused))
180 #define USED_FUNC __attribute__((__used__))
181 #define RESTRICT __restrict__
182 #define MUST_CHECK __attribute__((warn_unused_result))
183 #define PACKED __attribute__((packed))
185 * Force compiler to realod context variable.
187 #define MEMORY_BARRIER asm volatile ("" : : : "memory")
190 #define DEPRECATED __attribute__((__deprecated__))
194 #define ASSERT_TYPE_EQUAL(var1, var2) \
195 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2)))
196 #define ASSERT_TYPE_IS(var, type) \
197 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type))
200 /* Include some standard C89/C99 stuff */
205 #include <sys/types.h> /* for ssize_t */
210 * Disallow some C++ keywords as identifiers in C programs,
211 * for improved portability.
213 #pragma GCC poison new delete class template typename
214 #pragma GCC poison private protected public operator
215 #pragma GCC poison friend mutable using namespace
216 #pragma GCC poison cin cout cerr clog
221 #elif defined(__MWERKS__)
223 /* Compiler features */
224 #define COMPILER_VARIADIC_MACROS 1
225 #define COMPILER_TYPEOF 1
226 #define COMPILER_STATEMENT_EXPRESSIONS 1
228 #define typeof __typeof__
230 #define UNUSED_ARG(type,arg) type
236 // CodeWarrior has size_t as built-in type, but does not define this symbol.
237 #define _SIZE_T_DEFINED
240 #error unknown compiler
244 /* Defaults for compiler extensions. */
247 * \def COMPILER_VARIADIC_MACROS
248 * Support for macros with variable arguments.
250 #ifndef COMPILER_VARIADIC_MACROS
251 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
255 * \def COMPILER_TYPEOF
256 * Support for dynamic type identification.
258 #ifndef COMPILER_TYPEOF
259 #define COMPILER_TYPEOF 0
263 * \def COMPILER_STATEMENT_EXPRESSIONS
264 * Support for statement expressions.
266 #ifndef COMPILER_STATEMENT_EXPRESSIONS
267 #define COMPILER_STATEMENT_EXPRESSIONS 0
270 /* A few defaults for missing compiler features. */
272 #define INLINE static inline
275 #define NORETURN /* nothing */
278 #define FORMAT(type,fmt,first) /* nothing */
281 #define DEPRECATED /* nothing */
284 #define UNUSED_ARG(type,arg) type arg
287 #define UNUSED_VAR(type,name) type name
290 #define USED_VAR(type,name) type name
293 #define REGISTER /* nothing */
299 #define UNLIKELY(x) x
302 #define PURE_FUNC /* nothing */
305 #define CONST_FUNC /* nothing */
308 #define UNUSED_FUNC /* nothing */
311 #define USED_FUNC /* nothing */
314 #define RESTRICT /* nothing */
317 #define MUST_CHECK /* nothing */
320 #define PACKED /* nothing */
322 #ifndef MEMORY_BARRIER
323 #define MEMORY_BARRIER /* nothing */
324 #warning No memory barrier defined for select compiler. If you use the kernel check it.
328 /* Misc definitions */
330 #define NULL (void *)0
336 /* Support for hybrid C/C++ applications. */
338 #define EXTERN_C extern "C"
339 #define EXTERN_C_BEGIN extern "C" {
340 #define EXTERN_C_END }
341 #define EXTERN_CONST extern const
342 #define CONST_CAST(TYPE,EXPR) (const_cast<TYPE>(EXPR))
344 #define EXTERN_C extern
345 #define EXTERN_C_BEGIN /* nothing */
346 #define EXTERN_C_END /* nothing */
347 #define EXTERN_CONST const
348 #define CONST_CAST(TYPE,EXPR) ((TYPE)(EXPR)) /* FIXME: How can we suppress the warning in C? */
352 #if defined(_MSC_VER) \
353 || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
355 * \name ISO C99 fixed-size types
357 * These should be in <stdint.h>, but a few compilers lack them.
360 typedef signed char int8_t;
361 typedef unsigned char uint8_t;
362 typedef short int int16_t;
363 typedef unsigned short int uint16_t;
364 typedef long int int32_t; /* _WIN64 safe */
365 typedef unsigned long int uint32_t; /* _WIN64 safe */
368 typedef __int64 int64_t;
369 typedef unsigned __int64 uint64_t;
371 typedef long long int64_t;
372 typedef unsigned long long uint64_t;
376 /* This is the standard location. */
382 * The ATmega8 has a very small Flash, so we can't afford
383 * to link in support routines for 32bit integer arithmetic.
385 typedef int16_t ticks_t; /**< Type for time expressed in ticks. */
386 typedef int16_t mtime_t; /**< Type for time expressed in milliseconds. */
387 typedef int16_t utime_t; /**< Type for time expressed in microseconds. */
388 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
389 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
390 #define MTIME_INFINITE 0x7FFFL
392 typedef int32_t ticks_t; /**< Type for time expressed in ticks. */
394 typedef int32_t utime_t; /**< Type for time expressed in microseconds. */
395 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
397 #ifndef DEVLIB_MTIME_DEFINED
398 #define DEVLIB_MTIME_DEFINED 1 /* Resolve conflict with <os/hptime.h> */
399 typedef int32_t mtime_t; /**< Type for time expressed in milliseconds. */
400 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
401 #define MTIME_INFINITE 0x7FFFFFFFL
405 /** Bulk storage large enough for both pointers or integers. */
406 typedef void * iptr_t;
408 /** Bulk storage large enough for both pointers to constants or integers. */
409 typedef const void * const_iptr_t;
411 typedef unsigned char sigbit_t; /**< Type for signal bits. */
412 typedef unsigned char sigmask_t; /**< Type for signal masks. */
413 typedef unsigned char page_t; /**< Type for banked memory pages. */
417 * \name Standard type definitions.
419 * These should be in <sys/types.h> or <stddef.h>, but many compilers
420 * and C libraries lack them.
422 * We check for some common definitions to avoid redefinitions:
424 * glibc, avr-libc: _SIZE_T_DEFINED, __ssize_t_defined
425 * Darwin libc: _BSD_SIZE_T_DEFINED_, _SIZE_T
430 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
433 /* 32bit or 64bit (32bit for _WIN64). */
434 typedef unsigned long size_t;
440 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
442 /* 32bit or 64bit (32bit for _WIN64). */
443 typedef long ssize_t;
447 /* 16bit (missing in avr-libc's sys/types.h). */
457 * \name Types for hardware registers.
459 * Only use these types for registers whose contents can
460 * be changed asynchronously by external hardware.
465 /* Registers can be accessed only through 16-bit pointers */
466 typedef volatile uint16_t reg16_t;
468 typedef volatile uint8_t reg8_t;
469 typedef volatile uint16_t reg16_t;
470 typedef volatile uint32_t reg32_t;
475 /* Quasi-ANSI macros */
478 * Return the byte offset of the member \a m in struct \a s.
480 * \note This macro should be defined in "stddef.h" and is sometimes
481 * compiler-specific (g++ has a builtin for it).
483 #define offsetof(s,m) (size_t)&(((s *)0)->m)
487 * Count the number of elements in the static array \a a.
489 * \note This macro is non-standard, but implements a very common idiom
491 #define countof(a) (sizeof(a) / sizeof(*(a)))
495 * Cast a member of a structure out to the containing structure.
497 * \param ptr the pointer to the member.
498 * \param type the type of the container struct this is embedded in.
499 * \param member the name of the member within the struct.
501 #if COMPILER_TYPEOF && COMPILER_STATEMENT_EXPRESSIONS
502 #define containerof(ptr, type, member) ({ \
503 const typeof( ((type *)0)->member ) *_mptr = (ptr); /* type check */ \
504 (type *)((char *)_mptr - offsetof(type, member)); \
507 #define containerof(ptr, type, member) \
508 ( (type *)((char *)(ptr) - offsetof(type, member)) )
511 /** Issue a compilation error if the \a condition is false */
512 #define STATIC_ASSERT(condition) \
513 UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
515 #ifndef ASSERT_TYPE_EQUAL
516 /** Ensure two variables have the same type. */
517 #define ASSERT_TYPE_EQUAL(var1, var2) \
518 do { (void)(&(var1) == &(var2)); } while(0)
521 #ifndef ASSERT_TYPE_IS
522 /** Ensure variable is of specified type. */
523 #define ASSERT_TYPE_IS(var, type) \
524 do { (void)(&(var) == (type *)0); } while(0)
527 #endif /* BERTOS_COMPILER_H */