DIE.cpp revision e76111a0ed9970f98e133d6b3e8d697c94298e26
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 "DwarfPrinter.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/CodeGen/AsmPrinter.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCSymbol.h"
21#include "llvm/Target/TargetData.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(const DwarfPrinter *DP) const {
57  // Emit its Dwarf tag type.
58  // FIXME: Doing work even in non-asm-verbose runs.
59  DP->EmitULEB128(Tag, dwarf::TagString(Tag));
60
61  // Emit whether it has children DIEs.
62  // FIXME: Doing work even in non-asm-verbose runs.
63  DP->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    DP->EmitULEB128(AttrData.getAttribute(),
72                    dwarf::AttributeString(AttrData.getAttribute()));
73
74    // Emit form type.
75    // FIXME: Doing work even in non-asm-verbose runs.
76    DP->EmitULEB128(AttrData.getForm(),
77                    dwarf::FormEncodingString(AttrData.getForm()));
78  }
79
80  // Mark end of abbreviation.
81  DP->EmitULEB128(0, "EOM(1)");
82  DP->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/// addSiblingOffset - Add a sibling offset field to the front of the DIE.
116///
117DIEValue *DIE::addSiblingOffset() {
118  DIEInteger *DI = new DIEInteger(0);
119  Values.insert(Values.begin(), DI);
120  Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
121  return DI;
122}
123
124#ifndef NDEBUG
125void DIE::print(raw_ostream &O, unsigned IncIndent) {
126  IndentCount += IncIndent;
127  const std::string Indent(IndentCount, ' ');
128  bool isBlock = Abbrev.getTag() == 0;
129
130  if (!isBlock) {
131    O << Indent
132      << "Die: "
133      << format("0x%lx", (long)(intptr_t)this)
134      << ", Offset: " << Offset
135      << ", Size: " << Size << "\n";
136
137    O << Indent
138      << dwarf::TagString(Abbrev.getTag())
139      << " "
140      << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
141  } else {
142    O << "Size: " << Size << "\n";
143  }
144
145  const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
146
147  IndentCount += 2;
148  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
149    O << Indent;
150
151    if (!isBlock)
152      O << dwarf::AttributeString(Data[i].getAttribute());
153    else
154      O << "Blk[" << i << "]";
155
156    O <<  "  "
157      << dwarf::FormEncodingString(Data[i].getForm())
158      << " ";
159    Values[i]->print(O);
160    O << "\n";
161  }
162  IndentCount -= 2;
163
164  for (unsigned j = 0, M = Children.size(); j < M; ++j) {
165    Children[j]->print(O, 4);
166  }
167
168  if (!isBlock) O << "\n";
169  IndentCount -= IncIndent;
170}
171
172void DIE::dump() {
173  print(dbgs());
174}
175#endif
176
177
178#ifndef NDEBUG
179void DIEValue::dump() {
180  print(dbgs());
181}
182#endif
183
184//===----------------------------------------------------------------------===//
185// DIEInteger Implementation
186//===----------------------------------------------------------------------===//
187
188/// EmitValue - Emit integer of appropriate size.
189///
190void DIEInteger::EmitValue(DwarfPrinter *D, unsigned Form) const {
191  const AsmPrinter *Asm = D->getAsm();
192  unsigned Size = ~0U;
193  switch (Form) {
194  case dwarf::DW_FORM_flag:  // Fall thru
195  case dwarf::DW_FORM_ref1:  // Fall thru
196  case dwarf::DW_FORM_data1: Size = 1; break;
197  case dwarf::DW_FORM_ref2:  // Fall thru
198  case dwarf::DW_FORM_data2: Size = 2; break;
199  case dwarf::DW_FORM_ref4:  // Fall thru
200  case dwarf::DW_FORM_data4: Size = 4; break;
201  case dwarf::DW_FORM_ref8:  // Fall thru
202  case dwarf::DW_FORM_data8: Size = 8; break;
203  case dwarf::DW_FORM_udata: D->EmitULEB128(Integer); return;
204  case dwarf::DW_FORM_sdata: D->EmitSLEB128(Integer, ""); return;
205  default: llvm_unreachable("DIE Value form not supported yet");
206  }
207  Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
208}
209
210/// SizeOf - Determine size of integer value in bytes.
211///
212unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
213  switch (Form) {
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  default: llvm_unreachable("DIE Value form not supported yet"); break;
226  }
227  return 0;
228}
229
230#ifndef NDEBUG
231void DIEInteger::print(raw_ostream &O) {
232  O << "Int: " << (int64_t)Integer
233    << format("  0x%llx", (unsigned long long)Integer);
234}
235#endif
236
237//===----------------------------------------------------------------------===//
238// DIEString Implementation
239//===----------------------------------------------------------------------===//
240
241/// EmitValue - Emit string value.
242///
243void DIEString::EmitValue(DwarfPrinter *D, unsigned Form) const {
244  D->getAsm()->OutStreamer.EmitBytes(Str, /*addrspace*/0);
245  // Emit nul terminator.
246  D->getAsm()->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0);
247}
248
249#ifndef NDEBUG
250void DIEString::print(raw_ostream &O) {
251  O << "Str: \"" << Str << "\"";
252}
253#endif
254
255//===----------------------------------------------------------------------===//
256// DIELabel Implementation
257//===----------------------------------------------------------------------===//
258
259/// EmitValue - Emit label value.
260///
261void DIELabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
262  bool IsSmall = Form == dwarf::DW_FORM_data4;
263  unsigned Size = IsSmall ? 4 : D->getTargetData()->getPointerSize();
264  D->getAsm()->OutStreamer.EmitSymbolValue(Label, Size, 0/*AddrSpace*/);
265}
266
267/// SizeOf - Determine size of label value in bytes.
268///
269unsigned DIELabel::SizeOf(const TargetData *TD, unsigned Form) const {
270  if (Form == dwarf::DW_FORM_data4) return 4;
271  return TD->getPointerSize();
272}
273
274#ifndef NDEBUG
275void DIELabel::print(raw_ostream &O) {
276  O << "Lbl: " << Label->getName();
277}
278#endif
279
280//===----------------------------------------------------------------------===//
281// DIEDelta Implementation
282//===----------------------------------------------------------------------===//
283
284/// EmitValue - Emit delta value.
285///
286void DIEDelta::EmitValue(DwarfPrinter *D, unsigned Form) const {
287  bool IsSmall = Form == dwarf::DW_FORM_data4;
288  D->EmitDifference(LabelHi, LabelLo, IsSmall);
289}
290
291/// SizeOf - Determine size of delta value in bytes.
292///
293unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
294  if (Form == dwarf::DW_FORM_data4) return 4;
295  return TD->getPointerSize();
296}
297
298#ifndef NDEBUG
299void DIEDelta::print(raw_ostream &O) {
300  O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
301}
302#endif
303
304//===----------------------------------------------------------------------===//
305// DIEEntry Implementation
306//===----------------------------------------------------------------------===//
307
308/// EmitValue - Emit debug information entry offset.
309///
310void DIEEntry::EmitValue(DwarfPrinter *D, unsigned Form) const {
311  D->getAsm()->EmitInt32(Entry->getOffset());
312}
313
314#ifndef NDEBUG
315void DIEEntry::print(raw_ostream &O) {
316  O << format("Die: 0x%lx", (long)(intptr_t)Entry);
317}
318#endif
319
320//===----------------------------------------------------------------------===//
321// DIEBlock Implementation
322//===----------------------------------------------------------------------===//
323
324/// ComputeSize - calculate the size of the block.
325///
326unsigned DIEBlock::ComputeSize(const TargetData *TD) {
327  if (!Size) {
328    const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
329    for (unsigned i = 0, N = Values.size(); i < N; ++i)
330      Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
331  }
332
333  return Size;
334}
335
336/// EmitValue - Emit block data.
337///
338void DIEBlock::EmitValue(DwarfPrinter *D, unsigned Form) const {
339  const AsmPrinter *Asm = D->getAsm();
340  switch (Form) {
341  case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);         break;
342  case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);        break;
343  case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);        break;
344  case dwarf::DW_FORM_block:  D->EmitULEB128(Size);        break;
345  default: llvm_unreachable("Improper form for block");    break;
346  }
347
348  const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
349  for (unsigned i = 0, N = Values.size(); i < N; ++i)
350    Values[i]->EmitValue(D, AbbrevData[i].getForm());
351}
352
353/// SizeOf - Determine size of block data in bytes.
354///
355unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
356  switch (Form) {
357  case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
358  case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
359  case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
360  case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
361  default: llvm_unreachable("Improper form for block"); break;
362  }
363  return 0;
364}
365
366#ifndef NDEBUG
367void DIEBlock::print(raw_ostream &O) {
368  O << "Blk: ";
369  DIE::print(O, 5);
370}
371#endif
372