forked

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_toolchain.h Source File

mbed_toolchain.h

00001 
00002 /** \addtogroup platform */
00003 /** @{*/
00004 /* mbed Microcontroller Library
00005  * Copyright (c) 2006-2013 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 #ifndef MBED_TOOLCHAIN_H
00020 #define MBED_TOOLCHAIN_H
00021 
00022 #include "mbed_preprocessor.h"
00023 
00024 
00025 // Warning for unsupported compilers
00026 #if !defined(__GNUC__)   /* GCC        */ \
00027  && !defined(__CC_ARM)   /* ARMCC      */ \
00028  && !defined(__clang__)  /* LLVM/Clang */ \
00029  && !defined(__ICCARM__) /* IAR        */
00030 #warning "This compiler is not yet supported."
00031 #endif
00032 
00033 
00034 // Attributes
00035 
00036 /** MBED_PACKED
00037  *  Pack a structure, preventing any padding from being added between fields.
00038  *
00039  *  @code
00040  *  #include "mbed_toolchain.h"
00041  *
00042  *  MBED_PACKED(struct) foo {
00043  *      char x;
00044  *      int y;
00045  *  };
00046  *  @endcode
00047  */
00048 #ifndef MBED_PACKED
00049 #if defined(__ICCARM__)
00050 #define MBED_PACKED(struct) __packed struct
00051 #else
00052 #define MBED_PACKED(struct) struct __attribute__((packed))
00053 #endif
00054 #endif
00055 
00056 /** MBED_ALIGN(N)
00057  *  Declare a variable to be aligned on an N-byte boundary.
00058  *
00059  *  @note
00060  *  IAR does not support alignment greater than word size on the stack
00061  *  
00062  *  @code
00063  *  #include "mbed_toolchain.h"
00064  *
00065  *  MBED_ALIGN(16) char a;
00066  *  @endcode
00067  */
00068 #ifndef MBED_ALIGN
00069 #if defined(__ICCARM__)
00070 #define MBED_ALIGN(N) _Pragma(MBED_STRINGIFY(data_alignment=N))
00071 #else
00072 #define MBED_ALIGN(N) __attribute__((aligned(N)))
00073 #endif
00074 #endif
00075 
00076 /** MBED_UNUSED
00077  *  Declare a function argument to be unused, suppressing compiler warnings
00078  *
00079  *  @code
00080  *  #include "mbed_toolchain.h"
00081  *
00082  *  void foo(MBED_UNUSED int arg) {
00083  *
00084  *  }
00085  *  @endcode
00086  */
00087 #ifndef MBED_UNUSED
00088 #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
00089 #define MBED_UNUSED __attribute__((__unused__))
00090 #else
00091 #define MBED_UNUSED
00092 #endif
00093 #endif
00094 
00095 /** MBED_WEAK
00096  *  Mark a function as being weak.
00097  *
00098  *  @note
00099  *  Functions should only be marked as weak in the source file. The header file
00100  *  should contain a regular function declaration to insure the function is emitted.
00101  *  A function marked weak will not be emitted if an alternative non-weak
00102  *  implementation is defined.
00103  *  
00104  *  @note
00105  *  Weak functions are not friendly to making code re-usable, as they can only
00106  *  be overridden once (and if they are multiply overridden the linker will emit
00107  *  no warning). You should not normally use weak symbols as part of the API to
00108  *  re-usable modules.
00109  *  
00110  *  @code
00111  *  #include "mbed_toolchain.h"
00112  *  
00113  *  MBED_WEAK void foo() {
00114  *      // a weak implementation of foo that can be overriden by a definition
00115  *      // without  __weak
00116  *  }
00117  *  @endcode
00118  */
00119 #ifndef MBED_WEAK
00120 #if defined(__ICCARM__)
00121 #define MBED_WEAK __weak
00122 #else
00123 #define MBED_WEAK __attribute__((weak))
00124 #endif
00125 #endif
00126 
00127 /** MBED_PURE
00128  *  Hint to the compiler that a function depends only on parameters
00129  *
00130  *  @code
00131  *  #include "mbed_toolchain.h"
00132  *
00133  *  MBED_PURE int foo(int arg){
00134  *      // no access to global variables
00135  *  }
00136  *  @endcode
00137  */
00138 #ifndef MBED_PURE
00139 #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
00140 #define MBED_PURE __attribute__((const))
00141 #else
00142 #define MBED_PURE
00143 #endif
00144 #endif
00145 
00146 /** MBED_NOINLINE
00147  *  Declare a function that must not be inlined.
00148  *
00149  *  @code
00150  *  #include "mbed_toolchain.h"
00151  *  
00152  *  MBED_NOINLINE void foo() {
00153  *  
00154  *  }
00155  *  @endcode
00156  */
00157 #ifndef MBED_NOINLINE
00158 #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
00159 #define MBED_NOINLINE __attribute__((noinline))
00160 #elif defined(__ICCARM__)
00161 #define MBED_NOINLINE _Pragma("inline=never")
00162 #else
00163 #define MBED_NOINLINE
00164 #endif
00165 #endif
00166 
00167 /** MBED_FORCEINLINE
00168  *  Declare a function that must always be inlined. Failure to inline
00169  *  such a function will result in an error.
00170  *
00171  *  @code
00172  *  #include "mbed_toolchain.h"
00173  *  
00174  *  MBED_FORCEINLINE void foo() {
00175  *  
00176  *  }
00177  *  @endcode
00178  */
00179 #ifndef MBED_FORCEINLINE
00180 #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
00181 #define MBED_FORCEINLINE static inline __attribute__((always_inline))
00182 #elif defined(__ICCARM__)
00183 #define MBED_FORCEINLINE _Pragma("inline=forced") static
00184 #else
00185 #define MBED_FORCEINLINE static inline
00186 #endif
00187 #endif
00188 
00189 /** MBED_NORETURN
00190  *  Declare a function that will never return.
00191  *
00192  *  @code
00193  *  #include "mbed_toolchain.h"
00194  *  
00195  *  MBED_NORETURN void foo() {
00196  *      // must never return
00197  *      while (1) {}
00198  *  }
00199  *  @endcode
00200  */
00201 #ifndef MBED_NORETURN
00202 #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
00203 #define MBED_NORETURN __attribute__((noreturn))
00204 #elif defined(__ICCARM__)
00205 #define MBED_NORETURN __noreturn
00206 #else
00207 #define MBED_NORETURN
00208 #endif
00209 #endif
00210 
00211 /** MBED_UNREACHABLE
00212  *  An unreachable statement. If the statement is reached,
00213  *  behaviour is undefined. Useful in situations where the compiler
00214  *  cannot deduce the unreachability of code.
00215  *
00216  *  @code
00217  *  #include "mbed_toolchain.h"
00218  *
00219  *  void foo(int arg) {
00220  *      switch (arg) {
00221  *          case 1: return 1;
00222  *          case 2: return 2;
00223  *          ...
00224  *      }
00225  *      MBED_UNREACHABLE;
00226  *  }
00227  *  @endcode
00228  */
00229 #ifndef MBED_UNREACHABLE
00230 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
00231 #define MBED_UNREACHABLE __builtin_unreachable()
00232 #else
00233 #define MBED_UNREACHABLE while (1)
00234 #endif
00235 #endif
00236 
00237 /** MBED_DEPRECATED("message string")
00238  *  Mark a function declaration as deprecated, if it used then a warning will be
00239  *  issued by the compiler possibly including the provided message. Note that not
00240  *  all compilers are able to display the message.
00241  *
00242  *  @code
00243  *  #include "mbed_toolchain.h"
00244  *  
00245  *  MBED_DEPRECATED("don't foo any more, bar instead")
00246  *  void foo(int arg);
00247  *  @endcode
00248  */
00249 #ifndef MBED_DEPRECATED
00250 #if defined(__CC_ARM)
00251 #define MBED_DEPRECATED(M) __attribute__((deprecated))
00252 #elif defined(__GNUC__) || defined(__clang__)
00253 #define MBED_DEPRECATED(M) __attribute__((deprecated(M)))
00254 #else
00255 #define MBED_DEPRECATED(M)
00256 #endif
00257 #endif
00258 
00259 /** MBED_DEPRECATED_SINCE("version", "message string")
00260  *  Mark a function declaration as deprecated, noting that the declaration was
00261  *  deprecated on the specified version. If the function is used then a warning
00262  *  will be issued by the compiler possibly including the provided message.
00263  *  Note that not all compilers are able to display this message.
00264  *
00265  *  @code
00266  *  #include "mbed_toolchain.h"
00267  *
00268  *  MBED_DEPRECATED_SINCE("mbed-os-5.1", "don't foo any more, bar instead")
00269  *  void foo(int arg);
00270  *  @endcode
00271  */
00272 #define MBED_DEPRECATED_SINCE(D, M) MBED_DEPRECATED(M " [since " D "]")
00273 
00274 /** MBED_CALLER_ADDR()
00275  * Returns the caller of the current function.
00276  *
00277  * @note
00278  * This macro is only implemented for GCC and ARMCC.
00279  *
00280  * @code
00281  * #include "mbed_toolchain.h"
00282  *
00283  * printf("This function was called from %p", MBED_CALLER_ADDR());
00284  * @endcode
00285  *
00286  * @return Address of the calling function
00287  */
00288 #ifndef MBED_CALLER_ADDR
00289 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
00290 #define MBED_CALLER_ADDR() __builtin_extract_return_addr(__builtin_return_address(0))
00291 #elif defined(__CC_ARM)
00292 #define MBED_CALLER_ADDR() __builtin_return_address(0)
00293 #else
00294 #define MBED_CALLER_ADDR() (NULL)
00295 #endif
00296 #endif
00297 
00298 #ifndef MBED_SECTION
00299 #if (defined(__GNUC__) || defined(__clang__)) || defined(__CC_ARM)
00300 #define MBED_SECTION(name) __attribute__ ((section (name)))
00301 #elif defined(__ICCARM__)
00302 #define MBED_SECTION(name) _Pragma(MBED_STRINGIFY(location=name))
00303 #else
00304 #error "Missing MBED_SECTION directive"
00305 #endif
00306 #endif
00307 
00308 #ifndef MBED_PRINTF
00309 #if defined(__GNUC__) || defined(__CC_ARM)
00310 #define MBED_PRINTF(format_idx, first_param_idx) __attribute__ ((__format__(__printf__, format_idx, first_param_idx)))
00311 #else
00312 #define MBED_PRINTF(format_idx, first_param_idx)
00313 #endif
00314 #endif
00315 
00316 #ifndef MBED_PRINTF_METHOD
00317 #if defined(__GNUC__) || defined(__CC_ARM)
00318 #define MBED_PRINTF_METHOD(format_idx, first_param_idx) __attribute__ ((__format__(__printf__, format_idx+1, first_param_idx+1)))
00319 #else
00320 #define MBED_PRINTF_METHOD(format_idx, first_param_idx)
00321 #endif
00322 #endif
00323 
00324 #ifndef MBED_SCANF
00325 #if defined(__GNUC__) || defined(__CC_ARM)
00326 #define MBED_SCANF(format_idx, first_param_idx) __attribute__ ((__format__(__scanf__, format_idx, first_param_idx)))
00327 #else
00328 #define MBED_SCANF(format_idx, first_param_idx)
00329 #endif
00330 #endif
00331 
00332 #ifndef MBED_SCANF_METHOD
00333 #if defined(__GNUC__) || defined(__CC_ARM)
00334 #define MBED_SCANF_METHOD(format_idx, first_param_idx) __attribute__ ((__format__(__scanf__, format_idx+1, first_param_idx+1)))
00335 #else
00336 #define MBED_SCANF_METHOD(format_idx, first_param_idx)
00337 #endif
00338 #endif
00339 
00340 // FILEHANDLE declaration
00341 #if defined(TOOLCHAIN_ARM)
00342 #include <rt_sys.h>
00343 #endif
00344 
00345 #ifndef FILEHANDLE
00346 typedef int FILEHANDLE;
00347 #endif
00348 
00349 // Backwards compatibility
00350 #ifndef WEAK
00351 #define WEAK MBED_WEAK
00352 #endif
00353 
00354 #ifndef PACKED
00355 #define PACKED MBED_PACKED()
00356 #endif
00357 
00358 #ifndef EXTERN
00359 #define EXTERN extern
00360 #endif
00361 
00362 #endif
00363 
00364 /** @}*/