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