DIE.cpp revision 2df938ad713140c352b2d81b49490e337c018891
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 "llvm/ADT/Twine.h"
16#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCSymbol.h"
21#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/Format.h"
25#include "llvm/Support/FormattedStream.h"
26using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// DIEAbbrevData Implementation
30//===----------------------------------------------------------------------===//
31
32/// Profile - Used to gather unique data for the abbreviation folding set.
33///
34void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
35  ID.AddInteger(Attribute);
36  ID.AddInteger(Form);
37}
38
39//===----------------------------------------------------------------------===//
40// DIEAbbrev Implementation
41//===----------------------------------------------------------------------===//
42
43/// Profile - Used to gather unique data for the abbreviation folding set.
44///
45void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
46  ID.AddInteger(Tag);
47  ID.AddInteger(ChildrenFlag);
48
49  // For each attribute description.
50  for (unsigned i = 0, N = Data.size(); i < N; ++i)
51    Data[i].Profile(ID);
52}
53
54/// Emit - Print the abbreviation using the specified asm printer.
55///
56void DIEAbbrev::Emit(AsmPrinter *AP) const {
57  // Emit its Dwarf tag type.
58  // FIXME: Doing work even in non-asm-verbose runs.
59  AP->EmitULEB128(Tag, dwarf::TagString(Tag));
60
61  // Emit whether it has children DIEs.
62  // FIXME: Doing work even in non-asm-verbose runs.
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    // FIXME: Doing work even in non-asm-verbose runs.
71    AP->EmitULEB128(AttrData.getAttribute(),
72                    dwarf::AttributeString(AttrData.getAttribute()));
73
74    // Emit form type.
75    // FIXME: Doing work even in non-asm-verbose runs.
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
115/// Climb up the parent chain to get the compile unit DIE this DIE belongs to.
116DIE *DIE::getCompileUnit() const{
117  DIE *p = getParent();
118  while (p) {
119    if (p->getTag() == dwarf::DW_TAG_compile_unit)
120      return p;
121    p = p->getParent();
122  }
123  llvm_unreachable("We should not have orphaned DIEs.");
124}
125
126#ifndef NDEBUG
127void DIE::print(raw_ostream &O, unsigned IncIndent) {
128  IndentCount += IncIndent;
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 SmallVector<DIEAbbrevData, 12> &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, 4);
168  }
169
170  if (!isBlock) O << "\n";
171  IndentCount -= IncIndent;
172}
173
174void DIE::dump() {
175  print(dbgs());
176}
177#endif
178
179void DIEValue::anchor() { }
180
181#ifndef NDEBUG
182void DIEValue::dump() {
183  print(dbgs());
184}
185#endif
186
187//===----------------------------------------------------------------------===//
188// DIEInteger Implementation
189//===----------------------------------------------------------------------===//
190
191/// EmitValue - Emit integer of appropriate size.
192///
193void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
194  unsigned Size = ~0U;
195  switch (Form) {
196  case dwarf::DW_FORM_flag_present:
197    // Emit something to keep the lines and comments in sync.
198    // FIXME: Is there a better way to do this?
199    if (Asm->OutStreamer.hasRawTextSupport())
200      Asm->OutStreamer.EmitRawText(StringRef(""));
201    return;
202  case dwarf::DW_FORM_flag:  // Fall thru
203  case dwarf::DW_FORM_ref1:  // Fall thru
204  case dwarf::DW_FORM_data1: Size = 1; break;
205  case dwarf::DW_FORM_ref2:  // Fall thru
206  case dwarf::DW_FORM_data2: Size = 2; break;
207  case dwarf::DW_FORM_sec_offset: // Fall thru
208  case dwarf::DW_FORM_ref4:  // Fall thru
209  case dwarf::DW_FORM_data4: Size = 4; break;
210  case dwarf::DW_FORM_ref8:  // Fall thru
211  case dwarf::DW_FORM_data8: Size = 8; break;
212  case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
213  case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
214  case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
215  case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
216  case dwarf::DW_FORM_addr:
217    Size = Asm->getDataLayout().getPointerSize(); break;
218  default: llvm_unreachable("DIE Value form not supported yet");
219  }
220  Asm->OutStreamer.EmitIntValue(Integer, Size);
221}
222
223/// SizeOf - Determine size of integer value in bytes.
224///
225unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
226  switch (Form) {
227  case dwarf::DW_FORM_flag_present: return 0;
228  case dwarf::DW_FORM_flag:  // Fall thru
229  case dwarf::DW_FORM_ref1:  // Fall thru
230  case dwarf::DW_FORM_data1: return sizeof(int8_t);
231  case dwarf::DW_FORM_ref2:  // Fall thru
232  case dwarf::DW_FORM_data2: return sizeof(int16_t);
233  case dwarf::DW_FORM_sec_offset: // Fall thru
234  case dwarf::DW_FORM_ref4:  // Fall thru
235  case dwarf::DW_FORM_data4: return sizeof(int32_t);
236  case dwarf::DW_FORM_ref8:  // Fall thru
237  case dwarf::DW_FORM_data8: return sizeof(int64_t);
238  case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
239  case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
240  case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
241  case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
242  case dwarf::DW_FORM_addr:  return AP->getDataLayout().getPointerSize();
243  default: llvm_unreachable("DIE Value form not supported yet");
244  }
245}
246
247#ifndef NDEBUG
248void DIEInteger::print(raw_ostream &O) {
249  O << "Int: " << (int64_t)Integer << "  0x";
250  O.write_hex(Integer);
251}
252#endif
253
254//===----------------------------------------------------------------------===//
255// DIELabel Implementation
256//===----------------------------------------------------------------------===//
257
258/// EmitValue - Emit label value.
259///
260void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
261  AP->OutStreamer.EmitSymbolValue(Label, SizeOf(AP, Form));
262}
263
264/// SizeOf - Determine size of label value in bytes.
265///
266unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
267  if (Form == dwarf::DW_FORM_data4) return 4;
268  if (Form == dwarf::DW_FORM_sec_offset) return 4;
269  if (Form == dwarf::DW_FORM_strp) return 4;
270  return AP->getDataLayout().getPointerSize();
271}
272
273#ifndef NDEBUG
274void DIELabel::print(raw_ostream &O) {
275  O << "Lbl: " << Label->getName();
276}
277#endif
278
279//===----------------------------------------------------------------------===//
280// DIEDelta Implementation
281//===----------------------------------------------------------------------===//
282
283/// EmitValue - Emit delta value.
284///
285void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
286  AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
287}
288
289/// SizeOf - Determine size of delta value in bytes.
290///
291unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
292  if (Form == dwarf::DW_FORM_data4) return 4;
293  if (Form == dwarf::DW_FORM_strp) return 4;
294  return AP->getDataLayout().getPointerSize();
295}
296
297#ifndef NDEBUG
298void DIEDelta::print(raw_ostream &O) {
299  O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
300}
301#endif
302
303//===----------------------------------------------------------------------===//
304// DIEEntry Implementation
305//===----------------------------------------------------------------------===//
306
307/// EmitValue - Emit debug information entry offset.
308///
309void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
310  AP->EmitInt32(Entry->getOffset());
311}
312
313#ifndef NDEBUG
314void DIEEntry::print(raw_ostream &O) {
315  O << format("Die: 0x%lx", (long)(intptr_t)Entry);
316}
317#endif
318
319//===----------------------------------------------------------------------===//
320// DIEBlock Implementation
321//===----------------------------------------------------------------------===//
322
323/// ComputeSize - calculate the size of the block.
324///
325unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
326  if (!Size) {
327    const SmallVector<DIEAbbrevData, 12> &AbbrevData = Abbrev.getData();
328    for (unsigned i = 0, N = Values.size(); i < N; ++i)
329      Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
330  }
331
332  return Size;
333}
334
335/// EmitValue - Emit block data.
336///
337void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
338  switch (Form) {
339  default: llvm_unreachable("Improper form for block");
340  case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
341  case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
342  case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
343  case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
344  }
345
346  const SmallVector<DIEAbbrevData, 12> &AbbrevData = Abbrev.getData();
347  for (unsigned i = 0, N = Values.size(); i < N; ++i)
348    Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
349}
350
351/// SizeOf - Determine size of block data in bytes.
352///
353unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
354  switch (Form) {
355  case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
356  case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
357  case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
358  case dwarf::DW_FORM_block:  return Size + MCAsmInfo::getULEB128Size(Size);
359  default: llvm_unreachable("Improper form for block");
360  }
361}
362
363#ifndef NDEBUG
364void DIEBlock::print(raw_ostream &O) {
365  O << "Blk: ";
366  DIE::print(O, 5);
367}
368#endif
369