ARMUnwindOpAsm.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //===-- ARMUnwindOpAsm.cpp - ARM Unwind Opcodes Assembler -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the unwind opcode assembler for ARM exception handling
  10. // table.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ARMUnwindOpAsm.h"
  14. #include "llvm/Support/ARMEHABI.h"
  15. #include "llvm/Support/LEB128.h"
  16. #include "llvm/Support/MathExtras.h"
  17. #include <cassert>
  18. using namespace llvm;
  19. namespace {
  20. /// UnwindOpcodeStreamer - The simple wrapper over SmallVector to emit bytes
  21. /// with MSB to LSB per uint32_t ordering. For example, the first byte will
  22. /// be placed in Vec[3], and the following bytes will be placed in 2, 1, 0,
  23. /// 7, 6, 5, 4, 11, 10, 9, 8, and so on.
  24. class UnwindOpcodeStreamer {
  25. private:
  26. SmallVectorImpl<uint8_t> &Vec;
  27. size_t Pos = 3;
  28. public:
  29. UnwindOpcodeStreamer(SmallVectorImpl<uint8_t> &V) : Vec(V) {}
  30. /// Emit the byte in MSB to LSB per uint32_t order.
  31. void EmitByte(uint8_t elem) {
  32. Vec[Pos] = elem;
  33. Pos = (((Pos ^ 0x3u) + 1) ^ 0x3u);
  34. }
  35. /// Emit the size prefix.
  36. void EmitSize(size_t Size) {
  37. size_t SizeInWords = (Size + 3) / 4;
  38. assert(SizeInWords <= 0x100u &&
  39. "Only 256 additional words are allowed for unwind opcodes");
  40. EmitByte(static_cast<uint8_t>(SizeInWords - 1));
  41. }
  42. /// Emit the personality index prefix.
  43. void EmitPersonalityIndex(unsigned PI) {
  44. assert(PI < ARM::EHABI::NUM_PERSONALITY_INDEX &&
  45. "Invalid personality prefix");
  46. EmitByte(ARM::EHABI::EHT_COMPACT | PI);
  47. }
  48. /// Fill the rest of bytes with FINISH opcode.
  49. void FillFinishOpcode() {
  50. while (Pos < Vec.size())
  51. EmitByte(ARM::EHABI::UNWIND_OPCODE_FINISH);
  52. }
  53. };
  54. } // end anonymous namespace
  55. void UnwindOpcodeAssembler::EmitRegSave(uint32_t RegSave) {
  56. if (RegSave == 0u)
  57. return;
  58. // One byte opcode to save register r14 and r11-r4
  59. if (RegSave & (1u << 4)) {
  60. // The one byte opcode will always save r4, thus we can't use the one byte
  61. // opcode when r4 is not in .save directive.
  62. // Compute the consecutive registers from r4 to r11.
  63. uint32_t Mask = RegSave & 0xff0u;
  64. uint32_t Range = countTrailingOnes(Mask >> 5); // Exclude r4.
  65. // Mask off non-consecutive registers. Keep r4.
  66. Mask &= ~(0xffffffe0u << Range);
  67. // Emit this opcode when the mask covers every registers.
  68. uint32_t UnmaskedReg = RegSave & 0xfff0u & (~Mask);
  69. if (UnmaskedReg == 0u) {
  70. // Pop r[4 : (4 + n)]
  71. EmitInt8(ARM::EHABI::UNWIND_OPCODE_POP_REG_RANGE_R4 | Range);
  72. RegSave &= 0x000fu;
  73. } else if (UnmaskedReg == (1u << 14)) {
  74. // Pop r[14] + r[4 : (4 + n)]
  75. EmitInt8(ARM::EHABI::UNWIND_OPCODE_POP_REG_RANGE_R4_R14 | Range);
  76. RegSave &= 0x000fu;
  77. }
  78. }
  79. // Two bytes opcode to save register r15-r4
  80. if ((RegSave & 0xfff0u) != 0)
  81. EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_REG_MASK_R4 | (RegSave >> 4));
  82. // Opcode to save register r3-r0
  83. if ((RegSave & 0x000fu) != 0)
  84. EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_REG_MASK | (RegSave & 0x000fu));
  85. }
  86. /// Emit unwind opcodes for .vsave directives
  87. void UnwindOpcodeAssembler::EmitVFPRegSave(uint32_t VFPRegSave) {
  88. // We only have 4 bits to save the offset in the opcode so look at the lower
  89. // and upper 16 bits separately.
  90. for (uint32_t Regs : {VFPRegSave & 0xffff0000u, VFPRegSave & 0x0000ffffu}) {
  91. while (Regs) {
  92. // Now look for a run of set bits. Remember the MSB and LSB of the run.
  93. auto RangeMSB = 32 - countLeadingZeros(Regs);
  94. auto RangeLen = countLeadingOnes(Regs << (32 - RangeMSB));
  95. auto RangeLSB = RangeMSB - RangeLen;
  96. int Opcode = RangeLSB >= 16
  97. ? ARM::EHABI::UNWIND_OPCODE_POP_VFP_REG_RANGE_FSTMFDD_D16
  98. : ARM::EHABI::UNWIND_OPCODE_POP_VFP_REG_RANGE_FSTMFDD;
  99. EmitInt16(Opcode | ((RangeLSB % 16) << 4) | (RangeLen - 1));
  100. // Zero out bits we're done with.
  101. Regs &= ~(-1u << RangeLSB);
  102. }
  103. }
  104. }
  105. /// Emit unwind opcodes to copy address from source register to $sp.
  106. void UnwindOpcodeAssembler::EmitSetSP(uint16_t Reg) {
  107. EmitInt8(ARM::EHABI::UNWIND_OPCODE_SET_VSP | Reg);
  108. }
  109. /// Emit unwind opcodes to add $sp with an offset.
  110. void UnwindOpcodeAssembler::EmitSPOffset(int64_t Offset) {
  111. if (Offset > 0x200) {
  112. uint8_t Buff[16];
  113. Buff[0] = ARM::EHABI::UNWIND_OPCODE_INC_VSP_ULEB128;
  114. size_t ULEBSize = encodeULEB128((Offset - 0x204) >> 2, Buff + 1);
  115. emitBytes(Buff, ULEBSize + 1);
  116. } else if (Offset > 0) {
  117. if (Offset > 0x100) {
  118. EmitInt8(ARM::EHABI::UNWIND_OPCODE_INC_VSP | 0x3fu);
  119. Offset -= 0x100;
  120. }
  121. EmitInt8(ARM::EHABI::UNWIND_OPCODE_INC_VSP |
  122. static_cast<uint8_t>((Offset - 4) >> 2));
  123. } else if (Offset < 0) {
  124. while (Offset < -0x100) {
  125. EmitInt8(ARM::EHABI::UNWIND_OPCODE_DEC_VSP | 0x3fu);
  126. Offset += 0x100;
  127. }
  128. EmitInt8(ARM::EHABI::UNWIND_OPCODE_DEC_VSP |
  129. static_cast<uint8_t>(((-Offset) - 4) >> 2));
  130. }
  131. }
  132. void UnwindOpcodeAssembler::Finalize(unsigned &PersonalityIndex,
  133. SmallVectorImpl<uint8_t> &Result) {
  134. UnwindOpcodeStreamer OpStreamer(Result);
  135. if (HasPersonality) {
  136. // User-specifed personality routine: [ SIZE , OP1 , OP2 , ... ]
  137. PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
  138. size_t TotalSize = Ops.size() + 1;
  139. size_t RoundUpSize = (TotalSize + 3) / 4 * 4;
  140. Result.resize(RoundUpSize);
  141. OpStreamer.EmitSize(RoundUpSize);
  142. } else {
  143. // If no personalityindex is specified, select ane
  144. if (PersonalityIndex == ARM::EHABI::NUM_PERSONALITY_INDEX)
  145. PersonalityIndex = (Ops.size() <= 3) ? ARM::EHABI::AEABI_UNWIND_CPP_PR0
  146. : ARM::EHABI::AEABI_UNWIND_CPP_PR1;
  147. if (PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0) {
  148. // __aeabi_unwind_cpp_pr0: [ 0x80 , OP1 , OP2 , OP3 ]
  149. assert(Ops.size() <= 3 && "too many opcodes for __aeabi_unwind_cpp_pr0");
  150. Result.resize(4);
  151. OpStreamer.EmitPersonalityIndex(PersonalityIndex);
  152. } else {
  153. // __aeabi_unwind_cpp_pr{1,2}: [ {0x81,0x82} , SIZE , OP1 , OP2 , ... ]
  154. size_t TotalSize = Ops.size() + 2;
  155. size_t RoundUpSize = (TotalSize + 3) / 4 * 4;
  156. Result.resize(RoundUpSize);
  157. OpStreamer.EmitPersonalityIndex(PersonalityIndex);
  158. OpStreamer.EmitSize(RoundUpSize);
  159. }
  160. }
  161. // Copy the unwind opcodes
  162. for (size_t i = OpBegins.size() - 1; i > 0; --i)
  163. for (size_t j = OpBegins[i - 1], end = OpBegins[i]; j < end; ++j)
  164. OpStreamer.EmitByte(Ops[j]);
  165. // Emit the padding finish opcodes if the size is not multiple of 4.
  166. OpStreamer.FillFinishOpcode();
  167. // Reset the assembler state
  168. Reset();
  169. }