LLVM  6.0.0svn
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"
20 #include "llvm/CodeGen/DIE.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSection.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.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 unsigned leb128 value.
51 void AsmPrinter::EmitPaddedULEB128(uint64_t Value, unsigned PadTo,
52  const char *Desc) const {
53  if (isVerbose() && Desc)
54  OutStreamer->AddComment(Desc);
55 
56  OutStreamer->EmitPaddedULEB128IntValue(Value, PadTo);
57 }
58 
59 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc) const {
60  if (isVerbose() && Desc)
61  OutStreamer->AddComment(Desc);
62 
63  OutStreamer->EmitULEB128IntValue(Value);
64 }
65 
66 static const char *DecodeDWARFEncoding(unsigned Encoding) {
67  switch (Encoding) {
69  return "absptr";
71  return "omit";
73  return "pcrel";
75  return "udata4";
77  return "udata8";
79  return "sdata4";
81  return "sdata8";
83  return "pcrel udata4";
85  return "pcrel sdata4";
87  return "pcrel udata8";
89  return "pcrel sdata8";
91  :
92  return "indirect pcrel udata4";
94  :
95  return "indirect pcrel sdata4";
97  :
98  return "indirect pcrel udata8";
100  :
101  return "indirect pcrel sdata8";
102  }
103 
104  return "<unknown encoding>";
105 }
106 
107 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
108 /// encoding. If verbose assembly output is enabled, we output comments
109 /// describing the encoding. Desc is an optional string saying what the
110 /// encoding is specifying (e.g. "LSDA").
111 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
112  if (isVerbose()) {
113  if (Desc)
114  OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
115  Twine(DecodeDWARFEncoding(Val)));
116  else
117  OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
118  }
119 
120  OutStreamer->EmitIntValue(Val, 1);
121 }
122 
123 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
124 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
125  if (Encoding == dwarf::DW_EH_PE_omit)
126  return 0;
127 
128  switch (Encoding & 0x07) {
129  default:
130  llvm_unreachable("Invalid encoded value.");
132  return MF->getDataLayout().getPointerSize();
134  return 2;
136  return 4;
138  return 8;
139  }
140 }
141 
143  unsigned Encoding) const {
144  if (GV) {
146 
147  const MCExpr *Exp =
148  TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
149  OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
150  } else
151  OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
152 }
153 
155  bool ForceOffset) const {
156  if (!ForceOffset) {
157  // On COFF targets, we have to emit the special .secrel32 directive.
159  OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
160  return;
161  }
162 
163  // If the format uses relocations with dwarf, refer to the symbol directly.
165  OutStreamer->EmitSymbolValue(Label, 4);
166  return;
167  }
168  }
169 
170  // Otherwise, emit it as a label difference from the start of the section.
171  EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
172 }
173 
177  return;
178  }
179 
180  // Just emit the offset directly; no need for symbol math.
181  EmitInt32(S.getOffset());
182 }
183 
184 //===----------------------------------------------------------------------===//
185 // Dwarf Lowering Routines
186 //===----------------------------------------------------------------------===//
187 
189  switch (Inst.getOperation()) {
190  default:
191  llvm_unreachable("Unexpected instruction");
193  OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
194  break;
196  OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
197  break;
199  OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
200  break;
202  OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
203  break;
205  OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
206  break;
208  OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
209  break;
211  OutStreamer->EmitCFIWindowSave();
212  break;
214  OutStreamer->EmitCFISameValue(Inst.getRegister());
215  break;
217  OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
218  break;
220  OutStreamer->EmitCFIEscape(Inst.getValues());
221  break;
222  }
223 }
224 
225 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
226  // Emit the code (index) for the abbreviation.
227  if (isVerbose())
228  OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
229  Twine::utohexstr(Die.getOffset()) + ":0x" +
230  Twine::utohexstr(Die.getSize()) + " " +
231  dwarf::TagString(Die.getTag()));
233 
234  // Emit the DIE attribute values.
235  for (const auto &V : Die.values()) {
236  dwarf::Attribute Attr = V.getAttribute();
237  assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
238 
239  if (isVerbose()) {
240  OutStreamer->AddComment(dwarf::AttributeString(Attr));
241  if (Attr == dwarf::DW_AT_accessibility)
242  OutStreamer->AddComment(
243  dwarf::AccessibilityString(V.getDIEInteger().getValue()));
244  }
245 
246  // Emit an attribute using the defined form.
247  V.EmitValue(this);
248  }
249 
250  // Emit the DIE children if any.
251  if (Die.hasChildren()) {
252  for (auto &Child : Die.children())
253  emitDwarfDIE(Child);
254 
255  OutStreamer->AddComment("End Of Children Mark");
256  EmitInt8(0);
257  }
258 }
259 
260 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
261  // Emit the abbreviations code (base 1 index.)
262  EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
263 
264  // Emit the abbreviations data.
265  Abbrev.Emit(this);
266 }
unsigned getSize() const
Definition: DIE.h:701
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:193
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void EmitInt8(int Value) const
Emit a byte directive and value.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MCSection & getSection(bool SetUsed=true) const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:268
Attribute
Attributes.
Definition: Dwarf.h:105
child_range children()
Definition: DIE.h:710
const MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:96
bool hasChildren() const
Definition: DIE.h:702
bool needsDwarfSectionOffsetDirective() const
Definition: MCAsmInfo.h:453
void emitDwarfSymbolReference(const MCSymbol *Label, bool ForceOffset=false) const
Emit a reference to a symbol for use in dwarf.
void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const
Emit the 4-byte offset of a string from the start of its section.
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
String pool entry reference.
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:72
bool doesDwarfUseRelocationsAcrossSections() const
Definition: MCAsmInfo.h:574
bool isVerbose() const
Return true if assembly output should contain comments.
Definition: AsmPrinter.h:191
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:99
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
void EmitInt32(int Value) const
Emit a long directive and value.
void Emit(const AsmPrinter *AP) const
Print the abbreviation using the specified asm printer.
Definition: DIE.cpp:68
void EmitPaddedULEB128(uint64_t Value, unsigned PadTo, const char *Desc=nullptr) const
Emit the specified unsigned leb128 value padded to a specific number bytes.
StringRef AccessibilityString(unsigned Access)
Definition: Dwarf.cpp:255
unsigned getRegister2() const
Definition: MCDwarf.h:480
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:84
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:605
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:80
int getOffset() const
Definition: MCDwarf.h:485
OpType getOperation() const
Definition: MCDwarf.h:469
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:492
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:472
void emitCFIInstruction(const MachineInstr &MI)
Definition: AsmPrinter.cpp:921
void EmitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:395
MCSymbol * getBeginSymbol()
Definition: MCSection.h:106
void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const
Emit reference to a ttype global with a specified encoding.
static const char * DecodeDWARFEncoding(unsigned Encoding)
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:21
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 getAbbrevNumber() const
Definition: DIE.h:697
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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