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 #ifndef UNUSED_VAR
00287 #define UNUSED_VAR(type,name) type name
00288 #endif
00289 #ifndef USED_VAR
00290 #define USED_VAR(type,name) type name
00291 #endif
00292 #ifndef REGISTER
00293 #define REGISTER
00294 #endif
00295 #ifndef LIKELY
00296 #define LIKELY(x) x
00297 #endif
00298 #ifndef UNLIKELY
00299 #define UNLIKELY(x) x
00300 #endif
00301 #ifndef PURE_FUNC
00302 #define PURE_FUNC
00303 #endif
00304 #ifndef CONST_FUNC
00305 #define CONST_FUNC
00306 #endif
00307 #ifndef UNUSED_FUNC
00308 #define UNUSED_FUNC
00309 #endif
00310 #ifndef USED_FUNC
00311 #define USED_FUNC
00312 #endif
00313 #ifndef RESTRICT
00314 #define RESTRICT
00315 #endif
00316 #ifndef MUST_CHECK
00317 #define MUST_CHECK
00318 #endif
00319 #ifndef PACKED
00320 #define PACKED
00321 #endif
00322 #ifndef MEMORY_BARRIER
00323 #define MEMORY_BARRIER
00324 #warning No memory barrier defined for select compiler. If you use the kernel check it.
00325 #endif
00326
00327
00328
00329 #ifndef NULL
00330 #define NULL (void *)0
00331 #endif
00332 #ifndef EOF
00333 #define EOF (-1)
00334 #endif
00335
00336
00337 #ifdef __cplusplus
00338 #define EXTERN_C extern "C"
00339 #define EXTERN_C_BEGIN extern "C" {
00340 #define EXTERN_C_END }
00341 #define EXTERN_CONST extern const
00342 #define CONST_CAST(TYPE,EXPR) (const_cast<TYPE>(EXPR))
00343 #else
00344 #define EXTERN_C extern
00345 #define EXTERN_C_BEGIN
00346 #define EXTERN_C_END
00347 #define EXTERN_CONST const
00348 #define CONST_CAST(TYPE,EXPR) ((TYPE)(EXPR))
00349 #endif
00350
00351
00352 #if defined(_MSC_VER) \
00353 || ((defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)) && CPU_I196)
00354
00360 typedef signed char int8_t;
00361 typedef unsigned char uint8_t;
00362 typedef short int int16_t;
00363 typedef unsigned short int uint16_t;
00364 typedef long int int32_t;
00365 typedef unsigned long int uint32_t;
00366
00367 #ifdef _MSC_VER
00368 typedef __int64 int64_t;
00369 typedef unsigned __int64 uint64_t;
00370 #else
00371 typedef long long int64_t;
00372 typedef unsigned long long uint64_t;
00373 #endif
00374
00375 #else
00376
00377 #include <stdint.h>
00378 #endif
00379
00380 #if CPU_AVR_ATMEGA8
00381
00382
00383
00384
00385 typedef int16_t ticks_t;
00386 typedef int16_t mtime_t;
00387 typedef int16_t utime_t;
00388 #define SIZEOF_MTIME_T (16 / CPU_BITS_PER_CHAR)
00389 #define SIZEOF_UTIME_T (16 / CPU_BITS_PER_CHAR)
00390 #define MTIME_INFINITE 0x7FFFL
00391 #else
00392 typedef int32_t ticks_t;
00394 typedef int32_t utime_t;
00395 #define SIZEOF_UTIME_T (32 / CPU_BITS_PER_CHAR)
00396
00397 #ifndef DEVLIB_MTIME_DEFINED
00398 #define DEVLIB_MTIME_DEFINED 1
00399 typedef int32_t mtime_t;
00400 #define SIZEOF_MTIME_T (32 / CPU_BITS_PER_CHAR)
00401 #define MTIME_INFINITE 0x7FFFFFFFL
00402 #endif
00403 #endif
00404
00406 typedef void * iptr_t;
00407
00409 typedef const void * const_iptr_t;
00410
00411 typedef unsigned char sigbit_t;
00412 typedef unsigned char sigmask_t;
00413 typedef unsigned char page_t;
00430 #if !(defined(size_t) || defined(_SIZE_T_DEFINED) || defined(_BSD_SIZE_T_DEFINED_) \
00431 || defined(_SIZE_T))
00432 #if CPU_X86
00433
00434 typedef unsigned long size_t;
00435 #else
00436 #error Unknown CPU
00437 #endif
00438 #endif
00439
00440 #if !(defined(ssize_t) || defined(_SSIZE_T) || defined(__ssize_t_defined))
00441 #if CPU_X86
00442
00443 typedef long ssize_t;
00444 #elif CPU_ARM
00445 typedef int ssize_t;
00446 #elif CPU_AVR
00447
00448 typedef int ssize_t;
00449 #else
00450 #error Unknown CPU
00451 #endif
00452 #endif
00453
00454
00455
00464 #if CPU_DSP56K
00465
00466 typedef volatile uint16_t reg16_t;
00467 #else
00468 typedef volatile uint8_t reg8_t;
00469 typedef volatile uint16_t reg16_t;
00470 typedef volatile uint32_t reg32_t;
00471 #endif
00472
00473
00474
00475
00476 #ifndef offsetof
00477
00483 #define offsetof(s,m) (size_t)&(((s *)0)->m)
00484 #endif
00485 #ifndef countof
00486
00491 #define countof(a) (sizeof(a) / sizeof(*(a)))
00492 #endif
00493
00501 #if COMPILER_TYPEOF && COMPILER_STATEMENT_EXPRESSIONS
00502 #define containerof(ptr, type, member) ({ \
00503 const typeof( ((type *)0)->member ) *_mptr = (ptr); \
00504 (type *)((char *)_mptr - offsetof(type, member)); \
00505 })
00506 #else
00507 #define containerof(ptr, type, member) \
00508 ( (type *)((char *)(ptr) - offsetof(type, member)) )
00509 #endif
00510
00512 #define STATIC_ASSERT(condition) \
00513 UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1])
00514
00515 #ifndef ASSERT_TYPE_EQUAL
00516
00517 #define ASSERT_TYPE_EQUAL(var1, var2) \
00518 do { (void)(&(var1) == &(var2)); } while(0)
00519 #endif
00520
00521 #ifndef ASSERT_TYPE_IS
00522
00523 #define ASSERT_TYPE_IS(var, type) \
00524 do { (void)(&(var) == (type *)0); } while(0)
00525 #endif
00526
00527 #endif