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
36 * \author Bernie Innocenti <bernie@codewiz.org>
39 #ifndef BERTOS_COMPILER_H
40 #define BERTOS_COMPILER_H
42 #include <cpu/detect.h>
45 #if defined __GNUC__ && defined __GNUC_MINOR__
46 #define GNUC_PREREQ(maj, min) \
47 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
49 #define GNUC_PREREQ(maj, min) 0
52 /* Some CW versions do not allow enabling C99 from the settings panel. */
53 #if defined(__MWERKS__)
57 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
58 #define COMPILER_C99 1
60 #define COMPILER_C99 0
64 /** Concatenate two different preprocessor tokens (allowing macros to expand) */
65 #define PP_CAT(x,y) PP_CAT__(x,y)
66 #define PP_CAT__(x,y) x ## y
67 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
68 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
69 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
71 /** String-ize a token (allowing macros to expand) */
72 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
73 #define PP_STRINGIZE__(x) #x
79 #define COUNT_PARMS2(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _, ...) _
80 #define COUNT_PARMS(...) \
81 COUNT_PARMS2(11 , ## __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
86 * #define foo_init(...) PP_CAT(foo_init_, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
91 #define COUNT_PARMS2(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _, ...) _
92 #define COUNT_PARMS(args...) \
93 COUNT_PARMS2(11 , ## args, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
98 * #define foo_init(args...) PP_CAT(foo_init_, COUNT_PARMS(args)) (args)
104 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
106 #pragma language=extended
110 #define COMPILER_VARIADIC_MACROS 1
112 #define INTERRUPT(x) __irq __arm void x (void)
113 #define INLINE static inline
115 /* Include some standard C89/C99 stuff */
122 // IAR has size_t as built-in type, but does not define this symbol.
123 #define _SIZE_T_DEFINED
125 #define INTERRUPT(x) interrupt [x]
126 #define REGISTER shortad
127 #define INLINE /* unsupported */
130 * Imported from <longjmp.h>. Unfortunately, we can't just include
131 * this header because it typedefs jmp_buf to be an array of chars.
132 * This would allow the compiler to place the buffer on an odd address.
133 * The CPU _should_ be able to perform word accesses to
134 * unaligned data, but there are *BUGS* in the 80196KC with
135 * some combinations of opcodes and addressing modes. One of
136 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
137 * provided by the IAR compiler ANSI C library. When ?GR contains
138 * an odd address, surprisingly the CPU will copy the high order
139 * byte of the source operand (SP) in the low order byte of the
140 * destination operand (the memory location pointed to by ?GR).
142 * We also need to replace the library setjmp()/longjmp() with
143 * our own versions because the IAR implementation "forgets" to
144 * save the contents of local registers (?LR).
148 void *sp; /* Stack pointer */
149 void *return_addr; /* Return address */
150 int lr[6]; /* 6 local registers */
153 typedef struct _JMP_BUF jmp_buf[1];
155 int setjmp(jmp_buf env);
156 void longjmp(jmp_buf env, int val);
158 /* Fake bool support */
161 typedef unsigned char bool;
164 #error Unsupported CPU
167 #elif defined(_MSC_VER) /* Win32 emulation support */
169 /* MSVC doesn't provide <stdbool.h>. */
174 #endif /* !__cplusplus */
176 /* These C99 functions are oddly named in MSVCRT32.lib */
177 #define snprintf _snprintf
178 #define vsnprintf _vsnprintf
180 /* MSVC doesn't support C99's __func__, but has a similar extension. */
181 #define __func__ __FUNCTION__
183 /* MSVC doesn't support C99's inline keyword */
185 #define INLINE __inline
188 #elif defined(__GNUC__)
190 /* Compiler features */
191 #define COMPILER_VARIADIC_MACROS 1 /* Even in C++ */
192 #define COMPILER_TYPEOF 1
193 #define COMPILER_STATEMENT_EXPRESSIONS 1
196 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
197 #define NORETURN __attribute__((__noreturn__))
198 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
199 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
200 #define USED_VAR(type,name) __attribute__((__used__)) type name
201 #define INLINE static inline __attribute__((__always_inline__))
202 #define NOINLINE __attribute__((noinline))
203 #define LIKELY(x) __builtin_expect(!!(x), 1)
204 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
205 #define PURE_FUNC __attribute__((pure))
206 #define CONST_FUNC __attribute__((const))
207 #define UNUSED_FUNC __attribute__((unused))
208 #define USED_FUNC __attribute__((__used__))
209 #define RESTRICT __restrict__
210 #define MUST_CHECK __attribute__((warn_unused_result))
211 #define PACKED __attribute__((packed))
212 #define ALIGNED(x) __attribute__ ((__aligned__(x)))
213 #if CPU_ARM | CPU_CM3
214 #define NAKED __attribute__((naked))
220 * Force compiler to realod context variable.
222 #define MEMORY_BARRIER asm volatile ("" : : : "memory")
225 #define DEPRECATED __attribute__((__deprecated__))
229 #define UNREACHABLE() __builtin_unreachable()
233 #define ASSERT_TYPE_EQUAL(var1, var2) \
234 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2)))
235 #define ASSERT_TYPE_IS(var, type) \
236 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type))
239 /* Include some standard C89/C99 stuff */
244 #include <sys/types.h> /* for ssize_t */
249 * Disallow some C++ keywords as identifiers in C programs,
250 * for improved portability.
252 #pragma GCC poison new delete class template typename
253 #pragma GCC poison private protected public operator
254 #pragma GCC poison friend mutable using namespace
255 #pragma GCC poison cin cout cerr clog
260 #elif defined(__MWERKS__)
262 /* Compiler features */
263 #define COMPILER_VARIADIC_MACROS 1
264 #define COMPILER_TYPEOF 1
265 #define COMPILER_STATEMENT_EXPRESSIONS 1
267 #define typeof __typeof__
269 #define UNUSED_ARG(type,arg) type
275 // CodeWarrior has size_t as built-in type, but does not define this symbol.
276 #define _SIZE_T_DEFINED
279 #error unknown compiler
283 /* Defaults for compiler extensions. */
286 * \def COMPILER_VARIADIC_MACROS
287 * Support for macros with variable arguments.
289 #ifndef COMPILER_VARIADIC_MACROS
290 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
294 * \def COMPILER_TYPEOF
295 * Support for dynamic type identification.
297 #ifndef COMPILER_TYPEOF
298 #define COMPILER_TYPEOF 0
302 * \def COMPILER_STATEMENT_EXPRESSIONS
303 * Support for statement expressions.
305 #ifndef COMPILER_STATEMENT_EXPRESSIONS
306 #define COMPILER_STATEMENT_EXPRESSIONS 0
309 /* A few defaults for missing compiler features. */
311 #define INLINE static inline
314 #define NOINLINE /* nothing */
317 #define NORETURN /* nothing */
320 #define FORMAT(type,fmt,first) /* nothing */
323 #define DEPRECATED /* nothing */
326 #define UNUSED_ARG(type,arg) type arg
329 #define UNUSED_VAR(type,name) type name
332 #define USED_VAR(type,name) type name
335 #define REGISTER /* nothing */
341 #define UNLIKELY(x) x
344 #define PURE_FUNC /* nothing */
347 #define CONST_FUNC /* nothing */
350 #define UNUSED_FUNC /* nothing */
353 #define USED_FUNC /* nothing */
356 #define RESTRICT /* nothing */
359 #define MUST_CHECK /* nothing */
362 #define PACKED /* nothing */
365 #define ALIGNED /* nothing */
367 #ifndef MEMORY_BARRIER
368 #define MEMORY_BARRIER /* nothing */
369 #warning No memory barrier defined for select compiler. If you use the kernel check it.
372 #define UNREACHABLE() for (;;)
376 /* Misc definitions */
378 #define NULL (void *)0
384 /* Support for hybrid C/C++ applications. */
386 #define EXTERN_C extern "C"
387 #define EXTERN_C_BEGIN extern "C" {
388 #define EXTERN_C_END }
389 #define EXTERN_CONST extern const
390 #define CONST_CAST(TYPE,EXPR) (const_cast<TYPE>(EXPR))
392 #define EXTERN_C extern
393 #define EXTERN_C_BEGIN /* nothing */
394 #define EXTERN_C_END /* nothing */
395 #define EXTERN_CONST const
396 #define CONST_CAST(TYPE,EXPR) ((TYPE)(EXPR)) /* FIXME: How can we suppress the warning in C? */
400 #if defined(_MSC_VER) \
401 || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
403 * \name ISO C99 fixed-size types
405 * These should be in <stdint.h>, but a few compilers lack them.
408 typedef signed char int8_t;
409 typedef unsigned char uint8_t;
410 typedef short int int16_t;
411 typedef unsigned short int uint16_t;
412 typedef long int int32_t; /* _WIN64 safe */
413 typedef unsigned long int uint32_t; /* _WIN64 safe */
416 typedef __int64 int64_t;
417 typedef unsigned __int64 uint64_t;
419 typedef long long int64_t;
420 typedef unsigned long long uint64_t;
424 /* This is the standard location. */
430 * The ATmega8 has a very small Flash, so we can't afford
431 * to link in support routines for 32bit integer arithmetic.
433 typedef int16_t ticks_t; /**< Type for time expressed in ticks. */
434 typedef int16_t mtime_t; /**< Type for time expressed in milliseconds. */
435 typedef int16_t utime_t; /**< Type for time expressed in microseconds. */
436 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
437 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
438 #define MTIME_INFINITE 0x7FFFL
440 typedef int32_t ticks_t; /**< Type for time expressed in ticks. */
442 typedef int32_t utime_t; /**< Type for time expressed in microseconds. */
443 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
445 #ifndef DEVLIB_MTIME_DEFINED
446 #define DEVLIB_MTIME_DEFINED 1 /* Resolve conflict with <os/hptime.h> */
447 typedef int32_t mtime_t; /**< Type for time expressed in milliseconds. */
448 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
449 #define MTIME_INFINITE 0x7FFFFFFFL
453 /** User defined callback type */
454 typedef void (*Hook)(void *);
456 /** Bulk storage large enough for both pointers or integers. */
457 typedef void * iptr_t;
459 /** Bulk storage large enough for both pointers to constants or integers. */
460 typedef const void * const_iptr_t;
462 typedef unsigned char sigbit_t; /**< Type for signal bits. */
463 typedef unsigned char sigmask_t; /**< Type for signal masks. */
468 typedef struct Signal
470 sigmask_t wait; /**< Signals the process is waiting for */
471 sigmask_t recv; /**< Received signals */
475 * \name Standard type definitions.
477 * These should be in <sys/types.h> or <stddef.h>, but many compilers
478 * and C libraries lack them.
480 * We check for some common definitions to avoid redefinitions:
482 * glibc, avr-libc: _SIZE_T_DEFINED, __ssize_t_defined
483 * Darwin libc: _BSD_SIZE_T_DEFINED_, _SIZE_T
488 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
491 /* 32bit or 64bit (32bit for _WIN64). */
492 typedef unsigned long size_t;
498 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
500 /* 32bit or 64bit (32bit for _WIN64). */
501 typedef long ssize_t;
502 #elif CPU_ARM || CPU_CM3
504 #elif CPU_AVR || CPU_MSP430
505 /* 16bit (missing in avr-/msp430-libc's sys/types.h). */
515 * \name Types for hardware registers.
517 * Only use these types for registers whose contents can
518 * be changed asynchronously by external hardware.
523 /* Registers can be accessed only through 16-bit pointers */
524 typedef volatile uint16_t reg16_t;
526 typedef volatile uint8_t reg8_t;
527 typedef volatile uint16_t reg16_t;
528 typedef volatile uint32_t reg32_t;
533 /* Quasi-ANSI macros */
536 * Return the byte offset of the member \a m in struct \a s.
538 * \note This macro should be defined in "stddef.h" and is sometimes
539 * compiler-specific (g++ has a builtin for it).
541 #define offsetof(s,m) (size_t)&(((s *)0)->m)
545 * Count the number of elements in the static array \a a.
547 * \note This macro is non-standard, but implements a very common idiom
549 #if defined(__GNUC__) && !defined(__cplusplus)
551 * Perform a compile time type checking: countof() can only
552 * work with static arrays, so throw a compile time error if a
553 * pointer is passed as argument.
555 * NOTE: the construct __builtin_types_compatible_p() is only
558 #define countof(a) (sizeof(a) / sizeof(*(a)) + \
559 STATIC_ASSERT_EXPR( \
560 !__builtin_types_compatible_p( \
561 typeof(a), typeof(&a[0]))))
563 #define countof(a) (sizeof(a) / sizeof(*(a)))
568 * Return the alignment in memory of a generic data type.
570 * \note We need to worry about alignment when allocating memory that
571 * will be used later by unknown objects (e.g., malloc()) or, more
572 * generally, whenever creating generic container types.
574 #define alignof(type) offsetof(struct { char c; type member; }, member)
578 * Cast a member of a structure out to the containing structure.
580 * \param ptr the pointer to the member.
581 * \param type the type of the container struct this is embedded in.
582 * \param member the name of the member within the struct.
584 #if COMPILER_TYPEOF && COMPILER_STATEMENT_EXPRESSIONS
585 #define containerof(ptr, type, member) ({ \
586 typeof( ((type *)0)->member ) *_mptr = (ptr); /* type check */ \
587 (type *)(void *)((char *)_mptr - offsetof(type, member)); \
590 #define containerof(ptr, type, member) \
591 ( (type *)(void *)((char *)(ptr) - offsetof(type, member)) )
594 /** Issue a compilation error if the \a condition is false */
595 #define STATIC_ASSERT(condition) \
596 UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
599 * Issue a compilation error if \a __cond is false (this can be used inside an
602 #define STATIC_ASSERT_EXPR(__cond) \
603 (sizeof(struct { int STATIC_ASSERTION_FAILED__:!!(__cond); }) * 0)
605 #ifndef ASSERT_TYPE_EQUAL
606 /** Ensure two variables have the same type. */
607 #define ASSERT_TYPE_EQUAL(var1, var2) \
608 do { (void)(&(var1) == &(var2)); } while(0)
611 #ifndef ASSERT_TYPE_IS
612 /** Ensure variable is of specified type. */
613 #define ASSERT_TYPE_IS(var, type) \
614 do { (void)(&(var) == (type *)0); } while(0)
618 * Prevent the compiler from optimizing access to the variable \a x, enforcing
619 * a refetch from memory. This also forbid from reordering successing instances
622 #define ACCESS_SAFE(x) (*(volatile typeof(x) *)&(x))
624 #endif /* BERTOS_COMPILER_H */