25dec5893c68fe8aaf39e4bc061b3659bb80618a
[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
16 #include "DwarfCompileUnit.h"
17 #include "DwarfDebug.h"
18 #include "DwarfUnit.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Support/LEB128.h"
30 #include "llvm/Support/MD5.h"
31 using namespace llvm;
32
33 //===----------------------------------------------------------------------===//
34 // DIEAbbrevData Implementation
35 //===----------------------------------------------------------------------===//
36
37 /// Profile - Used to gather unique data for the abbreviation folding set.
38 ///
39 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
40   // Explicitly cast to an integer type for which FoldingSetNodeID has
41   // overloads.  Otherwise MSVC 2010 thinks this call is ambiguous.
42   ID.AddInteger(unsigned(Attribute));
43   ID.AddInteger(unsigned(Form));
44 }
45
46 //===----------------------------------------------------------------------===//
47 // DIEAbbrev Implementation
48 //===----------------------------------------------------------------------===//
49
50 /// Profile - Used to gather unique data for the abbreviation folding set.
51 ///
52 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
53   ID.AddInteger(unsigned(Tag));
54   ID.AddInteger(unsigned(Children));
55
56   // For each attribute description.
57   for (unsigned i = 0, N = Data.size(); i < N; ++i)
58     Data[i].Profile(ID);
59 }
60
61 /// Emit - Print the abbreviation using the specified asm printer.
62 ///
63 void DIEAbbrev::Emit(AsmPrinter *AP) const {
64   // Emit its Dwarf tag type.
65   AP->EmitULEB128(Tag, dwarf::TagString(Tag));
66
67   // Emit whether it has children DIEs.
68   AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
69
70   // For each attribute description.
71   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
72     const DIEAbbrevData &AttrData = Data[i];
73
74     // Emit attribute type.
75     AP->EmitULEB128(AttrData.getAttribute(),
76                     dwarf::AttributeString(AttrData.getAttribute()));
77
78     // Emit form type.
79     AP->EmitULEB128(AttrData.getForm(),
80                     dwarf::FormEncodingString(AttrData.getForm()));
81   }
82
83   // Mark end of abbreviation.
84   AP->EmitULEB128(0, "EOM(1)");
85   AP->EmitULEB128(0, "EOM(2)");
86 }
87
88 #ifndef NDEBUG
89 void DIEAbbrev::print(raw_ostream &O) {
90   O << "Abbreviation @"
91     << format("0x%lx", (long)(intptr_t)this)
92     << "  "
93     << dwarf::TagString(Tag)
94     << " "
95     << dwarf::ChildrenString(Children)
96     << '\n';
97
98   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
99     O << "  "
100       << dwarf::AttributeString(Data[i].getAttribute())
101       << "  "
102       << dwarf::FormEncodingString(Data[i].getForm())
103       << '\n';
104   }
105 }
106 void DIEAbbrev::dump() { print(dbgs()); }
107 #endif
108
109 /// Climb up the parent chain to get the unit DIE to which this DIE
110 /// belongs.
111 const DIE *DIE::getUnit() const {
112   const DIE *Cu = getUnitOrNull();
113   assert(Cu && "We should not have orphaned DIEs.");
114   return Cu;
115 }
116
117 /// Climb up the parent chain to get the unit DIE this DIE belongs
118 /// to. Return NULL if DIE is not added to an owner yet.
119 const DIE *DIE::getUnitOrNull() const {
120   const DIE *p = this;
121   while (p) {
122     if (p->getTag() == dwarf::DW_TAG_compile_unit ||
123         p->getTag() == dwarf::DW_TAG_type_unit)
124       return p;
125     p = p->getParent();
126   }
127   return nullptr;
128 }
129
130 DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
131   const SmallVectorImpl<DIEValue *> &Values = getValues();
132   const DIEAbbrev &Abbrevs = getAbbrev();
133
134   // Iterate through all the attributes until we find the one we're
135   // looking for, if we can't find it return NULL.
136   for (size_t i = 0; i < Values.size(); ++i)
137     if (Abbrevs.getData()[i].getAttribute() == Attribute)
138       return Values[i];
139   return nullptr;
140 }
141
142 #ifndef NDEBUG
143 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
144   const std::string Indent(IndentCount, ' ');
145   bool isBlock = Abbrev.getTag() == 0;
146
147   if (!isBlock) {
148     O << Indent
149       << "Die: "
150       << format("0x%lx", (long)(intptr_t)this)
151       << ", Offset: " << Offset
152       << ", Size: " << Size << "\n";
153
154     O << Indent
155       << dwarf::TagString(Abbrev.getTag())
156       << " "
157       << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
158   } else {
159     O << "Size: " << Size << "\n";
160   }
161
162   const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
163
164   IndentCount += 2;
165   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
166     O << Indent;
167
168     if (!isBlock)
169       O << dwarf::AttributeString(Data[i].getAttribute());
170     else
171       O << "Blk[" << i << "]";
172
173     O <<  "  "
174       << dwarf::FormEncodingString(Data[i].getForm())
175       << " ";
176     Values[i]->print(O);
177     O << "\n";
178   }
179   IndentCount -= 2;
180
181   for (unsigned j = 0, M = Children.size(); j < M; ++j) {
182     Children[j]->print(O, IndentCount+4);
183   }
184
185   if (!isBlock) O << "\n";
186 }
187
188 void DIE::dump() {
189   print(dbgs());
190 }
191 #endif
192
193 void DIEValue::anchor() { }
194
195 #ifndef NDEBUG
196 void DIEValue::dump() const {
197   print(dbgs());
198 }
199 #endif
200
201 //===----------------------------------------------------------------------===//
202 // DIEInteger Implementation
203 //===----------------------------------------------------------------------===//
204
205 /// EmitValue - Emit integer of appropriate size.
206 ///
207 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
208   unsigned Size = ~0U;
209   switch (Form) {
210   case dwarf::DW_FORM_flag_present:
211     // Emit something to keep the lines and comments in sync.
212     // FIXME: Is there a better way to do this?
213     Asm->OutStreamer.AddBlankLine();
214     return;
215   case dwarf::DW_FORM_flag:  // Fall thru
216   case dwarf::DW_FORM_ref1:  // Fall thru
217   case dwarf::DW_FORM_data1: Size = 1; break;
218   case dwarf::DW_FORM_ref2:  // Fall thru
219   case dwarf::DW_FORM_data2: Size = 2; break;
220   case dwarf::DW_FORM_sec_offset: // Fall thru
221   case dwarf::DW_FORM_ref4:  // Fall thru
222   case dwarf::DW_FORM_data4: Size = 4; break;
223   case dwarf::DW_FORM_ref8:  // Fall thru
224   case dwarf::DW_FORM_ref_sig8:  // Fall thru
225   case dwarf::DW_FORM_data8: Size = 8; break;
226   case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
227   case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
228   case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
229   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
230   case dwarf::DW_FORM_addr:
231     Size = Asm->getDataLayout().getPointerSize(); break;
232   default: llvm_unreachable("DIE Value form not supported yet");
233   }
234   Asm->OutStreamer.EmitIntValue(Integer, Size);
235 }
236
237 /// SizeOf - Determine size of integer value in bytes.
238 ///
239 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
240   switch (Form) {
241   case dwarf::DW_FORM_flag_present: return 0;
242   case dwarf::DW_FORM_flag:  // Fall thru
243   case dwarf::DW_FORM_ref1:  // Fall thru
244   case dwarf::DW_FORM_data1: return sizeof(int8_t);
245   case dwarf::DW_FORM_ref2:  // Fall thru
246   case dwarf::DW_FORM_data2: return sizeof(int16_t);
247   case dwarf::DW_FORM_sec_offset: // Fall thru
248   case dwarf::DW_FORM_ref4:  // Fall thru
249   case dwarf::DW_FORM_data4: return sizeof(int32_t);
250   case dwarf::DW_FORM_ref8:  // Fall thru
251   case dwarf::DW_FORM_ref_sig8:  // Fall thru
252   case dwarf::DW_FORM_data8: return sizeof(int64_t);
253   case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
254   case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
255   case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
256   case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
257   case dwarf::DW_FORM_addr:  return AP->getDataLayout().getPointerSize();
258   default: llvm_unreachable("DIE Value form not supported yet");
259   }
260 }
261
262 #ifndef NDEBUG
263 void DIEInteger::print(raw_ostream &O) const {
264   O << "Int: " << (int64_t)Integer << "  0x";
265   O.write_hex(Integer);
266 }
267 #endif
268
269 //===----------------------------------------------------------------------===//
270 // DIEExpr Implementation
271 //===----------------------------------------------------------------------===//
272
273 /// EmitValue - Emit expression value.
274 ///
275 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
276   AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
277 }
278
279 /// SizeOf - Determine size of expression value in bytes.
280 ///
281 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
282   if (Form == dwarf::DW_FORM_data4) return 4;
283   if (Form == dwarf::DW_FORM_sec_offset) return 4;
284   if (Form == dwarf::DW_FORM_strp) return 4;
285   return AP->getDataLayout().getPointerSize();
286 }
287
288 #ifndef NDEBUG
289 void DIEExpr::print(raw_ostream &O) const {
290   O << "Expr: ";
291   Expr->print(O);
292 }
293 #endif
294
295 //===----------------------------------------------------------------------===//
296 // DIELabel Implementation
297 //===----------------------------------------------------------------------===//
298
299 /// EmitValue - Emit label value.
300 ///
301 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
302   AP->EmitLabelReference(Label, SizeOf(AP, Form),
303                          Form == dwarf::DW_FORM_strp ||
304                              Form == dwarf::DW_FORM_sec_offset ||
305                              Form == dwarf::DW_FORM_ref_addr);
306 }
307
308 /// SizeOf - Determine size of label value in bytes.
309 ///
310 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
311   if (Form == dwarf::DW_FORM_data4) return 4;
312   if (Form == dwarf::DW_FORM_sec_offset) return 4;
313   if (Form == dwarf::DW_FORM_strp) return 4;
314   return AP->getDataLayout().getPointerSize();
315 }
316
317 #ifndef NDEBUG
318 void DIELabel::print(raw_ostream &O) const {
319   O << "Lbl: " << Label->getName();
320 }
321 #endif
322
323 //===----------------------------------------------------------------------===//
324 // DIEDelta Implementation
325 //===----------------------------------------------------------------------===//
326
327 /// EmitValue - Emit delta value.
328 ///
329 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
330   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
331 }
332
333 /// SizeOf - Determine size of delta value in bytes.
334 ///
335 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
336   if (Form == dwarf::DW_FORM_data4) return 4;
337   if (Form == dwarf::DW_FORM_sec_offset) return 4;
338   if (Form == dwarf::DW_FORM_strp) return 4;
339   return AP->getDataLayout().getPointerSize();
340 }
341
342 #ifndef NDEBUG
343 void DIEDelta::print(raw_ostream &O) const {
344   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
345 }
346 #endif
347
348 //===----------------------------------------------------------------------===//
349 // DIEString Implementation
350 //===----------------------------------------------------------------------===//
351
352 /// EmitValue - Emit string value.
353 ///
354 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
355   Access->EmitValue(AP, Form);
356 }
357
358 /// SizeOf - Determine size of delta value in bytes.
359 ///
360 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
361   return Access->SizeOf(AP, Form);
362 }
363
364 #ifndef NDEBUG
365 void DIEString::print(raw_ostream &O) const {
366   O << "String: " << Str << "\tSymbol: ";
367   Access->print(O);
368 }
369 #endif
370
371 //===----------------------------------------------------------------------===//
372 // DIEEntry Implementation
373 //===----------------------------------------------------------------------===//
374
375 /// EmitValue - Emit debug information entry offset.
376 ///
377 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
378
379   if (Form == dwarf::DW_FORM_ref_addr) {
380     const DwarfDebug *DD = AP->getDwarfDebug();
381     unsigned Addr = Entry.getOffset();
382     assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
383     // For DW_FORM_ref_addr, output the offset from beginning of debug info
384     // section. Entry->getOffset() returns the offset from start of the
385     // compile unit.
386     DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
387     assert(CU && "CUDie should belong to a CU.");
388     Addr += CU->getDebugInfoOffset();
389     if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
390       AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
391                               DIEEntry::getRefAddrSize(AP));
392     else
393       AP->EmitLabelOffsetDifference(CU->getSectionSym(), Addr,
394                                     CU->getSectionSym(),
395                                     DIEEntry::getRefAddrSize(AP));
396   } else
397     AP->EmitInt32(Entry.getOffset());
398 }
399
400 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
401   // DWARF4: References that use the attribute form DW_FORM_ref_addr are
402   // specified to be four bytes in the DWARF 32-bit format and eight bytes
403   // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
404   // references have the same size as an address on the target system.
405   const DwarfDebug *DD = AP->getDwarfDebug();
406   assert(DD && "Expected Dwarf Debug info to be available");
407   if (DD->getDwarfVersion() == 2)
408     return AP->getDataLayout().getPointerSize();
409   return sizeof(int32_t);
410 }
411
412 #ifndef NDEBUG
413 void DIEEntry::print(raw_ostream &O) const {
414   O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
415 }
416 #endif
417
418 //===----------------------------------------------------------------------===//
419 // DIETypeSignature Implementation
420 //===----------------------------------------------------------------------===//
421 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
422   assert(Form == dwarf::DW_FORM_ref_sig8);
423   Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
424 }
425
426 #ifndef NDEBUG
427 void DIETypeSignature::print(raw_ostream &O) const {
428   O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
429 }
430
431 void DIETypeSignature::dump() const { print(dbgs()); }
432 #endif
433
434 //===----------------------------------------------------------------------===//
435 // DIELoc Implementation
436 //===----------------------------------------------------------------------===//
437
438 /// ComputeSize - calculate the size of the location expression.
439 ///
440 unsigned DIELoc::ComputeSize(AsmPrinter *AP) const {
441   if (!Size) {
442     const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
443     for (unsigned i = 0, N = Values.size(); i < N; ++i)
444       Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
445   }
446
447   return Size;
448 }
449
450 /// EmitValue - Emit location data.
451 ///
452 void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
453   switch (Form) {
454   default: llvm_unreachable("Improper form for block");
455   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
456   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
457   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
458   case dwarf::DW_FORM_block:
459   case dwarf::DW_FORM_exprloc:
460     Asm->EmitULEB128(Size); break;
461   }
462
463   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
464   for (unsigned i = 0, N = Values.size(); i < N; ++i)
465     Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
466 }
467
468 /// SizeOf - Determine size of location data in bytes.
469 ///
470 unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
471   switch (Form) {
472   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
473   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
474   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
475   case dwarf::DW_FORM_block:
476   case dwarf::DW_FORM_exprloc:
477     return Size + getULEB128Size(Size);
478   default: llvm_unreachable("Improper form for block");
479   }
480 }
481
482 #ifndef NDEBUG
483 void DIELoc::print(raw_ostream &O) const {
484   O << "ExprLoc: ";
485   DIE::print(O, 5);
486 }
487 #endif
488
489 //===----------------------------------------------------------------------===//
490 // DIEBlock Implementation
491 //===----------------------------------------------------------------------===//
492
493 /// ComputeSize - calculate the size of the block.
494 ///
495 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const {
496   if (!Size) {
497     const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
498     for (unsigned i = 0, N = Values.size(); i < N; ++i)
499       Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
500   }
501
502   return Size;
503 }
504
505 /// EmitValue - Emit block data.
506 ///
507 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
508   switch (Form) {
509   default: llvm_unreachable("Improper form for block");
510   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
511   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
512   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
513   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
514   }
515
516   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
517   for (unsigned i = 0, N = Values.size(); i < N; ++i)
518     Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
519 }
520
521 /// SizeOf - Determine size of block data in bytes.
522 ///
523 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
524   switch (Form) {
525   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
526   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
527   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
528   case dwarf::DW_FORM_block:  return Size + getULEB128Size(Size);
529   default: llvm_unreachable("Improper form for block");
530   }
531 }
532
533 #ifndef NDEBUG
534 void DIEBlock::print(raw_ostream &O) const {
535   O << "Blk: ";
536   DIE::print(O, 5);
537 }
538 #endif
539
540 //===----------------------------------------------------------------------===//
541 // DIELocList Implementation
542 //===----------------------------------------------------------------------===//
543
544 unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
545   if (Form == dwarf::DW_FORM_data4)
546     return 4;
547   if (Form == dwarf::DW_FORM_sec_offset)
548     return 4;
549   return AP->getDataLayout().getPointerSize();
550 }
551
552 /// EmitValue - Emit label value.
553 ///
554 void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
555   DwarfDebug *DD = AP->getDwarfDebug();
556   MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
557
558   if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
559     AP->EmitSectionOffset(Label, DD->getDebugLocSym());
560   else
561     AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
562 }
563
564 #ifndef NDEBUG
565 void DIELocList::print(raw_ostream &O) const {
566   O << "LocList: " << Index;
567
568 }
569 #endif