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" |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
| 12 | #include "llvm/MC/MCContext.h" |
| 13 | #include "llvm/MC/MCObjectFileInfo.h" |
| 14 | #include "llvm/MC/MCObjectWriter.h" |
| 15 | #include "llvm/MC/MCRegisterInfo.h" |
Rafael Espindola | ad8aaa0 | 2010-11-22 11:53:17 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCStreamer.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSymbol.h" |
| 18 | #include "llvm/MC/MCExpr.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
| 23 | #include "llvm/Support/SourceMgr.h" |
Bill Wendling | 2b2dc7c | 2011-07-06 22:52:32 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/FoldingSet.h" |
| 25 | #include "llvm/ADT/SmallString.h" |
Bill Wendling | 2b2dc7c | 2011-07-06 22:52:32 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Twine.h" |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 27 | #include "llvm/Config/config.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 30 | // Given a special op, return the address skip amount (in units of |
| 31 | // DWARF2_LINE_MIN_INSN_LENGTH. |
| 32 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
| 33 | |
| 34 | // 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] | 35 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 36 | |
| 37 | // First special line opcode - leave room for the standard opcodes. |
| 38 | // Note: If you want to change this, you'll have to update the |
| 39 | // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 40 | #define DWARF2_LINE_OPCODE_BASE 13 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 41 | |
| 42 | // Minimum line offset in a special line info. opcode. This value |
| 43 | // was chosen to give a reasonable range of values. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 44 | #define DWARF2_LINE_BASE -5 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 45 | |
| 46 | // Range of line offsets in a special line info. opcode. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 47 | #define DWARF2_LINE_RANGE 14 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 48 | |
| 49 | // Define the architecture-dependent minimum instruction length (in bytes). |
| 50 | // This value should be rather too small than too big. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 51 | #define DWARF2_LINE_MIN_INSN_LENGTH 1 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 52 | |
| 53 | // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting, |
| 54 | // this routine is a nop and will be optimized away. |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 55 | static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 56 | if (DWARF2_LINE_MIN_INSN_LENGTH == 1) |
| 57 | return AddrDelta; |
| 58 | if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) { |
| 59 | // TODO: report this error, but really only once. |
| 60 | ; |
| 61 | } |
| 62 | return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH; |
| 63 | } |
| 64 | |
| 65 | // |
| 66 | // This is called when an instruction is assembled into the specified section |
| 67 | // and if there is information from the last .loc directive that has yet to have |
| 68 | // a line entry made for it is made. |
| 69 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 70 | void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 71 | if (!MCOS->getContext().getDwarfLocSeen()) |
| 72 | return; |
| 73 | |
| 74 | // Create a symbol at in the current section for use in the line entry. |
| 75 | MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol(); |
| 76 | // Set the value of the symbol to use for the MCLineEntry. |
| 77 | MCOS->EmitLabel(LineSym); |
| 78 | |
| 79 | // Get the current .loc info saved in the context. |
| 80 | const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc(); |
| 81 | |
| 82 | // Create a (local) line entry with the symbol and the current .loc info. |
| 83 | MCLineEntry LineEntry(LineSym, DwarfLoc); |
| 84 | |
| 85 | // clear DwarfLocSeen saying the current .loc info is now used. |
Kevin Enderby | 3f55c24 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 86 | MCOS->getContext().ClearDwarfLocSeen(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 87 | |
| 88 | // Get the MCLineSection for this section, if one does not exist for this |
| 89 | // section create it. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 90 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 91 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 92 | MCLineSection *LineSection = MCLineSections.lookup(Section); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 93 | if (!LineSection) { |
| 94 | // Create a new MCLineSection. This will be deleted after the dwarf line |
| 95 | // table is created using it by iterating through the MCLineSections |
| 96 | // DenseMap. |
| 97 | LineSection = new MCLineSection; |
| 98 | // Save a pointer to the new LineSection into the MCLineSections DenseMap. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 99 | MCOS->getContext().addMCLineSection(Section, LineSection); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Add the line entry to this section's entries. |
| 103 | LineSection->addLineEntry(LineEntry); |
| 104 | } |
| 105 | |
| 106 | // |
| 107 | // This helper routine returns an expression of End - Start + IntVal . |
| 108 | // |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 109 | static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS, |
| 110 | const MCSymbol &Start, |
| 111 | const MCSymbol &End, |
| 112 | int IntVal) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 113 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 114 | const MCExpr *Res = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 115 | MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 116 | const MCExpr *RHS = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 117 | MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 118 | const MCExpr *Res1 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 119 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 120 | const MCExpr *Res2 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 121 | MCConstantExpr::Create(IntVal, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 122 | const MCExpr *Res3 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 123 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 124 | return Res3; |
| 125 | } |
| 126 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 127 | // |
| 128 | // This emits the Dwarf line table for the specified section from the entries |
| 129 | // in the LineSection. |
| 130 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 131 | static inline void EmitDwarfLineTable(MCStreamer *MCOS, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 132 | const MCSection *Section, |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 133 | const MCLineSection *LineSection) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 134 | unsigned FileNum = 1; |
| 135 | unsigned LastLine = 1; |
| 136 | unsigned Column = 0; |
| 137 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 138 | unsigned Isa = 0; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 139 | MCSymbol *LastLabel = NULL; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 140 | |
| 141 | // Loop through each MCLineEntry and encode the dwarf line number table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 142 | for (MCLineSection::const_iterator |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 143 | it = LineSection->getMCLineEntries()->begin(), |
| 144 | ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) { |
| 145 | |
| 146 | if (FileNum != it->getFileNum()) { |
| 147 | FileNum = it->getFileNum(); |
| 148 | MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 149 | MCOS->EmitULEB128IntValue(FileNum); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 150 | } |
| 151 | if (Column != it->getColumn()) { |
| 152 | Column = it->getColumn(); |
| 153 | MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 154 | MCOS->EmitULEB128IntValue(Column); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 155 | } |
| 156 | if (Isa != it->getIsa()) { |
| 157 | Isa = it->getIsa(); |
| 158 | MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 159 | MCOS->EmitULEB128IntValue(Isa); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 160 | } |
| 161 | if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) { |
| 162 | Flags = it->getFlags(); |
| 163 | MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1); |
| 164 | } |
| 165 | if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK) |
| 166 | MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1); |
| 167 | if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END) |
| 168 | MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1); |
| 169 | if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN) |
| 170 | MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1); |
| 171 | |
Rafael Espindola | 64185cc | 2010-11-13 01:06:27 +0000 | [diff] [blame] | 172 | int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 173 | MCSymbol *Label = it->getLabel(); |
| 174 | |
| 175 | // At this point we want to emit/create the sequence to encode the delta in |
| 176 | // line numbers and the increment of the address from the previous Label |
| 177 | // and the current Label. |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 178 | const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo(); |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 179 | MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label, |
| 180 | asmInfo.getPointerSize()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 181 | |
| 182 | LastLine = it->getLine(); |
| 183 | LastLabel = Label; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Emit a DW_LNE_end_sequence for the end of the section. |
| 187 | // Using the pointer Section create a temporary label at the end of the |
| 188 | // section and use that and the LastLabel to compute the address delta |
| 189 | // and use INT64_MAX as the line delta which is the signal that this is |
| 190 | // actually a DW_LNE_end_sequence. |
| 191 | |
| 192 | // Switch to the section to be able to create a symbol at its end. |
| 193 | MCOS->SwitchSection(Section); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 194 | |
| 195 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 196 | // Create a symbol at the end of the section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 197 | MCSymbol *SectionEnd = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 198 | // Set the value of the symbol, as we are at the end of the section. |
| 199 | MCOS->EmitLabel(SectionEnd); |
| 200 | |
| 201 | // Switch back the the dwarf line section. |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 202 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection()); |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 203 | |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 204 | const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo(); |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 205 | MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd, |
| 206 | asmInfo.getPointerSize()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // |
| 210 | // This emits the Dwarf file and the line tables. |
| 211 | // |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 212 | void MCDwarfFileTable::Emit(MCStreamer *MCOS) { |
| 213 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 214 | // Switch to the section where the table will be emitted into. |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 215 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 216 | |
| 217 | // Create a symbol at the beginning of this section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 218 | MCSymbol *LineStartSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 219 | // Set the value of the symbol, as we are at the start of the section. |
| 220 | MCOS->EmitLabel(LineStartSym); |
| 221 | |
| 222 | // 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] | 223 | MCSymbol *LineEndSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 224 | |
| 225 | // The first 4 bytes is the total length of the information for this |
| 226 | // compilation unit (not including these 4 bytes for the length). |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 227 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4), |
Rafael Espindola | 0bbe0b4 | 2010-12-06 17:27:56 +0000 | [diff] [blame] | 228 | 4); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 229 | |
| 230 | // Next 2 bytes is the Version, which is Dwarf 2. |
| 231 | MCOS->EmitIntValue(2, 2); |
| 232 | |
| 233 | // 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] | 234 | MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 235 | |
| 236 | // Length of the prologue, is the next 4 bytes. Which is the start of the |
| 237 | // section to the end of the prologue. Not including the 4 bytes for the |
| 238 | // total length, the 2 bytes for the version, and these 4 bytes for the |
| 239 | // length of the prologue. |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 240 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 241 | (4 + 2 + 4)), |
Rafael Espindola | 5d4918d | 2010-12-04 03:21:47 +0000 | [diff] [blame] | 242 | 4, 0); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 243 | |
| 244 | // Parameters of the state machine, are next. |
| 245 | MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1); |
| 246 | MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); |
| 247 | MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); |
| 248 | MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); |
| 249 | MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1); |
| 250 | |
| 251 | // Standard opcode lengths |
| 252 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy |
| 253 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc |
| 254 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line |
| 255 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file |
| 256 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column |
| 257 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt |
| 258 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block |
| 259 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc |
| 260 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc |
| 261 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end |
| 262 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin |
| 263 | MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa |
| 264 | |
| 265 | // Put out the directory and file tables. |
| 266 | |
| 267 | // First the directory table. |
| 268 | const std::vector<StringRef> &MCDwarfDirs = |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 269 | context.getMCDwarfDirs(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 270 | for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { |
| 271 | MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName |
| 272 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
| 273 | } |
| 274 | MCOS->EmitIntValue(0, 1); // Terminate the directory list |
| 275 | |
| 276 | // Second the file table. |
| 277 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 278 | MCOS->getContext().getMCDwarfFiles(); |
| 279 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
| 280 | MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName |
| 281 | MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 282 | // the Directory num |
| 283 | MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 284 | MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0) |
| 285 | MCOS->EmitIntValue(0, 1); // filesize (always 0) |
| 286 | } |
| 287 | MCOS->EmitIntValue(0, 1); // Terminate the file list |
| 288 | |
| 289 | // This is the end of the prologue, so set the value of the symbol at the |
| 290 | // end of the prologue (that was used in a previous expression). |
| 291 | MCOS->EmitLabel(ProEndSym); |
| 292 | |
| 293 | // Put out the line tables. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 294 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 295 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 296 | const std::vector<const MCSection *> &MCLineSectionOrder = |
| 297 | MCOS->getContext().getMCLineSectionOrder(); |
| 298 | for (std::vector<const MCSection*>::const_iterator it = |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 299 | MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie; |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 300 | ++it) { |
| 301 | const MCSection *Sec = *it; |
| 302 | const MCLineSection *Line = MCLineSections.lookup(Sec); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 303 | EmitDwarfLineTable(MCOS, Sec, Line); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 304 | |
| 305 | // Now delete the MCLineSections that were created in MCLineEntry::Make() |
| 306 | // and used to emit the line table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 307 | delete Line; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Rafael Espindola | 767b1be | 2010-12-04 00:31:13 +0000 | [diff] [blame] | 310 | if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines() |
| 311 | && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) { |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 312 | // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures |
| 313 | // it requires: |
| 314 | // total_length >= prologue_length + 10 |
| 315 | // We are 4 bytes short, since we have total_length = 51 and |
| 316 | // prologue_length = 45 |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 317 | |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 318 | // The regular end_sequence should be sufficient. |
| 319 | MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0); |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 322 | // This is the end of the section, so set the value of the symbol at the end |
| 323 | // of this section (that was used in a previous expression). |
| 324 | MCOS->EmitLabel(LineEndSym); |
| 325 | } |
| 326 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 327 | /// Utility function to write the encoding to an object writer. |
| 328 | void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta, |
| 329 | uint64_t AddrDelta) { |
| 330 | SmallString<256> Tmp; |
| 331 | raw_svector_ostream OS(Tmp); |
| 332 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 333 | OW->WriteBytes(OS.str()); |
| 334 | } |
| 335 | |
| 336 | /// Utility function to emit the encoding to a streamer. |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 337 | void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 338 | uint64_t AddrDelta) { |
| 339 | SmallString<256> Tmp; |
| 340 | raw_svector_ostream OS(Tmp); |
| 341 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 342 | MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0); |
| 343 | } |
| 344 | |
| 345 | /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. |
| 346 | void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta, |
| 347 | raw_ostream &OS) { |
| 348 | uint64_t Temp, Opcode; |
| 349 | bool NeedCopy = false; |
| 350 | |
| 351 | // Scale the address delta by the minimum instruction length. |
| 352 | AddrDelta = ScaleAddrDelta(AddrDelta); |
| 353 | |
| 354 | // A LineDelta of INT64_MAX is a signal that this is actually a |
| 355 | // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the |
| 356 | // end_sequence to emit the matrix entry. |
| 357 | if (LineDelta == INT64_MAX) { |
| 358 | if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) |
| 359 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 360 | else { |
| 361 | OS << char(dwarf::DW_LNS_advance_pc); |
Benjamin Kramer | dcf0e0c | 2011-06-18 14:42:47 +0000 | [diff] [blame] | 362 | MCObjectWriter::EncodeULEB128(AddrDelta, OS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 363 | } |
| 364 | OS << char(dwarf::DW_LNS_extended_op); |
| 365 | OS << char(1); |
| 366 | OS << char(dwarf::DW_LNE_end_sequence); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // Bias the line delta by the base. |
| 371 | Temp = LineDelta - DWARF2_LINE_BASE; |
| 372 | |
| 373 | // If the line increment is out of range of a special opcode, we must encode |
| 374 | // it with DW_LNS_advance_line. |
| 375 | if (Temp >= DWARF2_LINE_RANGE) { |
| 376 | OS << char(dwarf::DW_LNS_advance_line); |
Benjamin Kramer | 2dd4239 | 2011-11-09 13:19:15 +0000 | [diff] [blame] | 377 | MCObjectWriter::EncodeSLEB128(LineDelta, OS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 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); |
Benjamin Kramer | 2dd4239 | 2011-11-09 13:19:15 +0000 | [diff] [blame] | 413 | MCObjectWriter::EncodeULEB128(AddrDelta, OS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 414 | |
| 415 | if (NeedCopy) |
| 416 | OS << char(dwarf::DW_LNS_copy); |
| 417 | else |
| 418 | OS << char(Temp); |
| 419 | } |
| 420 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 421 | void MCDwarfFile::print(raw_ostream &OS) const { |
| 422 | OS << '"' << getName() << '"'; |
| 423 | } |
| 424 | |
| 425 | void MCDwarfFile::dump() const { |
| 426 | print(dbgs()); |
| 427 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 428 | |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 429 | // Utility function to write a tuple for .debug_abbrev. |
| 430 | static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) { |
| 431 | MCOS->EmitULEB128IntValue(Name); |
| 432 | MCOS->EmitULEB128IntValue(Form); |
| 433 | } |
| 434 | |
| 435 | // When generating dwarf for assembly source files this emits |
| 436 | // the data for .debug_abbrev section which contains three DIEs. |
| 437 | static void EmitGenDwarfAbbrev(MCStreamer *MCOS) { |
| 438 | MCContext &context = MCOS->getContext(); |
| 439 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection()); |
| 440 | |
| 441 | // DW_TAG_compile_unit DIE abbrev (1). |
| 442 | MCOS->EmitULEB128IntValue(1); |
| 443 | MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit); |
| 444 | MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1); |
| 445 | EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4); |
| 446 | EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr); |
| 447 | EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr); |
| 448 | EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string); |
| 449 | EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string); |
| 450 | StringRef DwarfDebugFlags = context.getDwarfDebugFlags(); |
| 451 | if (!DwarfDebugFlags.empty()) |
| 452 | EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string); |
| 453 | EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string); |
| 454 | EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2); |
| 455 | EmitAbbrev(MCOS, 0, 0); |
| 456 | |
| 457 | // DW_TAG_subprogram DIE abbrev (2). |
| 458 | MCOS->EmitULEB128IntValue(2); |
| 459 | MCOS->EmitULEB128IntValue(dwarf::DW_TAG_subprogram); |
| 460 | MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1); |
| 461 | EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string); |
| 462 | EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4); |
| 463 | EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4); |
| 464 | EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr); |
| 465 | EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr); |
| 466 | EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag); |
| 467 | EmitAbbrev(MCOS, 0, 0); |
| 468 | |
| 469 | // DW_TAG_unspecified_parameters DIE abbrev (3). |
| 470 | MCOS->EmitULEB128IntValue(3); |
| 471 | MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters); |
| 472 | MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1); |
| 473 | EmitAbbrev(MCOS, 0, 0); |
| 474 | |
| 475 | // Terminate the abbreviations for this compilation unit. |
| 476 | MCOS->EmitIntValue(0, 1); |
| 477 | } |
| 478 | |
| 479 | // When generating dwarf for assembly source files this emits the data for |
| 480 | // .debug_aranges section. Which contains a header and a table of pairs of |
| 481 | // PointerSize'ed values for the address and size of section(s) with line table |
| 482 | // entries (just the default .text in our case) and a terminating pair of zeros. |
| 483 | static void EmitGenDwarfAranges(MCStreamer *MCOS) { |
| 484 | MCContext &context = MCOS->getContext(); |
| 485 | |
| 486 | // Create a symbol at the end of the section that we are creating the dwarf |
| 487 | // debugging info to use later in here as part of the expression to calculate |
| 488 | // the size of the section for the table. |
| 489 | MCOS->SwitchSection(context.getGenDwarfSection()); |
| 490 | MCSymbol *SectionEndSym = context.CreateTempSymbol(); |
| 491 | MCOS->EmitLabel(SectionEndSym); |
| 492 | context.setGenDwarfSectionEndSym(SectionEndSym); |
| 493 | |
| 494 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection()); |
| 495 | |
| 496 | // This will be the length of the .debug_aranges section, first account for |
| 497 | // the size of each item in the header (see below where we emit these items). |
| 498 | int Length = 4 + 2 + 4 + 1 + 1; |
| 499 | |
| 500 | // Figure the padding after the header before the table of address and size |
| 501 | // pairs who's values are PointerSize'ed. |
| 502 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
| 503 | int AddrSize = asmInfo.getPointerSize(); |
| 504 | int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1)); |
| 505 | if (Pad == 2 * AddrSize) |
| 506 | Pad = 0; |
| 507 | Length += Pad; |
| 508 | |
| 509 | // Add the size of the pair of PointerSize'ed values for the address and size |
| 510 | // of the one default .text section we have in the table. |
| 511 | Length += 2 * AddrSize; |
| 512 | // And the pair of terminating zeros. |
| 513 | Length += 2 * AddrSize; |
| 514 | |
| 515 | |
| 516 | // Emit the header for this section. |
| 517 | // The 4 byte length not including the 4 byte value for the length. |
| 518 | MCOS->EmitIntValue(Length - 4, 4); |
| 519 | // The 2 byte version, which is 2. |
| 520 | MCOS->EmitIntValue(2, 2); |
| 521 | // The 4 byte offset to the compile unit in the .debug_info from the start |
| 522 | // of the .debug_info, it is at the start of that section so this is zero. |
| 523 | MCOS->EmitIntValue(0, 4); |
| 524 | // The 1 byte size of an address. |
| 525 | MCOS->EmitIntValue(AddrSize, 1); |
| 526 | // The 1 byte size of a segment descriptor, we use a value of zero. |
| 527 | MCOS->EmitIntValue(0, 1); |
| 528 | // Align the header with the padding if needed, before we put out the table. |
| 529 | for(int i = 0; i < Pad; i++) |
| 530 | MCOS->EmitIntValue(0, 1); |
| 531 | |
| 532 | // Now emit the table of pairs of PointerSize'ed values for the section(s) |
| 533 | // address and size, in our case just the one default .text section. |
| 534 | const MCExpr *Addr = MCSymbolRefExpr::Create( |
| 535 | context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context); |
| 536 | const MCExpr *Size = MakeStartMinusEndExpr(*MCOS, |
| 537 | *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0); |
| 538 | MCOS->EmitAbsValue(Addr, AddrSize); |
| 539 | MCOS->EmitAbsValue(Size, AddrSize); |
| 540 | |
| 541 | // And finally the pair of terminating zeros. |
| 542 | MCOS->EmitIntValue(0, AddrSize); |
| 543 | MCOS->EmitIntValue(0, AddrSize); |
| 544 | } |
| 545 | |
| 546 | // When generating dwarf for assembly source files this emits the data for |
| 547 | // .debug_info section which contains three parts. The header, the compile_unit |
| 548 | // DIE and a list of subprogram DIEs. |
| 549 | static void EmitGenDwarfInfo(MCStreamer *MCOS) { |
| 550 | MCContext &context = MCOS->getContext(); |
| 551 | |
| 552 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection()); |
| 553 | |
| 554 | // Create a symbol at the start and end of this section used in here for the |
| 555 | // expression to calculate the length in the header. |
| 556 | MCSymbol *InfoStart = context.CreateTempSymbol(); |
| 557 | MCOS->EmitLabel(InfoStart); |
| 558 | MCSymbol *InfoEnd = context.CreateTempSymbol(); |
| 559 | |
| 560 | // First part: the header. |
| 561 | |
| 562 | // The 4 byte total length of the information for this compilation unit, not |
| 563 | // including these 4 bytes. |
| 564 | const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4); |
| 565 | MCOS->EmitAbsValue(Length, 4); |
| 566 | |
| 567 | // The 2 byte DWARF version, which is 2. |
| 568 | MCOS->EmitIntValue(2, 2); |
| 569 | |
| 570 | // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev, |
| 571 | // it is at the start of that section so this is zero. |
| 572 | MCOS->EmitIntValue(0, 4); |
| 573 | |
| 574 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
| 575 | int AddrSize = asmInfo.getPointerSize(); |
| 576 | // The 1 byte size of an address. |
| 577 | MCOS->EmitIntValue(AddrSize, 1); |
| 578 | |
| 579 | // Second part: the compile_unit DIE. |
| 580 | |
| 581 | // The DW_TAG_compile_unit DIE abbrev (1). |
| 582 | MCOS->EmitULEB128IntValue(1); |
| 583 | |
| 584 | // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section, |
| 585 | // which is at the start of that section so this is zero. |
| 586 | MCOS->EmitIntValue(0, 4); |
| 587 | |
| 588 | // AT_low_pc, the first address of the default .text section. |
| 589 | const MCExpr *Start = MCSymbolRefExpr::Create( |
| 590 | context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context); |
| 591 | MCOS->EmitAbsValue(Start, AddrSize); |
| 592 | |
| 593 | // AT_high_pc, the last address of the default .text section. |
| 594 | const MCExpr *End = MCSymbolRefExpr::Create( |
| 595 | context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context); |
| 596 | MCOS->EmitAbsValue(End, AddrSize); |
| 597 | |
| 598 | // AT_name, the name of the source file. Reconstruct from the first directory |
| 599 | // and file table entries. |
| 600 | const std::vector<StringRef> &MCDwarfDirs = |
| 601 | context.getMCDwarfDirs(); |
| 602 | if (MCDwarfDirs.size() > 0) { |
| 603 | MCOS->EmitBytes(MCDwarfDirs[0], 0); |
| 604 | MCOS->EmitBytes("/", 0); |
| 605 | } |
| 606 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 607 | MCOS->getContext().getMCDwarfFiles(); |
| 608 | MCOS->EmitBytes(MCDwarfFiles[1]->getName(), 0); |
| 609 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 610 | |
| 611 | // AT_comp_dir, the working directory the assembly was done in. |
| 612 | llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory(); |
| 613 | MCOS->EmitBytes(StringRef(CWD.c_str()), 0); |
| 614 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 615 | |
| 616 | // AT_APPLE_flags, the command line arguments of the assembler tool. |
| 617 | StringRef DwarfDebugFlags = context.getDwarfDebugFlags(); |
| 618 | if (!DwarfDebugFlags.empty()){ |
| 619 | MCOS->EmitBytes(DwarfDebugFlags, 0); |
| 620 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 621 | } |
| 622 | |
| 623 | // AT_producer, the version of the assembler tool. |
| 624 | MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "), 0); |
| 625 | MCOS->EmitBytes(StringRef(PACKAGE_VERSION), 0); |
| 626 | MCOS->EmitBytes(StringRef(")"), 0); |
| 627 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 628 | |
| 629 | // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2 |
| 630 | // draft has no standard code for assembler. |
| 631 | MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2); |
| 632 | |
| 633 | // Third part: the list of subprogram DIEs. |
| 634 | |
| 635 | // Loop on saved info for dwarf subprograms and create the DIEs for them. |
| 636 | const std::vector<const MCGenDwarfSubprogramEntry *> &Entries = |
| 637 | MCOS->getContext().getMCGenDwarfSubprogramEntries(); |
| 638 | for (std::vector<const MCGenDwarfSubprogramEntry *>::const_iterator it = |
| 639 | Entries.begin(), ie = Entries.end(); it != ie; |
| 640 | ++it) { |
| 641 | const MCGenDwarfSubprogramEntry *Entry = *it; |
| 642 | |
| 643 | // The DW_TAG_subprogram DIE abbrev (2). |
| 644 | MCOS->EmitULEB128IntValue(2); |
| 645 | |
| 646 | // AT_name, of the label without any leading underbar. |
| 647 | MCOS->EmitBytes(Entry->getName(), 0); |
| 648 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 649 | |
| 650 | // AT_decl_file, index into the file table. |
| 651 | MCOS->EmitIntValue(Entry->getFileNumber(), 4); |
| 652 | |
| 653 | // AT_decl_line, source line number. |
| 654 | MCOS->EmitIntValue(Entry->getLineNumber(), 4); |
| 655 | |
| 656 | // AT_low_pc, start address of the label. |
| 657 | const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(), |
| 658 | MCSymbolRefExpr::VK_None, context); |
| 659 | MCOS->EmitAbsValue(AT_low_pc, AddrSize); |
| 660 | |
| 661 | // AT_high_pc, end address which is the next label or end of the section. |
| 662 | std::vector<const MCGenDwarfSubprogramEntry *>::const_iterator next = it+1; |
| 663 | if (next != Entries.end()){ |
| 664 | const MCGenDwarfSubprogramEntry *NextEntry = *next; |
| 665 | const MCExpr *AT_high_pc = MCSymbolRefExpr::Create(NextEntry->getLabel(), |
| 666 | MCSymbolRefExpr::VK_None, context); |
| 667 | MCOS->EmitAbsValue(AT_high_pc, AddrSize); |
| 668 | } else { |
| 669 | MCOS->EmitAbsValue(End, AddrSize); |
| 670 | } |
| 671 | |
| 672 | // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype. |
| 673 | MCOS->EmitIntValue(0, 1); |
| 674 | |
| 675 | // The DW_TAG_unspecified_parameters DIE abbrev (3). |
| 676 | MCOS->EmitULEB128IntValue(3); |
| 677 | |
| 678 | // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's. |
| 679 | MCOS->EmitIntValue(0, 1); |
| 680 | } |
| 681 | // Deallocate the MCGenDwarfSubprogramEntry classes that saved away the info |
| 682 | // for the dwarf subprograms. |
| 683 | for (std::vector<const MCGenDwarfSubprogramEntry *>::const_iterator it = |
| 684 | Entries.begin(), ie = Entries.end(); it != ie; |
| 685 | ++it) { |
| 686 | const MCGenDwarfSubprogramEntry *Entry = *it; |
| 687 | delete Entry; |
| 688 | } |
| 689 | |
| 690 | // Add the NULL DIE terminating the Compile Unit DIE's. |
| 691 | MCOS->EmitIntValue(0, 1); |
| 692 | |
| 693 | // Now set the value of the symbol at the end of the info section. |
| 694 | MCOS->EmitLabel(InfoEnd); |
| 695 | } |
| 696 | |
| 697 | // |
| 698 | // When generating dwarf for assembly source files this emits the Dwarf |
| 699 | // sections. |
| 700 | // |
| 701 | void MCGenDwarfInfo::Emit(MCStreamer *MCOS) { |
| 702 | // Create the dwarf sections in this order (.debug_line already created). |
| 703 | MCContext &context = MCOS->getContext(); |
| 704 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection()); |
| 705 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection()); |
| 706 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection()); |
| 707 | |
| 708 | // If there are no line table entries then do not emit any section contents. |
| 709 | if (context.getMCLineSections().empty()) |
| 710 | return; |
| 711 | |
| 712 | // Output the data for .debug_aranges section. |
| 713 | EmitGenDwarfAranges(MCOS); |
| 714 | |
| 715 | // Output the data for .debug_abbrev section. |
| 716 | EmitGenDwarfAbbrev(MCOS); |
| 717 | |
| 718 | // Output the data for .debug_info section. |
| 719 | EmitGenDwarfInfo(MCOS); |
| 720 | } |
| 721 | |
| 722 | // |
| 723 | // When generating dwarf for assembly source files this is called when symbol |
| 724 | // for a label is created. If this symbol is not a temporary and is in the |
| 725 | // section that dwarf is being generated for, save the needed info to create |
| 726 | // a dwarf subprogram. |
| 727 | // |
| 728 | void MCGenDwarfSubprogramEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS, |
| 729 | SourceMgr &SrcMgr, SMLoc &Loc) { |
| 730 | // We won't create dwarf subprogram's for temporary symbols or symbols not in |
| 731 | // the default text. |
| 732 | if (Symbol->isTemporary()) |
| 733 | return; |
| 734 | MCContext &context = MCOS->getContext(); |
| 735 | if (context.getGenDwarfSection() != MCOS->getCurrentSection()) |
| 736 | return; |
| 737 | |
| 738 | // The dwarf subprogram's name does not have the symbol name's leading |
| 739 | // underbar if any. |
| 740 | StringRef Name = Symbol->getName(); |
| 741 | if (Name.startswith("_")) |
| 742 | Name = Name.substr(1, Name.size()-1); |
| 743 | |
| 744 | // Get the dwarf file number to be used for the dwarf subprogram. |
| 745 | unsigned FileNumber = context.getGenDwarfFileNumber(); |
| 746 | |
| 747 | // Finding the line number is the expensive part which is why we just don't |
| 748 | // pass it in as for some symbols we won't create a dwarf subprogram. |
| 749 | int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); |
| 750 | unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer); |
| 751 | |
| 752 | // We create a temporary symbol for use for the AT_high_pc and AT_low_pc |
| 753 | // values so that they don't have things like an ARM thumb bit from the |
| 754 | // original symbol. So when used they won't get a low bit set after |
| 755 | // relocation. |
| 756 | MCSymbol *Label = context.CreateTempSymbol(); |
| 757 | MCOS->EmitLabel(Label); |
| 758 | |
| 759 | // Create and entry for the info and add it to the other entries. |
| 760 | MCGenDwarfSubprogramEntry *Entry = |
| 761 | new MCGenDwarfSubprogramEntry(Name, FileNumber, LineNumber, Label); |
| 762 | MCOS->getContext().addMCGenDwarfSubprogramEntry(Entry); |
| 763 | } |
| 764 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 765 | static int getDataAlignmentFactor(MCStreamer &streamer) { |
| 766 | MCContext &context = streamer.getContext(); |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 767 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 768 | int size = asmInfo.getPointerSize(); |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 769 | if (asmInfo.isStackGrowthDirectionUp()) |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 770 | return size; |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 771 | else |
| 772 | return -size; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 775 | static unsigned getSizeForEncoding(MCStreamer &streamer, |
| 776 | unsigned symbolEncoding) { |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 777 | MCContext &context = streamer.getContext(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 778 | unsigned format = symbolEncoding & 0x0f; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 779 | switch (format) { |
| 780 | default: |
| 781 | assert(0 && "Unknown Encoding"); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 782 | case dwarf::DW_EH_PE_absptr: |
| 783 | case dwarf::DW_EH_PE_signed: |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 784 | return context.getAsmInfo().getPointerSize(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 785 | case dwarf::DW_EH_PE_udata2: |
| 786 | case dwarf::DW_EH_PE_sdata2: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 787 | return 2; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 788 | case dwarf::DW_EH_PE_udata4: |
| 789 | case dwarf::DW_EH_PE_sdata4: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 790 | return 4; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 791 | case dwarf::DW_EH_PE_udata8: |
| 792 | case dwarf::DW_EH_PE_sdata8: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 793 | return 8; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 794 | } |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol, |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 798 | unsigned symbolEncoding, const char *comment = 0) { |
Rafael Espindola | a0057ca | 2011-04-28 21:04:39 +0000 | [diff] [blame] | 799 | MCContext &context = streamer.getContext(); |
| 800 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
| 801 | const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol, |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 802 | symbolEncoding, |
Rafael Espindola | a0057ca | 2011-04-28 21:04:39 +0000 | [diff] [blame] | 803 | streamer); |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 804 | unsigned size = getSizeForEncoding(streamer, symbolEncoding); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 805 | if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 806 | streamer.EmitAbsValue(v, size); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 809 | static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol, |
| 810 | unsigned symbolEncoding) { |
| 811 | MCContext &context = streamer.getContext(); |
| 812 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 813 | const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol, |
| 814 | symbolEncoding, |
| 815 | streamer); |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 816 | unsigned size = getSizeForEncoding(streamer, symbolEncoding); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 817 | streamer.EmitValue(v, size); |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 820 | static const MachineLocation TranslateMachineLocation( |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 821 | const MCRegisterInfo &MRI, |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 822 | const MachineLocation &Loc) { |
| 823 | unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ? |
| 824 | MachineLocation::VirtualFP : |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 825 | unsigned(MRI.getDwarfRegNum(Loc.getReg(), true)); |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 826 | const MachineLocation &NewLoc = Loc.isReg() ? |
| 827 | MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset()); |
| 828 | return NewLoc; |
| 829 | } |
| 830 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 831 | namespace { |
| 832 | class FrameEmitterImpl { |
| 833 | int CFAOffset; |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 834 | int CIENum; |
Rafael Espindola | 5426a9e | 2011-05-01 04:49:54 +0000 | [diff] [blame] | 835 | bool UsingCFI; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 836 | bool IsEH; |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 837 | const MCSymbol *SectionStart; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 838 | public: |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 839 | FrameEmitterImpl(bool usingCFI, bool isEH) |
| 840 | : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH), |
| 841 | SectionStart(0) {} |
| 842 | |
| 843 | void setSectionStart(const MCSymbol *Label) { SectionStart = Label; } |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 844 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 845 | /// EmitCompactUnwind - Emit the unwind information in a compact way. If |
| 846 | /// we're successful, return 'true'. Otherwise, return 'false' and it will |
| 847 | /// emit the normal CIE and FDE. |
| 848 | bool EmitCompactUnwind(MCStreamer &streamer, |
| 849 | const MCDwarfFrameInfo &frame); |
| 850 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 851 | const MCSymbol &EmitCIE(MCStreamer &streamer, |
| 852 | const MCSymbol *personality, |
| 853 | unsigned personalityEncoding, |
| 854 | const MCSymbol *lsda, |
| 855 | unsigned lsdaEncoding); |
| 856 | MCSymbol *EmitFDE(MCStreamer &streamer, |
| 857 | const MCSymbol &cieStart, |
Rafael Espindola | 9f270da | 2011-05-10 03:01:39 +0000 | [diff] [blame] | 858 | const MCDwarfFrameInfo &frame); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 859 | void EmitCFIInstructions(MCStreamer &streamer, |
| 860 | const std::vector<MCCFIInstruction> &Instrs, |
| 861 | MCSymbol *BaseLabel); |
| 862 | void EmitCFIInstruction(MCStreamer &Streamer, |
| 863 | const MCCFIInstruction &Instr); |
| 864 | }; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 865 | |
| 866 | } // end anonymous namespace |
| 867 | |
| 868 | static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding, |
| 869 | StringRef Prefix) { |
| 870 | if (Streamer.isVerboseAsm()) { |
| 871 | const char *EncStr = 0; |
| 872 | switch (Encoding) { |
| 873 | default: EncStr = "<unknown encoding>"; |
| 874 | case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; |
| 875 | case dwarf::DW_EH_PE_omit: EncStr = "omit"; |
| 876 | case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; |
| 877 | case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; |
| 878 | case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; |
| 879 | case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; |
| 880 | case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; |
| 881 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4: EncStr = "pcrel udata4"; |
| 882 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4: EncStr = "pcrel sdata4"; |
| 883 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8: EncStr = "pcrel udata8"; |
| 884 | case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8: EncStr = "pcrel sdata8"; |
| 885 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4: |
| 886 | EncStr = "indirect pcrel udata4"; |
| 887 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4: |
| 888 | EncStr = "indirect pcrel sdata4"; |
| 889 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8: |
| 890 | EncStr = "indirect pcrel udata8"; |
| 891 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8: |
| 892 | EncStr = "indirect pcrel sdata8"; |
| 893 | } |
| 894 | |
| 895 | Streamer.AddComment(Twine(Prefix) + " = " + EncStr); |
| 896 | } |
| 897 | |
| 898 | Streamer.EmitIntValue(Encoding, 1); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer, |
| 902 | const MCCFIInstruction &Instr) { |
| 903 | int dataAlignmentFactor = getDataAlignmentFactor(Streamer); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 904 | bool VerboseAsm = Streamer.isVerboseAsm(); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 905 | |
| 906 | switch (Instr.getOperation()) { |
| 907 | case MCCFIInstruction::Move: |
| 908 | case MCCFIInstruction::RelMove: { |
| 909 | const MachineLocation &Dst = Instr.getDestination(); |
| 910 | const MachineLocation &Src = Instr.getSource(); |
Rafael Espindola | 5d7dcd3 | 2011-04-12 18:53:30 +0000 | [diff] [blame] | 911 | const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 912 | |
| 913 | // If advancing cfa. |
| 914 | if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 915 | if (Src.getReg() == MachineLocation::VirtualFP) { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 916 | if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 917 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1); |
| 918 | } else { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 919 | if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 920 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 921 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + |
| 922 | Twine(Src.getReg())); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 923 | Streamer.EmitULEB128IntValue(Src.getReg()); |
| 924 | } |
| 925 | |
Rafael Espindola | 5d7dcd3 | 2011-04-12 18:53:30 +0000 | [diff] [blame] | 926 | if (IsRelative) |
| 927 | CFAOffset += Src.getOffset(); |
| 928 | else |
| 929 | CFAOffset = -Src.getOffset(); |
| 930 | |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 931 | if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 932 | Streamer.EmitULEB128IntValue(CFAOffset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 933 | return; |
| 934 | } |
| 935 | |
| 936 | if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) { |
| 937 | assert(Dst.isReg() && "Machine move not supported yet."); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 938 | if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 939 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 940 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg())); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 941 | Streamer.EmitULEB128IntValue(Dst.getReg()); |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | unsigned Reg = Src.getReg(); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 946 | int Offset = Dst.getOffset(); |
| 947 | if (IsRelative) |
| 948 | Offset -= CFAOffset; |
| 949 | Offset = Offset / dataAlignmentFactor; |
| 950 | |
| 951 | if (Offset < 0) { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 952 | if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 953 | Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 954 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 955 | Streamer.EmitULEB128IntValue(Reg); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 956 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 957 | Streamer.EmitSLEB128IntValue(Offset); |
| 958 | } else if (Reg < 64) { |
Bill Wendling | e08d433 | 2011-06-30 23:47:40 +0000 | [diff] [blame] | 959 | if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") + |
| 960 | Twine(Reg) + ")"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 961 | Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 962 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 963 | Streamer.EmitULEB128IntValue(Offset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 964 | } else { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 965 | if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 966 | Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 967 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 968 | Streamer.EmitULEB128IntValue(Reg); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 969 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 970 | Streamer.EmitULEB128IntValue(Offset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 971 | } |
| 972 | return; |
| 973 | } |
Rafael Espindola | c25680f | 2011-12-29 21:09:08 +0000 | [diff] [blame^] | 974 | case MCCFIInstruction::RememberState: |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 975 | if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 976 | Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1); |
| 977 | return; |
Rafael Espindola | c25680f | 2011-12-29 21:09:08 +0000 | [diff] [blame^] | 978 | case MCCFIInstruction::RestoreState: |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 979 | if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 980 | Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1); |
| 981 | return; |
| 982 | case MCCFIInstruction::SameValue: { |
| 983 | unsigned Reg = Instr.getDestination().getReg(); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 984 | if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 985 | Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 986 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 987 | Streamer.EmitULEB128IntValue(Reg); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 988 | return; |
| 989 | } |
Rafael Espindola | 6f0b181 | 2011-12-29 20:24:47 +0000 | [diff] [blame] | 990 | case MCCFIInstruction::Escape: |
| 991 | if (VerboseAsm) Streamer.AddComment("Escape bytes"); |
| 992 | Streamer.EmitBytes(Instr.getValues(), 0); |
| 993 | return; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 994 | } |
| 995 | llvm_unreachable("Unhandled case in switch"); |
| 996 | } |
| 997 | |
| 998 | /// EmitFrameMoves - Emit frame instructions to describe the layout of the |
| 999 | /// frame. |
| 1000 | void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer, |
| 1001 | const std::vector<MCCFIInstruction> &Instrs, |
| 1002 | MCSymbol *BaseLabel) { |
| 1003 | for (unsigned i = 0, N = Instrs.size(); i < N; ++i) { |
| 1004 | const MCCFIInstruction &Instr = Instrs[i]; |
| 1005 | MCSymbol *Label = Instr.getLabel(); |
| 1006 | // Throw out move if the label is invalid. |
| 1007 | if (Label && !Label->isDefined()) continue; // Not emitted, in dead code. |
| 1008 | |
| 1009 | // Advance row if new location. |
| 1010 | if (BaseLabel && Label) { |
| 1011 | MCSymbol *ThisSym = Label; |
| 1012 | if (ThisSym != BaseLabel) { |
Bill Wendling | d221cd6 | 2011-06-30 22:35:49 +0000 | [diff] [blame] | 1013 | if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1014 | streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym); |
| 1015 | BaseLabel = ThisSym; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | EmitCFIInstruction(streamer, Instr); |
| 1020 | } |
| 1021 | } |
| 1022 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1023 | /// EmitCompactUnwind - Emit the unwind information in a compact way. If we're |
| 1024 | /// successful, return 'true'. Otherwise, return 'false' and it will emit the |
| 1025 | /// normal CIE and FDE. |
| 1026 | bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer, |
| 1027 | const MCDwarfFrameInfo &Frame) { |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1028 | MCContext &Context = Streamer.getContext(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1029 | const MCObjectFileInfo *MOFI = Context.getObjectFileInfo(); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1030 | bool VerboseAsm = Streamer.isVerboseAsm(); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1031 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1032 | // range-start range-length compact-unwind-enc personality-func lsda |
| 1033 | // _foo LfooEnd-_foo 0x00000023 0 0 |
| 1034 | // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1 |
| 1035 | // |
| 1036 | // .section __LD,__compact_unwind,regular,debug |
| 1037 | // |
| 1038 | // # compact unwind for _foo |
| 1039 | // .quad _foo |
| 1040 | // .set L1,LfooEnd-_foo |
| 1041 | // .long L1 |
| 1042 | // .long 0x01010001 |
| 1043 | // .quad 0 |
| 1044 | // .quad 0 |
| 1045 | // |
| 1046 | // # compact unwind for _bar |
| 1047 | // .quad _bar |
| 1048 | // .set L2,LbarEnd-_bar |
| 1049 | // .long L2 |
| 1050 | // .long 0x01020011 |
| 1051 | // .quad __gxx_personality |
| 1052 | // .quad except_tab1 |
| 1053 | |
Bill Wendling | e52e3f2 | 2011-07-19 00:06:12 +0000 | [diff] [blame] | 1054 | uint32_t Encoding = Frame.CompactUnwindEncoding; |
Bill Wendling | 6a6b8c3 | 2011-07-07 00:54:13 +0000 | [diff] [blame] | 1055 | if (!Encoding) return false; |
| 1056 | |
Bill Wendling | 4bb5b03 | 2011-07-14 23:34:45 +0000 | [diff] [blame] | 1057 | // The encoding needs to know we have an LSDA. |
| 1058 | if (Frame.Lsda) |
| 1059 | Encoding |= 0x40000000; |
| 1060 | |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1061 | Streamer.SwitchSection(MOFI->getCompactUnwindSection()); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1062 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1063 | // Range Start |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 1064 | unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI); |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1065 | unsigned Size = getSizeForEncoding(Streamer, FDEEncoding); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1066 | if (VerboseAsm) Streamer.AddComment("Range Start"); |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1067 | Streamer.EmitSymbolValue(Frame.Function, Size); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Range Length |
| 1070 | const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin, |
| 1071 | *Frame.End, 0); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1072 | if (VerboseAsm) Streamer.AddComment("Range Length"); |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1073 | Streamer.EmitAbsValue(Range, 4); |
| 1074 | |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1075 | // Compact Encoding |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1076 | Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4); |
Benjamin Kramer | 70be28a | 2011-11-07 21:00:59 +0000 | [diff] [blame] | 1077 | if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" + |
| 1078 | Twine::utohexstr(Encoding)); |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1079 | Streamer.EmitIntValue(Encoding, Size); |
| 1080 | |
Bill Wendling | e52e3f2 | 2011-07-19 00:06:12 +0000 | [diff] [blame] | 1081 | |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1082 | // Personality Function |
Bill Wendling | 4bb5b03 | 2011-07-14 23:34:45 +0000 | [diff] [blame] | 1083 | Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1084 | if (VerboseAsm) Streamer.AddComment("Personality Function"); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1085 | if (Frame.Personality) |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1086 | Streamer.EmitSymbolValue(Frame.Personality, Size); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1087 | else |
| 1088 | Streamer.EmitIntValue(0, Size); // No personality fn |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1089 | |
| 1090 | // LSDA |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1091 | Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1092 | if (VerboseAsm) Streamer.AddComment("LSDA"); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1093 | if (Frame.Lsda) |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1094 | Streamer.EmitSymbolValue(Frame.Lsda, Size); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1095 | else |
| 1096 | Streamer.EmitIntValue(0, Size); // No LSDA |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1097 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1098 | return true; |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1101 | const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer, |
| 1102 | const MCSymbol *personality, |
| 1103 | unsigned personalityEncoding, |
| 1104 | const MCSymbol *lsda, |
| 1105 | unsigned lsdaEncoding) { |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1106 | MCContext &context = streamer.getContext(); |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1107 | const MCRegisterInfo &MRI = context.getRegisterInfo(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1108 | const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1109 | bool verboseAsm = streamer.isVerboseAsm(); |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 1110 | |
| 1111 | MCSymbol *sectionStart; |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1112 | if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH) |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 1113 | sectionStart = context.CreateTempSymbol(); |
| 1114 | else |
| 1115 | sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum)); |
| 1116 | |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1117 | streamer.EmitLabel(sectionStart); |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 1118 | CIENum++; |
| 1119 | |
Bill Wendling | 6a6b8c3 | 2011-07-07 00:54:13 +0000 | [diff] [blame] | 1120 | MCSymbol *sectionEnd = context.CreateTempSymbol(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Length |
| 1123 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart, |
| 1124 | *sectionEnd, 4); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1125 | if (verboseAsm) streamer.AddComment("CIE Length"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 1126 | streamer.EmitAbsValue(Length, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1127 | |
| 1128 | // CIE ID |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1129 | unsigned CIE_ID = IsEH ? 0 : -1; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1130 | if (verboseAsm) streamer.AddComment("CIE ID Tag"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1131 | streamer.EmitIntValue(CIE_ID, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Version |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1134 | if (verboseAsm) streamer.AddComment("DW_CIE_VERSION"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1135 | streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1); |
| 1136 | |
| 1137 | // Augmentation String |
| 1138 | SmallString<8> Augmentation; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1139 | if (IsEH) { |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1140 | if (verboseAsm) streamer.AddComment("CIE Augmentation"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1141 | Augmentation += "z"; |
| 1142 | if (personality) |
| 1143 | Augmentation += "P"; |
| 1144 | if (lsda) |
| 1145 | Augmentation += "L"; |
| 1146 | Augmentation += "R"; |
| 1147 | streamer.EmitBytes(Augmentation.str(), 0); |
| 1148 | } |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1149 | streamer.EmitIntValue(0, 1); |
| 1150 | |
| 1151 | // Code Alignment Factor |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1152 | if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1153 | streamer.EmitULEB128IntValue(1); |
| 1154 | |
| 1155 | // Data Alignment Factor |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1156 | if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1157 | streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer)); |
| 1158 | |
| 1159 | // Return Address Register |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1160 | if (verboseAsm) streamer.AddComment("CIE Return Address Column"); |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1161 | streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true)); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1162 | |
| 1163 | // Augmentation Data Length (optional) |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1164 | |
| 1165 | unsigned augmentationLength = 0; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1166 | if (IsEH) { |
| 1167 | if (personality) { |
| 1168 | // Personality Encoding |
| 1169 | augmentationLength += 1; |
| 1170 | // Personality |
| 1171 | augmentationLength += getSizeForEncoding(streamer, personalityEncoding); |
| 1172 | } |
| 1173 | if (lsda) |
| 1174 | augmentationLength += 1; |
| 1175 | // Encoding of the FDE pointers |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1176 | augmentationLength += 1; |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1177 | |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1178 | if (verboseAsm) streamer.AddComment("Augmentation Size"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1179 | streamer.EmitULEB128IntValue(augmentationLength); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1180 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1181 | // Augmentation Data (optional) |
| 1182 | if (personality) { |
| 1183 | // Personality Encoding |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1184 | EmitEncodingByte(streamer, personalityEncoding, |
| 1185 | "Personality Encoding"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1186 | // Personality |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1187 | if (verboseAsm) streamer.AddComment("Personality"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1188 | EmitPersonality(streamer, *personality, personalityEncoding); |
| 1189 | } |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1190 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1191 | if (lsda) |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1192 | EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding"); |
| 1193 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1194 | // Encoding of the FDE pointers |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 1195 | EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI), |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1196 | "FDE Encoding"); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1197 | } |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Initial Instructions |
| 1200 | |
Evan Cheng | 2d28617 | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 1201 | const MCAsmInfo &MAI = context.getAsmInfo(); |
| 1202 | const std::vector<MachineMove> &Moves = MAI.getInitialFrameState(); |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 1203 | std::vector<MCCFIInstruction> Instructions; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1204 | |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 1205 | for (int i = 0, n = Moves.size(); i != n; ++i) { |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1206 | MCSymbol *Label = Moves[i].getLabel(); |
| 1207 | const MachineLocation &Dst = |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1208 | TranslateMachineLocation(MRI, Moves[i].getDestination()); |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1209 | const MachineLocation &Src = |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1210 | TranslateMachineLocation(MRI, Moves[i].getSource()); |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1211 | MCCFIInstruction Inst(Label, Dst, Src); |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 1212 | Instructions.push_back(Inst); |
| 1213 | } |
| 1214 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1215 | EmitCFIInstructions(streamer, Instructions, NULL); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1216 | |
| 1217 | // Padding |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 1218 | streamer.EmitValueToAlignment(IsEH |
| 1219 | ? 4 : context.getAsmInfo().getPointerSize()); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1220 | |
| 1221 | streamer.EmitLabel(sectionEnd); |
| 1222 | return *sectionStart; |
| 1223 | } |
| 1224 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1225 | MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer, |
| 1226 | const MCSymbol &cieStart, |
Rafael Espindola | 9f270da | 2011-05-10 03:01:39 +0000 | [diff] [blame] | 1227 | const MCDwarfFrameInfo &frame) { |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1228 | MCContext &context = streamer.getContext(); |
| 1229 | MCSymbol *fdeStart = context.CreateTempSymbol(); |
| 1230 | MCSymbol *fdeEnd = context.CreateTempSymbol(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1231 | const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1232 | bool verboseAsm = streamer.isVerboseAsm(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1233 | |
Evan Cheng | 5fbe5e7 | 2011-08-24 22:31:37 +0000 | [diff] [blame] | 1234 | if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) { |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1235 | MCSymbol *EHSym = |
| 1236 | context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh")); |
Rafael Espindola | 8bca410 | 2011-04-28 12:50:37 +0000 | [diff] [blame] | 1237 | streamer.EmitEHSymAttributes(frame.Function, EHSym); |
Rafael Espindola | a8cfb87 | 2011-04-28 02:46:42 +0000 | [diff] [blame] | 1238 | streamer.EmitLabel(EHSym); |
| 1239 | } |
| 1240 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1241 | // Length |
| 1242 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1243 | if (verboseAsm) streamer.AddComment("FDE Length"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 1244 | streamer.EmitAbsValue(Length, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1245 | |
| 1246 | streamer.EmitLabel(fdeStart); |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 1247 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1248 | // CIE Pointer |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 1249 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 1250 | if (IsEH) { |
| 1251 | const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart, |
| 1252 | 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1253 | if (verboseAsm) streamer.AddComment("FDE CIE Offset"); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 1254 | streamer.EmitAbsValue(offset, 4); |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 1255 | } else if (!asmInfo.doesDwarfRequireRelocationForSectionOffset()) { |
| 1256 | const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart, |
| 1257 | cieStart, 0); |
| 1258 | streamer.EmitAbsValue(offset, 4); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 1259 | } else { |
| 1260 | streamer.EmitSymbolValue(&cieStart, 4); |
| 1261 | } |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1262 | |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 1263 | unsigned fdeEncoding = MOFI->getFDEEncoding(UsingCFI); |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 1264 | unsigned size = getSizeForEncoding(streamer, fdeEncoding); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1265 | |
| 1266 | // PC Begin |
Rafael Espindola | 9989ef4 | 2011-05-10 22:28:35 +0000 | [diff] [blame] | 1267 | unsigned PCBeginEncoding = IsEH ? fdeEncoding : |
| 1268 | (unsigned)dwarf::DW_EH_PE_absptr; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1269 | unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1270 | EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1271 | |
| 1272 | // PC Range |
| 1273 | const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin, |
| 1274 | *frame.End, 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1275 | if (verboseAsm) streamer.AddComment("FDE address range"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 1276 | streamer.EmitAbsValue(Range, size); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1277 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1278 | if (IsEH) { |
| 1279 | // Augmentation Data Length |
| 1280 | unsigned augmentationLength = 0; |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1281 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1282 | if (frame.Lsda) |
| 1283 | augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding); |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1284 | |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1285 | if (verboseAsm) streamer.AddComment("Augmentation size"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1286 | streamer.EmitULEB128IntValue(augmentationLength); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1287 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1288 | // Augmentation Data |
| 1289 | if (frame.Lsda) |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1290 | EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding, |
| 1291 | "Language Specific Data Area"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1292 | } |
Rafael Espindola | 4892dff | 2011-04-29 03:06:29 +0000 | [diff] [blame] | 1293 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1294 | // Call Frame Instructions |
| 1295 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1296 | EmitCFIInstructions(streamer, frame.Instructions, frame.Begin); |
Rafael Espindola | 5bba084 | 2010-12-28 04:15:37 +0000 | [diff] [blame] | 1297 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1298 | // Padding |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1299 | streamer.EmitValueToAlignment(PCBeginSize); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1300 | |
| 1301 | return fdeEnd; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1304 | namespace { |
| 1305 | struct CIEKey { |
| 1306 | static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1); } |
| 1307 | static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0); } |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1308 | |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1309 | CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_, |
| 1310 | unsigned LsdaEncoding_) : Personality(Personality_), |
| 1311 | PersonalityEncoding(PersonalityEncoding_), |
| 1312 | LsdaEncoding(LsdaEncoding_) { |
| 1313 | } |
| 1314 | const MCSymbol* Personality; |
| 1315 | unsigned PersonalityEncoding; |
| 1316 | unsigned LsdaEncoding; |
| 1317 | }; |
| 1318 | } |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1319 | |
| 1320 | namespace llvm { |
| 1321 | template <> |
| 1322 | struct DenseMapInfo<CIEKey> { |
| 1323 | static CIEKey getEmptyKey() { |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1324 | return CIEKey::getEmptyKey(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1325 | } |
| 1326 | static CIEKey getTombstoneKey() { |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1327 | return CIEKey::getTombstoneKey(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1328 | } |
| 1329 | static unsigned getHashValue(const CIEKey &Key) { |
| 1330 | FoldingSetNodeID ID; |
| 1331 | ID.AddPointer(Key.Personality); |
| 1332 | ID.AddInteger(Key.PersonalityEncoding); |
| 1333 | ID.AddInteger(Key.LsdaEncoding); |
| 1334 | return ID.ComputeHash(); |
| 1335 | } |
| 1336 | static bool isEqual(const CIEKey &LHS, |
| 1337 | const CIEKey &RHS) { |
| 1338 | return LHS.Personality == RHS.Personality && |
| 1339 | LHS.PersonalityEncoding == RHS.PersonalityEncoding && |
| 1340 | LHS.LsdaEncoding == RHS.LsdaEncoding; |
| 1341 | } |
| 1342 | }; |
| 1343 | } |
| 1344 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1345 | void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, |
| 1346 | bool UsingCFI, |
| 1347 | bool IsEH) { |
| 1348 | MCContext &Context = Streamer.getContext(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1349 | MCObjectFileInfo *MOFI = |
| 1350 | const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo()); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1351 | FrameEmitterImpl Emitter(UsingCFI, IsEH); |
Bill Wendling | d8ab8e9 | 2011-09-06 18:37:11 +0000 | [diff] [blame] | 1352 | ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos(); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1353 | |
Bill Wendling | d8ab8e9 | 2011-09-06 18:37:11 +0000 | [diff] [blame] | 1354 | // Emit the compact unwind info if available. |
Bill Wendling | 7a13b72 | 2011-12-15 00:14:24 +0000 | [diff] [blame] | 1355 | if (IsEH && MOFI->getCompactUnwindSection()) |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1356 | for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) { |
| 1357 | const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i); |
Bill Wendling | a2ff3e2 | 2011-11-08 22:23:43 +0000 | [diff] [blame] | 1358 | if (Frame.CompactUnwindEncoding) |
Bill Wendling | d8ab8e9 | 2011-09-06 18:37:11 +0000 | [diff] [blame] | 1359 | Emitter.EmitCompactUnwind(Streamer, Frame); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1362 | const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() : |
| 1363 | *MOFI->getDwarfFrameSection(); |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1364 | Streamer.SwitchSection(&Section); |
| 1365 | MCSymbol *SectionStart = Context.CreateTempSymbol(); |
| 1366 | Streamer.EmitLabel(SectionStart); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1367 | Emitter.setSectionStart(SectionStart); |
Rafael Espindola | 9099813 | 2011-04-29 02:42:28 +0000 | [diff] [blame] | 1368 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1369 | MCSymbol *FDEEnd = NULL; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1370 | DenseMap<CIEKey, const MCSymbol*> CIEStarts; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 1371 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1372 | const MCSymbol *DummyDebugKey = NULL; |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1373 | for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) { |
| 1374 | const MCDwarfFrameInfo &Frame = FrameArray[i]; |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1375 | CIEKey Key(Frame.Personality, Frame.PersonalityEncoding, |
| 1376 | Frame.LsdaEncoding); |
| 1377 | const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey; |
| 1378 | if (!CIEStart) |
| 1379 | CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality, |
| 1380 | Frame.PersonalityEncoding, Frame.Lsda, |
| 1381 | Frame.LsdaEncoding); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1382 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1383 | FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1384 | |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1385 | if (i != n - 1) |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1386 | Streamer.EmitLabel(FDEEnd); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1387 | } |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 1388 | |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 1389 | Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize()); |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1390 | if (FDEEnd) |
| 1391 | Streamer.EmitLabel(FDEEnd); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1392 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1393 | |
| 1394 | void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer, |
| 1395 | uint64_t AddrDelta) { |
| 1396 | SmallString<256> Tmp; |
| 1397 | raw_svector_ostream OS(Tmp); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1398 | MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1399 | Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0); |
| 1400 | } |
| 1401 | |
| 1402 | void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta, |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1403 | raw_ostream &OS) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1404 | // FIXME: Assumes the code alignment factor is 1. |
Rafael Espindola | 3b78cdc | 2010-12-28 23:38:03 +0000 | [diff] [blame] | 1405 | if (AddrDelta == 0) { |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1406 | } else if (isUIntN(6, AddrDelta)) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1407 | uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta; |
| 1408 | OS << Opcode; |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1409 | } else if (isUInt<8>(AddrDelta)) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1410 | OS << uint8_t(dwarf::DW_CFA_advance_loc1); |
| 1411 | OS << uint8_t(AddrDelta); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1412 | } else if (isUInt<16>(AddrDelta)) { |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1413 | // FIXME: check what is the correct behavior on a big endian machine. |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1414 | OS << uint8_t(dwarf::DW_CFA_advance_loc2); |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1415 | OS << uint8_t( AddrDelta & 0xff); |
| 1416 | OS << uint8_t((AddrDelta >> 8) & 0xff); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1417 | } else { |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1418 | // FIXME: check what is the correct behavior on a big endian machine. |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1419 | assert(isUInt<32>(AddrDelta)); |
| 1420 | OS << uint8_t(dwarf::DW_CFA_advance_loc4); |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1421 | OS << uint8_t( AddrDelta & 0xff); |
| 1422 | OS << uint8_t((AddrDelta >> 8) & 0xff); |
| 1423 | OS << uint8_t((AddrDelta >> 16) & 0xff); |
| 1424 | OS << uint8_t((AddrDelta >> 24) & 0xff); |
| 1425 | |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |