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 | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/FoldingSet.h" |
Rafael Espindola | 767b1be | 2010-12-04 00:31:13 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCDwarf.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAssembler.h" |
Rafael Espindola | ad8aaa0 | 2010-11-22 11:53:17 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCStreamer.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSymbol.h" |
| 16 | #include "llvm/MC/MCExpr.h" |
| 17 | #include "llvm/MC/MCContext.h" |
| 18 | #include "llvm/MC/MCObjectWriter.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetAsmBackend.h" |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetAsmInfo.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 26 | // Given a special op, return the address skip amount (in units of |
| 27 | // DWARF2_LINE_MIN_INSN_LENGTH. |
| 28 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
| 29 | |
| 30 | // The maximum address skip amount that can be encoded with a special op. |
| 31 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
| 32 | |
| 33 | // First special line opcode - leave room for the standard opcodes. |
| 34 | // Note: If you want to change this, you'll have to update the |
| 35 | // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). |
| 36 | #define DWARF2_LINE_OPCODE_BASE 13 |
| 37 | |
| 38 | // Minimum line offset in a special line info. opcode. This value |
| 39 | // was chosen to give a reasonable range of values. |
| 40 | #define DWARF2_LINE_BASE -5 |
| 41 | |
| 42 | // Range of line offsets in a special line info. opcode. |
| 43 | # define DWARF2_LINE_RANGE 14 |
| 44 | |
| 45 | // Define the architecture-dependent minimum instruction length (in bytes). |
| 46 | // This value should be rather too small than too big. |
| 47 | # define DWARF2_LINE_MIN_INSN_LENGTH 1 |
| 48 | |
| 49 | // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting, |
| 50 | // this routine is a nop and will be optimized away. |
| 51 | static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) |
| 52 | { |
| 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. |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 175 | MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 176 | |
| 177 | LastLine = it->getLine(); |
| 178 | LastLabel = Label; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Emit a DW_LNE_end_sequence for the end of the section. |
| 182 | // Using the pointer Section create a temporary label at the end of the |
| 183 | // section and use that and the LastLabel to compute the address delta |
| 184 | // and use INT64_MAX as the line delta which is the signal that this is |
| 185 | // actually a DW_LNE_end_sequence. |
| 186 | |
| 187 | // Switch to the section to be able to create a symbol at its end. |
| 188 | MCOS->SwitchSection(Section); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 189 | |
| 190 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 191 | // Create a symbol at the end of the section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 192 | MCSymbol *SectionEnd = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 193 | // Set the value of the symbol, as we are at the end of the section. |
| 194 | MCOS->EmitLabel(SectionEnd); |
| 195 | |
| 196 | // Switch back the the dwarf line section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 197 | MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection()); |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 198 | |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 199 | MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // |
| 203 | // This emits the Dwarf file and the line tables. |
| 204 | // |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 205 | void MCDwarfFileTable::Emit(MCStreamer *MCOS) { |
| 206 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 207 | // Switch to the section where the table will be emitted into. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 208 | MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 209 | |
| 210 | // Create a symbol at the beginning of this section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 211 | MCSymbol *LineStartSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 212 | // Set the value of the symbol, as we are at the start of the section. |
| 213 | MCOS->EmitLabel(LineStartSym); |
| 214 | |
| 215 | // 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] | 216 | MCSymbol *LineEndSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 217 | |
| 218 | // The first 4 bytes is the total length of the information for this |
| 219 | // compilation unit (not including these 4 bytes for the length). |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 220 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4), |
Rafael Espindola | 0bbe0b4 | 2010-12-06 17:27:56 +0000 | [diff] [blame] | 221 | 4); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 222 | |
| 223 | // Next 2 bytes is the Version, which is Dwarf 2. |
| 224 | MCOS->EmitIntValue(2, 2); |
| 225 | |
| 226 | // 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] | 227 | MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 228 | |
| 229 | // Length of the prologue, is the next 4 bytes. Which is the start of the |
| 230 | // section to the end of the prologue. Not including the 4 bytes for the |
| 231 | // total length, the 2 bytes for the version, and these 4 bytes for the |
| 232 | // length of the prologue. |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 233 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 234 | (4 + 2 + 4)), |
Rafael Espindola | 5d4918d | 2010-12-04 03:21:47 +0000 | [diff] [blame] | 235 | 4, 0); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 236 | |
| 237 | // Parameters of the state machine, are next. |
| 238 | MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1); |
| 239 | MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); |
| 240 | MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); |
| 241 | MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); |
| 242 | MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1); |
| 243 | |
| 244 | // Standard opcode lengths |
| 245 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy |
| 246 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc |
| 247 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line |
| 248 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file |
| 249 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column |
| 250 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt |
| 251 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block |
| 252 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc |
| 253 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc |
| 254 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end |
| 255 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin |
| 256 | MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa |
| 257 | |
| 258 | // Put out the directory and file tables. |
| 259 | |
| 260 | // First the directory table. |
| 261 | const std::vector<StringRef> &MCDwarfDirs = |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 262 | context.getMCDwarfDirs(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 263 | for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { |
| 264 | MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName |
| 265 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
| 266 | } |
| 267 | MCOS->EmitIntValue(0, 1); // Terminate the directory list |
| 268 | |
| 269 | // Second the file table. |
| 270 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 271 | MCOS->getContext().getMCDwarfFiles(); |
| 272 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
| 273 | MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName |
| 274 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 275 | // the Directory num |
| 276 | MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 277 | MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0) |
| 278 | MCOS->EmitIntValue(0, 1); // filesize (always 0) |
| 279 | } |
| 280 | MCOS->EmitIntValue(0, 1); // Terminate the file list |
| 281 | |
| 282 | // This is the end of the prologue, so set the value of the symbol at the |
| 283 | // end of the prologue (that was used in a previous expression). |
| 284 | MCOS->EmitLabel(ProEndSym); |
| 285 | |
| 286 | // Put out the line tables. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 287 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 288 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 289 | const std::vector<const MCSection *> &MCLineSectionOrder = |
| 290 | MCOS->getContext().getMCLineSectionOrder(); |
| 291 | for (std::vector<const MCSection*>::const_iterator it = |
| 292 | MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie; |
| 293 | ++it) { |
| 294 | const MCSection *Sec = *it; |
| 295 | const MCLineSection *Line = MCLineSections.lookup(Sec); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 296 | EmitDwarfLineTable(MCOS, Sec, Line); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 297 | |
| 298 | // Now delete the MCLineSections that were created in MCLineEntry::Make() |
| 299 | // and used to emit the line table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 300 | delete Line; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Rafael Espindola | 767b1be | 2010-12-04 00:31:13 +0000 | [diff] [blame] | 303 | if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines() |
| 304 | && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) { |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 305 | // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures |
| 306 | // it requires: |
| 307 | // total_length >= prologue_length + 10 |
| 308 | // We are 4 bytes short, since we have total_length = 51 and |
| 309 | // prologue_length = 45 |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 310 | |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 311 | // The regular end_sequence should be sufficient. |
| 312 | MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0); |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 315 | // This is the end of the section, so set the value of the symbol at the end |
| 316 | // of this section (that was used in a previous expression). |
| 317 | MCOS->EmitLabel(LineEndSym); |
| 318 | } |
| 319 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 320 | /// Utility function to write the encoding to an object writer. |
| 321 | void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta, |
| 322 | uint64_t AddrDelta) { |
| 323 | SmallString<256> Tmp; |
| 324 | raw_svector_ostream OS(Tmp); |
| 325 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 326 | OW->WriteBytes(OS.str()); |
| 327 | } |
| 328 | |
| 329 | /// Utility function to emit the encoding to a streamer. |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 330 | void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 331 | uint64_t AddrDelta) { |
| 332 | SmallString<256> Tmp; |
| 333 | raw_svector_ostream OS(Tmp); |
| 334 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 335 | MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0); |
| 336 | } |
| 337 | |
| 338 | /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. |
| 339 | void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta, |
| 340 | raw_ostream &OS) { |
| 341 | uint64_t Temp, Opcode; |
| 342 | bool NeedCopy = false; |
| 343 | |
| 344 | // Scale the address delta by the minimum instruction length. |
| 345 | AddrDelta = ScaleAddrDelta(AddrDelta); |
| 346 | |
| 347 | // A LineDelta of INT64_MAX is a signal that this is actually a |
| 348 | // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the |
| 349 | // end_sequence to emit the matrix entry. |
| 350 | if (LineDelta == INT64_MAX) { |
| 351 | if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) |
| 352 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 353 | else { |
| 354 | OS << char(dwarf::DW_LNS_advance_pc); |
| 355 | SmallString<32> Tmp; |
| 356 | raw_svector_ostream OSE(Tmp); |
| 357 | MCObjectWriter::EncodeULEB128(AddrDelta, OSE); |
| 358 | OS << OSE.str(); |
| 359 | } |
| 360 | OS << char(dwarf::DW_LNS_extended_op); |
| 361 | OS << char(1); |
| 362 | OS << char(dwarf::DW_LNE_end_sequence); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | // Bias the line delta by the base. |
| 367 | Temp = LineDelta - DWARF2_LINE_BASE; |
| 368 | |
| 369 | // If the line increment is out of range of a special opcode, we must encode |
| 370 | // it with DW_LNS_advance_line. |
| 371 | if (Temp >= DWARF2_LINE_RANGE) { |
| 372 | OS << char(dwarf::DW_LNS_advance_line); |
| 373 | SmallString<32> Tmp; |
| 374 | raw_svector_ostream OSE(Tmp); |
| 375 | MCObjectWriter::EncodeSLEB128(LineDelta, OSE); |
| 376 | OS << OSE.str(); |
| 377 | |
| 378 | LineDelta = 0; |
| 379 | Temp = 0 - DWARF2_LINE_BASE; |
| 380 | NeedCopy = true; |
| 381 | } |
| 382 | |
| 383 | // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode. |
| 384 | if (LineDelta == 0 && AddrDelta == 0) { |
| 385 | OS << char(dwarf::DW_LNS_copy); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | // Bias the opcode by the special opcode base. |
| 390 | Temp += DWARF2_LINE_OPCODE_BASE; |
| 391 | |
| 392 | // Avoid overflow when addr_delta is large. |
| 393 | if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) { |
| 394 | // Try using a special opcode. |
| 395 | Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE; |
| 396 | if (Opcode <= 255) { |
| 397 | OS << char(Opcode); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | // Try using DW_LNS_const_add_pc followed by special op. |
| 402 | Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
| 403 | if (Opcode <= 255) { |
| 404 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 405 | OS << char(Opcode); |
| 406 | return; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Otherwise use DW_LNS_advance_pc. |
| 411 | OS << char(dwarf::DW_LNS_advance_pc); |
| 412 | SmallString<32> Tmp; |
| 413 | raw_svector_ostream OSE(Tmp); |
| 414 | MCObjectWriter::EncodeULEB128(AddrDelta, OSE); |
| 415 | OS << OSE.str(); |
| 416 | |
| 417 | if (NeedCopy) |
| 418 | OS << char(dwarf::DW_LNS_copy); |
| 419 | else |
| 420 | OS << char(Temp); |
| 421 | } |
| 422 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 423 | void MCDwarfFile::print(raw_ostream &OS) const { |
| 424 | OS << '"' << getName() << '"'; |
| 425 | } |
| 426 | |
| 427 | void MCDwarfFile::dump() const { |
| 428 | print(dbgs()); |
| 429 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 430 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 431 | static int getDataAlignmentFactor(MCStreamer &streamer) { |
| 432 | MCContext &context = streamer.getContext(); |
| 433 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
| 434 | int size = asmInfo.getPointerSize(); |
| 435 | if (asmInfo.getStackGrowthDirection() == TargetFrameInfo::StackGrowsUp) |
| 436 | return size; |
| 437 | else |
| 438 | return -size; |
| 439 | } |
| 440 | |
| 441 | /// EmitFrameMoves - Emit frame instructions to describe the layout of the |
| 442 | /// frame. |
| 443 | static void EmitFrameMoves(MCStreamer &streamer, |
| 444 | const std::vector<MachineMove> &Moves, |
| 445 | MCSymbol *BaseLabel, bool isEH) { |
| 446 | MCContext &context = streamer.getContext(); |
| 447 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
| 448 | int dataAlignmentFactor = getDataAlignmentFactor(streamer); |
| 449 | |
| 450 | for (unsigned i = 0, N = Moves.size(); i < N; ++i) { |
| 451 | const MachineMove &Move = Moves[i]; |
| 452 | MCSymbol *Label = Move.getLabel(); |
| 453 | // Throw out move if the label is invalid. |
| 454 | if (Label && !Label->isDefined()) continue; // Not emitted, in dead code. |
| 455 | |
| 456 | const MachineLocation &Dst = Move.getDestination(); |
| 457 | const MachineLocation &Src = Move.getSource(); |
| 458 | |
| 459 | // Advance row if new location. |
| 460 | if (BaseLabel && Label) { |
| 461 | MCSymbol *ThisSym = Label; |
| 462 | if (ThisSym != BaseLabel) { |
Rafael Espindola | 5bba084 | 2010-12-28 04:15:37 +0000 | [diff] [blame^] | 463 | // FIXME: We should relax this instead of using a DW_CFA_advance_loc4 |
| 464 | // for every address change! |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 465 | streamer.EmitIntValue(dwarf::DW_CFA_advance_loc4, 1); |
| 466 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *BaseLabel, |
Rafael Espindola | 5bba084 | 2010-12-28 04:15:37 +0000 | [diff] [blame^] | 467 | *ThisSym, 0); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 468 | streamer.EmitValue(Length, 4); |
| 469 | BaseLabel = ThisSym; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // If advancing cfa. |
| 474 | if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { |
| 475 | assert(!Src.isReg() && "Machine move not supported yet."); |
| 476 | |
| 477 | if (Src.getReg() == MachineLocation::VirtualFP) { |
| 478 | streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1); |
| 479 | } else { |
| 480 | streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 481 | streamer.EmitULEB128IntValue(asmInfo.getDwarfRegNum(Src.getReg(), |
| 482 | isEH)); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | streamer.EmitULEB128IntValue(-Src.getOffset(), 1); |
| 486 | continue; |
| 487 | } |
| 488 | |
| 489 | if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) { |
| 490 | assert(Dst.isReg() && "Machine move not supported yet."); |
| 491 | streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); |
| 492 | streamer.EmitULEB128IntValue(asmInfo.getDwarfRegNum(Dst.getReg(), isEH)); |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | unsigned Reg = asmInfo.getDwarfRegNum(Src.getReg(), isEH); |
| 497 | int Offset = Dst.getOffset() / dataAlignmentFactor; |
| 498 | |
| 499 | if (Offset < 0) { |
| 500 | streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1); |
| 501 | streamer.EmitULEB128IntValue(Reg); |
| 502 | streamer.EmitSLEB128IntValue(Offset); |
| 503 | } else if (Reg < 64) { |
| 504 | streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1); |
| 505 | streamer.EmitULEB128IntValue(Offset, 1); |
| 506 | } else { |
| 507 | streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1); |
| 508 | streamer.EmitULEB128IntValue(Reg, 1); |
| 509 | streamer.EmitULEB128IntValue(Offset, 1); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 514 | static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol, |
| 515 | unsigned symbolEncoding) { |
| 516 | MCContext &context = streamer.getContext(); |
| 517 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
| 518 | unsigned format = symbolEncoding & 0x0f; |
| 519 | unsigned application = symbolEncoding & 0xf0; |
| 520 | unsigned size; |
| 521 | switch (format) { |
| 522 | default: |
| 523 | assert(0 && "Unknown Encoding"); |
| 524 | break; |
| 525 | case dwarf::DW_EH_PE_absptr: |
| 526 | case dwarf::DW_EH_PE_signed: |
| 527 | size = asmInfo.getPointerSize(); |
| 528 | break; |
| 529 | case dwarf::DW_EH_PE_udata2: |
| 530 | case dwarf::DW_EH_PE_sdata2: |
| 531 | size = 2; |
| 532 | break; |
| 533 | case dwarf::DW_EH_PE_udata4: |
| 534 | case dwarf::DW_EH_PE_sdata4: |
| 535 | size = 4; |
| 536 | break; |
| 537 | case dwarf::DW_EH_PE_udata8: |
| 538 | case dwarf::DW_EH_PE_sdata8: |
| 539 | size = 8; |
| 540 | break; |
| 541 | } |
| 542 | switch (application) { |
| 543 | default: |
| 544 | assert(0 && "Unknown Encoding"); |
| 545 | break; |
| 546 | case 0: |
| 547 | case dwarf::DW_EH_PE_indirect: |
| 548 | streamer.EmitSymbolValue(&symbol, size); |
| 549 | break; |
| 550 | case dwarf::DW_EH_PE_pcrel: |
| 551 | streamer.EmitPCRelSymbolValue(&symbol, size); |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 556 | static const MCSymbol &EmitCIE(MCStreamer &streamer, |
Rafael Espindola | 3a83c40 | 2010-12-27 00:36:05 +0000 | [diff] [blame] | 557 | const MCSymbol *personality, |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 558 | unsigned personalityEncoding, |
| 559 | const MCSymbol *lsda, |
| 560 | unsigned lsdaEncoding) { |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 561 | MCContext &context = streamer.getContext(); |
| 562 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
| 563 | const MCSection §ion = *asmInfo.getEHFrameSection(); |
| 564 | streamer.SwitchSection(§ion); |
| 565 | MCSymbol *sectionStart = streamer.getContext().CreateTempSymbol(); |
| 566 | MCSymbol *sectionEnd = streamer.getContext().CreateTempSymbol(); |
| 567 | |
| 568 | // Length |
| 569 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart, |
| 570 | *sectionEnd, 4); |
| 571 | streamer.EmitLabel(sectionStart); |
| 572 | streamer.EmitValue(Length, 4); |
| 573 | |
| 574 | // CIE ID |
| 575 | streamer.EmitIntValue(0, 4); |
| 576 | |
| 577 | // Version |
| 578 | streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1); |
| 579 | |
| 580 | // Augmentation String |
| 581 | SmallString<8> Augmentation; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 582 | Augmentation += "z"; |
| 583 | if (personality) |
| 584 | Augmentation += "P"; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 585 | if (lsda) |
| 586 | Augmentation += "L"; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 587 | Augmentation += "R"; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 588 | streamer.EmitBytes(Augmentation.str(), 0); |
| 589 | streamer.EmitIntValue(0, 1); |
| 590 | |
| 591 | // Code Alignment Factor |
| 592 | streamer.EmitULEB128IntValue(1); |
| 593 | |
| 594 | // Data Alignment Factor |
| 595 | streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer)); |
| 596 | |
| 597 | // Return Address Register |
| 598 | streamer.EmitULEB128IntValue(asmInfo.getDwarfRARegNum(true)); |
| 599 | |
| 600 | // Augmentation Data Length (optional) |
| 601 | MCSymbol *augmentationStart = streamer.getContext().CreateTempSymbol(); |
| 602 | MCSymbol *augmentationEnd = streamer.getContext().CreateTempSymbol(); |
| 603 | const MCExpr *augmentationLength = MakeStartMinusEndExpr(streamer, |
| 604 | *augmentationStart, |
| 605 | *augmentationEnd, 0); |
| 606 | streamer.EmitULEB128Value(augmentationLength); |
| 607 | |
| 608 | // Augmentation Data (optional) |
| 609 | streamer.EmitLabel(augmentationStart); |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 610 | if (personality) { |
| 611 | // Personality Encoding |
Rafael Espindola | 3a83c40 | 2010-12-27 00:36:05 +0000 | [diff] [blame] | 612 | streamer.EmitIntValue(personalityEncoding, 1); |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 613 | // Personality |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 614 | EmitSymbol(streamer, *personality, personalityEncoding); |
| 615 | } |
| 616 | if (lsda) { |
| 617 | // LSDA Encoding |
| 618 | streamer.EmitIntValue(lsdaEncoding, 1); |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 619 | } |
| 620 | // Encoding of the FDE pointers |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 621 | streamer.EmitIntValue(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4, 1); |
| 622 | streamer.EmitLabel(augmentationEnd); |
| 623 | |
| 624 | // Initial Instructions |
| 625 | |
| 626 | const std::vector<MachineMove> Moves = asmInfo.getInitialFrameState(); |
| 627 | |
| 628 | EmitFrameMoves(streamer, Moves, NULL, true); |
| 629 | |
| 630 | // Padding |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 631 | streamer.EmitValueToAlignment(4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 632 | |
| 633 | streamer.EmitLabel(sectionEnd); |
| 634 | return *sectionStart; |
| 635 | } |
| 636 | |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 637 | static MCSymbol *EmitFDE(MCStreamer &streamer, |
| 638 | const MCSymbol &cieStart, |
| 639 | const MCDwarfFrameInfo &frame) { |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 640 | MCContext &context = streamer.getContext(); |
| 641 | MCSymbol *fdeStart = context.CreateTempSymbol(); |
| 642 | MCSymbol *fdeEnd = context.CreateTempSymbol(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 643 | |
| 644 | // Length |
| 645 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0); |
| 646 | streamer.EmitValue(Length, 4); |
| 647 | |
| 648 | streamer.EmitLabel(fdeStart); |
| 649 | // CIE Pointer |
| 650 | const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart, |
| 651 | 0); |
| 652 | streamer.EmitValue(offset, 4); |
| 653 | |
| 654 | // PC Begin |
| 655 | streamer.EmitPCRelSymbolValue(frame.Begin, 4); |
| 656 | |
| 657 | // PC Range |
| 658 | const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin, |
| 659 | *frame.End, 0); |
| 660 | streamer.EmitValue(Range, 4); |
| 661 | |
| 662 | // Augmentation Data Length |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 663 | MCSymbol *augmentationStart = streamer.getContext().CreateTempSymbol(); |
| 664 | MCSymbol *augmentationEnd = streamer.getContext().CreateTempSymbol(); |
| 665 | const MCExpr *augmentationLength = MakeStartMinusEndExpr(streamer, |
| 666 | *augmentationStart, |
| 667 | *augmentationEnd, 0); |
| 668 | streamer.EmitULEB128Value(augmentationLength); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 669 | |
| 670 | // Augmentation Data |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 671 | streamer.EmitLabel(augmentationStart); |
| 672 | if (frame.Lsda) |
| 673 | EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding); |
| 674 | streamer.EmitLabel(augmentationEnd); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 675 | // Call Frame Instructions |
| 676 | |
Rafael Espindola | 5bba084 | 2010-12-28 04:15:37 +0000 | [diff] [blame^] | 677 | EmitFrameMoves(streamer, frame.Moves, frame.Begin, true); |
| 678 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 679 | // Padding |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 680 | streamer.EmitValueToAlignment(4); |
| 681 | |
| 682 | return fdeEnd; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 685 | struct CIEKey { |
| 686 | static const CIEKey EmptyKey; |
| 687 | static const CIEKey TombstoneKey; |
| 688 | |
| 689 | CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_, |
| 690 | unsigned LsdaEncoding_) : Personality(Personality_), |
| 691 | PersonalityEncoding(PersonalityEncoding_), |
| 692 | LsdaEncoding(LsdaEncoding_) { |
| 693 | } |
| 694 | const MCSymbol* Personality; |
| 695 | unsigned PersonalityEncoding; |
| 696 | unsigned LsdaEncoding; |
| 697 | }; |
| 698 | |
| 699 | const CIEKey CIEKey::EmptyKey(0, 0, -1); |
| 700 | const CIEKey CIEKey::TombstoneKey(0, -1, 0); |
| 701 | |
| 702 | namespace llvm { |
| 703 | template <> |
| 704 | struct DenseMapInfo<CIEKey> { |
| 705 | static CIEKey getEmptyKey() { |
| 706 | return CIEKey::EmptyKey; |
| 707 | } |
| 708 | static CIEKey getTombstoneKey() { |
| 709 | return CIEKey::TombstoneKey; |
| 710 | } |
| 711 | static unsigned getHashValue(const CIEKey &Key) { |
| 712 | FoldingSetNodeID ID; |
| 713 | ID.AddPointer(Key.Personality); |
| 714 | ID.AddInteger(Key.PersonalityEncoding); |
| 715 | ID.AddInteger(Key.LsdaEncoding); |
| 716 | return ID.ComputeHash(); |
| 717 | } |
| 718 | static bool isEqual(const CIEKey &LHS, |
| 719 | const CIEKey &RHS) { |
| 720 | return LHS.Personality == RHS.Personality && |
| 721 | LHS.PersonalityEncoding == RHS.PersonalityEncoding && |
| 722 | LHS.LsdaEncoding == RHS.LsdaEncoding; |
| 723 | } |
| 724 | }; |
| 725 | } |
| 726 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 727 | void MCDwarfFrameEmitter::Emit(MCStreamer &streamer) { |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 728 | const MCContext &context = streamer.getContext(); |
| 729 | const TargetAsmInfo &asmInfo = context.getTargetAsmInfo(); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 730 | MCSymbol *fdeEnd = NULL; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 731 | DenseMap<CIEKey, const MCSymbol*> CIEStarts; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 732 | |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 733 | for (unsigned i = 0, n = streamer.getNumFrameInfos(); i < n; ++i) { |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 734 | const MCDwarfFrameInfo &frame = streamer.getFrameInfo(i); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 735 | CIEKey key(frame.Personality, frame.PersonalityEncoding, |
| 736 | frame.LsdaEncoding); |
| 737 | const MCSymbol *&cieStart = CIEStarts[key]; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 738 | if (!cieStart) |
Rafael Espindola | 3a83c40 | 2010-12-27 00:36:05 +0000 | [diff] [blame] | 739 | cieStart = &EmitCIE(streamer, frame.Personality, |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 740 | frame.PersonalityEncoding, frame.Lsda, |
| 741 | frame.LsdaEncoding); |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 742 | fdeEnd = EmitFDE(streamer, *cieStart, frame); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 743 | if (i != n - 1) |
| 744 | streamer.EmitLabel(fdeEnd); |
| 745 | } |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 746 | |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 747 | streamer.EmitValueToAlignment(asmInfo.getPointerSize()); |
| 748 | if (fdeEnd) |
| 749 | streamer.EmitLabel(fdeEnd); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 750 | } |