Chris Lattner | 7e1a8f8 | 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 | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSymbol.h" |
Chandler Carruth | d04a8d4 | 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 | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetFrameLowering.h" |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 02b86b9 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 02b86b9 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Dwarf Emission Helper Routines |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 35 | /// EmitSLEB128 - emit the specified signed leb128 value. |
| 36 | void AsmPrinter::EmitSLEB128(int Value, const char *Desc) const { |
| 37 | if (isVerbose() && Desc) |
| 38 | OutStreamer.AddComment(Desc); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 39 | |
Benjamin Kramer | c25c908 | 2011-11-05 11:52:44 +0000 | [diff] [blame] | 40 | OutStreamer.EmitSLEB128IntValue(Value); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /// EmitULEB128 - emit the specified signed leb128 value. |
| 44 | void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc, |
| 45 | unsigned PadTo) const { |
| 46 | if (isVerbose() && Desc) |
| 47 | OutStreamer.AddComment(Desc); |
Rafael Espindola | 7387345 | 2010-11-04 18:17:08 +0000 | [diff] [blame] | 48 | |
Benjamin Kramer | c25c908 | 2011-11-05 11:52:44 +0000 | [diff] [blame] | 49 | OutStreamer.EmitULEB128IntValue(Value, 0/*addrspace*/, PadTo); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | 7a101f4 | 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()) { |
| 55 | if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64) |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 56 | OutStreamer.AddComment("DW_CFA_offset + Reg (" + |
Chris Lattner | 7a101f4 | 2010-04-04 20:01:25 +0000 | [diff] [blame] | 57 | Twine(Val-dwarf::DW_CFA_offset) + ")"); |
| 58 | else |
| 59 | OutStreamer.AddComment(dwarf::CallFrameString(Val)); |
| 60 | } |
| 61 | OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/); |
| 62 | } |
| 63 | |
Chris Lattner | ca6190b | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 64 | static const char *DecodeDWARFEncoding(unsigned Encoding) { |
| 65 | switch (Encoding) { |
| 66 | case dwarf::DW_EH_PE_absptr: return "absptr"; |
| 67 | case dwarf::DW_EH_PE_omit: return "omit"; |
| 68 | case dwarf::DW_EH_PE_pcrel: return "pcrel"; |
| 69 | case dwarf::DW_EH_PE_udata4: return "udata4"; |
| 70 | case dwarf::DW_EH_PE_udata8: return "udata8"; |
| 71 | case dwarf::DW_EH_PE_sdata4: return "sdata4"; |
| 72 | case dwarf::DW_EH_PE_sdata8: return "sdata8"; |
| 73 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4"; |
| 74 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4"; |
| 75 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8"; |
| 76 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8"; |
| 77 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4: |
| 78 | return "indirect pcrel udata4"; |
| 79 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4: |
| 80 | return "indirect pcrel sdata4"; |
| 81 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8: |
| 82 | return "indirect pcrel udata8"; |
| 83 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8: |
| 84 | return "indirect pcrel sdata8"; |
| 85 | } |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 86 | |
Chris Lattner | ca6190b | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 87 | return "<unknown encoding>"; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an |
| 92 | /// encoding. If verbose assembly output is enabled, we output comments |
| 93 | /// describing the encoding. Desc is an optional string saying what the |
| 94 | /// encoding is specifying (e.g. "LSDA"). |
Chris Lattner | 02b86b9 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 95 | void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const { |
Chris Lattner | ca6190b | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 96 | if (isVerbose()) { |
| 97 | if (Desc != 0) |
| 98 | OutStreamer.AddComment(Twine(Desc)+" Encoding = " + |
| 99 | Twine(DecodeDWARFEncoding(Val))); |
| 100 | else |
| 101 | OutStreamer.AddComment(Twine("Encoding = ") + |
| 102 | DecodeDWARFEncoding(Val)); |
| 103 | } |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 104 | |
Chris Lattner | ca6190b | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 105 | OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/); |
| 106 | } |
| 107 | |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 108 | /// GetSizeOfEncodedValue - Return the size of the encoding in bytes. |
| 109 | unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const { |
| 110 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 111 | return 0; |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 112 | |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 113 | switch (Encoding & 0x07) { |
Craig Topper | 5e25ee8 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 114 | default: llvm_unreachable("Invalid encoded value."); |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 115 | case dwarf::DW_EH_PE_absptr: return TM.getDataLayout()->getPointerSize(); |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 116 | case dwarf::DW_EH_PE_udata2: return 2; |
| 117 | case dwarf::DW_EH_PE_udata4: return 4; |
| 118 | case dwarf::DW_EH_PE_udata8: return 8; |
| 119 | } |
| 120 | } |
| 121 | |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 122 | void AsmPrinter::EmitTTypeReference(const GlobalValue *GV, |
| 123 | unsigned Encoding) const { |
Anton Korobeynikov | 239938f | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 124 | if (GV) { |
| 125 | const TargetLoweringObjectFile &TLOF = getObjFileLowering(); |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 126 | |
Anton Korobeynikov | 239938f | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 127 | const MCExpr *Exp = |
| 128 | TLOF.getTTypeGlobalReference(GV, Mang, MMI, Encoding, OutStreamer); |
| 129 | OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0); |
| 130 | } else |
| 131 | OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding), 0); |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 133 | |
| 134 | /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its |
| 135 | /// section. This can be done with a special directive if the target supports |
| 136 | /// it (e.g. cygwin) or by emitting it as an offset from a label at the start |
| 137 | /// of the section. |
| 138 | /// |
| 139 | /// SectionLabel is a temporary label emitted at the start of the section that |
| 140 | /// Label lives in. |
| 141 | void AsmPrinter::EmitSectionOffset(const MCSymbol *Label, |
| 142 | const MCSymbol *SectionLabel) const { |
| 143 | // On COFF targets, we have to emit the special .secrel32 directive. |
Rafael Espindola | 8f7d12c | 2011-12-17 01:14:52 +0000 | [diff] [blame] | 144 | if (MAI->getDwarfSectionOffsetDirective()) { |
| 145 | OutStreamer.EmitCOFFSecRel32(Label); |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 146 | return; |
| 147 | } |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 149 | // Get the section that we're referring to, based on SectionLabel. |
| 150 | const MCSection &Section = SectionLabel->getSection(); |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 152 | // If Label has already been emitted, verify that it is in the same section as |
| 153 | // section label for sanity. |
| 154 | assert((!Label->isInSection() || &Label->getSection() == &Section) && |
| 155 | "Section offset using wrong section base for label"); |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 156 | |
Duncan Sands | ba99346 | 2011-03-12 13:07:37 +0000 | [diff] [blame] | 157 | // If the section in question will end up with an address of 0 anyway, we can |
| 158 | // just emit an absolute reference to save a relocation. |
| 159 | if (Section.isBaseAddressKnownZero()) { |
| 160 | OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/); |
| 161 | return; |
| 162 | } |
Eric Christopher | aaa50b9 | 2012-11-20 20:34:47 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 164 | // Otherwise, emit it as a label difference from the start of the section. |
| 165 | EmitLabelDifference(Label, SectionLabel, 4); |
| 166 | } |
| 167 | |
Chris Lattner | 02b86b9 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
| 169 | // Dwarf Lowering Routines |
| 170 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 171 | |
Rafael Espindola | ffa6408 | 2011-05-06 15:28:56 +0000 | [diff] [blame] | 172 | /// EmitCFIFrameMove - Emit a frame instruction. |
Rafael Espindola | 126ae68 | 2011-04-15 20:32:03 +0000 | [diff] [blame] | 173 | void AsmPrinter::EmitCFIFrameMove(const MachineMove &Move) const { |
| 174 | const TargetRegisterInfo *RI = TM.getRegisterInfo(); |
| 175 | |
| 176 | const MachineLocation &Dst = Move.getDestination(); |
| 177 | const MachineLocation &Src = Move.getSource(); |
| 178 | |
| 179 | // If advancing cfa. |
| 180 | if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { |
Rafael Espindola | 126ae68 | 2011-04-15 20:32:03 +0000 | [diff] [blame] | 181 | if (Src.getReg() == MachineLocation::VirtualFP) { |
| 182 | OutStreamer.EmitCFIDefCfaOffset(-Src.getOffset()); |
| 183 | } else { |
Rafael Espindola | 126ae68 | 2011-04-15 20:32:03 +0000 | [diff] [blame] | 184 | // Reg + Offset |
Jim Grosbach | 2b6f711 | 2011-05-20 21:23:17 +0000 | [diff] [blame] | 185 | OutStreamer.EmitCFIDefCfa(RI->getDwarfRegNum(Src.getReg(), true), |
| 186 | Src.getOffset()); |
Rafael Espindola | 126ae68 | 2011-04-15 20:32:03 +0000 | [diff] [blame] | 187 | } |
| 188 | } else if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) { |
| 189 | assert(Dst.isReg() && "Machine move not supported yet."); |
| 190 | OutStreamer.EmitCFIDefCfaRegister(RI->getDwarfRegNum(Dst.getReg(), true)); |
| 191 | } else { |
| 192 | assert(!Dst.isReg() && "Machine move not supported yet."); |
| 193 | OutStreamer.EmitCFIOffset(RI->getDwarfRegNum(Src.getReg(), true), |
Rafael Espindola | b28d4f1 | 2011-04-26 03:58:56 +0000 | [diff] [blame] | 194 | Dst.getOffset()); |
Rafael Espindola | 126ae68 | 2011-04-15 20:32:03 +0000 | [diff] [blame] | 195 | } |
| 196 | } |