LLVM  3.7.0
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 "DwarfDebug.h"
16 #include "DwarfExpression.h"
17 #include "llvm/ADT/Twine.h"
19 #include "llvm/CodeGen/DIE.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Support/Dwarf.h"
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "asm-printer"
37 
38 //===----------------------------------------------------------------------===//
39 // Dwarf Emission Helper Routines
40 //===----------------------------------------------------------------------===//
41 
42 /// EmitSLEB128 - emit the specified signed leb128 value.
43 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
44  if (isVerbose() && Desc)
45  OutStreamer->AddComment(Desc);
46 
47  OutStreamer->EmitSLEB128IntValue(Value);
48 }
49 
50 /// EmitULEB128 - emit the specified signed leb128 value.
51 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
52  unsigned PadTo) const {
53  if (isVerbose() && Desc)
54  OutStreamer->AddComment(Desc);
55 
56  OutStreamer->EmitULEB128IntValue(Value, PadTo);
57 }
58 
59 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
60 void AsmPrinter::EmitCFAByte(unsigned Val) const {
61  if (isVerbose()) {
62  if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
63  OutStreamer->AddComment("DW_CFA_offset + Reg (" +
64  Twine(Val - dwarf::DW_CFA_offset) + ")");
65  else
66  OutStreamer->AddComment(dwarf::CallFrameString(Val));
67  }
68  OutStreamer->EmitIntValue(Val, 1);
69 }
70 
71 static const char *DecodeDWARFEncoding(unsigned Encoding) {
72  switch (Encoding) {
74  return "absptr";
76  return "omit";
78  return "pcrel";
80  return "udata4";
82  return "udata8";
84  return "sdata4";
86  return "sdata8";
88  return "pcrel udata4";
90  return "pcrel sdata4";
92  return "pcrel udata8";
94  return "pcrel sdata8";
96  :
97  return "indirect pcrel udata4";
99  :
100  return "indirect pcrel sdata4";
102  :
103  return "indirect pcrel udata8";
105  :
106  return "indirect pcrel sdata8";
107  }
108 
109  return "<unknown encoding>";
110 }
111 
112 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
113 /// encoding. If verbose assembly output is enabled, we output comments
114 /// describing the encoding. Desc is an optional string saying what the
115 /// encoding is specifying (e.g. "LSDA").
116 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
117  if (isVerbose()) {
118  if (Desc)
119  OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
120  Twine(DecodeDWARFEncoding(Val)));
121  else
122  OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
123  }
124 
125  OutStreamer->EmitIntValue(Val, 1);
126 }
127 
128 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
129 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
130  if (Encoding == dwarf::DW_EH_PE_omit)
131  return 0;
132 
133  switch (Encoding & 0x07) {
134  default:
135  llvm_unreachable("Invalid encoded value.");
137  return TM.getDataLayout()->getPointerSize();
139  return 2;
141  return 4;
143  return 8;
144  }
145 }
146 
148  unsigned Encoding) const {
149  if (GV) {
151 
152  const MCExpr *Exp =
153  TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI,
154  *OutStreamer);
155  OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
156  } else
157  OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
158 }
159 
161  bool ForceOffset) const {
162  if (!ForceOffset) {
163  // On COFF targets, we have to emit the special .secrel32 directive.
165  OutStreamer->EmitCOFFSecRel32(Label);
166  return;
167  }
168 
169  // If the format uses relocations with dwarf, refer to the symbol directly.
171  OutStreamer->EmitSymbolValue(Label, 4);
172  return;
173  }
174  }
175 
176  // Otherwise, emit it as a label difference from the start of the section.
177  EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
178 }
179 
183  return;
184  }
185 
186  // Just emit the offset directly; no need for symbol math.
187  EmitInt32(S.getOffset());
188 }
189 
190 /// EmitDwarfRegOp - Emit dwarf register operation.
192  const MachineLocation &MLoc) const {
194  getDwarfDebug()->getDwarfVersion(), Streamer);
195  const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
196  int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
197  if (Reg < 0) {
198  // We assume that pointers are always in an addressable register.
199  if (MLoc.isIndirect())
200  // FIXME: We have no reasonable way of handling errors in here. The
201  // caller might be in the middle of a dwarf expression. We should
202  // probably assert that Reg >= 0 once debug info generation is more
203  // mature.
204  return Expr.EmitOp(dwarf::DW_OP_nop,
205  "nop (could not find a dwarf register number)");
206 
207  // Attempt to find a valid super- or sub-register.
208  if (!Expr.AddMachineRegPiece(MLoc.getReg()))
209  Expr.EmitOp(dwarf::DW_OP_nop,
210  "nop (could not find a dwarf register number)");
211  return;
212  }
213 
214  if (MLoc.isIndirect())
215  Expr.AddRegIndirect(Reg, MLoc.getOffset());
216  else
217  Expr.AddReg(Reg);
218 }
219 
220 //===----------------------------------------------------------------------===//
221 // Dwarf Lowering Routines
222 //===----------------------------------------------------------------------===//
223 
225  switch (Inst.getOperation()) {
226  default:
227  llvm_unreachable("Unexpected instruction");
229  OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
230  break;
232  OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
233  break;
235  OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
236  break;
238  OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
239  break;
241  OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
242  break;
244  OutStreamer->EmitCFIWindowSave();
245  break;
247  OutStreamer->EmitCFISameValue(Inst.getRegister());
248  break;
249  }
250 }
251 
252 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
253  // Emit the code (index) for the abbreviation.
254  if (isVerbose())
255  OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
256  Twine::utohexstr(Die.getOffset()) + ":0x" +
257  Twine::utohexstr(Die.getSize()) + " " +
258  dwarf::TagString(Die.getTag()));
260 
261  // Emit the DIE attribute values.
262  for (const auto &V : Die.values()) {
263  dwarf::Attribute Attr = V.getAttribute();
264  assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
265 
266  if (isVerbose()) {
267  OutStreamer->AddComment(dwarf::AttributeString(Attr));
268  if (Attr == dwarf::DW_AT_accessibility)
269  OutStreamer->AddComment(
270  dwarf::AccessibilityString(V.getDIEInteger().getValue()));
271  }
272 
273  // Emit an attribute using the defined form.
274  V.EmitValue(this);
275  }
276 
277  // Emit the DIE children if any.
278  if (Die.hasChildren()) {
279  for (auto &Child : Die.children())
280  emitDwarfDIE(Child);
281 
282  OutStreamer->AddComment("End Of Children Mark");
283  EmitInt8(0);
284  }
285 }
286 
287 void
288 AsmPrinter::emitDwarfAbbrevs(const std::vector<DIEAbbrev *>& Abbrevs) const {
289  // For each abbrevation.
290  for (const DIEAbbrev *Abbrev : Abbrevs) {
291  // Emit the abbrevations code (base 1 index.)
292  EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
293 
294  // Emit the abbreviations data.
295  Abbrev->Emit(this);
296  }
297 
298  // Mark end of abbreviations.
299  EmitULEB128(0, "EOM(3)");
300 }
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:83
virtual void EmitDwarfRegOp(ByteStreamer &BS, const MachineLocation &MLoc) const
EmitDwarfRegOp - Emit a dwarf register operation.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
void EmitInt8(int Value) const
Emit a byte directive and value.
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...
child_range children()
Definition: DIE.h:673
const MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:86
const char * AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:46
void EmitInt32(int Value) const
Emit a long directive and value.
bool isIndirect() const
unsigned getRegister() const
Definition: MCDwarf.h:444
int getOffset() const
Definition: MCDwarf.h:457
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
const char * TagString(unsigned Tag)
Definition: Dwarf.cpp:21
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
void emitDwarfDIE(const DIE &Die) const
Recursively emit Dwarf DIE tree.
String pool entry reference.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Reg
All possible values of the reg field in the ModR/M byte.
void EmitEncodingByte(unsigned Val, const char *Desc=nullptr) const
Emit a .byte 42 directive that corresponds to an encoding.
DwarfExpression implementation for .debug_loc entries.
Mangler * Mang
Name-mangler for global names.
Definition: AsmPrinter.h:93
void emitDwarfSymbolReference(const MCSymbol *Label, bool ForceOffset=false) const
Emit a reference to a symbol for use in dwarf.
dwarf::Tag getTag() const
Definition: DIE.h:663
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:89
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
bool needsDwarfSectionOffsetDirective() const
Definition: MCAsmInfo.h:425
bool hasChildren() const
Definition: DIE.h:666
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:74
unsigned getAbbrevNumber() const
Definition: DIE.h:662
unsigned GetSizeOfEncodedValue(unsigned Encoding) const
Return the size of the encoding in bytes.
void EmitULEB128(uint64_t Value, const char *Desc=nullptr, unsigned PadTo=0) const
Emit the specified unsigned leb128 value.
DIE - A structured debug information entry.
Definition: DIE.h:623
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:70
DwarfDebug * getDwarfDebug()
Definition: AsmPrinter.h:147
unsigned getSize() const
Definition: DIE.h:665
void emitDwarfAbbrevs(const std::vector< DIEAbbrev * > &Abbrevs) const
Emit Dwarf abbreviation table.
OpType getOperation() const
Definition: MCDwarf.h:441
void emitCFIInstruction(const MachineInstr &MI)
Definition: AsmPrinter.cpp:756
const MCContext & getContext() const
const DataLayout * getDataLayout() const
Deprecated in 3.7, will be removed in 3.8.
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:264
unsigned getOffset() const
Definition: DIE.h:664
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:386
MCSymbol * getBeginSymbol()
Definition: MCSection.h:113
unsigned getRegister2() const
Definition: MCDwarf.h:452
void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const
Emit reference to a ttype global with a specified encoding.
static const char * DecodeDWARFEncoding(unsigned Encoding)
virtual const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 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...
const char * CallFrameString(unsigned Encoding)
Definition: Dwarf.cpp:449
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:227
DIEAbbrev - Dwarf abbreviation, describes the organization of a debug information object...
Definition: DIE.h:59
void EmitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
const char * AccessibilityString(unsigned Access)
Definition: Dwarf.cpp:303
void EmitCFAByte(unsigned Val) const
Emit a .byte 42 directive for a DW_CFA_xxx value.
void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const
Emit the 4-byte offset of a string from the start of its section.
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:134
LLVM Value Representation.
Definition: Value.h:69
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
value_range values()
Definition: DIE.h:683
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:593
unsigned getReg() const
bool isVerbose() const
Return true if assembly output should contain comments.
Definition: AsmPrinter.h:152
unsigned getDwarfVersion() const
Returns the Dwarf Version.
Definition: DwarfDebug.h:576
bool doesDwarfUseRelocationsAcrossSections() const
Definition: MCAsmInfo.h:527