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 | |
| 10 | #include "llvm/MC/MCDwarf.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAssembler.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" |
| 17 | #include "llvm/ADT/SmallString.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetAsmBackend.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 23 | // Given a special op, return the address skip amount (in units of |
| 24 | // DWARF2_LINE_MIN_INSN_LENGTH. |
| 25 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
| 26 | |
| 27 | // The maximum address skip amount that can be encoded with a special op. |
| 28 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
| 29 | |
| 30 | // First special line opcode - leave room for the standard opcodes. |
| 31 | // Note: If you want to change this, you'll have to update the |
| 32 | // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). |
| 33 | #define DWARF2_LINE_OPCODE_BASE 13 |
| 34 | |
| 35 | // Minimum line offset in a special line info. opcode. This value |
| 36 | // was chosen to give a reasonable range of values. |
| 37 | #define DWARF2_LINE_BASE -5 |
| 38 | |
| 39 | // Range of line offsets in a special line info. opcode. |
| 40 | # define DWARF2_LINE_RANGE 14 |
| 41 | |
| 42 | // Define the architecture-dependent minimum instruction length (in bytes). |
| 43 | // This value should be rather too small than too big. |
| 44 | # define DWARF2_LINE_MIN_INSN_LENGTH 1 |
| 45 | |
| 46 | // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting, |
| 47 | // this routine is a nop and will be optimized away. |
| 48 | static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) |
| 49 | { |
| 50 | if (DWARF2_LINE_MIN_INSN_LENGTH == 1) |
| 51 | return AddrDelta; |
| 52 | if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) { |
| 53 | // TODO: report this error, but really only once. |
| 54 | ; |
| 55 | } |
| 56 | return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH; |
| 57 | } |
| 58 | |
| 59 | // |
| 60 | // This is called when an instruction is assembled into the specified section |
| 61 | // and if there is information from the last .loc directive that has yet to have |
| 62 | // a line entry made for it is made. |
| 63 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 64 | void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 65 | if (!MCOS->getContext().getDwarfLocSeen()) |
| 66 | return; |
| 67 | |
| 68 | // Create a symbol at in the current section for use in the line entry. |
| 69 | MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol(); |
| 70 | // Set the value of the symbol to use for the MCLineEntry. |
| 71 | MCOS->EmitLabel(LineSym); |
| 72 | |
| 73 | // Get the current .loc info saved in the context. |
| 74 | const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc(); |
| 75 | |
| 76 | // Create a (local) line entry with the symbol and the current .loc info. |
| 77 | MCLineEntry LineEntry(LineSym, DwarfLoc); |
| 78 | |
| 79 | // clear DwarfLocSeen saying the current .loc info is now used. |
Kevin Enderby | 3f55c24 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 80 | MCOS->getContext().ClearDwarfLocSeen(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 81 | |
| 82 | // Get the MCLineSection for this section, if one does not exist for this |
| 83 | // section create it. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 84 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 85 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 86 | MCLineSection *LineSection = MCLineSections.lookup(Section); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 87 | if (!LineSection) { |
| 88 | // Create a new MCLineSection. This will be deleted after the dwarf line |
| 89 | // table is created using it by iterating through the MCLineSections |
| 90 | // DenseMap. |
| 91 | LineSection = new MCLineSection; |
| 92 | // Save a pointer to the new LineSection into the MCLineSections DenseMap. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 93 | MCOS->getContext().addMCLineSection(Section, LineSection); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Add the line entry to this section's entries. |
| 97 | LineSection->addLineEntry(LineEntry); |
| 98 | } |
| 99 | |
| 100 | // |
| 101 | // This helper routine returns an expression of End - Start + IntVal . |
| 102 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 103 | static inline const MCExpr *MakeStartMinusEndExpr(MCStreamer *MCOS, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 104 | MCSymbol *Start, |
| 105 | MCSymbol *End, int IntVal) { |
| 106 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 107 | const MCExpr *Res = |
| 108 | MCSymbolRefExpr::Create(End, Variant, MCOS->getContext()); |
| 109 | const MCExpr *RHS = |
| 110 | MCSymbolRefExpr::Create(Start, Variant, MCOS->getContext()); |
| 111 | const MCExpr *Res1 = |
| 112 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS->getContext()); |
| 113 | const MCExpr *Res2 = |
| 114 | MCConstantExpr::Create(IntVal, MCOS->getContext()); |
| 115 | const MCExpr *Res3 = |
| 116 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS->getContext()); |
| 117 | return Res3; |
| 118 | } |
| 119 | |
| 120 | // |
| 121 | // This emits an "absolute" address used in the start of a dwarf line number |
| 122 | // table. This will result in a relocatation entry for the address. |
| 123 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 124 | static inline void EmitDwarfSetAddress(MCStreamer *MCOS, |
| 125 | MCSymbol *Symbol, |
| 126 | int PointerSize) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 127 | MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1); |
| 128 | |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 129 | MCOS->EmitULEB128IntValue(PointerSize + 1); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 130 | |
| 131 | MCOS->EmitIntValue(dwarf::DW_LNE_set_address, 1); |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 132 | MCOS->EmitSymbolValue(Symbol, PointerSize); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // |
| 136 | // This emits the Dwarf line table for the specified section from the entries |
| 137 | // in the LineSection. |
| 138 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 139 | static inline void EmitDwarfLineTable(MCStreamer *MCOS, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 140 | const MCSection *Section, |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 141 | const MCLineSection *LineSection, |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 142 | const MCSection *DwarfLineSection, |
| 143 | MCSectionData *DLS, |
| 144 | int PointerSize) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 145 | unsigned FileNum = 1; |
| 146 | unsigned LastLine = 1; |
| 147 | unsigned Column = 0; |
| 148 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 149 | unsigned Isa = 0; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 150 | MCSymbol *LastLabel = NULL; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 151 | |
| 152 | // Loop through each MCLineEntry and encode the dwarf line number table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 153 | for (MCLineSection::const_iterator |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 154 | it = LineSection->getMCLineEntries()->begin(), |
| 155 | ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) { |
| 156 | |
| 157 | if (FileNum != it->getFileNum()) { |
| 158 | FileNum = it->getFileNum(); |
| 159 | MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 160 | MCOS->EmitULEB128IntValue(FileNum); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 161 | } |
| 162 | if (Column != it->getColumn()) { |
| 163 | Column = it->getColumn(); |
| 164 | MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 165 | MCOS->EmitULEB128IntValue(Column); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 166 | } |
| 167 | if (Isa != it->getIsa()) { |
| 168 | Isa = it->getIsa(); |
| 169 | MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 170 | MCOS->EmitULEB128IntValue(Isa); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 171 | } |
| 172 | if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) { |
| 173 | Flags = it->getFlags(); |
| 174 | MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1); |
| 175 | } |
| 176 | if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK) |
| 177 | MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1); |
| 178 | if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END) |
| 179 | MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1); |
| 180 | if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN) |
| 181 | MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1); |
| 182 | |
Rafael Espindola | 64185cc | 2010-11-13 01:06:27 +0000 | [diff] [blame] | 183 | int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 184 | MCSymbol *Label = it->getLabel(); |
| 185 | |
| 186 | // At this point we want to emit/create the sequence to encode the delta in |
| 187 | // line numbers and the increment of the address from the previous Label |
| 188 | // and the current Label. |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 189 | if (LastLabel == NULL || DLS == NULL) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 190 | // emit the sequence to set the address |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 191 | EmitDwarfSetAddress(MCOS, Label, PointerSize); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 192 | // emit the sequence for the LineDelta (from 1) and a zero address delta. |
| 193 | MCDwarfLineAddr::Emit(MCOS, LineDelta, 0); |
| 194 | } |
| 195 | else { |
| 196 | // Create an expression for the address delta from the LastLabel and |
| 197 | // this Label (plus 0). |
| 198 | const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, Label,0); |
| 199 | // Create a Dwarf Line fragment for the LineDelta and AddrDelta. |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 200 | new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, DLS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | LastLine = it->getLine(); |
| 204 | LastLabel = Label; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Emit a DW_LNE_end_sequence for the end of the section. |
| 208 | // Using the pointer Section create a temporary label at the end of the |
| 209 | // section and use that and the LastLabel to compute the address delta |
| 210 | // and use INT64_MAX as the line delta which is the signal that this is |
| 211 | // actually a DW_LNE_end_sequence. |
| 212 | |
| 213 | // Switch to the section to be able to create a symbol at its end. |
| 214 | MCOS->SwitchSection(Section); |
| 215 | // Create a symbol at the end of the section. |
| 216 | MCSymbol *SectionEnd = MCOS->getContext().CreateTempSymbol(); |
| 217 | // Set the value of the symbol, as we are at the end of the section. |
| 218 | MCOS->EmitLabel(SectionEnd); |
| 219 | |
| 220 | // Switch back the the dwarf line section. |
| 221 | MCOS->SwitchSection(DwarfLineSection); |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 222 | |
| 223 | if (DLS == NULL) { |
| 224 | // emit the sequence to set the address |
| 225 | EmitDwarfSetAddress(MCOS, SectionEnd, PointerSize); |
| 226 | // emit the sequence for the LineDelta (from 1) and a zero address delta. |
| 227 | MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0); |
| 228 | } else { |
| 229 | // Create an expression for the address delta from the LastLabel and this |
| 230 | // SectionEnd label. |
| 231 | const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, SectionEnd, |
| 232 | 0); |
| 233 | // Create a Dwarf Line fragment for the LineDelta and AddrDelta. |
| 234 | new MCDwarfLineAddrFragment(INT64_MAX, *AddrDelta, DLS); |
| 235 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // |
| 239 | // This emits the Dwarf file and the line tables. |
| 240 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 241 | void MCDwarfFileTable::Emit(MCStreamer *MCOS, |
| 242 | const MCSection *DwarfLineSection, |
| 243 | MCSectionData *DLS, |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame^] | 244 | int PointerSize, |
| 245 | const MCSection *TextSection) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 246 | // Switch to the section where the table will be emitted into. |
| 247 | MCOS->SwitchSection(DwarfLineSection); |
| 248 | |
| 249 | // Create a symbol at the beginning of this section. |
| 250 | MCSymbol *LineStartSym = MCOS->getContext().CreateTempSymbol(); |
| 251 | // Set the value of the symbol, as we are at the start of the section. |
| 252 | MCOS->EmitLabel(LineStartSym); |
| 253 | |
| 254 | // Create a symbol for the end of the section (to be set when we get there). |
| 255 | MCSymbol *LineEndSym = MCOS->getContext().CreateTempSymbol(); |
| 256 | |
| 257 | // The first 4 bytes is the total length of the information for this |
| 258 | // compilation unit (not including these 4 bytes for the length). |
| 259 | MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, LineEndSym, 4), |
Devang Patel | ee4854f | 2010-12-02 21:32:30 +0000 | [diff] [blame] | 260 | 4, 0, true /*UseSet*/); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 261 | |
| 262 | // Next 2 bytes is the Version, which is Dwarf 2. |
| 263 | MCOS->EmitIntValue(2, 2); |
| 264 | |
| 265 | // Create a symbol for the end of the prologue (to be set when we get there). |
| 266 | MCSymbol *ProEndSym = MCOS->getContext().CreateTempSymbol(); // Lprologue_end |
| 267 | |
| 268 | // Length of the prologue, is the next 4 bytes. Which is the start of the |
| 269 | // section to the end of the prologue. Not including the 4 bytes for the |
| 270 | // total length, the 2 bytes for the version, and these 4 bytes for the |
| 271 | // length of the prologue. |
| 272 | MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, ProEndSym, |
| 273 | (4 + 2 + 4)), |
Devang Patel | ee4854f | 2010-12-02 21:32:30 +0000 | [diff] [blame] | 274 | 4, 0, true /*UseSet*/); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 275 | |
| 276 | // Parameters of the state machine, are next. |
| 277 | MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1); |
| 278 | MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); |
| 279 | MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); |
| 280 | MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); |
| 281 | MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1); |
| 282 | |
| 283 | // Standard opcode lengths |
| 284 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy |
| 285 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc |
| 286 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line |
| 287 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file |
| 288 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column |
| 289 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt |
| 290 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block |
| 291 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc |
| 292 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc |
| 293 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end |
| 294 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin |
| 295 | MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa |
| 296 | |
| 297 | // Put out the directory and file tables. |
| 298 | |
| 299 | // First the directory table. |
| 300 | const std::vector<StringRef> &MCDwarfDirs = |
| 301 | MCOS->getContext().getMCDwarfDirs(); |
| 302 | for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { |
| 303 | MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName |
| 304 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
| 305 | } |
| 306 | MCOS->EmitIntValue(0, 1); // Terminate the directory list |
| 307 | |
| 308 | // Second the file table. |
| 309 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 310 | MCOS->getContext().getMCDwarfFiles(); |
| 311 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
| 312 | MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName |
| 313 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 314 | // the Directory num |
| 315 | MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 316 | MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0) |
| 317 | MCOS->EmitIntValue(0, 1); // filesize (always 0) |
| 318 | } |
| 319 | MCOS->EmitIntValue(0, 1); // Terminate the file list |
| 320 | |
| 321 | // This is the end of the prologue, so set the value of the symbol at the |
| 322 | // end of the prologue (that was used in a previous expression). |
| 323 | MCOS->EmitLabel(ProEndSym); |
| 324 | |
| 325 | // Put out the line tables. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 326 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 327 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 328 | const std::vector<const MCSection *> &MCLineSectionOrder = |
| 329 | MCOS->getContext().getMCLineSectionOrder(); |
| 330 | for (std::vector<const MCSection*>::const_iterator it = |
| 331 | MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie; |
| 332 | ++it) { |
| 333 | const MCSection *Sec = *it; |
| 334 | const MCLineSection *Line = MCLineSections.lookup(Sec); |
| 335 | EmitDwarfLineTable(MCOS, Sec, Line, DwarfLineSection, DLS, |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 336 | PointerSize); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 337 | |
| 338 | // Now delete the MCLineSections that were created in MCLineEntry::Make() |
| 339 | // and used to emit the line table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 340 | delete Line; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame^] | 343 | if (TextSection && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) { |
| 344 | // Emit dummy entry if line table is empty. |
| 345 | |
| 346 | MCOS->SwitchSection(TextSection); |
| 347 | MCSymbol *SectionEnd = MCOS->getContext().CreateTempSymbol(); |
| 348 | // Set the value of the symbol, as we are at the end of the section. |
| 349 | MCOS->EmitLabel(SectionEnd); |
| 350 | |
| 351 | // Switch back the the dwarf line section. |
| 352 | MCOS->SwitchSection(DwarfLineSection); |
| 353 | |
| 354 | // emit the sequence to set the address |
| 355 | EmitDwarfSetAddress(MCOS, SectionEnd, PointerSize); |
| 356 | // emit the sequence for the LineDelta (from 1) and a zero address delta. |
| 357 | MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0); |
| 358 | } |
| 359 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 360 | // This is the end of the section, so set the value of the symbol at the end |
| 361 | // of this section (that was used in a previous expression). |
| 362 | MCOS->EmitLabel(LineEndSym); |
| 363 | } |
| 364 | |
| 365 | /// Utility function to compute the size of the encoding. |
| 366 | uint64_t MCDwarfLineAddr::ComputeSize(int64_t LineDelta, uint64_t AddrDelta) { |
| 367 | SmallString<256> Tmp; |
| 368 | raw_svector_ostream OS(Tmp); |
| 369 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 370 | return OS.GetNumBytesInBuffer(); |
| 371 | } |
| 372 | |
| 373 | /// Utility function to write the encoding to an object writer. |
| 374 | void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta, |
| 375 | uint64_t AddrDelta) { |
| 376 | SmallString<256> Tmp; |
| 377 | raw_svector_ostream OS(Tmp); |
| 378 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 379 | OW->WriteBytes(OS.str()); |
| 380 | } |
| 381 | |
| 382 | /// Utility function to emit the encoding to a streamer. |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 383 | void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 384 | uint64_t AddrDelta) { |
| 385 | SmallString<256> Tmp; |
| 386 | raw_svector_ostream OS(Tmp); |
| 387 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 388 | MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0); |
| 389 | } |
| 390 | |
| 391 | /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. |
| 392 | void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta, |
| 393 | raw_ostream &OS) { |
| 394 | uint64_t Temp, Opcode; |
| 395 | bool NeedCopy = false; |
| 396 | |
| 397 | // Scale the address delta by the minimum instruction length. |
| 398 | AddrDelta = ScaleAddrDelta(AddrDelta); |
| 399 | |
| 400 | // A LineDelta of INT64_MAX is a signal that this is actually a |
| 401 | // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the |
| 402 | // end_sequence to emit the matrix entry. |
| 403 | if (LineDelta == INT64_MAX) { |
| 404 | if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) |
| 405 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 406 | else { |
| 407 | OS << char(dwarf::DW_LNS_advance_pc); |
| 408 | SmallString<32> Tmp; |
| 409 | raw_svector_ostream OSE(Tmp); |
| 410 | MCObjectWriter::EncodeULEB128(AddrDelta, OSE); |
| 411 | OS << OSE.str(); |
| 412 | } |
| 413 | OS << char(dwarf::DW_LNS_extended_op); |
| 414 | OS << char(1); |
| 415 | OS << char(dwarf::DW_LNE_end_sequence); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | // Bias the line delta by the base. |
| 420 | Temp = LineDelta - DWARF2_LINE_BASE; |
| 421 | |
| 422 | // If the line increment is out of range of a special opcode, we must encode |
| 423 | // it with DW_LNS_advance_line. |
| 424 | if (Temp >= DWARF2_LINE_RANGE) { |
| 425 | OS << char(dwarf::DW_LNS_advance_line); |
| 426 | SmallString<32> Tmp; |
| 427 | raw_svector_ostream OSE(Tmp); |
| 428 | MCObjectWriter::EncodeSLEB128(LineDelta, OSE); |
| 429 | OS << OSE.str(); |
| 430 | |
| 431 | LineDelta = 0; |
| 432 | Temp = 0 - DWARF2_LINE_BASE; |
| 433 | NeedCopy = true; |
| 434 | } |
| 435 | |
| 436 | // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode. |
| 437 | if (LineDelta == 0 && AddrDelta == 0) { |
| 438 | OS << char(dwarf::DW_LNS_copy); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | // Bias the opcode by the special opcode base. |
| 443 | Temp += DWARF2_LINE_OPCODE_BASE; |
| 444 | |
| 445 | // Avoid overflow when addr_delta is large. |
| 446 | if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) { |
| 447 | // Try using a special opcode. |
| 448 | Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE; |
| 449 | if (Opcode <= 255) { |
| 450 | OS << char(Opcode); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | // Try using DW_LNS_const_add_pc followed by special op. |
| 455 | Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
| 456 | if (Opcode <= 255) { |
| 457 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 458 | OS << char(Opcode); |
| 459 | return; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // Otherwise use DW_LNS_advance_pc. |
| 464 | OS << char(dwarf::DW_LNS_advance_pc); |
| 465 | SmallString<32> Tmp; |
| 466 | raw_svector_ostream OSE(Tmp); |
| 467 | MCObjectWriter::EncodeULEB128(AddrDelta, OSE); |
| 468 | OS << OSE.str(); |
| 469 | |
| 470 | if (NeedCopy) |
| 471 | OS << char(dwarf::DW_LNS_copy); |
| 472 | else |
| 473 | OS << char(Temp); |
| 474 | } |
| 475 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 476 | void MCDwarfFile::print(raw_ostream &OS) const { |
| 477 | OS << '"' << getName() << '"'; |
| 478 | } |
| 479 | |
| 480 | void MCDwarfFile::dump() const { |
| 481 | print(dbgs()); |
| 482 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 483 | |