LLVM API Documentation
00001 //===-- ARMUnwindOpAsm.cpp - ARM Unwind Opcodes Assembler -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the unwind opcode assmebler for ARM exception handling 00011 // table. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "ARMUnwindOpAsm.h" 00016 #include "llvm/Support/ARMEHABI.h" 00017 #include "llvm/Support/ErrorHandling.h" 00018 #include "llvm/Support/LEB128.h" 00019 00020 using namespace llvm; 00021 00022 namespace { 00023 /// UnwindOpcodeStreamer - The simple wrapper over SmallVector to emit bytes 00024 /// with MSB to LSB per uint32_t ordering. For example, the first byte will 00025 /// be placed in Vec[3], and the following bytes will be placed in 2, 1, 0, 00026 /// 7, 6, 5, 4, 11, 10, 9, 8, and so on. 00027 class UnwindOpcodeStreamer { 00028 private: 00029 SmallVectorImpl<uint8_t> &Vec; 00030 size_t Pos; 00031 00032 public: 00033 UnwindOpcodeStreamer(SmallVectorImpl<uint8_t> &V) : Vec(V), Pos(3) { 00034 } 00035 00036 /// Emit the byte in MSB to LSB per uint32_t order. 00037 inline void EmitByte(uint8_t elem) { 00038 Vec[Pos] = elem; 00039 Pos = (((Pos ^ 0x3u) + 1) ^ 0x3u); 00040 } 00041 00042 /// Emit the size prefix. 00043 inline void EmitSize(size_t Size) { 00044 size_t SizeInWords = (Size + 3) / 4; 00045 assert(SizeInWords <= 0x100u && 00046 "Only 256 additional words are allowed for unwind opcodes"); 00047 EmitByte(static_cast<uint8_t>(SizeInWords - 1)); 00048 } 00049 00050 /// Emit the personality index prefix. 00051 inline void EmitPersonalityIndex(unsigned PI) { 00052 assert(PI < ARM::EHABI::NUM_PERSONALITY_INDEX && 00053 "Invalid personality prefix"); 00054 EmitByte(ARM::EHABI::EHT_COMPACT | PI); 00055 } 00056 00057 /// Fill the rest of bytes with FINISH opcode. 00058 inline void FillFinishOpcode() { 00059 while (Pos < Vec.size()) 00060 EmitByte(ARM::EHABI::UNWIND_OPCODE_FINISH); 00061 } 00062 }; 00063 } 00064 00065 void UnwindOpcodeAssembler::EmitRegSave(uint32_t RegSave) { 00066 if (RegSave == 0u) 00067 return; 00068 00069 // One byte opcode to save register r14 and r11-r4 00070 if (RegSave & (1u << 4)) { 00071 // The one byte opcode will always save r4, thus we can't use the one byte 00072 // opcode when r4 is not in .save directive. 00073 00074 // Compute the consecutive registers from r4 to r11. 00075 uint32_t Range = 0; 00076 uint32_t Mask = (1u << 4); 00077 for (uint32_t Bit = (1u << 5); Bit < (1u << 12); Bit <<= 1) { 00078 if ((RegSave & Bit) == 0u) 00079 break; 00080 ++Range; 00081 Mask |= Bit; 00082 } 00083 00084 // Emit this opcode when the mask covers every registers. 00085 uint32_t UnmaskedReg = RegSave & 0xfff0u & (~Mask); 00086 if (UnmaskedReg == 0u) { 00087 // Pop r[4 : (4 + n)] 00088 EmitInt8(ARM::EHABI::UNWIND_OPCODE_POP_REG_RANGE_R4 | Range); 00089 RegSave &= 0x000fu; 00090 } else if (UnmaskedReg == (1u << 14)) { 00091 // Pop r[14] + r[4 : (4 + n)] 00092 EmitInt8(ARM::EHABI::UNWIND_OPCODE_POP_REG_RANGE_R4_R14 | Range); 00093 RegSave &= 0x000fu; 00094 } 00095 } 00096 00097 // Two bytes opcode to save register r15-r4 00098 if ((RegSave & 0xfff0u) != 0) 00099 EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_REG_MASK_R4 | (RegSave >> 4)); 00100 00101 // Opcode to save register r3-r0 00102 if ((RegSave & 0x000fu) != 0) 00103 EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_REG_MASK | (RegSave & 0x000fu)); 00104 } 00105 00106 /// Emit unwind opcodes for .vsave directives 00107 void UnwindOpcodeAssembler::EmitVFPRegSave(uint32_t VFPRegSave) { 00108 size_t i = 32; 00109 00110 while (i > 16) { 00111 uint32_t Bit = 1u << (i - 1); 00112 if ((VFPRegSave & Bit) == 0u) { 00113 --i; 00114 continue; 00115 } 00116 00117 uint32_t Range = 0; 00118 00119 --i; 00120 Bit >>= 1; 00121 00122 while (i > 16 && (VFPRegSave & Bit)) { 00123 --i; 00124 ++Range; 00125 Bit >>= 1; 00126 } 00127 00128 EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_VFP_REG_RANGE_FSTMFDD_D16 | 00129 ((i - 16) << 4) | Range); 00130 } 00131 00132 while (i > 0) { 00133 uint32_t Bit = 1u << (i - 1); 00134 if ((VFPRegSave & Bit) == 0u) { 00135 --i; 00136 continue; 00137 } 00138 00139 uint32_t Range = 0; 00140 00141 --i; 00142 Bit >>= 1; 00143 00144 while (i > 0 && (VFPRegSave & Bit)) { 00145 --i; 00146 ++Range; 00147 Bit >>= 1; 00148 } 00149 00150 EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_VFP_REG_RANGE_FSTMFDD | (i << 4) | 00151 Range); 00152 } 00153 } 00154 00155 /// Emit unwind opcodes to copy address from source register to $sp. 00156 void UnwindOpcodeAssembler::EmitSetSP(uint16_t Reg) { 00157 EmitInt8(ARM::EHABI::UNWIND_OPCODE_SET_VSP | Reg); 00158 } 00159 00160 /// Emit unwind opcodes to add $sp with an offset. 00161 void UnwindOpcodeAssembler::EmitSPOffset(int64_t Offset) { 00162 if (Offset > 0x200) { 00163 uint8_t Buff[16]; 00164 Buff[0] = ARM::EHABI::UNWIND_OPCODE_INC_VSP_ULEB128; 00165 size_t ULEBSize = encodeULEB128((Offset - 0x204) >> 2, Buff + 1); 00166 EmitBytes(Buff, ULEBSize + 1); 00167 } else if (Offset > 0) { 00168 if (Offset > 0x100) { 00169 EmitInt8(ARM::EHABI::UNWIND_OPCODE_INC_VSP | 0x3fu); 00170 Offset -= 0x100; 00171 } 00172 EmitInt8(ARM::EHABI::UNWIND_OPCODE_INC_VSP | 00173 static_cast<uint8_t>((Offset - 4) >> 2)); 00174 } else if (Offset < 0) { 00175 while (Offset < -0x100) { 00176 EmitInt8(ARM::EHABI::UNWIND_OPCODE_DEC_VSP | 0x3fu); 00177 Offset += 0x100; 00178 } 00179 EmitInt8(ARM::EHABI::UNWIND_OPCODE_DEC_VSP | 00180 static_cast<uint8_t>(((-Offset) - 4) >> 2)); 00181 } 00182 } 00183 00184 void UnwindOpcodeAssembler::Finalize(unsigned &PersonalityIndex, 00185 SmallVectorImpl<uint8_t> &Result) { 00186 00187 UnwindOpcodeStreamer OpStreamer(Result); 00188 00189 if (HasPersonality) { 00190 // User-specifed personality routine: [ SIZE , OP1 , OP2 , ... ] 00191 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX; 00192 size_t TotalSize = Ops.size() + 1; 00193 size_t RoundUpSize = (TotalSize + 3) / 4 * 4; 00194 Result.resize(RoundUpSize); 00195 OpStreamer.EmitSize(RoundUpSize); 00196 } else { 00197 // If no personalityindex is specified, select ane 00198 if (PersonalityIndex == ARM::EHABI::NUM_PERSONALITY_INDEX) 00199 PersonalityIndex = (Ops.size() <= 3) ? ARM::EHABI::AEABI_UNWIND_CPP_PR0 00200 : ARM::EHABI::AEABI_UNWIND_CPP_PR1; 00201 if (PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0) { 00202 // __aeabi_unwind_cpp_pr0: [ 0x80 , OP1 , OP2 , OP3 ] 00203 assert(Ops.size() <= 3 && "too many opcodes for __aeabi_unwind_cpp_pr0"); 00204 Result.resize(4); 00205 OpStreamer.EmitPersonalityIndex(PersonalityIndex); 00206 } else { 00207 // __aeabi_unwind_cpp_pr{1,2}: [ {0x81,0x82} , SIZE , OP1 , OP2 , ... ] 00208 size_t TotalSize = Ops.size() + 2; 00209 size_t RoundUpSize = (TotalSize + 3) / 4 * 4; 00210 Result.resize(RoundUpSize); 00211 OpStreamer.EmitPersonalityIndex(PersonalityIndex); 00212 OpStreamer.EmitSize(RoundUpSize); 00213 } 00214 } 00215 00216 // Copy the unwind opcodes 00217 for (size_t i = OpBegins.size() - 1; i > 0; --i) 00218 for (size_t j = OpBegins[i - 1], end = OpBegins[i]; j < end; ++j) 00219 OpStreamer.EmitByte(Ops[j]); 00220 00221 // Emit the padding finish opcodes if the size is not multiple of 4. 00222 OpStreamer.FillFinishOpcode(); 00223 00224 // Reset the assembler state 00225 Reset(); 00226 }