LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DIE.cpp
Go to the documentation of this file.
1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
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 // Data structures for DWARF info entries.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "DIE.h"
15 #include "DwarfDebug.h"
16 #include "llvm/ADT/Twine.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Format.h"
27 #include "llvm/Support/MD5.h"
28 using namespace llvm;
29 
30 //===----------------------------------------------------------------------===//
31 // DIEAbbrevData Implementation
32 //===----------------------------------------------------------------------===//
33 
34 /// Profile - Used to gather unique data for the abbreviation folding set.
35 ///
37  // Explicitly cast to an integer type for which FoldingSetNodeID has
38  // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
39  ID.AddInteger(unsigned(Attribute));
40  ID.AddInteger(unsigned(Form));
41 }
42 
43 //===----------------------------------------------------------------------===//
44 // DIEAbbrev Implementation
45 //===----------------------------------------------------------------------===//
46 
47 /// Profile - Used to gather unique data for the abbreviation folding set.
48 ///
50  ID.AddInteger(unsigned(Tag));
51  ID.AddInteger(ChildrenFlag);
52 
53  // For each attribute description.
54  for (unsigned i = 0, N = Data.size(); i < N; ++i)
55  Data[i].Profile(ID);
56 }
57 
58 /// Emit - Print the abbreviation using the specified asm printer.
59 ///
60 void DIEAbbrev::Emit(AsmPrinter *AP) const {
61  // Emit its Dwarf tag type.
62  AP->EmitULEB128(Tag, dwarf::TagString(Tag));
63 
64  // Emit whether it has children DIEs.
65  AP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
66 
67  // For each attribute description.
68  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
69  const DIEAbbrevData &AttrData = Data[i];
70 
71  // Emit attribute type.
72  AP->EmitULEB128(AttrData.getAttribute(),
74 
75  // Emit form type.
76  AP->EmitULEB128(AttrData.getForm(),
78  }
79 
80  // Mark end of abbreviation.
81  AP->EmitULEB128(0, "EOM(1)");
82  AP->EmitULEB128(0, "EOM(2)");
83 }
84 
85 #ifndef NDEBUG
87  O << "Abbreviation @"
88  << format("0x%lx", (long)(intptr_t)this)
89  << " "
90  << dwarf::TagString(Tag)
91  << " "
92  << dwarf::ChildrenString(ChildrenFlag)
93  << '\n';
94 
95  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
96  O << " "
97  << dwarf::AttributeString(Data[i].getAttribute())
98  << " "
99  << dwarf::FormEncodingString(Data[i].getForm())
100  << '\n';
101  }
102 }
103 void DIEAbbrev::dump() { print(dbgs()); }
104 #endif
105 
106 //===----------------------------------------------------------------------===//
107 // DIE Implementation
108 //===----------------------------------------------------------------------===//
109 
111  for (unsigned i = 0, N = Children.size(); i < N; ++i)
112  delete Children[i];
113 }
114 
115 /// Climb up the parent chain to get the compile unit DIE to which this DIE
116 /// belongs.
117 const DIE *DIE::getCompileUnit() const {
118  const DIE *Cu = getCompileUnitOrNull();
119  assert(Cu && "We should not have orphaned DIEs.");
120  return Cu;
121 }
122 
123 /// Climb up the parent chain to get the compile unit DIE this DIE belongs
124 /// to. Return NULL if DIE is not added to an owner yet.
126  const DIE *p = this;
127  while (p) {
128  if (p->getTag() == dwarf::DW_TAG_compile_unit)
129  return p;
130  p = p->getParent();
131  }
132  return NULL;
133 }
134 
137  const DIEAbbrev &Abbrevs = getAbbrev();
138 
139  // Iterate through all the attributes until we find the one we're
140  // looking for, if we can't find it return NULL.
141  for (size_t i = 0; i < Values.size(); ++i)
142  if (Abbrevs.getData()[i].getAttribute() == Attribute)
143  return Values[i];
144  return NULL;
145 }
146 
147 #ifndef NDEBUG
148 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
149  const std::string Indent(IndentCount, ' ');
150  bool isBlock = Abbrev.getTag() == 0;
151 
152  if (!isBlock) {
153  O << Indent
154  << "Die: "
155  << format("0x%lx", (long)(intptr_t)this)
156  << ", Offset: " << Offset
157  << ", Size: " << Size << "\n";
158 
159  O << Indent
161  << " "
163  } else {
164  O << "Size: " << Size << "\n";
165  }
166 
168 
169  IndentCount += 2;
170  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
171  O << Indent;
172 
173  if (!isBlock)
174  O << dwarf::AttributeString(Data[i].getAttribute());
175  else
176  O << "Blk[" << i << "]";
177 
178  O << " "
179  << dwarf::FormEncodingString(Data[i].getForm())
180  << " ";
181  Values[i]->print(O);
182  O << "\n";
183  }
184  IndentCount -= 2;
185 
186  for (unsigned j = 0, M = Children.size(); j < M; ++j) {
187  Children[j]->print(O, IndentCount+4);
188  }
189 
190  if (!isBlock) O << "\n";
191 }
192 
193 void DIE::dump() {
194  print(dbgs());
195 }
196 #endif
197 
198 void DIEValue::anchor() { }
199 
200 #ifndef NDEBUG
201 void DIEValue::dump() const {
202  print(dbgs());
203 }
204 #endif
205 
206 //===----------------------------------------------------------------------===//
207 // DIEInteger Implementation
208 //===----------------------------------------------------------------------===//
209 
210 /// EmitValue - Emit integer of appropriate size.
211 ///
212 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
213  unsigned Size = ~0U;
214  switch (Form) {
215  case dwarf::DW_FORM_flag_present:
216  // Emit something to keep the lines and comments in sync.
217  // FIXME: Is there a better way to do this?
218  if (Asm->OutStreamer.hasRawTextSupport())
219  Asm->OutStreamer.EmitRawText("");
220  return;
221  case dwarf::DW_FORM_flag: // Fall thru
222  case dwarf::DW_FORM_ref1: // Fall thru
223  case dwarf::DW_FORM_data1: Size = 1; break;
224  case dwarf::DW_FORM_ref2: // Fall thru
225  case dwarf::DW_FORM_data2: Size = 2; break;
226  case dwarf::DW_FORM_sec_offset: // Fall thru
227  case dwarf::DW_FORM_ref4: // Fall thru
228  case dwarf::DW_FORM_data4: Size = 4; break;
229  case dwarf::DW_FORM_ref8: // Fall thru
230  case dwarf::DW_FORM_data8: Size = 8; break;
231  case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
232  case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
233  case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
234  case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
235  case dwarf::DW_FORM_addr:
236  Size = Asm->getDataLayout().getPointerSize(); break;
237  default: llvm_unreachable("DIE Value form not supported yet");
238  }
239  Asm->OutStreamer.EmitIntValue(Integer, Size);
240 }
241 
242 /// SizeOf - Determine size of integer value in bytes.
243 ///
244 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
245  switch (Form) {
246  case dwarf::DW_FORM_flag_present: return 0;
247  case dwarf::DW_FORM_flag: // Fall thru
248  case dwarf::DW_FORM_ref1: // Fall thru
249  case dwarf::DW_FORM_data1: return sizeof(int8_t);
250  case dwarf::DW_FORM_ref2: // Fall thru
251  case dwarf::DW_FORM_data2: return sizeof(int16_t);
252  case dwarf::DW_FORM_sec_offset: // Fall thru
253  case dwarf::DW_FORM_ref4: // Fall thru
254  case dwarf::DW_FORM_data4: return sizeof(int32_t);
255  case dwarf::DW_FORM_ref8: // Fall thru
256  case dwarf::DW_FORM_data8: return sizeof(int64_t);
257  case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
258  case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
259  case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
260  case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
261  case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
262  default: llvm_unreachable("DIE Value form not supported yet");
263  }
264 }
265 
266 #ifndef NDEBUG
268  O << "Int: " << (int64_t)Integer << " 0x";
269  O.write_hex(Integer);
270 }
271 #endif
272 
273 //===----------------------------------------------------------------------===//
274 // DIEExpr Implementation
275 //===----------------------------------------------------------------------===//
276 
277 /// EmitValue - Emit expression value.
278 ///
279 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
280  AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
281 }
282 
283 /// SizeOf - Determine size of expression value in bytes.
284 ///
285 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
286  if (Form == dwarf::DW_FORM_data4) return 4;
287  if (Form == dwarf::DW_FORM_sec_offset) return 4;
288  if (Form == dwarf::DW_FORM_strp) return 4;
289  return AP->getDataLayout().getPointerSize();
290 }
291 
292 #ifndef NDEBUG
293 void DIEExpr::print(raw_ostream &O) const {
294  O << "Expr: ";
295  Expr->print(O);
296 }
297 #endif
298 
299 //===----------------------------------------------------------------------===//
300 // DIELabel Implementation
301 //===----------------------------------------------------------------------===//
302 
303 /// EmitValue - Emit label value.
304 ///
305 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
306  AP->EmitLabelReference(Label, SizeOf(AP, Form),
307  Form == dwarf::DW_FORM_strp ||
308  Form == dwarf::DW_FORM_sec_offset ||
309  Form == dwarf::DW_FORM_ref_addr);
310 }
311 
312 /// SizeOf - Determine size of label value in bytes.
313 ///
314 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
315  if (Form == dwarf::DW_FORM_data4) return 4;
316  if (Form == dwarf::DW_FORM_sec_offset) return 4;
317  if (Form == dwarf::DW_FORM_strp) return 4;
318  return AP->getDataLayout().getPointerSize();
319 }
320 
321 #ifndef NDEBUG
322 void DIELabel::print(raw_ostream &O) const {
323  O << "Lbl: " << Label->getName();
324 }
325 #endif
326 
327 //===----------------------------------------------------------------------===//
328 // DIEDelta Implementation
329 //===----------------------------------------------------------------------===//
330 
331 /// EmitValue - Emit delta value.
332 ///
333 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
334  AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
335 }
336 
337 /// SizeOf - Determine size of delta value in bytes.
338 ///
339 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
340  if (Form == dwarf::DW_FORM_data4) return 4;
341  if (Form == dwarf::DW_FORM_strp) return 4;
342  return AP->getDataLayout().getPointerSize();
343 }
344 
345 #ifndef NDEBUG
346 void DIEDelta::print(raw_ostream &O) const {
347  O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
348 }
349 #endif
350 
351 //===----------------------------------------------------------------------===//
352 // DIEString Implementation
353 //===----------------------------------------------------------------------===//
354 
355 /// EmitValue - Emit string value.
356 ///
357 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
358  Access->EmitValue(AP, Form);
359 }
360 
361 /// SizeOf - Determine size of delta value in bytes.
362 ///
363 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
364  return Access->SizeOf(AP, Form);
365 }
366 
367 #ifndef NDEBUG
369  O << "String: " << Str << "\tSymbol: ";
370  Access->print(O);
371 }
372 #endif
373 
374 //===----------------------------------------------------------------------===//
375 // DIEEntry Implementation
376 //===----------------------------------------------------------------------===//
377 
378 /// EmitValue - Emit debug information entry offset.
379 ///
380 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
381  AP->EmitInt32(Entry->getOffset());
382 }
383 
385  // DWARF4: References that use the attribute form DW_FORM_ref_addr are
386  // specified to be four bytes in the DWARF 32-bit format and eight bytes
387  // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
388  // references have the same size as an address on the target system.
389  if (AP->getDwarfDebug()->getDwarfVersion() == 2)
390  return AP->getDataLayout().getPointerSize();
391  return sizeof(int32_t);
392 }
393 
394 #ifndef NDEBUG
395 void DIEEntry::print(raw_ostream &O) const {
396  O << format("Die: 0x%lx", (long)(intptr_t)Entry);
397 }
398 #endif
399 
400 //===----------------------------------------------------------------------===//
401 // DIEBlock Implementation
402 //===----------------------------------------------------------------------===//
403 
404 /// ComputeSize - calculate the size of the block.
405 ///
407  if (!Size) {
408  const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
409  for (unsigned i = 0, N = Values.size(); i < N; ++i)
410  Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
411  }
412 
413  return Size;
414 }
415 
416 /// EmitValue - Emit block data.
417 ///
418 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
419  switch (Form) {
420  default: llvm_unreachable("Improper form for block");
421  case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
422  case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
423  case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
424  case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
425  }
426 
427  const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
428  for (unsigned i = 0, N = Values.size(); i < N; ++i)
429  Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
430 }
431 
432 /// SizeOf - Determine size of block data in bytes.
433 ///
434 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
435  switch (Form) {
436  case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
437  case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
438  case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
439  case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
440  default: llvm_unreachable("Improper form for block");
441  }
442 }
443 
444 #ifndef NDEBUG
445 void DIEBlock::print(raw_ostream &O) const {
446  O << "Blk: ";
447  DIE::print(O, 5);
448 }
449 #endif
std::vector< DIE * > Children
Definition: DIE.h:125
virtual ~DIE()
Definition: DIE.cpp:110
const char * FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:266
void EmitRawText(const Twine &String)
Definition: MCStreamer.cpp:582
virtual void print(raw_ostream &O) const
Definition: DIE.cpp:293
const DataLayout & getDataLayout() const
getDataLayout - Return information about data layout.
Definition: AsmPrinter.cpp:134
void EmitInt8(int Value) const
virtual void print(raw_ostream &O) const
Definition: DIE.cpp:322
virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:339
unsigned getPointerSize(unsigned AS=0) const
Definition: DataLayout.h:261
void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) const
virtual bool hasRawTextSupport() const
Definition: MCStreamer.h:198
DIEValue * findAttribute(uint16_t Attribute)
Definition: DIE.cpp:135
void EmitInt32(int Value) const
void EmitLabelReference(const MCSymbol *Label, unsigned Size, bool IsSectionRelative=false) const
Definition: AsmPrinter.h:375
const DIE * getCompileUnitOrNull() const
Definition: DIE.cpp:125
virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:363
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:418
void print(raw_ostream &O, unsigned IndentCount=0) const
Definition: DIE.cpp:148
void Profile(FoldingSetNodeID &ID) const
Definition: DIE.cpp:49
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:357
dwarf::Form getForm() const
Definition: DIE.h:46
void dump() const
Definition: DIE.cpp:201
static unsigned getSLEB128Size(int64_t Value)
Definition: MCAsmInfo.cpp:106
#define llvm_unreachable(msg)
unsigned Offset
Definition: DIE.h:113
void AddInteger(signed I)
Definition: FoldingSet.cpp:60
void dump()
Definition: DIE.cpp:193
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
format_object1< T > format(const char *Fmt, const T &Val)
Definition: Format.h:180
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:279
virtual void print(raw_ostream &O) const
Definition: DIE.cpp:445
raw_ostream & write_hex(unsigned long long N)
write_hex - Output N in hexadecimal, without any prefix or padding.
MCStreamer & OutStreamer
Definition: AsmPrinter.h:78
void EmitInt16(int Value) const
DIEAbbrev & getAbbrev()
Definition: DIE.h:140
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:380
unsigned Size
Definition: DIE.h:117
dwarf::Tag getTag() const
Definition: DIE.h:143
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Definition: MCStreamer.cpp:104
void EmitValue(const MCExpr *Value, unsigned Size)
Definition: MCStreamer.cpp:141
Definition: DIE.h:109
DIEAbbrev Abbrev
Definition: DIE.h:121
virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const =0
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const =0
virtual void print(raw_ostream &O) const
Definition: DIE.cpp:346
DIE * getParent() const
Definition: DIE.h:148
void EmitSLEB128(int64_t Value, const char *Desc=0) const
EmitSLEB128 - emit the specified signed leb128 value.
const DwarfDebug * getDwarfDebug() const
Definition: AsmPrinter.h:126
dwarf::Attribute getAttribute() const
Definition: DIE.h:45
virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:285
const DIE * getCompileUnit() const
Definition: DIE.cpp:117
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:212
const char * TagString(unsigned Tag)
Definition: Dwarf.cpp:22
unsigned getOffset() const
Definition: DIE.h:144
virtual void print(raw_ostream &O) const
Definition: DIE.cpp:368
const SmallVectorImpl< DIEValue * > & getValues() const
Definition: DIE.h:147
SmallVector< DIEValue *, 12 > Values
Definition: DIE.h:131
void Profile(FoldingSetNodeID &ID) const
Definition: DIE.cpp:36
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:305
const char * AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:115
void print(raw_ostream &OS) const
Definition: MCExpr.cpp:31
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:70
void EmitULEB128(uint64_t Value, const char *Desc=0, unsigned PadTo=0) const
EmitULEB128 - emit the specified unsigned leb128 value.
virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:314
virtual void print(raw_ostream &O) const
Definition: DIE.cpp:267
#define N
virtual void print(raw_ostream &O) const =0
void print(raw_ostream &O)
Definition: DIE.cpp:86
const SmallVectorImpl< DIEAbbrevData > & getData() const
Definition: DIE.h:80
virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:333
virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:244
unsigned ComputeSize(AsmPrinter *AP)
Definition: DIE.cpp:406
dwarf::Tag getTag() const
Definition: DIE.h:77
static unsigned getULEB128Size(uint64_t Value)
Definition: MCAsmInfo.cpp:97
uint16_t getChildrenFlag() const
Definition: DIE.h:79
void Emit(AsmPrinter *AP) const
Definition: DIE.cpp:60
static unsigned getRefAddrSize(AsmPrinter *AP)
Returns size of a ref_addr entry.
Definition: DIE.cpp:384
const char * ChildrenString(unsigned Children)
Definition: Dwarf.cpp:105
unsigned getDwarfVersion() const
Returns the Dwarf Version.
Definition: DwarfDebug.h:727
virtual void print(raw_ostream &O) const
Definition: DIE.cpp:395
void dump()
Definition: DIE.cpp:103
virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:434