Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rafael Espindola | 767b1be | 2010-12-04 00:31:13 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCAsmInfo.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCDwarf.h" |
Rafael Espindola | ad8aaa0 | 2010-11-22 11:53:17 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCStreamer.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCSymbol.h" |
| 14 | #include "llvm/MC/MCExpr.h" |
| 15 | #include "llvm/MC/MCContext.h" |
| 16 | #include "llvm/MC/MCObjectWriter.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetAsmInfo.h" |
Bill Wendling | 2b2dc7c | 2011-07-06 22:52:32 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/FoldingSet.h" |
| 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/Twine.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 27 | // Given a special op, return the address skip amount (in units of |
| 28 | // DWARF2_LINE_MIN_INSN_LENGTH. |
| 29 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
| 30 | |
| 31 | // The maximum address skip amount that can be encoded with a special op. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 32 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 33 | |
| 34 | // First special line opcode - leave room for the standard opcodes. |
| 35 | // Note: If you want to change this, you'll have to update the |
| 36 | // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 37 | #define DWARF2_LINE_OPCODE_BASE 13 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 38 | |
| 39 | // Minimum line offset in a special line info. opcode. This value |
| 40 | // was chosen to give a reasonable range of values. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 41 | #define DWARF2_LINE_BASE -5 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 42 | |
| 43 | // Range of line offsets in a special line info. opcode. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 44 | #define DWARF2_LINE_RANGE 14 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 45 | |
| 46 | // Define the architecture-dependent minimum instruction length (in bytes). |
| 47 | // This value should be rather too small than too big. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 48 | #define DWARF2_LINE_MIN_INSN_LENGTH 1 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 49 | |
| 50 | // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting, |
| 51 | // this routine is a nop and will be optimized away. |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 52 | static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 53 | if (DWARF2_LINE_MIN_INSN_LENGTH == 1) |
| 54 | return AddrDelta; |
| 55 | if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) { |
| 56 | // TODO: report this error, but really only once. |
| 57 | ; |
| 58 | } |
| 59 | return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH; |
| 60 | } |
| 61 | |
| 62 | // |
| 63 | // This is called when an instruction is assembled into the specified section |
| 64 | // and if there is information from the last .loc directive that has yet to have |
| 65 | // a line entry made for it is made. |
| 66 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 67 | void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 68 | if (!MCOS->getContext().getDwarfLocSeen()) |
| 69 | return; |
| 70 | |
| 71 | // Create a symbol at in the current section for use in the line entry. |
| 72 | MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol(); |
| 73 | // Set the value of the symbol to use for the MCLineEntry. |
| 74 | MCOS->EmitLabel(LineSym); |
| 75 | |
| 76 | // Get the current .loc info saved in the context. |
| 77 | const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc(); |
| 78 | |
| 79 | // Create a (local) line entry with the symbol and the current .loc info. |
| 80 | MCLineEntry LineEntry(LineSym, DwarfLoc); |
| 81 | |
| 82 | // clear DwarfLocSeen saying the current .loc info is now used. |
Kevin Enderby | 3f55c24 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 83 | MCOS->getContext().ClearDwarfLocSeen(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 84 | |
| 85 | // Get the MCLineSection for this section, if one does not exist for this |
| 86 | // section create it. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 87 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 88 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 89 | MCLineSection *LineSection = MCLineSections.lookup(Section); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 90 | if (!LineSection) { |
| 91 | // Create a new MCLineSection. This will be deleted after the dwarf line |
| 92 | // table is created using it by iterating through the MCLineSections |
| 93 | // DenseMap. |
| 94 | LineSection = new MCLineSection; |
| 95 | // Save a pointer to the new LineSection into the MCLineSections DenseMap. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 96 | MCOS->getContext().addMCLineSection(Section, LineSection); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Add the line entry to this section's entries. |
| 100 | LineSection->addLineEntry(LineEntry); |
| 101 | } |
| 102 | |
| 103 | // |
| 104 | // This helper routine returns an expression of End - Start + IntVal . |
| 105 | // |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 106 | static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS, |
| 107 | const MCSymbol &Start, |
| 108 | const MCSymbol &End, |
| 109 | int IntVal) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 110 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 111 | const MCExpr *Res = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 112 | MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 113 | const MCExpr *RHS = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 114 | MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 115 | const MCExpr *Res1 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 116 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 117 | const MCExpr *Res2 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 118 | MCConstantExpr::Create(IntVal, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 119 | const MCExpr *Res3 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 120 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 121 | return Res3; |
| 122 | } |
| 123 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 124 | // |
| 125 | // This emits the Dwarf line table for the specified section from the entries |
| 126 | // in the LineSection. |
| 127 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 128 | static inline void EmitDwarfLineTable(MCStreamer *MCOS, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 129 | const MCSection *Section, |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 130 | const MCLineSection *LineSection) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 131 | unsigned FileNum = 1; |
| 132 | unsigned LastLine = 1; |
| 133 | unsigned Column = 0; |
| 134 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 135 | unsigned Isa = 0; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 136 | MCSymbol *LastLabel = NULL; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 137 | |
| 138 | // Loop through each MCLineEntry and encode the dwarf line number table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 139 | for (MCLineSection::const_iterator |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 140 | it = LineSection->getMCLineEntries()->begin(), |
| 141 | ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) { |
| 142 | |
| 143 | if (FileNum != it->getFileNum()) { |
| 144 | FileNum = it->getFileNum(); |
| 145 | MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 146 | MCOS->EmitULEB128IntValue(FileNum); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 147 | } |
| 148 | if (Column != it->getColumn()) { |
| 149 | Column = it->getColumn(); |
| 150 | MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 151 | MCOS->EmitULEB128IntValue(Column); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 152 | } |
| 153 | if (Isa != it->getIsa()) { |
| 154 | Isa = it->getIsa(); |
| 155 | MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 156 | MCOS->EmitULEB128IntValue(Isa); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 157 | } |
| 158 | if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) { |
| 159 | Flags = it->getFlags(); |
| 160 | MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1); |
| 161 | } |
| 162 | if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK) |
| 163 | MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1); |
| 164 | if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END) |
| 165 | MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1); |
| 166 | if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN) |
| 167 | MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1); |
| 168 | |
Rafael Espindola | 64185cc | 2010-11-13 01:06:27 +0000 | [diff] [blame] | 169 | int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 170 | MCSymbol *Label = it->getLabel(); |
| 171 | |
| 172 | // At this point we want to emit/create the sequence to encode the delta in |
| 173 | // line numbers and the increment of the address from the previous Label |
| 174 | // and the current Label. |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 175 | const TargetAsmInfo &asmInfo = MCOS->getContext().getTargetAsmInfo(); |
| 176 | MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label, |
| 177 | asmInfo.getPointerSize()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 178 | |
| 179 | LastLine = it->getLine(); |
| 180 | LastLabel = Label; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Emit a DW_LNE_end_sequence for the end of the section. |
| 184 | // Using the pointer Section create a temporary label at the end of the |
| 185 | // section and use that and the LastLabel to compute the address delta |
| 186 | // and use INT64_MAX as the line delta which is the signal that this is |
| 187 | // actually a DW_LNE_end_sequence. |
| 188 | |
| 189 | // Switch to the section to be able to create a symbol at its end. |
| 190 | MCOS->SwitchSection(Section); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 191 | |
| 192 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 193 | // Create a symbol at the end of the section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 194 | MCSymbol *SectionEnd = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 195 | // Set the value of the symbol, as we are at the end of the section. |
| 196 | MCOS->EmitLabel(SectionEnd); |
| 197 | |
| 198 | // Switch back the the dwarf line section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 199 | MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection()); |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 200 | |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 201 | const TargetAsmInfo &asmInfo = MCOS->getContext().getTargetAsmInfo(); |
| 202 | MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd, |
| 203 | asmInfo.getPointerSize()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // |
| 207 | // This emits the Dwarf file and the line tables. |
| 208 | // |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 209 | void MCDwarfFileTable::Emit(MCStreamer *MCOS) { |
| 210 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 211 | // Switch to the section where the table will be emitted into. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 212 | MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 213 | |
| 214 | // Create a symbol at the beginning of this section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 215 | MCSymbol *LineStartSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 216 | // Set the value of the symbol, as we are at the start of the section. |
| 217 | MCOS->EmitLabel(LineStartSym); |
| 218 | |
| 219 | // Create a symbol for the end of the section (to be set when we get there). |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 220 | MCSymbol *LineEndSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 221 | |
| 222 | // The first 4 bytes is the total length of the information for this |
| 223 | // compilation unit (not including these 4 bytes for the length). |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 224 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4), |
Rafael Espindola | 0bbe0b4 | 2010-12-06 17:27:56 +0000 | [diff] [blame] | 225 | 4); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 226 | |
| 227 | // Next 2 bytes is the Version, which is Dwarf 2. |
| 228 | MCOS->EmitIntValue(2, 2); |
| 229 | |
| 230 | // Create a symbol for the end of the prologue (to be set when we get there). |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 231 | MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 232 | |
| 233 | // Length of the prologue, is the next 4 bytes. Which is the start of the |
| 234 | // section to the end of the prologue. Not including the 4 bytes for the |
| 235 | // total length, the 2 bytes for the version, and these 4 bytes for the |
| 236 | // length of the prologue. |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 237 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 238 | (4 + 2 + 4)), |
Rafael Espindola | 5d4918d | 2010-12-04 03:21:47 +0000 | [diff] [blame] | 239 | 4, 0); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 240 | |
| 241 | // Parameters of the state machine, are next. |
| 242 | MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1); |
| 243 | MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); |
| 244 | MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); |
| 245 | MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); |
| 246 | MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1); |
| 247 | |
| 248 | // Standard opcode lengths |
| 249 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy |
| 250 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc |
| 251 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line |
| 252 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file |
| 253 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column |
| 254 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt |
| 255 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block |
| 256 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc |
| 257 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc |
| 258 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end |
| 259 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin |
| 260 | MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa |
| 261 | |
| 262 | // Put out the directory and file tables. |
| 263 | |
| 264 | // First the directory table. |
| 265 | const std::vector<StringRef> &MCDwarfDirs = |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 266 | context.getMCDwarfDirs(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 267 | for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { |
| 268 | MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName |
| 269 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
| 270 | } |
| 271 | MCOS->EmitIntValue(0, 1); // Terminate the directory list |
| 272 | |
| 273 | // Second the file table. |
| 274 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 275 | MCOS->getContext().getMCDwarfFiles(); |
| 276 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
| 277 | MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName |
| 278 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 279 | // the Directory num |
| 280 | MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 281 | MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0) |
| 282 | MCOS->EmitIntValue(0, 1); // filesize (always 0) |
| 283 | } |
| 284 | MCOS->EmitIntValue(0, 1); // Terminate the file list |
| 285 | |
| 286 | // This is the end of the prologue, so set the value of the symbol at the |
| 287 | // end of the prologue (that was used in a previous expression). |
| 288 | MCOS->EmitLabel(ProEndSym); |
| 289 | |
| 290 | // Put out the line tables. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 291 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 292 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 293 | const std::vector<const MCSection *> &MCLineSectionOrder = |
| 294 | MCOS->getContext().getMCLineSectionOrder(); |
| 295 | for (std::vector<const MCSection*>::const_iterator it = |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 296 | MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie; |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 297 | ++it) { |
| 298 | const MCSection *Sec = *it; |
| 299 | const MCLineSection *Line = MCLineSections.lookup(Sec); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 300 | EmitDwarfLineTable(MCOS, Sec, Line); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 301 | |
| 302 | // Now delete the MCLineSections that were created in MCLineEntry::Make() |
| 303 | // and used to emit the line table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 304 | delete Line; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Rafael Espindola | 767b1be | 2010-12-04 00:31:13 +0000 | [diff] [blame] | 307 | if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines() |
| 308 | && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) { |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 309 | // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures |
| 310 | // it requires: |
| 311 | // total_length >= prologue_length + 10 |
| 312 | // We are 4 bytes short, since we have total_length = 51 and |
| 313 | // prologue_length = 45 |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 314 | |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 315 | // The regular end_sequence should be sufficient. |
| 316 | MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0); |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 319 | // This is the end of the section, so set the value of the symbol at the end |
| 320 | // of this section (that was used in a previous expression). |
| 321 | MCOS->EmitLabel(LineEndSym); |
| 322 | } |
| 323 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 324 | /// Utility function to write the encoding to an object writer. |
| 325 | void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta, |
| 326 | uint64_t AddrDelta) { |
| 327 | SmallString<256> Tmp; |
| 328 | raw_svector_ostream OS(Tmp); |
| 329 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 330 | OW->WriteBytes(OS.str()); |
| 331 | } |
| 332 | |
| 333 | /// Utility function to emit the encoding to a streamer. |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 334 | void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 335 | uint64_t AddrDelta) { |
| 336 | SmallString<256> Tmp; |
| 337 | raw_svector_ostream OS(Tmp); |
| 338 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 339 | MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0); |
| 340 | } |
| 341 | |
| 342 | /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. |
| 343 | void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta, |
| 344 | raw_ostream &OS) { |
| 345 | uint64_t Temp, Opcode; |
| 346 | bool NeedCopy = false; |
| 347 | |
| 348 | // Scale the address delta by the minimum instruction length. |
| 349 | AddrDelta = ScaleAddrDelta(AddrDelta); |
| 350 | |
| 351 | // A LineDelta of INT64_MAX is a signal that this is actually a |
| 352 | // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the |
| 353 | // end_sequence to emit the matrix entry. |
| 354 | if (LineDelta == INT64_MAX) { |
| 355 | if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) |
| 356 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 357 | else { |
| 358 | OS << char(dwarf::DW_LNS_advance_pc); |
Benjamin Kramer | dcf0e0c | 2011-06-18 14:42:47 +0000 | [diff] [blame] | 359 | MCObjectWriter::EncodeULEB128(AddrDelta, OS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 360 | } |
| 361 | OS << char(dwarf::DW_LNS_extended_op); |
| 362 | OS << char(1); |
| 363 | OS << char(dwarf::DW_LNE_end_sequence); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | // Bias the line delta by the base. |
| 368 | Temp = LineDelta - DWARF2_LINE_BASE; |
| 369 | |
| 370 | // If the line increment is out of range of a special opcode, we must encode |
| 371 | // it with DW_LNS_advance_line. |
| 372 | if (Temp >= DWARF2_LINE_RANGE) { |
| 373 | OS << char(dwarf::DW_LNS_advance_line); |
| 374 | SmallString<32> Tmp; |
| 375 | raw_svector_ostream OSE(Tmp); |
| 376 | MCObjectWriter::EncodeSLEB128(LineDelta, OSE); |
| 377 | OS << OSE.str(); |
| 378 | |
| 379 | LineDelta = 0; |
| 380 | Temp = 0 - DWARF2_LINE_BASE; |
| 381 | NeedCopy = true; |
| 382 | } |
| 383 | |
| 384 | // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode. |
| 385 | if (LineDelta == 0 && AddrDelta == 0) { |
| 386 | OS << char(dwarf::DW_LNS_copy); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | // Bias the opcode by the special opcode base. |
| 391 | Temp += DWARF2_LINE_OPCODE_BASE; |
| 392 | |
| 393 | // Avoid overflow when addr_delta is large. |
| 394 | if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) { |
| 395 | // Try using a special opcode. |
| 396 | Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE; |
| 397 | if (Opcode <= 255) { |
| 398 | OS << char(Opcode); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | // Try using DW_LNS_const_add_pc followed by special op. |
| 403 | Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
| 404 | if (Opcode <= 255) { |
| 405 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 406 | OS << char(Opcode); |
| 407 | return; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Otherwise use DW_LNS_advance_pc. |
| 412 | OS << char(dwarf::DW_LNS_advance_pc); |
| 413 | SmallString<32> Tmp; |
| 414 | raw_svector_ostream OSE(Tmp); |
| 415 | MCObjectWriter::EncodeULEB128(AddrDelta, OSE); |
| 416 | OS << OSE.str(); |
| 417 | |
| 418 | if (NeedCopy) |
| 419 | OS << char(dwarf::DW_LNS_copy); |
| 420 | else |
| 421 | OS << char(Temp); |
| 422 | } |
| 423 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 424 | void MCDwarfFile::print(raw_ostream &OS) const { |
| 425 | OS << '"' << getName() << '"'; |
| 426 | } |
| 427 | |
| 428 | void MCDwarfFile::dump() const { |
| 429 | print(dbgs()); |
| 430 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 431 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 432 | static int getDataAlignmentFactor(MCStreamer &streamer) { |
| 433 | MCContext &context = streamer.getContext(); |
| 434 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
| 435 | int size = asmInfo.getPointerSize(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 436 | if (asmInfo.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp) |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 437 | return size; |
| 438 | else |
| 439 | return -size; |
| 440 | } |
| 441 | |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 442 | static unsigned getSizeForEncoding(MCStreamer &streamer, |
| 443 | unsigned symbolEncoding) { |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 444 | MCContext &context = streamer.getContext(); |
| 445 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
| 446 | unsigned format = symbolEncoding & 0x0f; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 447 | switch (format) { |
| 448 | default: |
| 449 | assert(0 && "Unknown Encoding"); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 450 | case dwarf::DW_EH_PE_absptr: |
| 451 | case dwarf::DW_EH_PE_signed: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 452 | return asmInfo.getPointerSize(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 453 | case dwarf::DW_EH_PE_udata2: |
| 454 | case dwarf::DW_EH_PE_sdata2: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 455 | return 2; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 456 | case dwarf::DW_EH_PE_udata4: |
| 457 | case dwarf::DW_EH_PE_sdata4: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 458 | return 4; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 459 | case dwarf::DW_EH_PE_udata8: |
| 460 | case dwarf::DW_EH_PE_sdata8: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 461 | return 8; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 462 | } |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol, |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 466 | unsigned symbolEncoding, const char *comment = 0) { |
Rafael Espindola | a0057ca | 2011-04-28 21:04:39 +0000 | [diff] [blame] | 467 | MCContext &context = streamer.getContext(); |
| 468 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
| 469 | const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol, |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 470 | symbolEncoding, |
Rafael Espindola | a0057ca | 2011-04-28 21:04:39 +0000 | [diff] [blame] | 471 | streamer); |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 472 | unsigned size = getSizeForEncoding(streamer, symbolEncoding); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 473 | if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 474 | streamer.EmitAbsValue(v, size); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 477 | static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol, |
| 478 | unsigned symbolEncoding) { |
| 479 | MCContext &context = streamer.getContext(); |
| 480 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 481 | const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol, |
| 482 | symbolEncoding, |
| 483 | streamer); |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 484 | unsigned size = getSizeForEncoding(streamer, symbolEncoding); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 485 | streamer.EmitValue(v, size); |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 488 | static const MachineLocation TranslateMachineLocation( |
| 489 | const TargetAsmInfo &AsmInfo, |
| 490 | const MachineLocation &Loc) { |
| 491 | unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ? |
| 492 | MachineLocation::VirtualFP : |
| 493 | unsigned(AsmInfo.getDwarfRegNum(Loc.getReg(), true)); |
| 494 | const MachineLocation &NewLoc = Loc.isReg() ? |
| 495 | MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset()); |
| 496 | return NewLoc; |
| 497 | } |
| 498 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 499 | namespace { |
| 500 | class FrameEmitterImpl { |
| 501 | int CFAOffset; |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 502 | int CIENum; |
Rafael Espindola | 5426a9e | 2011-05-01 04:49:54 +0000 | [diff] [blame] | 503 | bool UsingCFI; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 504 | bool IsEH; |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 505 | const MCSymbol *SectionStart; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 506 | public: |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 507 | FrameEmitterImpl(bool usingCFI, bool isEH, const MCSymbol *sectionStart) : |
| 508 | CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH), |
| 509 | SectionStart(sectionStart) { |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 512 | /// EmitCompactUnwind - Emit the unwind information in a compact way. If |
| 513 | /// we're successful, return 'true'. Otherwise, return 'false' and it will |
| 514 | /// emit the normal CIE and FDE. |
| 515 | bool EmitCompactUnwind(MCStreamer &streamer, |
| 516 | const MCDwarfFrameInfo &frame); |
| 517 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 518 | const MCSymbol &EmitCIE(MCStreamer &streamer, |
| 519 | const MCSymbol *personality, |
| 520 | unsigned personalityEncoding, |
| 521 | const MCSymbol *lsda, |
| 522 | unsigned lsdaEncoding); |
| 523 | MCSymbol *EmitFDE(MCStreamer &streamer, |
| 524 | const MCSymbol &cieStart, |
Rafael Espindola | 9f270da | 2011-05-10 03:01:39 +0000 | [diff] [blame] | 525 | const MCDwarfFrameInfo &frame); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 526 | void EmitCFIInstructions(MCStreamer &streamer, |
| 527 | const std::vector<MCCFIInstruction> &Instrs, |
| 528 | MCSymbol *BaseLabel); |
| 529 | void EmitCFIInstruction(MCStreamer &Streamer, |
| 530 | const MCCFIInstruction &Instr); |
| 531 | }; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 532 | |
| 533 | } // end anonymous namespace |
| 534 | |
| 535 | static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding, |
| 536 | StringRef Prefix) { |
| 537 | if (Streamer.isVerboseAsm()) { |
| 538 | const char *EncStr = 0; |
| 539 | switch (Encoding) { |
| 540 | default: EncStr = "<unknown encoding>"; |
| 541 | case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; |
| 542 | case dwarf::DW_EH_PE_omit: EncStr = "omit"; |
| 543 | case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; |
| 544 | case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; |
| 545 | case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; |
| 546 | case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; |
| 547 | case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; |
| 548 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4: EncStr = "pcrel udata4"; |
| 549 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4: EncStr = "pcrel sdata4"; |
| 550 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8: EncStr = "pcrel udata8"; |
| 551 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8: EncStr = "pcrel sdata8"; |
| 552 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4: |
| 553 | EncStr = "indirect pcrel udata4"; |
| 554 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4: |
| 555 | EncStr = "indirect pcrel sdata4"; |
| 556 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8: |
| 557 | EncStr = "indirect pcrel udata8"; |
| 558 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8: |
| 559 | EncStr = "indirect pcrel sdata8"; |
| 560 | } |
| 561 | |
| 562 | Streamer.AddComment(Twine(Prefix) + " = " + EncStr); |
| 563 | } |
| 564 | |
| 565 | Streamer.EmitIntValue(Encoding, 1); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer, |
| 569 | const MCCFIInstruction &Instr) { |
| 570 | int dataAlignmentFactor = getDataAlignmentFactor(Streamer); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 571 | bool VerboseAsm = Streamer.isVerboseAsm(); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 572 | |
| 573 | switch (Instr.getOperation()) { |
| 574 | case MCCFIInstruction::Move: |
| 575 | case MCCFIInstruction::RelMove: { |
| 576 | const MachineLocation &Dst = Instr.getDestination(); |
| 577 | const MachineLocation &Src = Instr.getSource(); |
Rafael Espindola | 5d7dcd3 | 2011-04-12 18:53:30 +0000 | [diff] [blame] | 578 | const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 579 | |
| 580 | // If advancing cfa. |
| 581 | if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 582 | if (Src.getReg() == MachineLocation::VirtualFP) { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 583 | if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 584 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1); |
| 585 | } else { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 586 | if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 587 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 588 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + |
| 589 | Twine(Src.getReg())); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 590 | Streamer.EmitULEB128IntValue(Src.getReg()); |
| 591 | } |
| 592 | |
Rafael Espindola | 5d7dcd3 | 2011-04-12 18:53:30 +0000 | [diff] [blame] | 593 | if (IsRelative) |
| 594 | CFAOffset += Src.getOffset(); |
| 595 | else |
| 596 | CFAOffset = -Src.getOffset(); |
| 597 | |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 598 | if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 599 | Streamer.EmitULEB128IntValue(CFAOffset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 600 | return; |
| 601 | } |
| 602 | |
| 603 | if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) { |
| 604 | assert(Dst.isReg() && "Machine move not supported yet."); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 605 | if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 606 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 607 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg())); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 608 | Streamer.EmitULEB128IntValue(Dst.getReg()); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | unsigned Reg = Src.getReg(); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 613 | int Offset = Dst.getOffset(); |
| 614 | if (IsRelative) |
| 615 | Offset -= CFAOffset; |
| 616 | Offset = Offset / dataAlignmentFactor; |
| 617 | |
| 618 | if (Offset < 0) { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 619 | if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 620 | Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 621 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 622 | Streamer.EmitULEB128IntValue(Reg); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 623 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 624 | Streamer.EmitSLEB128IntValue(Offset); |
| 625 | } else if (Reg < 64) { |
Bill Wendling | e08d433 | 2011-06-30 23:47:40 +0000 | [diff] [blame] | 626 | if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") + |
| 627 | Twine(Reg) + ")"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 628 | Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 629 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 630 | Streamer.EmitULEB128IntValue(Offset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 631 | } else { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 632 | if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 633 | Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 634 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 635 | Streamer.EmitULEB128IntValue(Reg); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 636 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 637 | Streamer.EmitULEB128IntValue(Offset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 638 | } |
| 639 | return; |
| 640 | } |
| 641 | case MCCFIInstruction::Remember: |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 642 | if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 643 | Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1); |
| 644 | return; |
| 645 | case MCCFIInstruction::Restore: |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 646 | if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 647 | Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1); |
| 648 | return; |
| 649 | case MCCFIInstruction::SameValue: { |
| 650 | unsigned Reg = Instr.getDestination().getReg(); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 651 | if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 652 | Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 653 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 654 | Streamer.EmitULEB128IntValue(Reg); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 655 | return; |
| 656 | } |
| 657 | } |
| 658 | llvm_unreachable("Unhandled case in switch"); |
| 659 | } |
| 660 | |
| 661 | /// EmitFrameMoves - Emit frame instructions to describe the layout of the |
| 662 | /// frame. |
| 663 | void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer, |
| 664 | const std::vector<MCCFIInstruction> &Instrs, |
| 665 | MCSymbol *BaseLabel) { |
| 666 | for (unsigned i = 0, N = Instrs.size(); i < N; ++i) { |
| 667 | const MCCFIInstruction &Instr = Instrs[i]; |
| 668 | MCSymbol *Label = Instr.getLabel(); |
| 669 | // Throw out move if the label is invalid. |
| 670 | if (Label && !Label->isDefined()) continue; // Not emitted, in dead code. |
| 671 | |
| 672 | // Advance row if new location. |
| 673 | if (BaseLabel && Label) { |
| 674 | MCSymbol *ThisSym = Label; |
| 675 | if (ThisSym != BaseLabel) { |
Bill Wendling | d221cd6 | 2011-06-30 22:35:49 +0000 | [diff] [blame] | 676 | if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 677 | streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym); |
| 678 | BaseLabel = ThisSym; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | EmitCFIInstruction(streamer, Instr); |
| 683 | } |
| 684 | } |
| 685 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 686 | /// EmitCompactUnwind - Emit the unwind information in a compact way. If we're |
| 687 | /// successful, return 'true'. Otherwise, return 'false' and it will emit the |
| 688 | /// normal CIE and FDE. |
| 689 | bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer, |
| 690 | const MCDwarfFrameInfo &Frame) { |
| 691 | #if 1 |
| 692 | return false; |
| 693 | #else |
| 694 | MCContext &Context = Streamer.getContext(); |
| 695 | const TargetAsmInfo &TAI = Context.getTargetAsmInfo(); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 696 | bool VerboseAsm = Streamer.isVerboseAsm(); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 697 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 698 | // range-start range-length compact-unwind-enc personality-func lsda |
| 699 | // _foo LfooEnd-_foo 0x00000023 0 0 |
| 700 | // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1 |
| 701 | // |
| 702 | // .section __LD,__compact_unwind,regular,debug |
| 703 | // |
| 704 | // # compact unwind for _foo |
| 705 | // .quad _foo |
| 706 | // .set L1,LfooEnd-_foo |
| 707 | // .long L1 |
| 708 | // .long 0x01010001 |
| 709 | // .quad 0 |
| 710 | // .quad 0 |
| 711 | // |
| 712 | // # compact unwind for _bar |
| 713 | // .quad _bar |
| 714 | // .set L2,LbarEnd-_bar |
| 715 | // .long L2 |
| 716 | // .long 0x01020011 |
| 717 | // .quad __gxx_personality |
| 718 | // .quad except_tab1 |
| 719 | |
Bill Wendling | 6a6b8c3 | 2011-07-07 00:54:13 +0000 | [diff] [blame] | 720 | uint32_t Encoding = |
| 721 | TAI.getCompactUnwindEncoding(Frame.Instructions, |
| 722 | getDataAlignmentFactor(Streamer), IsEH); |
| 723 | if (!Encoding) return false; |
| 724 | |
Bill Wendling | 4bb5b03 | 2011-07-14 23:34:45 +0000 | [diff] [blame^] | 725 | // The encoding needs to know we have an LSDA. |
| 726 | if (Frame.Lsda) |
| 727 | Encoding |= 0x40000000; |
| 728 | |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 729 | Streamer.SwitchSection(TAI.getCompactUnwindSection()); |
| 730 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 731 | // Range Start |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 732 | unsigned FDEEncoding = TAI.getFDEEncoding(UsingCFI); |
| 733 | unsigned Size = getSizeForEncoding(Streamer, FDEEncoding); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 734 | if (VerboseAsm) Streamer.AddComment("Range Start"); |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 735 | Streamer.EmitSymbolValue(Frame.Function, Size); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 736 | |
| 737 | // Range Length |
| 738 | const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin, |
| 739 | *Frame.End, 0); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 740 | if (VerboseAsm) Streamer.AddComment("Range Length"); |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 741 | Streamer.EmitAbsValue(Range, 4); |
| 742 | |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 743 | // Compact Encoding |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 744 | Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4); |
Bill Wendling | 6a6b8c3 | 2011-07-07 00:54:13 +0000 | [diff] [blame] | 745 | if (VerboseAsm) Streamer.AddComment(Twine("Compact Unwind Encoding: 0x") + |
| 746 | Twine(llvm::utohexstr(Encoding))); |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 747 | Streamer.EmitIntValue(Encoding, Size); |
| 748 | |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 749 | // Personality Function |
Bill Wendling | 4bb5b03 | 2011-07-14 23:34:45 +0000 | [diff] [blame^] | 750 | Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 751 | if (VerboseAsm) Streamer.AddComment("Personality Function"); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 752 | if (Frame.Personality) |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 753 | Streamer.EmitSymbolValue(Frame.Personality, Size); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 754 | else |
| 755 | Streamer.EmitIntValue(0, Size); // No personality fn |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 756 | |
| 757 | // LSDA |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 758 | Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 759 | if (VerboseAsm) Streamer.AddComment("LSDA"); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 760 | if (Frame.Lsda) |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 761 | Streamer.EmitSymbolValue(Frame.Lsda, Size); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 762 | else |
| 763 | Streamer.EmitIntValue(0, Size); // No LSDA |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 764 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 765 | return true; |
| 766 | #endif |
| 767 | } |
| 768 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 769 | const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer, |
| 770 | const MCSymbol *personality, |
| 771 | unsigned personalityEncoding, |
| 772 | const MCSymbol *lsda, |
| 773 | unsigned lsdaEncoding) { |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 774 | MCContext &context = streamer.getContext(); |
| 775 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 776 | bool verboseAsm = streamer.isVerboseAsm(); |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 777 | |
| 778 | MCSymbol *sectionStart; |
Rafael Espindola | 12f197b | 2011-05-10 19:51:53 +0000 | [diff] [blame] | 779 | if (asmInfo.isFunctionEHFrameSymbolPrivate() || !IsEH) |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 780 | sectionStart = context.CreateTempSymbol(); |
| 781 | else |
| 782 | sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum)); |
| 783 | |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 784 | streamer.EmitLabel(sectionStart); |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 785 | CIENum++; |
| 786 | |
Bill Wendling | 6a6b8c3 | 2011-07-07 00:54:13 +0000 | [diff] [blame] | 787 | MCSymbol *sectionEnd = context.CreateTempSymbol(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 788 | |
| 789 | // Length |
| 790 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart, |
| 791 | *sectionEnd, 4); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 792 | if (verboseAsm) streamer.AddComment("CIE Length"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 793 | streamer.EmitAbsValue(Length, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 794 | |
| 795 | // CIE ID |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 796 | unsigned CIE_ID = IsEH ? 0 : -1; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 797 | if (verboseAsm) streamer.AddComment("CIE ID Tag"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 798 | streamer.EmitIntValue(CIE_ID, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 799 | |
| 800 | // Version |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 801 | if (verboseAsm) streamer.AddComment("DW_CIE_VERSION"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 802 | streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1); |
| 803 | |
| 804 | // Augmentation String |
| 805 | SmallString<8> Augmentation; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 806 | if (IsEH) { |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 807 | if (verboseAsm) streamer.AddComment("CIE Augmentation"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 808 | Augmentation += "z"; |
| 809 | if (personality) |
| 810 | Augmentation += "P"; |
| 811 | if (lsda) |
| 812 | Augmentation += "L"; |
| 813 | Augmentation += "R"; |
| 814 | streamer.EmitBytes(Augmentation.str(), 0); |
| 815 | } |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 816 | streamer.EmitIntValue(0, 1); |
| 817 | |
| 818 | // Code Alignment Factor |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 819 | if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 820 | streamer.EmitULEB128IntValue(1); |
| 821 | |
| 822 | // Data Alignment Factor |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 823 | if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 824 | streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer)); |
| 825 | |
| 826 | // Return Address Register |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 827 | if (verboseAsm) streamer.AddComment("CIE Return Address Column"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 828 | streamer.EmitULEB128IntValue(asmInfo.getDwarfRARegNum(true)); |
| 829 | |
| 830 | // Augmentation Data Length (optional) |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 831 | |
| 832 | unsigned augmentationLength = 0; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 833 | if (IsEH) { |
| 834 | if (personality) { |
| 835 | // Personality Encoding |
| 836 | augmentationLength += 1; |
| 837 | // Personality |
| 838 | augmentationLength += getSizeForEncoding(streamer, personalityEncoding); |
| 839 | } |
| 840 | if (lsda) |
| 841 | augmentationLength += 1; |
| 842 | // Encoding of the FDE pointers |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 843 | augmentationLength += 1; |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 844 | |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 845 | if (verboseAsm) streamer.AddComment("Augmentation Size"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 846 | streamer.EmitULEB128IntValue(augmentationLength); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 847 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 848 | // Augmentation Data (optional) |
| 849 | if (personality) { |
| 850 | // Personality Encoding |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 851 | EmitEncodingByte(streamer, personalityEncoding, |
| 852 | "Personality Encoding"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 853 | // Personality |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 854 | if (verboseAsm) streamer.AddComment("Personality"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 855 | EmitPersonality(streamer, *personality, personalityEncoding); |
| 856 | } |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 857 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 858 | if (lsda) |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 859 | EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding"); |
| 860 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 861 | // Encoding of the FDE pointers |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 862 | EmitEncodingByte(streamer, asmInfo.getFDEEncoding(UsingCFI), |
| 863 | "FDE Encoding"); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 864 | } |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 865 | |
| 866 | // Initial Instructions |
| 867 | |
Bill Wendling | 5b46e8e | 2011-06-23 07:55:41 +0000 | [diff] [blame] | 868 | const std::vector<MachineMove> &Moves = asmInfo.getInitialFrameState(); |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 869 | std::vector<MCCFIInstruction> Instructions; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 870 | |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 871 | for (int i = 0, n = Moves.size(); i != n; ++i) { |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 872 | MCSymbol *Label = Moves[i].getLabel(); |
| 873 | const MachineLocation &Dst = |
| 874 | TranslateMachineLocation(asmInfo, Moves[i].getDestination()); |
| 875 | const MachineLocation &Src = |
| 876 | TranslateMachineLocation(asmInfo, Moves[i].getSource()); |
| 877 | MCCFIInstruction Inst(Label, Dst, Src); |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 878 | Instructions.push_back(Inst); |
| 879 | } |
| 880 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 881 | EmitCFIInstructions(streamer, Instructions, NULL); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 882 | |
| 883 | // Padding |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 884 | streamer.EmitValueToAlignment(IsEH ? 4 : asmInfo.getPointerSize()); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 885 | |
| 886 | streamer.EmitLabel(sectionEnd); |
| 887 | return *sectionStart; |
| 888 | } |
| 889 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 890 | MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer, |
| 891 | const MCSymbol &cieStart, |
Rafael Espindola | 9f270da | 2011-05-10 03:01:39 +0000 | [diff] [blame] | 892 | const MCDwarfFrameInfo &frame) { |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 893 | MCContext &context = streamer.getContext(); |
| 894 | MCSymbol *fdeStart = context.CreateTempSymbol(); |
| 895 | MCSymbol *fdeEnd = context.CreateTempSymbol(); |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 896 | const TargetAsmInfo &TAsmInfo = context.getTargetAsmInfo(); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 897 | bool verboseAsm = streamer.isVerboseAsm(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 898 | |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 899 | if (!TAsmInfo.isFunctionEHFrameSymbolPrivate() && IsEH) { |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 900 | MCSymbol *EHSym = |
| 901 | context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh")); |
Rafael Espindola | 8bca410 | 2011-04-28 12:50:37 +0000 | [diff] [blame] | 902 | streamer.EmitEHSymAttributes(frame.Function, EHSym); |
Rafael Espindola | a8cfb87 | 2011-04-28 02:46:42 +0000 | [diff] [blame] | 903 | streamer.EmitLabel(EHSym); |
| 904 | } |
| 905 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 906 | // Length |
| 907 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 908 | if (verboseAsm) streamer.AddComment("FDE Length"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 909 | streamer.EmitAbsValue(Length, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 910 | |
| 911 | streamer.EmitLabel(fdeStart); |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 912 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 913 | // CIE Pointer |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 914 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 915 | if (IsEH) { |
| 916 | const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart, |
| 917 | 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 918 | if (verboseAsm) streamer.AddComment("FDE CIE Offset"); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 919 | streamer.EmitAbsValue(offset, 4); |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 920 | } else if (!asmInfo.doesDwarfRequireRelocationForSectionOffset()) { |
| 921 | const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart, |
| 922 | cieStart, 0); |
| 923 | streamer.EmitAbsValue(offset, 4); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 924 | } else { |
| 925 | streamer.EmitSymbolValue(&cieStart, 4); |
| 926 | } |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 927 | |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 928 | unsigned fdeEncoding = TAsmInfo.getFDEEncoding(UsingCFI); |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 929 | unsigned size = getSizeForEncoding(streamer, fdeEncoding); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 930 | |
| 931 | // PC Begin |
Rafael Espindola | 9989ef4 | 2011-05-10 22:28:35 +0000 | [diff] [blame] | 932 | unsigned PCBeginEncoding = IsEH ? fdeEncoding : |
| 933 | (unsigned)dwarf::DW_EH_PE_absptr; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 934 | unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 935 | EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 936 | |
| 937 | // PC Range |
| 938 | const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin, |
| 939 | *frame.End, 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 940 | if (verboseAsm) streamer.AddComment("FDE address range"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 941 | streamer.EmitAbsValue(Range, size); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 942 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 943 | if (IsEH) { |
| 944 | // Augmentation Data Length |
| 945 | unsigned augmentationLength = 0; |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 946 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 947 | if (frame.Lsda) |
| 948 | augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding); |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 949 | |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 950 | if (verboseAsm) streamer.AddComment("Augmentation size"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 951 | streamer.EmitULEB128IntValue(augmentationLength); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 952 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 953 | // Augmentation Data |
| 954 | if (frame.Lsda) |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 955 | EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding, |
| 956 | "Language Specific Data Area"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 957 | } |
Rafael Espindola | 4892dff | 2011-04-29 03:06:29 +0000 | [diff] [blame] | 958 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 959 | // Call Frame Instructions |
| 960 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 961 | EmitCFIInstructions(streamer, frame.Instructions, frame.Begin); |
Rafael Espindola | 5bba084 | 2010-12-28 04:15:37 +0000 | [diff] [blame] | 962 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 963 | // Padding |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 964 | streamer.EmitValueToAlignment(PCBeginSize); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 965 | |
| 966 | return fdeEnd; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 969 | namespace { |
| 970 | struct CIEKey { |
| 971 | static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1); } |
| 972 | static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0); } |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 973 | |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 974 | CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_, |
| 975 | unsigned LsdaEncoding_) : Personality(Personality_), |
| 976 | PersonalityEncoding(PersonalityEncoding_), |
| 977 | LsdaEncoding(LsdaEncoding_) { |
| 978 | } |
| 979 | const MCSymbol* Personality; |
| 980 | unsigned PersonalityEncoding; |
| 981 | unsigned LsdaEncoding; |
| 982 | }; |
| 983 | } |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 984 | |
| 985 | namespace llvm { |
| 986 | template <> |
| 987 | struct DenseMapInfo<CIEKey> { |
| 988 | static CIEKey getEmptyKey() { |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 989 | return CIEKey::getEmptyKey(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 990 | } |
| 991 | static CIEKey getTombstoneKey() { |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 992 | return CIEKey::getTombstoneKey(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 993 | } |
| 994 | static unsigned getHashValue(const CIEKey &Key) { |
| 995 | FoldingSetNodeID ID; |
| 996 | ID.AddPointer(Key.Personality); |
| 997 | ID.AddInteger(Key.PersonalityEncoding); |
| 998 | ID.AddInteger(Key.LsdaEncoding); |
| 999 | return ID.ComputeHash(); |
| 1000 | } |
| 1001 | static bool isEqual(const CIEKey &LHS, |
| 1002 | const CIEKey &RHS) { |
| 1003 | return LHS.Personality == RHS.Personality && |
| 1004 | LHS.PersonalityEncoding == RHS.PersonalityEncoding && |
| 1005 | LHS.LsdaEncoding == RHS.LsdaEncoding; |
| 1006 | } |
| 1007 | }; |
| 1008 | } |
| 1009 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1010 | void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, |
| 1011 | bool UsingCFI, |
| 1012 | bool IsEH) { |
| 1013 | MCContext &Context = Streamer.getContext(); |
| 1014 | const TargetAsmInfo &AsmInfo = Context.getTargetAsmInfo(); |
| 1015 | const MCSection &Section = IsEH ? *AsmInfo.getEHFrameSection() : |
| 1016 | *AsmInfo.getDwarfFrameSection(); |
| 1017 | Streamer.SwitchSection(&Section); |
| 1018 | MCSymbol *SectionStart = Context.CreateTempSymbol(); |
| 1019 | Streamer.EmitLabel(SectionStart); |
Rafael Espindola | 9099813 | 2011-04-29 02:42:28 +0000 | [diff] [blame] | 1020 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1021 | MCSymbol *FDEEnd = NULL; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1022 | DenseMap<CIEKey, const MCSymbol*> CIEStarts; |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1023 | FrameEmitterImpl Emitter(UsingCFI, IsEH, SectionStart); |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 1024 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1025 | const MCSymbol *DummyDebugKey = NULL; |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1026 | for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) { |
| 1027 | const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i); |
| 1028 | if (IsEH && AsmInfo.getCompactUnwindSection() && |
Bill Wendling | 8440fe2 | 2011-07-13 00:49:09 +0000 | [diff] [blame] | 1029 | Emitter.EmitCompactUnwind(Streamer, Frame)) { |
| 1030 | FDEEnd = NULL; |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1031 | continue; |
Bill Wendling | 8440fe2 | 2011-07-13 00:49:09 +0000 | [diff] [blame] | 1032 | } |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1033 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1034 | CIEKey Key(Frame.Personality, Frame.PersonalityEncoding, |
| 1035 | Frame.LsdaEncoding); |
| 1036 | const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey; |
| 1037 | if (!CIEStart) |
| 1038 | CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality, |
| 1039 | Frame.PersonalityEncoding, Frame.Lsda, |
| 1040 | Frame.LsdaEncoding); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1041 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1042 | FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1043 | |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1044 | if (i != n - 1) |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1045 | Streamer.EmitLabel(FDEEnd); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1046 | } |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 1047 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1048 | Streamer.EmitValueToAlignment(AsmInfo.getPointerSize()); |
| 1049 | if (FDEEnd) |
| 1050 | Streamer.EmitLabel(FDEEnd); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1051 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1052 | |
| 1053 | void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer, |
| 1054 | uint64_t AddrDelta) { |
| 1055 | SmallString<256> Tmp; |
| 1056 | raw_svector_ostream OS(Tmp); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1057 | MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1058 | Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0); |
| 1059 | } |
| 1060 | |
| 1061 | void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta, |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1062 | raw_ostream &OS) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1063 | // FIXME: Assumes the code alignment factor is 1. |
Rafael Espindola | 3b78cdc | 2010-12-28 23:38:03 +0000 | [diff] [blame] | 1064 | if (AddrDelta == 0) { |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1065 | } else if (isUIntN(6, AddrDelta)) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1066 | uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta; |
| 1067 | OS << Opcode; |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1068 | } else if (isUInt<8>(AddrDelta)) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1069 | OS << uint8_t(dwarf::DW_CFA_advance_loc1); |
| 1070 | OS << uint8_t(AddrDelta); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1071 | } else if (isUInt<16>(AddrDelta)) { |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1072 | // FIXME: check what is the correct behavior on a big endian machine. |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1073 | OS << uint8_t(dwarf::DW_CFA_advance_loc2); |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1074 | OS << uint8_t( AddrDelta & 0xff); |
| 1075 | OS << uint8_t((AddrDelta >> 8) & 0xff); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1076 | } else { |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1077 | // FIXME: check what is the correct behavior on a big endian machine. |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1078 | assert(isUInt<32>(AddrDelta)); |
| 1079 | OS << uint8_t(dwarf::DW_CFA_advance_loc4); |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1080 | OS << uint8_t( AddrDelta & 0xff); |
| 1081 | OS << uint8_t((AddrDelta >> 8) & 0xff); |
| 1082 | OS << uint8_t((AddrDelta >> 16) & 0xff); |
| 1083 | OS << uint8_t((AddrDelta >> 24) & 0xff); |
| 1084 | |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |