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