Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 1 | //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===// |
| 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 | // This file implements the Dwarf emissions parts of AsmPrinter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "asm-printer" |
| 15 | #include "llvm/CodeGen/AsmPrinter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSymbol.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MachineLocation.h" |
| 23 | #include "llvm/Support/Dwarf.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetFrameLowering.h" |
Chris Lattner | e619c0d | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | aabc604 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | aabc604 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Dwarf Emission Helper Routines |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 35 | /// EmitSLEB128 - emit the specified signed leb128 value. |
David Blaikie | 5acff7e | 2013-06-23 18:31:11 +0000 | [diff] [blame] | 36 | void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const { |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 37 | if (isVerbose() && Desc) |
| 38 | OutStreamer.AddComment(Desc); |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 39 | |
Benjamin Kramer | c74798d | 2011-11-05 11:52:44 +0000 | [diff] [blame] | 40 | OutStreamer.EmitSLEB128IntValue(Value); |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /// EmitULEB128 - emit the specified signed leb128 value. |
David Blaikie | 5acff7e | 2013-06-23 18:31:11 +0000 | [diff] [blame] | 44 | void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc, |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 45 | unsigned PadTo) const { |
| 46 | if (isVerbose() && Desc) |
| 47 | OutStreamer.AddComment(Desc); |
Rafael Espindola | 38d0756 | 2010-11-04 18:17:08 +0000 | [diff] [blame] | 48 | |
Eric Christopher | bf7bc49 | 2013-01-09 03:52:05 +0000 | [diff] [blame] | 49 | OutStreamer.EmitULEB128IntValue(Value, PadTo); |
Chris Lattner | 9efd118 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | baf2be0 | 2010-04-04 20:01:25 +0000 | [diff] [blame] | 52 | /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value. |
| 53 | void AsmPrinter::EmitCFAByte(unsigned Val) const { |
| 54 | if (isVerbose()) { |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 55 | if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64) |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 56 | OutStreamer.AddComment("DW_CFA_offset + Reg (" + |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 57 | Twine(Val - dwarf::DW_CFA_offset) + ")"); |
Chris Lattner | baf2be0 | 2010-04-04 20:01:25 +0000 | [diff] [blame] | 58 | else |
| 59 | OutStreamer.AddComment(dwarf::CallFrameString(Val)); |
| 60 | } |
Eric Christopher | ce0cfce | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 61 | OutStreamer.EmitIntValue(Val, 1); |
Chris Lattner | baf2be0 | 2010-04-04 20:01:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 64 | static const char *DecodeDWARFEncoding(unsigned Encoding) { |
| 65 | switch (Encoding) { |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 66 | case dwarf::DW_EH_PE_absptr: |
| 67 | return "absptr"; |
| 68 | case dwarf::DW_EH_PE_omit: |
| 69 | return "omit"; |
| 70 | case dwarf::DW_EH_PE_pcrel: |
| 71 | return "pcrel"; |
| 72 | case dwarf::DW_EH_PE_udata4: |
| 73 | return "udata4"; |
| 74 | case dwarf::DW_EH_PE_udata8: |
| 75 | return "udata8"; |
| 76 | case dwarf::DW_EH_PE_sdata4: |
| 77 | return "sdata4"; |
| 78 | case dwarf::DW_EH_PE_sdata8: |
| 79 | return "sdata8"; |
| 80 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: |
| 81 | return "pcrel udata4"; |
| 82 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: |
| 83 | return "pcrel sdata4"; |
| 84 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: |
| 85 | return "pcrel udata8"; |
| 86 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: |
| 87 | return "pcrel sdata8"; |
| 88 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4 |
| 89 | : |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 90 | return "indirect pcrel udata4"; |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 91 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
| 92 | : |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 93 | return "indirect pcrel sdata4"; |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 94 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8 |
| 95 | : |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 96 | return "indirect pcrel udata8"; |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 97 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8 |
| 98 | : |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 99 | return "indirect pcrel sdata8"; |
| 100 | } |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 101 | |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 102 | return "<unknown encoding>"; |
| 103 | } |
| 104 | |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 105 | /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an |
| 106 | /// encoding. If verbose assembly output is enabled, we output comments |
| 107 | /// describing the encoding. Desc is an optional string saying what the |
| 108 | /// encoding is specifying (e.g. "LSDA"). |
Chris Lattner | aabc604 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 109 | void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const { |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 110 | if (isVerbose()) { |
| 111 | if (Desc != 0) |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 112 | OutStreamer.AddComment(Twine(Desc) + " Encoding = " + |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 113 | Twine(DecodeDWARFEncoding(Val))); |
| 114 | else |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 115 | OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val)); |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 116 | } |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 117 | |
Eric Christopher | ce0cfce | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 118 | OutStreamer.EmitIntValue(Val, 1); |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | e619c0d | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 121 | /// GetSizeOfEncodedValue - Return the size of the encoding in bytes. |
| 122 | unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const { |
| 123 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 124 | return 0; |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 125 | |
Chris Lattner | e619c0d | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 126 | switch (Encoding & 0x07) { |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 127 | default: |
| 128 | llvm_unreachable("Invalid encoded value."); |
| 129 | case dwarf::DW_EH_PE_absptr: |
| 130 | return TM.getDataLayout()->getPointerSize(); |
| 131 | case dwarf::DW_EH_PE_udata2: |
| 132 | return 2; |
| 133 | case dwarf::DW_EH_PE_udata4: |
| 134 | return 4; |
| 135 | case dwarf::DW_EH_PE_udata8: |
| 136 | return 8; |
Chris Lattner | e619c0d | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 140 | void AsmPrinter::EmitTTypeReference(const GlobalValue *GV, |
| 141 | unsigned Encoding) const { |
Anton Korobeynikov | 097b0e9 | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 142 | if (GV) { |
| 143 | const TargetLoweringObjectFile &TLOF = getObjFileLowering(); |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 144 | |
Anton Korobeynikov | 097b0e9 | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 145 | const MCExpr *Exp = |
Eric Christopher | 596077b | 2013-12-04 22:26:43 +0000 | [diff] [blame^] | 146 | TLOF.getTTypeGlobalReference(GV, Mang, MMI, Encoding, OutStreamer); |
Eric Christopher | ce0cfce | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 147 | OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding)); |
Anton Korobeynikov | 097b0e9 | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 148 | } else |
Eric Christopher | ce0cfce | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 149 | OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding)); |
Chris Lattner | e619c0d | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 151 | |
| 152 | /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its |
| 153 | /// section. This can be done with a special directive if the target supports |
| 154 | /// it (e.g. cygwin) or by emitting it as an offset from a label at the start |
| 155 | /// of the section. |
| 156 | /// |
| 157 | /// SectionLabel is a temporary label emitted at the start of the section that |
| 158 | /// Label lives in. |
| 159 | void AsmPrinter::EmitSectionOffset(const MCSymbol *Label, |
| 160 | const MCSymbol *SectionLabel) const { |
| 161 | // On COFF targets, we have to emit the special .secrel32 directive. |
Matt Arsenault | 034ca0f | 2013-04-22 22:49:11 +0000 | [diff] [blame] | 162 | if (MAI->needsDwarfSectionOffsetDirective()) { |
Rafael Espindola | d3df3d3 | 2011-12-17 01:14:52 +0000 | [diff] [blame] | 163 | OutStreamer.EmitCOFFSecRel32(Label); |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 167 | // Get the section that we're referring to, based on SectionLabel. |
| 168 | const MCSection &Section = SectionLabel->getSection(); |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 170 | // If Label has already been emitted, verify that it is in the same section as |
| 171 | // section label for sanity. |
| 172 | assert((!Label->isInSection() || &Label->getSection() == &Section) && |
| 173 | "Section offset using wrong section base for label"); |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 174 | |
Duncan Sands | b847bf5 | 2011-03-12 13:07:37 +0000 | [diff] [blame] | 175 | // If the section in question will end up with an address of 0 anyway, we can |
| 176 | // just emit an absolute reference to save a relocation. |
| 177 | if (Section.isBaseAddressKnownZero()) { |
Eric Christopher | ce0cfce | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 178 | OutStreamer.EmitSymbolValue(Label, 4); |
Duncan Sands | b847bf5 | 2011-03-12 13:07:37 +0000 | [diff] [blame] | 179 | return; |
| 180 | } |
Eric Christopher | 1d6bd41 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 182 | // Otherwise, emit it as a label difference from the start of the section. |
| 183 | EmitLabelDifference(Label, SectionLabel, 4); |
| 184 | } |
| 185 | |
Chris Lattner | aabc604 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 186 | //===----------------------------------------------------------------------===// |
| 187 | // Dwarf Lowering Routines |
| 188 | //===----------------------------------------------------------------------===// |
Chris Lattner | 70a4fce | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 189 | |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 190 | void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const { |
| 191 | switch (Inst.getOperation()) { |
| 192 | default: |
| 193 | llvm_unreachable("Unexpected instruction"); |
| 194 | case MCCFIInstruction::OpDefCfaOffset: |
| 195 | OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset()); |
| 196 | break; |
| 197 | case MCCFIInstruction::OpDefCfa: |
| 198 | OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset()); |
| 199 | break; |
| 200 | case MCCFIInstruction::OpDefCfaRegister: |
| 201 | OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister()); |
| 202 | break; |
| 203 | case MCCFIInstruction::OpOffset: |
| 204 | OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset()); |
| 205 | break; |
Venkatraman Govindaraju | 4c0cdd7 | 2013-09-26 15:11:00 +0000 | [diff] [blame] | 206 | case MCCFIInstruction::OpRegister: |
| 207 | OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2()); |
| 208 | break; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 209 | case MCCFIInstruction::OpWindowSave: |
| 210 | OutStreamer.EmitCFIWindowSave(); |
| 211 | break; |
Rafael Espindola | beb74c3 | 2011-04-15 20:32:03 +0000 | [diff] [blame] | 212 | } |
| 213 | } |