LLVM  8.0.1
AsmPrinterDwarf.cpp
Go to the documentation of this file.
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 #include "ByteStreamer.h"
15 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/DIE.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "asm-printer"
33 
34 //===----------------------------------------------------------------------===//
35 // Dwarf Emission Helper Routines
36 //===----------------------------------------------------------------------===//
37 
38 /// EmitSLEB128 - emit the specified signed leb128 value.
39 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
40  if (isVerbose() && Desc)
41  OutStreamer->AddComment(Desc);
42 
43  OutStreamer->EmitSLEB128IntValue(Value);
44 }
45 
46 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc) const {
47  if (isVerbose() && Desc)
48  OutStreamer->AddComment(Desc);
49 
50  OutStreamer->EmitULEB128IntValue(Value);
51 }
52 
53 /// Emit something like ".uleb128 Hi-Lo".
55  const MCSymbol *Lo) const {
56  OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
57 }
58 
59 static const char *DecodeDWARFEncoding(unsigned Encoding) {
60  switch (Encoding) {
62  return "absptr";
64  return "omit";
66  return "pcrel";
68  return "uleb128";
70  return "sleb128";
72  return "udata4";
74  return "udata8";
76  return "sdata4";
78  return "sdata8";
80  return "pcrel udata4";
82  return "pcrel sdata4";
84  return "pcrel udata8";
86  return "pcrel sdata8";
88  :
89  return "indirect pcrel udata4";
91  :
92  return "indirect pcrel sdata4";
94  :
95  return "indirect pcrel udata8";
97  :
98  return "indirect pcrel sdata8";
99  }
100 
101  return "<unknown encoding>";
102 }
103 
104 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
105 /// encoding. If verbose assembly output is enabled, we output comments
106 /// describing the encoding. Desc is an optional string saying what the
107 /// encoding is specifying (e.g. "LSDA").
108 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
109  if (isVerbose()) {
110  if (Desc)
111  OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
112  Twine(DecodeDWARFEncoding(Val)));
113  else
114  OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
115  }
116 
117  OutStreamer->EmitIntValue(Val, 1);
118 }
119 
120 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
121 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
122  if (Encoding == dwarf::DW_EH_PE_omit)
123  return 0;
124 
125  switch (Encoding & 0x07) {
126  default:
127  llvm_unreachable("Invalid encoded value.");
129  return MF->getDataLayout().getPointerSize();
131  return 2;
133  return 4;
135  return 8;
136  }
137 }
138 
140  unsigned Encoding) const {
141  if (GV) {
143 
144  const MCExpr *Exp =
145  TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
146  OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
147  } else
148  OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
149 }
150 
152  bool ForceOffset) const {
153  if (!ForceOffset) {
154  // On COFF targets, we have to emit the special .secrel32 directive.
156  OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
157  return;
158  }
159 
160  // If the format uses relocations with dwarf, refer to the symbol directly.
162  OutStreamer->EmitSymbolValue(Label, 4);
163  return;
164  }
165  }
166 
167  // Otherwise, emit it as a label difference from the start of the section.
168  EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
169 }
170 
173  assert(S.Symbol && "No symbol available");
175  return;
176  }
177 
178  // Just emit the offset directly; no need for symbol math.
179  emitInt32(S.Offset);
180 }
181 
182 void AsmPrinter::EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
183  EmitLabelPlusOffset(Label, Offset, MAI->getCodePointerSize());
184 }
185 
186 //===----------------------------------------------------------------------===//
187 // Dwarf Lowering Routines
188 //===----------------------------------------------------------------------===//
189 
191  switch (Inst.getOperation()) {
192  default:
193  llvm_unreachable("Unexpected instruction");
195  OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
196  break;
198  OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
199  break;
201  OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
202  break;
204  OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
205  break;
207  OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
208  break;
210  OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
211  break;
213  OutStreamer->EmitCFIWindowSave();
214  break;
216  OutStreamer->EmitCFINegateRAState();
217  break;
219  OutStreamer->EmitCFISameValue(Inst.getRegister());
220  break;
222  OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
223  break;
225  OutStreamer->EmitCFIEscape(Inst.getValues());
226  break;
228  OutStreamer->EmitCFIRestore(Inst.getRegister());
229  break;
230  }
231 }
232 
233 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
234  // Emit the code (index) for the abbreviation.
235  if (isVerbose())
236  OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
237  Twine::utohexstr(Die.getOffset()) + ":0x" +
238  Twine::utohexstr(Die.getSize()) + " " +
239  dwarf::TagString(Die.getTag()));
241 
242  // Emit the DIE attribute values.
243  for (const auto &V : Die.values()) {
244  dwarf::Attribute Attr = V.getAttribute();
245  assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
246 
247  if (isVerbose()) {
248  OutStreamer->AddComment(dwarf::AttributeString(Attr));
249  if (Attr == dwarf::DW_AT_accessibility)
250  OutStreamer->AddComment(
251  dwarf::AccessibilityString(V.getDIEInteger().getValue()));
252  }
253 
254  // Emit an attribute using the defined form.
255  V.EmitValue(this);
256  }
257 
258  // Emit the DIE children if any.
259  if (Die.hasChildren()) {
260  for (auto &Child : Die.children())
261  emitDwarfDIE(Child);
262 
263  OutStreamer->AddComment("End Of Children Mark");
264  emitInt8(0);
265  }
266 }
267 
268 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
269  // Emit the abbreviations code (base 1 index.)
270  EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
271 
272  // Emit the abbreviations data.
273  Abbrev.Emit(this);
274 }
unsigned getSize() const
Definition: DIE.h:701
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:212
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:94
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
Attribute
Attributes.
Definition: Dwarf.h:115
child_range children()
Definition: DIE.h:710
bool hasChildren() const
Definition: DIE.h:702
bool needsDwarfSectionOffsetDirective() const
Definition: MCAsmInfo.h:467
void emitDwarfSymbolReference(const MCSymbol *Label, bool ForceOffset=false) const
Emit a reference to a symbol for use in dwarf.
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:97
void emitDwarfDIE(const DIE &Die) const
Recursively emit Dwarf DIE tree.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
StringRef AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:73
bool doesDwarfUseRelocationsAcrossSections() const
Definition: MCAsmInfo.h:590
bool isVerbose() const
Return true if assembly output should contain comments.
Definition: AsmPrinter.h:203
Data for a string pool entry.
void EmitLabelDifferenceAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) const
Emit something like ".uleb128 Hi-Lo".
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:100
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label+Offset" where the size in bytes of the directive is specified by Siz...
void Emit(const AsmPrinter *AP) const
Print the abbreviation using the specified asm printer.
Definition: DIE.cpp:69
StringRef AccessibilityString(unsigned Access)
Definition: Dwarf.cpp:256
unsigned getRegister2() const
Definition: MCDwarf.h:569
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:85
value_range values()
Definition: DIE.h:650
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:629
A structured debug information entry.
Definition: DIE.h:662
void EmitEncodingByte(unsigned Val, const char *Desc=nullptr) const
Emit a .byte 42 directive that corresponds to an encoding.
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:82
int getOffset() const
Definition: MCDwarf.h:574
OpType getOperation() const
Definition: MCDwarf.h:558
virtual const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const
Return an MCExpr to use for a reference to the specified global variable from exception handling info...
StringRef getValues() const
Definition: MCDwarf.h:581
unsigned GetSizeOfEncodedValue(unsigned Encoding) const
Return the size of the encoding in bytes.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned getRegister() const
Definition: MCDwarf.h:561
void emitCFIInstruction(const MachineInstr &MI)
Definition: AsmPrinter.cpp:968
void EmitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
void emitDwarfStringOffset(DwarfStringPoolEntry S) const
Emit the 4-byte offset of a string from the start of its section.
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:385
MCSymbol * getBeginSymbol()
Definition: MCSection.h:110
void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const
Emit reference to a ttype global with a specified encoding.
void emitInt32(int Value) const
Emit a long directive and value.
static const char * DecodeDWARFEncoding(unsigned Encoding)
This file contains constants used for implementing Dwarf debug support.
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:267
void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const
dwarf::Tag getTag() const
Definition: DIE.h:698
Dwarf abbreviation, describes the organization of a debug information object.
Definition: DIE.h:79
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:22
void EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const
Emit something like ".long Label + Offset".
void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) const
Emit something like ".long Hi-Lo" where the size in bytes of the directive is specified by Size and H...
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition: MCAsmInfo.h:396
unsigned getAbbrevNumber() const
Definition: DIE.h:697
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void emitInt8(int Value) const
Emit a byte directive and value.
LLVM Value Representation.
Definition: Value.h:73
unsigned getNumber() const
Definition: DIE.h:101
void EmitULEB128(uint64_t Value, const char *Desc=nullptr) const
Emit the specified unsigned leb128 value.
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:700