DIE.cpp revision 873cf0a0d7906083578d9b793008348750636138
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/MC/MCAsmInfo.h"
18#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCSymbol.h"
20#include "llvm/Target/TargetData.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#ifndef NDEBUG
116void DIE::print(raw_ostream &O, unsigned IncIndent) {
117  IndentCount += IncIndent;
118  const std::string Indent(IndentCount, ' ');
119  bool isBlock = Abbrev.getTag() == 0;
120
121  if (!isBlock) {
122    O << Indent
123      << "Die: "
124      << format("0x%lx", (long)(intptr_t)this)
125      << ", Offset: " << Offset
126      << ", Size: " << Size << "\n";
127
128    O << Indent
129      << dwarf::TagString(Abbrev.getTag())
130      << " "
131      << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
132  } else {
133    O << "Size: " << Size << "\n";
134  }
135
136  const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
137
138  IndentCount += 2;
139  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
140    O << Indent;
141
142    if (!isBlock)
143      O << dwarf::AttributeString(Data[i].getAttribute());
144    else
145      O << "Blk[" << i << "]";
146
147    O <<  "  "
148      << dwarf::FormEncodingString(Data[i].getForm())
149      << " ";
150    Values[i]->print(O);
151    O << "\n";
152  }
153  IndentCount -= 2;
154
155  for (unsigned j = 0, M = Children.size(); j < M; ++j) {
156    Children[j]->print(O, 4);
157  }
158
159  if (!isBlock) O << "\n";
160  IndentCount -= IncIndent;
161}
162
163void DIE::dump() {
164  print(dbgs());
165}
166#endif
167
168void DIEValue::anchor() { }
169
170#ifndef NDEBUG
171void DIEValue::dump() {
172  print(dbgs());
173}
174#endif
175
176//===----------------------------------------------------------------------===//
177// DIEInteger Implementation
178//===----------------------------------------------------------------------===//
179
180/// EmitValue - Emit integer of appropriate size.
181///
182void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
183  unsigned Size = ~0U;
184  switch (Form) {
185  case dwarf::DW_FORM_flag_present:
186    // Emit something to keep the lines and comments in sync.
187    // FIXME: Is there a better way to do this?
188    if (Asm->OutStreamer.hasRawTextSupport())
189      Asm->OutStreamer.EmitRawText(StringRef(""));
190    return;
191  case dwarf::DW_FORM_flag:  // Fall thru
192  case dwarf::DW_FORM_ref1:  // Fall thru
193  case dwarf::DW_FORM_data1: Size = 1; break;
194  case dwarf::DW_FORM_ref2:  // Fall thru
195  case dwarf::DW_FORM_data2: Size = 2; break;
196  case dwarf::DW_FORM_ref4:  // Fall thru
197  case dwarf::DW_FORM_data4: Size = 4; break;
198  case dwarf::DW_FORM_ref8:  // Fall thru
199  case dwarf::DW_FORM_data8: Size = 8; break;
200  case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
201  case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
202  case dwarf::DW_FORM_addr:  Size = Asm->getTargetData().getPointerSize(); break;
203  default: llvm_unreachable("DIE Value form not supported yet");
204  }
205  Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
206}
207
208/// SizeOf - Determine size of integer value in bytes.
209///
210unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
211  switch (Form) {
212  case dwarf::DW_FORM_flag_present: // Fall thru
213    return 0;
214  case dwarf::DW_FORM_flag:  // Fall thru
215  case dwarf::DW_FORM_ref1:  // Fall thru
216  case dwarf::DW_FORM_data1: return sizeof(int8_t);
217  case dwarf::DW_FORM_ref2:  // Fall thru
218  case dwarf::DW_FORM_data2: return sizeof(int16_t);
219  case dwarf::DW_FORM_ref4:  // Fall thru
220  case dwarf::DW_FORM_data4: return sizeof(int32_t);
221  case dwarf::DW_FORM_ref8:  // Fall thru
222  case dwarf::DW_FORM_data8: return sizeof(int64_t);
223  case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
224  case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
225  case dwarf::DW_FORM_addr:  return AP->getTargetData().getPointerSize();
226  default: llvm_unreachable("DIE Value form not supported yet");
227  }
228}
229
230#ifndef NDEBUG
231void DIEInteger::print(raw_ostream &O) {
232  O << "Int: " << (int64_t)Integer << "  0x";
233  O.write_hex(Integer);
234}
235#endif
236
237//===----------------------------------------------------------------------===//
238// DIELabel Implementation
239//===----------------------------------------------------------------------===//
240
241/// EmitValue - Emit label value.
242///
243void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
244  AP->OutStreamer.EmitSymbolValue(Label, SizeOf(AP, Form), 0/*AddrSpace*/);
245}
246
247/// SizeOf - Determine size of label value in bytes.
248///
249unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
250  if (Form == dwarf::DW_FORM_data4) return 4;
251  if (Form == dwarf::DW_FORM_strp) return 4;
252  return AP->getTargetData().getPointerSize();
253}
254
255#ifndef NDEBUG
256void DIELabel::print(raw_ostream &O) {
257  O << "Lbl: " << Label->getName();
258}
259#endif
260
261//===----------------------------------------------------------------------===//
262// DIEDelta Implementation
263//===----------------------------------------------------------------------===//
264
265/// EmitValue - Emit delta value.
266///
267void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
268  AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
269}
270
271/// SizeOf - Determine size of delta value in bytes.
272///
273unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
274  if (Form == dwarf::DW_FORM_data4) return 4;
275  if (Form == dwarf::DW_FORM_strp) return 4;
276  return AP->getTargetData().getPointerSize();
277}
278
279#ifndef NDEBUG
280void DIEDelta::print(raw_ostream &O) {
281  O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
282}
283#endif
284
285//===----------------------------------------------------------------------===//
286// DIEEntry Implementation
287//===----------------------------------------------------------------------===//
288
289/// EmitValue - Emit debug information entry offset.
290///
291void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
292  AP->EmitInt32(Entry->getOffset());
293}
294
295#ifndef NDEBUG
296void DIEEntry::print(raw_ostream &O) {
297  O << format("Die: 0x%lx", (long)(intptr_t)Entry);
298}
299#endif
300
301//===----------------------------------------------------------------------===//
302// DIEBlock Implementation
303//===----------------------------------------------------------------------===//
304
305/// ComputeSize - calculate the size of the block.
306///
307unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
308  if (!Size) {
309    const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
310    for (unsigned i = 0, N = Values.size(); i < N; ++i)
311      Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
312  }
313
314  return Size;
315}
316
317/// EmitValue - Emit block data.
318///
319void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
320  switch (Form) {
321  default: llvm_unreachable("Improper form for block");
322  case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
323  case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
324  case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
325  case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
326  }
327
328  const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
329  for (unsigned i = 0, N = Values.size(); i < N; ++i)
330    Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
331}
332
333/// SizeOf - Determine size of block data in bytes.
334///
335unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
336  switch (Form) {
337  case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
338  case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
339  case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
340  case dwarf::DW_FORM_block:  return Size + MCAsmInfo::getULEB128Size(Size);
341  default: llvm_unreachable("Improper form for block");
342  }
343}
344
345#ifndef NDEBUG
346void DIEBlock::print(raw_ostream &O) {
347  O << "Blk: ";
348  DIE::print(O, 5);
349}
350#endif
351