mbed-os
Fork of mbed-os by
platform/toolchain.h@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | |
elessair | 0:f269e3021894 | 2 | /** \addtogroup platform */ |
elessair | 0:f269e3021894 | 3 | /** @{*/ |
elessair | 0:f269e3021894 | 4 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 5 | * Copyright (c) 2006-2013 ARM Limited |
elessair | 0:f269e3021894 | 6 | * |
elessair | 0:f269e3021894 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 8 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 9 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 10 | * |
elessair | 0:f269e3021894 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 12 | * |
elessair | 0:f269e3021894 | 13 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 16 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 17 | * limitations under the License. |
elessair | 0:f269e3021894 | 18 | */ |
elessair | 0:f269e3021894 | 19 | #ifndef MBED_TOOLCHAIN_H |
elessair | 0:f269e3021894 | 20 | #define MBED_TOOLCHAIN_H |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | // Warning for unsupported compilers |
elessair | 0:f269e3021894 | 24 | #if !defined(__GNUC__) /* GCC */ \ |
elessair | 0:f269e3021894 | 25 | && !defined(__CC_ARM) /* ARMCC */ \ |
elessair | 0:f269e3021894 | 26 | && !defined(__clang__) /* LLVM/Clang */ \ |
elessair | 0:f269e3021894 | 27 | && !defined(__ICCARM__) /* IAR */ |
elessair | 0:f269e3021894 | 28 | #warning "This compiler is not yet supported." |
elessair | 0:f269e3021894 | 29 | #endif |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | // Attributes |
elessair | 0:f269e3021894 | 33 | |
elessair | 0:f269e3021894 | 34 | /** MBED_PACKED |
elessair | 0:f269e3021894 | 35 | * Pack a structure, preventing any padding from being added between fields. |
elessair | 0:f269e3021894 | 36 | * |
elessair | 0:f269e3021894 | 37 | * @code |
elessair | 0:f269e3021894 | 38 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 39 | * |
elessair | 0:f269e3021894 | 40 | * MBED_PACKED(struct) foo { |
elessair | 0:f269e3021894 | 41 | * char x; |
elessair | 0:f269e3021894 | 42 | * int y; |
elessair | 0:f269e3021894 | 43 | * }; |
elessair | 0:f269e3021894 | 44 | * @endcode |
elessair | 0:f269e3021894 | 45 | */ |
elessair | 0:f269e3021894 | 46 | #ifndef MBED_PACKED |
elessair | 0:f269e3021894 | 47 | #if defined(__ICCARM__) |
elessair | 0:f269e3021894 | 48 | #define MBED_PACKED(struct) __packed struct |
elessair | 0:f269e3021894 | 49 | #else |
elessair | 0:f269e3021894 | 50 | #define MBED_PACKED(struct) struct __attribute__((packed)) |
elessair | 0:f269e3021894 | 51 | #endif |
elessair | 0:f269e3021894 | 52 | #endif |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | /** MBED_ALIGN(N) |
elessair | 0:f269e3021894 | 55 | * Declare a variable to be aligned on an N-byte boundary. |
elessair | 0:f269e3021894 | 56 | * |
elessair | 0:f269e3021894 | 57 | * @note |
elessair | 0:f269e3021894 | 58 | * IAR does not support alignment greater than word size on the stack |
elessair | 0:f269e3021894 | 59 | * |
elessair | 0:f269e3021894 | 60 | * @code |
elessair | 0:f269e3021894 | 61 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 62 | * |
elessair | 0:f269e3021894 | 63 | * MBED_ALIGN(16) char a; |
elessair | 0:f269e3021894 | 64 | * @endcode |
elessair | 0:f269e3021894 | 65 | */ |
elessair | 0:f269e3021894 | 66 | #ifndef MBED_ALIGN |
elessair | 0:f269e3021894 | 67 | #if defined(__ICCARM__) |
elessair | 0:f269e3021894 | 68 | #define _MBED_ALIGN(N) _Pragma(#N) |
elessair | 0:f269e3021894 | 69 | #define MBED_ALIGN(N) _MBED_ALIGN(data_alignment=N) |
elessair | 0:f269e3021894 | 70 | #else |
elessair | 0:f269e3021894 | 71 | #define MBED_ALIGN(N) __attribute__((aligned(N))) |
elessair | 0:f269e3021894 | 72 | #endif |
elessair | 0:f269e3021894 | 73 | #endif |
elessair | 0:f269e3021894 | 74 | |
elessair | 0:f269e3021894 | 75 | /** MBED_UNUSED |
elessair | 0:f269e3021894 | 76 | * Declare a function argument to be unused, suppressing compiler warnings |
elessair | 0:f269e3021894 | 77 | * |
elessair | 0:f269e3021894 | 78 | * @code |
elessair | 0:f269e3021894 | 79 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 80 | * |
elessair | 0:f269e3021894 | 81 | * void foo(MBED_UNUSED int arg) { |
elessair | 0:f269e3021894 | 82 | * |
elessair | 0:f269e3021894 | 83 | * } |
elessair | 0:f269e3021894 | 84 | * @endcode |
elessair | 0:f269e3021894 | 85 | */ |
elessair | 0:f269e3021894 | 86 | #ifndef MBED_UNUSED |
elessair | 0:f269e3021894 | 87 | #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) |
elessair | 0:f269e3021894 | 88 | #define MBED_UNUSED __attribute__((__unused__)) |
elessair | 0:f269e3021894 | 89 | #else |
elessair | 0:f269e3021894 | 90 | #define MBED_UNUSED |
elessair | 0:f269e3021894 | 91 | #endif |
elessair | 0:f269e3021894 | 92 | #endif |
elessair | 0:f269e3021894 | 93 | |
elessair | 0:f269e3021894 | 94 | /** MBED_WEAK |
elessair | 0:f269e3021894 | 95 | * Mark a function as being weak. |
elessair | 0:f269e3021894 | 96 | * |
elessair | 0:f269e3021894 | 97 | * @note |
elessair | 0:f269e3021894 | 98 | * weak functions are not friendly to making code re-usable, as they can only |
elessair | 0:f269e3021894 | 99 | * be overridden once (and if they are multiply overridden the linker will emit |
elessair | 0:f269e3021894 | 100 | * no warning). You should not normally use weak symbols as part of the API to |
elessair | 0:f269e3021894 | 101 | * re-usable modules. |
elessair | 0:f269e3021894 | 102 | * |
elessair | 0:f269e3021894 | 103 | * @code |
elessair | 0:f269e3021894 | 104 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 105 | * |
elessair | 0:f269e3021894 | 106 | * MBED_WEAK void foo() { |
elessair | 0:f269e3021894 | 107 | * // a weak implementation of foo that can be overriden by a definition |
elessair | 0:f269e3021894 | 108 | * // without __weak |
elessair | 0:f269e3021894 | 109 | * } |
elessair | 0:f269e3021894 | 110 | * @endcode |
elessair | 0:f269e3021894 | 111 | */ |
elessair | 0:f269e3021894 | 112 | #ifndef MBED_WEAK |
elessair | 0:f269e3021894 | 113 | #if defined(__ICCARM__) |
elessair | 0:f269e3021894 | 114 | #define MBED_WEAK __weak |
elessair | 0:f269e3021894 | 115 | #else |
elessair | 0:f269e3021894 | 116 | #define MBED_WEAK __attribute__((weak)) |
elessair | 0:f269e3021894 | 117 | #endif |
elessair | 0:f269e3021894 | 118 | #endif |
elessair | 0:f269e3021894 | 119 | |
elessair | 0:f269e3021894 | 120 | /** MBED_PURE |
elessair | 0:f269e3021894 | 121 | * Hint to the compiler that a function depends only on parameters |
elessair | 0:f269e3021894 | 122 | * |
elessair | 0:f269e3021894 | 123 | * @code |
elessair | 0:f269e3021894 | 124 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 125 | * |
elessair | 0:f269e3021894 | 126 | * MBED_PURE int foo(int arg){ |
elessair | 0:f269e3021894 | 127 | * // no access to global variables |
elessair | 0:f269e3021894 | 128 | * } |
elessair | 0:f269e3021894 | 129 | * @endcode |
elessair | 0:f269e3021894 | 130 | */ |
elessair | 0:f269e3021894 | 131 | #ifndef MBED_PURE |
elessair | 0:f269e3021894 | 132 | #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) |
elessair | 0:f269e3021894 | 133 | #define MBED_PURE __attribute__((const)) |
elessair | 0:f269e3021894 | 134 | #else |
elessair | 0:f269e3021894 | 135 | #define MBED_PURE |
elessair | 0:f269e3021894 | 136 | #endif |
elessair | 0:f269e3021894 | 137 | #endif |
elessair | 0:f269e3021894 | 138 | |
elessair | 0:f269e3021894 | 139 | /** MBED_FORCEINLINE |
elessair | 0:f269e3021894 | 140 | * Declare a function that must always be inlined. Failure to inline |
elessair | 0:f269e3021894 | 141 | * such a function will result in an error. |
elessair | 0:f269e3021894 | 142 | * |
elessair | 0:f269e3021894 | 143 | * @code |
elessair | 0:f269e3021894 | 144 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 145 | * |
elessair | 0:f269e3021894 | 146 | * MBED_FORCEINLINE void foo() { |
elessair | 0:f269e3021894 | 147 | * |
elessair | 0:f269e3021894 | 148 | * } |
elessair | 0:f269e3021894 | 149 | * @endcode |
elessair | 0:f269e3021894 | 150 | */ |
elessair | 0:f269e3021894 | 151 | #ifndef MBED_FORCEINLINE |
elessair | 0:f269e3021894 | 152 | #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) |
elessair | 0:f269e3021894 | 153 | #define MBED_FORCEINLINE static inline __attribute__((always_inline)) |
elessair | 0:f269e3021894 | 154 | #elif defined(__ICCARM__) |
elessair | 0:f269e3021894 | 155 | #define MBED_FORCEINLINE _Pragma("inline=forced") static |
elessair | 0:f269e3021894 | 156 | #else |
elessair | 0:f269e3021894 | 157 | #define MBED_FORCEINLINE static inline |
elessair | 0:f269e3021894 | 158 | #endif |
elessair | 0:f269e3021894 | 159 | #endif |
elessair | 0:f269e3021894 | 160 | |
elessair | 0:f269e3021894 | 161 | /** MBED_NORETURN |
elessair | 0:f269e3021894 | 162 | * Declare a function that will never return. |
elessair | 0:f269e3021894 | 163 | * |
elessair | 0:f269e3021894 | 164 | * @code |
elessair | 0:f269e3021894 | 165 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 166 | * |
elessair | 0:f269e3021894 | 167 | * MBED_NORETURN void foo() { |
elessair | 0:f269e3021894 | 168 | * // must never return |
elessair | 0:f269e3021894 | 169 | * while (1) {} |
elessair | 0:f269e3021894 | 170 | * } |
elessair | 0:f269e3021894 | 171 | * @endcode |
elessair | 0:f269e3021894 | 172 | */ |
elessair | 0:f269e3021894 | 173 | #ifndef MBED_NORETURN |
elessair | 0:f269e3021894 | 174 | #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) |
elessair | 0:f269e3021894 | 175 | #define MBED_NORETURN __attribute__((noreturn)) |
elessair | 0:f269e3021894 | 176 | #elif defined(__ICCARM__) |
elessair | 0:f269e3021894 | 177 | #define MBED_NORETURN __noreturn |
elessair | 0:f269e3021894 | 178 | #else |
elessair | 0:f269e3021894 | 179 | #define MBED_NORETURN |
elessair | 0:f269e3021894 | 180 | #endif |
elessair | 0:f269e3021894 | 181 | #endif |
elessair | 0:f269e3021894 | 182 | |
elessair | 0:f269e3021894 | 183 | /** MBED_UNREACHABLE |
elessair | 0:f269e3021894 | 184 | * An unreachable statement. If the statement is reached, |
elessair | 0:f269e3021894 | 185 | * behaviour is undefined. Useful in situations where the compiler |
elessair | 0:f269e3021894 | 186 | * cannot deduce the unreachability of code. |
elessair | 0:f269e3021894 | 187 | * |
elessair | 0:f269e3021894 | 188 | * @code |
elessair | 0:f269e3021894 | 189 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 190 | * |
elessair | 0:f269e3021894 | 191 | * void foo(int arg) { |
elessair | 0:f269e3021894 | 192 | * switch (arg) { |
elessair | 0:f269e3021894 | 193 | * case 1: return 1; |
elessair | 0:f269e3021894 | 194 | * case 2: return 2; |
elessair | 0:f269e3021894 | 195 | * ... |
elessair | 0:f269e3021894 | 196 | * } |
elessair | 0:f269e3021894 | 197 | * MBED_UNREACHABLE; |
elessair | 0:f269e3021894 | 198 | * } |
elessair | 0:f269e3021894 | 199 | * @endcode |
elessair | 0:f269e3021894 | 200 | */ |
elessair | 0:f269e3021894 | 201 | #ifndef MBED_UNREACHABLE |
elessair | 0:f269e3021894 | 202 | #if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM) |
elessair | 0:f269e3021894 | 203 | #define MBED_UNREACHABLE __builtin_unreachable() |
elessair | 0:f269e3021894 | 204 | #else |
elessair | 0:f269e3021894 | 205 | #define MBED_UNREACHABLE while (1) |
elessair | 0:f269e3021894 | 206 | #endif |
elessair | 0:f269e3021894 | 207 | #endif |
elessair | 0:f269e3021894 | 208 | |
elessair | 0:f269e3021894 | 209 | /** MBED_DEPRECATED("message string") |
elessair | 0:f269e3021894 | 210 | * Mark a function declaration as deprecated, if it used then a warning will be |
elessair | 0:f269e3021894 | 211 | * issued by the compiler possibly including the provided message. Note that not |
elessair | 0:f269e3021894 | 212 | * all compilers are able to display the message. |
elessair | 0:f269e3021894 | 213 | * |
elessair | 0:f269e3021894 | 214 | * @code |
elessair | 0:f269e3021894 | 215 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 216 | * |
elessair | 0:f269e3021894 | 217 | * MBED_DEPRECATED("don't foo any more, bar instead") |
elessair | 0:f269e3021894 | 218 | * void foo(int arg); |
elessair | 0:f269e3021894 | 219 | * @endcode |
elessair | 0:f269e3021894 | 220 | */ |
elessair | 0:f269e3021894 | 221 | #ifndef MBED_DEPRECATED |
elessair | 0:f269e3021894 | 222 | #if defined(__GNUC__) || defined(__clang__) |
elessair | 0:f269e3021894 | 223 | #define MBED_DEPRECATED(M) __attribute__((deprecated(M))) |
elessair | 0:f269e3021894 | 224 | #elif defined(__CC_ARM) |
elessair | 0:f269e3021894 | 225 | #define MBED_DEPRECATED(M) __attribute__((deprecated)) |
elessair | 0:f269e3021894 | 226 | #else |
elessair | 0:f269e3021894 | 227 | #define MBED_DEPRECATED(M) |
elessair | 0:f269e3021894 | 228 | #endif |
elessair | 0:f269e3021894 | 229 | #endif |
elessair | 0:f269e3021894 | 230 | |
elessair | 0:f269e3021894 | 231 | /** MBED_DEPRECATED_SINCE("version", "message string") |
elessair | 0:f269e3021894 | 232 | * Mark a function declaration as deprecated, noting that the declaration was |
elessair | 0:f269e3021894 | 233 | * deprecated on the specified version. If the function is used then a warning |
elessair | 0:f269e3021894 | 234 | * will be issued by the compiler possibly including the provided message. |
elessair | 0:f269e3021894 | 235 | * Note that not all compilers are able to display this message. |
elessair | 0:f269e3021894 | 236 | * |
elessair | 0:f269e3021894 | 237 | * @code |
elessair | 0:f269e3021894 | 238 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 239 | * |
elessair | 0:f269e3021894 | 240 | * MBED_DEPRECATED_SINCE("mbed-os-5.1", "don't foo any more, bar instead") |
elessair | 0:f269e3021894 | 241 | * void foo(int arg); |
elessair | 0:f269e3021894 | 242 | * @endcode |
elessair | 0:f269e3021894 | 243 | */ |
elessair | 0:f269e3021894 | 244 | #define MBED_DEPRECATED_SINCE(D, M) MBED_DEPRECATED(M " [since " D "]") |
elessair | 0:f269e3021894 | 245 | |
elessair | 0:f269e3021894 | 246 | /** MBED_CALLER_ADDR() |
elessair | 0:f269e3021894 | 247 | * Returns the caller of the current function. |
elessair | 0:f269e3021894 | 248 | * |
elessair | 0:f269e3021894 | 249 | * @note |
elessair | 0:f269e3021894 | 250 | * This macro is only implemented for GCC and ARMCC. |
elessair | 0:f269e3021894 | 251 | * |
elessair | 0:f269e3021894 | 252 | * @code |
elessair | 0:f269e3021894 | 253 | * #include "toolchain.h" |
elessair | 0:f269e3021894 | 254 | * |
elessair | 0:f269e3021894 | 255 | * printf("This function was called from %p", MBED_CALLER_ADDR()); |
elessair | 0:f269e3021894 | 256 | * @endcode |
elessair | 0:f269e3021894 | 257 | * |
elessair | 0:f269e3021894 | 258 | * @return Address of the calling function |
elessair | 0:f269e3021894 | 259 | */ |
elessair | 0:f269e3021894 | 260 | #ifndef MBED_CALLER_ADDR |
elessair | 0:f269e3021894 | 261 | #if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM) |
elessair | 0:f269e3021894 | 262 | #define MBED_CALLER_ADDR() __builtin_extract_return_addr(__builtin_return_address(0)) |
elessair | 0:f269e3021894 | 263 | #elif defined(__CC_ARM) |
elessair | 0:f269e3021894 | 264 | #define MBED_CALLER_ADDR() __builtin_return_address(0) |
elessair | 0:f269e3021894 | 265 | #else |
elessair | 0:f269e3021894 | 266 | #define MBED_CALLER_ADDR() (NULL) |
elessair | 0:f269e3021894 | 267 | #endif |
elessair | 0:f269e3021894 | 268 | #endif |
elessair | 0:f269e3021894 | 269 | |
elessair | 0:f269e3021894 | 270 | // FILEHANDLE declaration |
elessair | 0:f269e3021894 | 271 | #if defined(TOOLCHAIN_ARM) |
elessair | 0:f269e3021894 | 272 | #include <rt_sys.h> |
elessair | 0:f269e3021894 | 273 | #endif |
elessair | 0:f269e3021894 | 274 | |
elessair | 0:f269e3021894 | 275 | #ifndef FILEHANDLE |
elessair | 0:f269e3021894 | 276 | typedef int FILEHANDLE; |
elessair | 0:f269e3021894 | 277 | #endif |
elessair | 0:f269e3021894 | 278 | |
elessair | 0:f269e3021894 | 279 | // Backwards compatibility |
elessair | 0:f269e3021894 | 280 | #ifndef WEAK |
elessair | 0:f269e3021894 | 281 | #define WEAK MBED_WEAK |
elessair | 0:f269e3021894 | 282 | #endif |
elessair | 0:f269e3021894 | 283 | |
elessair | 0:f269e3021894 | 284 | #ifndef PACKED |
elessair | 0:f269e3021894 | 285 | #define PACKED MBED_PACKED() |
elessair | 0:f269e3021894 | 286 | #endif |
elessair | 0:f269e3021894 | 287 | |
elessair | 0:f269e3021894 | 288 | #ifndef EXTERN |
elessair | 0:f269e3021894 | 289 | #define EXTERN extern |
elessair | 0:f269e3021894 | 290 | #endif |
elessair | 0:f269e3021894 | 291 | |
elessair | 0:f269e3021894 | 292 | #endif |
elessair | 0:f269e3021894 | 293 | |
elessair | 0:f269e3021894 | 294 | /** @}*/ |