gcc_arm_asm_common.hpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2009 Helge Bahmann
  7. * Copyright (c) 2013 Tim Blechmann
  8. * Copyright (c) 2014, 2020 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/gcc_arm_asm_common.hpp
  12. *
  13. * This header contains basic utilities for gcc asm-based ARM backend.
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_GCC_ARM_ASM_COMMON_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_GCC_ARM_ASM_COMMON_HPP_INCLUDED_
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/capabilities.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. // A memory barrier is effected using a "co-processor 15" instruction,
  23. // though a separate assembler mnemonic is available for it in v7.
  24. //
  25. // "Thumb 1" is a subset of the ARM instruction set that uses a 16-bit encoding. It
  26. // doesn't include all instructions and in particular it doesn't include the co-processor
  27. // instruction used for the memory barrier or the load-locked/store-conditional
  28. // instructions. So, if we're compiling in "Thumb 1" mode, we need to wrap all of our
  29. // asm blocks with code to temporarily change to ARM mode.
  30. //
  31. // You can only change between ARM and Thumb modes when branching using the bx instruction.
  32. // bx takes an address specified in a register. The least significant bit of the address
  33. // indicates the mode, so 1 is added to indicate that the destination code is Thumb.
  34. // A temporary register is needed for the address and is passed as an argument to these
  35. // macros. It must be one of the "low" registers accessible to Thumb code, specified
  36. // using the "l" attribute in the asm statement.
  37. //
  38. // Architecture v7 introduces "Thumb 2", which does include (almost?) all of the ARM
  39. // instruction set. (Actually, there was an extension of v6 called v6T2 which supported
  40. // "Thumb 2" mode, but its architecture manual is no longer available, referring to v7.)
  41. // So in v7 we don't need to change to ARM mode; we can write "universal
  42. // assembler" which will assemble to Thumb 2 or ARM code as appropriate. The only thing
  43. // we need to do to make this "universal" assembler mode work is to insert "IT" instructions
  44. // to annotate the conditional instructions. These are ignored in other modes (e.g. v6),
  45. // so they can always be present.
  46. // A note about memory_order_consume. Technically, this architecture allows to avoid
  47. // unnecessary memory barrier after consume load since it supports data dependency ordering.
  48. // However, some compiler optimizations may break a seemingly valid code relying on data
  49. // dependency tracking by injecting bogus branches to aid out of order execution.
  50. // This may happen not only in Boost.Atomic code but also in user's code, which we have no
  51. // control of. See this thread: http://lists.boost.org/Archives/boost/2014/06/213890.php.
  52. // For this reason we promote memory_order_consume to memory_order_acquire.
  53. #if defined(__thumb__) && !defined(__thumb2__)
  54. #define BOOST_ATOMIC_DETAIL_ARM_ASM_START(TMPREG) "adr " #TMPREG ", 8f\n\t" "bx " #TMPREG "\n\t" ".arm\n\t" ".align 4\n\t" "8:\n\t"
  55. #define BOOST_ATOMIC_DETAIL_ARM_ASM_END(TMPREG) "adr " #TMPREG ", 9f + 1\n\t" "bx " #TMPREG "\n\t" ".thumb\n\t" ".align 2\n\t" "9:\n\t"
  56. #define BOOST_ATOMIC_DETAIL_ARM_ASM_TMPREG_CONSTRAINT(var) "=&l" (var)
  57. #else
  58. // Indicate that start/end macros are empty and the tmpreg is not needed
  59. #define BOOST_ATOMIC_DETAIL_ARM_ASM_TMPREG_UNUSED
  60. #define BOOST_ATOMIC_DETAIL_ARM_ASM_START(TMPREG)
  61. #define BOOST_ATOMIC_DETAIL_ARM_ASM_END(TMPREG)
  62. #define BOOST_ATOMIC_DETAIL_ARM_ASM_TMPREG_CONSTRAINT(var) "=&l" (var)
  63. #endif
  64. #if defined(BOOST_ATOMIC_DETAIL_ARM_LITTLE_ENDIAN)
  65. #define BOOST_ATOMIC_DETAIL_ARM_ASM_ARG_LO(arg) "%" BOOST_STRINGIZE(arg)
  66. #define BOOST_ATOMIC_DETAIL_ARM_ASM_ARG_HI(arg) "%H" BOOST_STRINGIZE(arg)
  67. #else
  68. #define BOOST_ATOMIC_DETAIL_ARM_ASM_ARG_LO(arg) "%H" BOOST_STRINGIZE(arg)
  69. #define BOOST_ATOMIC_DETAIL_ARM_ASM_ARG_HI(arg) "%" BOOST_STRINGIZE(arg)
  70. #endif
  71. #endif // BOOST_ATOMIC_DETAIL_GCC_ARM_ASM_COMMON_HPP_INCLUDED_