00001
00040 #ifndef BERTOS_COMPILER_H
00041 #define BERTOS_COMPILER_H
00042
00043 #include <cpu/detect.h>
00044
00045
00046 #if defined __GNUC__ && defined __GNUC_MINOR__
00047 #define GNUC_PREREQ(maj, min) \
00048 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
00049 #else
00050 #define GNUC_PREREQ(maj, min) 0
00051 #endif
00052
00053
00054 #if defined(__MWERKS__)
00055 #pragma c99 on
00056 #endif
00057
00058 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
00059 #define COMPILER_C99 1
00060 #else
00061 #define COMPILER_C99 0
00062 #endif
00063
00064
00066 #define PP_CAT(x,y) PP_CAT__(x,y)
00067 #define PP_CAT__(x,y) x ## y
00068 #define PP_CAT3(x,y,z) PP_CAT(PP_CAT(x,y),z)
00069 #define PP_CAT4(x,y,z,w) PP_CAT(PP_CAT3(x,y,z),w)
00070 #define PP_CAT5(x,y,z,w,j) PP_CAT(PP_CAT4(x,y,z,w),j)
00071
00073 #define PP_STRINGIZE(x) PP_STRINGIZE__(x)
00074 #define PP_STRINGIZE__(x) #x
00075
00076
00077 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
00078
00079 #pragma language=extended
00080
00081 #if CPU_ARM
00082
00083 #define COMPILER_VARIADIC_MACROS 1
00084
00085 #define INTERRUPT(x) __irq __arm void x (void)
00086 #define INLINE static inline
00087
00088
00089 #include <stddef.h>
00090 #include <stdint.h>
00091 #include <stdbool.h>
00092
00093 #elif CPU_I196
00094
00095
00096 #define _SIZE_T_DEFINED
00097
00098 #define INTERRUPT(x) interrupt [x]
00099 #define REGISTER shortad
00100 #define INLINE
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 struct _JMP_BUF
00120 {
00121 void *sp;
00122 void *return_addr;
00123 int lr[6];
00124 };
00125
00126 typedef struct _JMP_BUF jmp_buf[1];
00127
00128 int setjmp(jmp_buf env);
00129 void longjmp(jmp_buf env, int val);
00130
00131
00132 #define true (1==1)
00133 #define false (1!=1)
00134 typedef unsigned char bool;
00135
00136 #else
00137 #error Unsupported CPU
00138 #endif
00139
00140 #elif defined(_MSC_VER)
00141
00142
00143 #ifndef __cplusplus
00144 #define true (1==1)
00145 #define false (1!=1)
00146 typedef int bool;
00147 #endif
00148
00149
00150 #define snprintf _snprintf
00151 #define vsnprintf _vsnprintf
00152
00153
00154 #define __func__ __FUNCTION__
00155
00156
00157 #ifndef __cplusplus
00158 #define INLINE __inline
00159 #endif
00160
00161 #elif defined(__GNUC__)
00162
00163
00164 #define COMPILER_VARIADIC_MACROS 1
00165 #define COMPILER_TYPEOF 1
00166 #define COMPILER_STATEMENT_EXPRESSIONS 1
00167
00168
00169 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
00170 #define NORETURN __attribute__((__noreturn__))
00171 #define UNUSED_ARG(type,arg) __attribute__((__unused__)) type arg
00172 #define UNUSED_VAR(type,name) __attribute__((__unused__)) type name
00173 #define USED_VAR(type,name) __attribute__((__used__)) type name
00174 #define INLINE static inline __attribute__((__always_inline__))
00175 #define LIKELY(x) __builtin_expect(!!(x), 1)
00176 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
00177 #define PURE_FUNC __attribute__((pure))
00178 #define CONST_FUNC __attribute__((const))
00179 #define UNUSED_FUNC __attribute__((unused))
00180 #define USED_FUNC __attribute__((__used__))
00181 #define RESTRICT __restrict__
00182 #define MUST_CHECK __attribute__((warn_unused_result))
00183 #define PACKED __attribute__((packed))
00184
00187 #define MEMORY_BARRIER asm volatile ("" : : : "memory")
00188
00189 #if GNUC_PREREQ(3,1)
00190 #define DEPRECATED __attribute__((__deprecated__))
00191 #endif
00192
00193 #ifndef __cplusplus
00194 #define ASSERT_TYPE_EQUAL(var1, var2) \
00195 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2)))
00196 #define ASSERT_TYPE_IS(var, type) \
00197 STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type))
00198 #endif
00199
00200
00201 #include <stddef.h>
00202 #include <stdint.h>
00203 #include <stdbool.h>
00204 #if !CPU_AVR
00205 #include <sys/types.h>
00206 #endif
00207
00208 #ifndef __cplusplus
00209
00210
00211
00212
00213 #pragma GCC poison new delete class template typename
00214 #pragma GCC poison private protected public operator
00215 #pragma GCC poison friend mutable using namespace
00216 #pragma GCC poison cin cout cerr clog
00217 #endif
00218
00219
00220
00221 #elif defined(__MWERKS__)
00222
00223
00224 #define COMPILER_VARIADIC_MACROS 1
00225 #define COMPILER_TYPEOF 1
00226 #define COMPILER_STATEMENT_EXPRESSIONS 1
00227
00228 #define typeof __typeof__
00229
00230 #define UNUSED_ARG(type,arg) type
00231
00232 #include <stddef.h>
00233 #include <stdint.h>
00234 #include <stdbool.h>
00235
00236
00237 #define _SIZE_T_DEFINED
00238
00239 #else
00240 #error unknown compiler
00241 #endif
00242
00243
00244
00245
00250 #ifndef COMPILER_VARIADIC_MACROS
00251 #define COMPILER_VARIADIC_MACROS (COMPILER_C99 != 0)
00252 #endif
00253
00258 #ifndef COMPILER_TYPEOF
00259 #define COMPILER_TYPEOF 0
00260 #endif
00261
00266 #ifndef COMPILER_STATEMENT_EXPRESSIONS
00267 #define COMPILER_STATEMENT_EXPRESSIONS 0
00268 #endif
00269
00270
00271 #ifndef INLINE
00272 #define INLINE static inline
00273 #endif
00274 #ifndef NORETURN
00275 #define NORETURN
00276 #endif
00277 #ifndef FORMAT
00278 #define FORMAT(type,fmt,first)
00279 #endif
00280 #ifndef DEPRECATED
00281 #define DEPRECATED
00282 #endif
00283 #ifndef UNUSED_ARG
00284 #define UNUSED_ARG(type,arg) type arg
00285 #endif
00286 #define UNUSED UNUSED_ARG
00287 #ifndef UNUSED_VAR
00288 #define UNUSED_VAR(type,name) type name
00289 #endif
00290 #ifndef USED_VAR
00291 #define USED_VAR(type,name) type name
00292 #endif
00293 #ifndef REGISTER
00294 #define REGISTER
00295 #endif
00296 #ifndef LIKELY
00297 #define LIKELY(x) x
00298 #endif
00299 #ifndef UNLIKELY
00300 #define UNLIKELY(x) x
00301 #endif
00302 #ifndef PURE_FUNC
00303 #define PURE_FUNC
00304 #endif
00305 #ifndef CONST_FUNC
00306 #define CONST_FUNC
00307 #endif
00308 #ifndef UNUSED_FUNC
00309 #define UNUSED_FUNC
00310 #endif
00311 #ifndef USED_FUNC
00312 #define USED_FUNC
00313 #endif
00314 #ifndef RESTRICT
00315 #define RESTRICT
00316 #endif
00317 #ifndef MUST_CHECK
00318 #define MUST_CHECK
00319 #endif
00320 #ifndef PACKED
00321 #define PACKED
00322 #endif
00323 #ifndef MEMORY_BARRIER
00324 #define MEMORY_BARRIER
00325 #warning No memory barrier defined for select compiler. If you use the kernel check it.
00326 #endif
00327
00328
00329
00330 #ifndef NULL
00331 #define NULL (void *)0
00332 #endif
00333 #ifndef EOF
00334 #define EOF (-1)
00335 #endif
00336
00337
00338 #ifdef __cplusplus
00339 #define EXTERN_C extern "C"
00340 #define EXTERN_C_BEGIN extern "C" {
00341 #define EXTERN_C_END }
00342 #define EXTERN_CONST extern const
00343 #define CONST_CAST(TYPE,EXPR) (const_cast<TYPE>(EXPR))
00344 #else
00345 #define EXTERN_C extern
00346 #define EXTERN_C_BEGIN
00347 #define EXTERN_C_END
00348 #define EXTERN_CONST const
00349 #define CONST_CAST(TYPE,EXPR) ((TYPE)(EXPR))
00350 #endif
00351
00352
00353 #if defined(_MSC_VER) \
00354 || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
00355
00361 typedef signed char int8_t;
00362 typedef unsigned char uint8_t;
00363 typedef short int int16_t;
00364 typedef unsigned short int uint16_t;
00365 typedef long int int32_t;
00366 typedef unsigned long int uint32_t;
00367
00368 #ifdef _MSC_VER
00369 typedef __int64 int64_t;
00370 typedef unsigned __int64 uint64_t;
00371 #else
00372 typedef long long int64_t;
00373 typedef unsigned long long uint64_t;
00374 #endif
00375
00376 #else
00377
00378 #include <stdint.h>
00379 #endif
00380
00381 #if CPU_AVR_ATMEGA8
00382
00383
00384
00385
00386 typedef int16_t ticks_t;
00387 typedef int16_t mtime_t;
00388 typedef int16_t utime_t;
00389 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
00390 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
00391 #define MTIME_INFINITE 0x7FFFL
00392 #else
00393 typedef int32_t ticks_t;
00395 typedef int32_t utime_t;
00396 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
00397
00398 #ifndef DEVLIB_MTIME_DEFINED
00399 #define DEVLIB_MTIME_DEFINED 1
00400 typedef int32_t mtime_t;
00401 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
00402 #define MTIME_INFINITE 0x7FFFFFFFL
00403 #endif
00404 #endif
00405
00407 typedef void * iptr_t;
00408
00410 typedef const void * const_iptr_t;
00411
00412 typedef unsigned char sigbit_t;
00413 typedef unsigned char sigmask_t;
00414 typedef unsigned char page_t;
00431 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
00432 || defined(_SIZE_T))
00433 #if CPU_X86
00434
00435 typedef unsigned long size_t;
00436 #else
00437 #error Unknown CPU
00438 #endif
00439 #endif
00440
00441 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
00442 #if CPU_X86
00443
00444 typedef long ssize_t;
00445 #elif CPU_ARM
00446 typedef int ssize_t;
00447 #elif CPU_AVR
00448
00449 typedef int ssize_t;
00450 #else
00451 #error Unknown CPU
00452 #endif
00453 #endif
00454
00455
00456
00465 #if CPU_DSP56K
00466
00467 typedef volatile uint16_t reg16_t;
00468 #else
00469 typedef volatile uint8_t reg8_t;
00470 typedef volatile uint16_t reg16_t;
00471 typedef volatile uint32_t reg32_t;
00472 #endif
00473
00474
00475
00476
00477 #ifndef offsetof
00478
00484 #define offsetof(s,m) (size_t)&(((s *)0)->m)
00485 #endif
00486 #ifndef countof
00487
00492 #define countof(a) (sizeof(a) / sizeof(*(a)))
00493 #endif
00494
00502 #if COMPILER_TYPEOF && COMPILER_STATEMENT_EXPRESSIONS
00503 #define containerof(ptr, type, member) ({ \
00504 const typeof( ((type *)0)->member ) *_mptr = (ptr); \
00505 (type *)((char *)_mptr - offsetof(type, member)); \
00506 })
00507 #else
00508 #define containerof(ptr, type, member) \
00509 ( (type *)((char *)(ptr) - offsetof(type, member)) )
00510 #endif
00511
00513 #define STATIC_ASSERT(condition) \
00514 UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
00515
00516 #ifndef ASSERT_TYPE_EQUAL
00517
00518 #define ASSERT_TYPE_EQUAL(var1, var2) \
00519 do { (void)(&(var1) == &(var2)); } while(0)
00520 #endif
00521
00522 #ifndef ASSERT_TYPE_IS
00523
00524 #define ASSERT_TYPE_IS(var, type) \
00525 do { (void)(&(var) == (type *)0); } while(0)
00526 #endif
00527
00528 #endif