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