• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
26 using namespace llvm;
27 
28 //===----------------------------------------------------------------------===//
29 // DIEAbbrevData Implementation
30 //===----------------------------------------------------------------------===//
31 
32 /// Profile - Used to gather unique data for the abbreviation folding set.
33 ///
Profile(FoldingSetNodeID & ID) const34 void 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 ///
Profile(FoldingSetNodeID & ID) const45 void 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 ///
Emit(AsmPrinter * AP) const56 void 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
print(raw_ostream & O)86 void 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 }
dump()103 void DIEAbbrev::dump() { print(dbgs()); }
104 #endif
105 
106 //===----------------------------------------------------------------------===//
107 // DIE Implementation
108 //===----------------------------------------------------------------------===//
109 
~DIE()110 DIE::~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 ///
addSiblingOffset(BumpPtrAllocator & A)117 DIEValue *DIE::addSiblingOffset(BumpPtrAllocator &A) {
118   DIEInteger *DI = new (A) 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
print(raw_ostream & O,unsigned IncIndent)125 void 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 
dump()172 void DIE::dump() {
173   print(dbgs());
174 }
175 #endif
176 
177 
178 #ifndef NDEBUG
dump()179 void DIEValue::dump() {
180   print(dbgs());
181 }
182 #endif
183 
184 //===----------------------------------------------------------------------===//
185 // DIEInteger Implementation
186 //===----------------------------------------------------------------------===//
187 
188 /// EmitValue - Emit integer of appropriate size.
189 ///
EmitValue(AsmPrinter * Asm,unsigned Form) const190 void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
191   unsigned Size = ~0U;
192   switch (Form) {
193   case dwarf::DW_FORM_flag:  // Fall thru
194   case dwarf::DW_FORM_ref1:  // Fall thru
195   case dwarf::DW_FORM_data1: Size = 1; break;
196   case dwarf::DW_FORM_ref2:  // Fall thru
197   case dwarf::DW_FORM_data2: Size = 2; break;
198   case dwarf::DW_FORM_ref4:  // Fall thru
199   case dwarf::DW_FORM_data4: Size = 4; break;
200   case dwarf::DW_FORM_ref8:  // Fall thru
201   case dwarf::DW_FORM_data8: Size = 8; break;
202   case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
203   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
204   case dwarf::DW_FORM_addr:  Size = Asm->getTargetData().getPointerSize(); break;
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 ///
SizeOf(AsmPrinter * AP,unsigned Form) const212 unsigned DIEInteger::SizeOf(AsmPrinter *AP, 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   case dwarf::DW_FORM_addr:  return AP->getTargetData().getPointerSize();
226   default: llvm_unreachable("DIE Value form not supported yet"); break;
227   }
228   return 0;
229 }
230 
231 #ifndef NDEBUG
print(raw_ostream & O)232 void DIEInteger::print(raw_ostream &O) {
233   O << "Int: " << (int64_t)Integer
234     << format("  0x%llx", (unsigned long long)Integer);
235 }
236 #endif
237 
238 //===----------------------------------------------------------------------===//
239 // DIEString Implementation
240 //===----------------------------------------------------------------------===//
241 
242 /// EmitValue - Emit string value.
243 ///
EmitValue(AsmPrinter * AP,unsigned Form) const244 void DIEString::EmitValue(AsmPrinter *AP, unsigned Form) const {
245   AP->OutStreamer.EmitBytes(Str, /*addrspace*/0);
246   // Emit nul terminator.
247   AP->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0);
248 }
249 
250 #ifndef NDEBUG
print(raw_ostream & O)251 void DIEString::print(raw_ostream &O) {
252   O << "Str: \"" << Str << "\"";
253 }
254 #endif
255 
256 //===----------------------------------------------------------------------===//
257 // DIELabel Implementation
258 //===----------------------------------------------------------------------===//
259 
260 /// EmitValue - Emit label value.
261 ///
EmitValue(AsmPrinter * AP,unsigned Form) const262 void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
263   AP->OutStreamer.EmitSymbolValue(Label, SizeOf(AP, Form), 0/*AddrSpace*/);
264 }
265 
266 /// SizeOf - Determine size of label value in bytes.
267 ///
SizeOf(AsmPrinter * AP,unsigned Form) const268 unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
269   if (Form == dwarf::DW_FORM_data4) return 4;
270   return AP->getTargetData().getPointerSize();
271 }
272 
273 #ifndef NDEBUG
print(raw_ostream & O)274 void 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 ///
EmitValue(AsmPrinter * AP,unsigned Form) const285 void 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 ///
SizeOf(AsmPrinter * AP,unsigned Form) const291 unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
292   if (Form == dwarf::DW_FORM_data4) return 4;
293   return AP->getTargetData().getPointerSize();
294 }
295 
296 #ifndef NDEBUG
print(raw_ostream & O)297 void DIEDelta::print(raw_ostream &O) {
298   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
299 }
300 #endif
301 
302 //===----------------------------------------------------------------------===//
303 // DIEEntry Implementation
304 //===----------------------------------------------------------------------===//
305 
306 /// EmitValue - Emit debug information entry offset.
307 ///
EmitValue(AsmPrinter * AP,unsigned Form) const308 void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
309   AP->EmitInt32(Entry->getOffset());
310 }
311 
312 #ifndef NDEBUG
print(raw_ostream & O)313 void DIEEntry::print(raw_ostream &O) {
314   O << format("Die: 0x%lx", (long)(intptr_t)Entry);
315 }
316 #endif
317 
318 //===----------------------------------------------------------------------===//
319 // DIEBlock Implementation
320 //===----------------------------------------------------------------------===//
321 
322 /// ComputeSize - calculate the size of the block.
323 ///
ComputeSize(AsmPrinter * AP)324 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
325   if (!Size) {
326     const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
327     for (unsigned i = 0, N = Values.size(); i < N; ++i)
328       Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
329   }
330 
331   return Size;
332 }
333 
334 /// EmitValue - Emit block data.
335 ///
EmitValue(AsmPrinter * Asm,unsigned Form) const336 void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
337   switch (Form) {
338   default: assert(0 && "Improper form for block");    break;
339   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
340   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
341   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
342   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
343   }
344 
345   const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
346   for (unsigned i = 0, N = Values.size(); i < N; ++i)
347     Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
348 }
349 
350 /// SizeOf - Determine size of block data in bytes.
351 ///
SizeOf(AsmPrinter * AP,unsigned Form) const352 unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
353   switch (Form) {
354   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
355   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
356   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
357   case dwarf::DW_FORM_block:  return Size + MCAsmInfo::getULEB128Size(Size);
358   default: llvm_unreachable("Improper form for block"); break;
359   }
360   return 0;
361 }
362 
363 #ifndef NDEBUG
print(raw_ostream & O)364 void DIEBlock::print(raw_ostream &O) {
365   O << "Blk: ";
366   DIE::print(O, 5);
367 }
368 #endif
369