]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/AsmPrinter/DIE.cpp
MFV r316083,316094:
[FreeBSD/FreeBSD.git] / contrib / llvm / 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 "llvm/CodeGen/DIE.h"
15 #include "DwarfCompileUnit.h"
16 #include "DwarfDebug.h"
17 #include "DwarfUnit.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.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 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm;
33
34 //===----------------------------------------------------------------------===//
35 // DIEAbbrevData Implementation
36 //===----------------------------------------------------------------------===//
37
38 /// Profile - Used to gather unique data for the abbreviation folding set.
39 ///
40 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
41   // Explicitly cast to an integer type for which FoldingSetNodeID has
42   // overloads.  Otherwise MSVC 2010 thinks this call is ambiguous.
43   ID.AddInteger(unsigned(Attribute));
44   ID.AddInteger(unsigned(Form));
45 }
46
47 //===----------------------------------------------------------------------===//
48 // DIEAbbrev Implementation
49 //===----------------------------------------------------------------------===//
50
51 /// Profile - Used to gather unique data for the abbreviation folding set.
52 ///
53 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
54   ID.AddInteger(unsigned(Tag));
55   ID.AddInteger(unsigned(Children));
56
57   // For each attribute description.
58   for (unsigned i = 0, N = Data.size(); i < N; ++i)
59     Data[i].Profile(ID);
60 }
61
62 /// Emit - Print the abbreviation using the specified asm printer.
63 ///
64 void DIEAbbrev::Emit(const AsmPrinter *AP) const {
65   // Emit its Dwarf tag type.
66   AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
67
68   // Emit whether it has children DIEs.
69   AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
70
71   // For each attribute description.
72   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
73     const DIEAbbrevData &AttrData = Data[i];
74
75     // Emit attribute type.
76     AP->EmitULEB128(AttrData.getAttribute(),
77                     dwarf::AttributeString(AttrData.getAttribute()).data());
78
79     // Emit form type.
80     AP->EmitULEB128(AttrData.getForm(),
81                     dwarf::FormEncodingString(AttrData.getForm()).data());
82
83     // Emit value for DW_FORM_implicit_const.
84     if (AttrData.getForm() == dwarf::DW_FORM_implicit_const) {
85       assert(AP->getDwarfVersion() >= 5 &&
86             "DW_FORM_implicit_const is supported starting from DWARFv5");
87       AP->EmitSLEB128(AttrData.getValue());
88     }
89   }
90
91   // Mark end of abbreviation.
92   AP->EmitULEB128(0, "EOM(1)");
93   AP->EmitULEB128(0, "EOM(2)");
94 }
95
96 LLVM_DUMP_METHOD
97 void DIEAbbrev::print(raw_ostream &O) {
98   O << "Abbreviation @"
99     << format("0x%lx", (long)(intptr_t)this)
100     << "  "
101     << dwarf::TagString(Tag)
102     << " "
103     << dwarf::ChildrenString(Children)
104     << '\n';
105
106   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
107     O << "  "
108       << dwarf::AttributeString(Data[i].getAttribute())
109       << "  "
110       << dwarf::FormEncodingString(Data[i].getForm())
111       << '\n';
112   }
113 }
114
115 LLVM_DUMP_METHOD
116 void DIEAbbrev::dump() { print(dbgs()); }
117
118 //===----------------------------------------------------------------------===//
119 // DIEAbbrevSet Implementation
120 //===----------------------------------------------------------------------===//
121
122 DIEAbbrevSet::~DIEAbbrevSet() {
123   for (DIEAbbrev *Abbrev : Abbreviations)
124     Abbrev->~DIEAbbrev();
125 }
126
127 DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {
128
129   FoldingSetNodeID ID;
130   DIEAbbrev Abbrev = Die.generateAbbrev();
131   Abbrev.Profile(ID);
132
133   void *InsertPos;
134   if (DIEAbbrev *Existing =
135           AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
136     Die.setAbbrevNumber(Existing->getNumber());
137     return *Existing;
138   }
139
140   // Move the abbreviation to the heap and assign a number.
141   DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));
142   Abbreviations.push_back(New);
143   New->setNumber(Abbreviations.size());
144   Die.setAbbrevNumber(Abbreviations.size());
145
146   // Store it for lookup.
147   AbbreviationsSet.InsertNode(New, InsertPos);
148   return *New;
149 }
150
151 void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {
152   if (!Abbreviations.empty()) {
153     // Start the debug abbrev section.
154     AP->OutStreamer->SwitchSection(Section);
155     AP->emitDwarfAbbrevs(Abbreviations);
156   }
157 }
158
159 //===----------------------------------------------------------------------===//
160 // DIE Implementation
161 //===----------------------------------------------------------------------===//
162
163 DIE *DIE::getParent() const {
164   return Owner.dyn_cast<DIE*>();
165 }
166
167 DIEAbbrev DIE::generateAbbrev() const {
168   DIEAbbrev Abbrev(Tag, hasChildren());
169   for (const DIEValue &V : values())
170     if (V.getForm() == dwarf::DW_FORM_implicit_const)
171       Abbrev.AddImplicitConstAttribute(V.getAttribute(),
172                                        V.getDIEInteger().getValue());
173     else
174       Abbrev.AddAttribute(V.getAttribute(), V.getForm());
175   return Abbrev;
176 }
177
178 unsigned DIE::getDebugSectionOffset() const {
179   const DIEUnit *Unit = getUnit();
180   assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
181   return Unit->getDebugSectionOffset() + getOffset();
182 }
183
184 const DIE *DIE::getUnitDie() const {
185   const DIE *p = this;
186   while (p) {
187     if (p->getTag() == dwarf::DW_TAG_compile_unit ||
188         p->getTag() == dwarf::DW_TAG_type_unit)
189       return p;
190     p = p->getParent();
191   }
192   return nullptr;
193 }
194
195 const DIEUnit *DIE::getUnit() const {
196   const DIE *UnitDie = getUnitDie();
197   if (UnitDie)
198     return UnitDie->Owner.dyn_cast<DIEUnit*>();
199   return nullptr;
200 }
201
202 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
203   // Iterate through all the attributes until we find the one we're
204   // looking for, if we can't find it return NULL.
205   for (const auto &V : values())
206     if (V.getAttribute() == Attribute)
207       return V;
208   return DIEValue();
209 }
210
211 LLVM_DUMP_METHOD
212 static void printValues(raw_ostream &O, const DIEValueList &Values,
213                         StringRef Type, unsigned Size, unsigned IndentCount) {
214   O << Type << ": Size: " << Size << "\n";
215
216   unsigned I = 0;
217   const std::string Indent(IndentCount, ' ');
218   for (const auto &V : Values.values()) {
219     O << Indent;
220     O << "Blk[" << I++ << "]";
221     O << "  " << dwarf::FormEncodingString(V.getForm()) << " ";
222     V.print(O);
223     O << "\n";
224   }
225 }
226
227 LLVM_DUMP_METHOD
228 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
229   const std::string Indent(IndentCount, ' ');
230   O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
231     << ", Offset: " << Offset << ", Size: " << Size << "\n";
232
233   O << Indent << dwarf::TagString(getTag()) << " "
234     << dwarf::ChildrenString(hasChildren()) << "\n";
235
236   IndentCount += 2;
237   for (const auto &V : values()) {
238     O << Indent;
239     O << dwarf::AttributeString(V.getAttribute());
240     O << "  " << dwarf::FormEncodingString(V.getForm()) << " ";
241     V.print(O);
242     O << "\n";
243   }
244   IndentCount -= 2;
245
246   for (const auto &Child : children())
247     Child.print(O, IndentCount + 4);
248
249   O << "\n";
250 }
251
252 LLVM_DUMP_METHOD
253 void DIE::dump() {
254   print(dbgs());
255 }
256
257 unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter *AP,
258                                        DIEAbbrevSet &AbbrevSet,
259                                        unsigned CUOffset) {
260   // Unique the abbreviation and fill in the abbreviation number so this DIE
261   // can be emitted.
262   const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);
263
264   // Set compile/type unit relative offset of this DIE.
265   setOffset(CUOffset);
266
267   // Add the byte size of the abbreviation code.
268   CUOffset += getULEB128Size(getAbbrevNumber());
269
270   // Add the byte size of all the DIE attribute values.
271   for (const auto &V : values())
272     CUOffset += V.SizeOf(AP);
273
274   // Let the children compute their offsets and abbreviation numbers.
275   if (hasChildren()) {
276     (void)Abbrev;
277     assert(Abbrev.hasChildren() && "Children flag not set");
278
279     for (auto &Child : children())
280       CUOffset = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset);
281
282     // Each child chain is terminated with a zero byte, adjust the offset.
283     CUOffset += sizeof(int8_t);
284   }
285
286   // Compute the byte size of this DIE and all of its children correctly. This
287   // is needed so that top level DIE can help the compile unit set its length
288   // correctly.
289   setSize(CUOffset - getOffset());
290   return CUOffset;
291 }
292
293 //===----------------------------------------------------------------------===//
294 // DIEUnit Implementation
295 //===----------------------------------------------------------------------===//
296 DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
297     : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
298       AddrSize(A)
299 {
300   Die.Owner = this;
301   assert((UnitTag == dwarf::DW_TAG_compile_unit ||
302           UnitTag == dwarf::DW_TAG_type_unit ||
303           UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG");
304 }
305
306 void DIEValue::EmitValue(const AsmPrinter *AP) const {
307   switch (Ty) {
308   case isNone:
309     llvm_unreachable("Expected valid DIEValue");
310 #define HANDLE_DIEVALUE(T)                                                     \
311   case is##T:                                                                  \
312     getDIE##T().EmitValue(AP, Form);                                           \
313     break;
314 #include "llvm/CodeGen/DIEValue.def"
315   }
316 }
317
318 unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
319   switch (Ty) {
320   case isNone:
321     llvm_unreachable("Expected valid DIEValue");
322 #define HANDLE_DIEVALUE(T)                                                     \
323   case is##T:                                                                  \
324     return getDIE##T().SizeOf(AP, Form);
325 #include "llvm/CodeGen/DIEValue.def"
326   }
327   llvm_unreachable("Unknown DIE kind");
328 }
329
330 LLVM_DUMP_METHOD
331 void DIEValue::print(raw_ostream &O) const {
332   switch (Ty) {
333   case isNone:
334     llvm_unreachable("Expected valid DIEValue");
335 #define HANDLE_DIEVALUE(T)                                                     \
336   case is##T:                                                                  \
337     getDIE##T().print(O);                                                      \
338     break;
339 #include "llvm/CodeGen/DIEValue.def"
340   }
341 }
342
343 LLVM_DUMP_METHOD
344 void DIEValue::dump() const {
345   print(dbgs());
346 }
347
348 //===----------------------------------------------------------------------===//
349 // DIEInteger Implementation
350 //===----------------------------------------------------------------------===//
351
352 /// EmitValue - Emit integer of appropriate size.
353 ///
354 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
355   switch (Form) {
356   case dwarf::DW_FORM_implicit_const:
357     LLVM_FALLTHROUGH;
358   case dwarf::DW_FORM_flag_present:
359     // Emit something to keep the lines and comments in sync.
360     // FIXME: Is there a better way to do this?
361     Asm->OutStreamer->AddBlankLine();
362     return;
363   case dwarf::DW_FORM_flag:
364     LLVM_FALLTHROUGH;
365   case dwarf::DW_FORM_ref1:
366     LLVM_FALLTHROUGH;
367   case dwarf::DW_FORM_data1:
368     LLVM_FALLTHROUGH;
369   case dwarf::DW_FORM_ref2:
370     LLVM_FALLTHROUGH;
371   case dwarf::DW_FORM_data2:
372     LLVM_FALLTHROUGH;
373   case dwarf::DW_FORM_strp:
374     LLVM_FALLTHROUGH;
375   case dwarf::DW_FORM_ref4:
376     LLVM_FALLTHROUGH;
377   case dwarf::DW_FORM_data4:
378     LLVM_FALLTHROUGH;
379   case dwarf::DW_FORM_ref8:
380     LLVM_FALLTHROUGH;
381   case dwarf::DW_FORM_ref_sig8:
382     LLVM_FALLTHROUGH;
383   case dwarf::DW_FORM_data8:
384     LLVM_FALLTHROUGH;
385   case dwarf::DW_FORM_GNU_ref_alt:
386     LLVM_FALLTHROUGH;
387   case dwarf::DW_FORM_GNU_strp_alt:
388     LLVM_FALLTHROUGH;
389   case dwarf::DW_FORM_line_strp:
390     LLVM_FALLTHROUGH;
391   case dwarf::DW_FORM_sec_offset:
392     LLVM_FALLTHROUGH;
393   case dwarf::DW_FORM_strp_sup:
394     LLVM_FALLTHROUGH;
395   case dwarf::DW_FORM_ref_sup:
396     LLVM_FALLTHROUGH;
397   case dwarf::DW_FORM_addr:
398     LLVM_FALLTHROUGH;
399   case dwarf::DW_FORM_ref_addr:
400     Asm->OutStreamer->EmitIntValue(Integer, SizeOf(Asm, Form));
401     return;
402   case dwarf::DW_FORM_GNU_str_index:
403     LLVM_FALLTHROUGH;
404   case dwarf::DW_FORM_GNU_addr_index:
405     LLVM_FALLTHROUGH;
406   case dwarf::DW_FORM_ref_udata:
407     LLVM_FALLTHROUGH;
408   case dwarf::DW_FORM_udata:
409     Asm->EmitULEB128(Integer);
410     return;
411   case dwarf::DW_FORM_sdata:
412     Asm->EmitSLEB128(Integer);
413     return;
414   default: llvm_unreachable("DIE Value form not supported yet");
415   }
416 }
417
418 /// SizeOf - Determine size of integer value in bytes.
419 ///
420 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
421   switch (Form) {
422   case dwarf::DW_FORM_implicit_const: LLVM_FALLTHROUGH;
423   case dwarf::DW_FORM_flag_present: return 0;
424   case dwarf::DW_FORM_flag:  LLVM_FALLTHROUGH;
425   case dwarf::DW_FORM_ref1:  LLVM_FALLTHROUGH;
426   case dwarf::DW_FORM_data1: return sizeof(int8_t);
427   case dwarf::DW_FORM_ref2:  LLVM_FALLTHROUGH;
428   case dwarf::DW_FORM_data2: return sizeof(int16_t);
429   case dwarf::DW_FORM_ref4:  LLVM_FALLTHROUGH;
430   case dwarf::DW_FORM_data4: return sizeof(int32_t);
431   case dwarf::DW_FORM_ref8:  LLVM_FALLTHROUGH;
432   case dwarf::DW_FORM_ref_sig8:  LLVM_FALLTHROUGH;
433   case dwarf::DW_FORM_data8: return sizeof(int64_t);
434   case dwarf::DW_FORM_ref_addr:
435     if (AP->getDwarfVersion() == 2)
436       return AP->getPointerSize();
437     LLVM_FALLTHROUGH;
438   case dwarf::DW_FORM_strp:
439     LLVM_FALLTHROUGH;
440   case dwarf::DW_FORM_GNU_ref_alt:
441     LLVM_FALLTHROUGH;
442   case dwarf::DW_FORM_GNU_strp_alt:
443     LLVM_FALLTHROUGH;
444   case dwarf::DW_FORM_line_strp:
445     LLVM_FALLTHROUGH;
446   case dwarf::DW_FORM_sec_offset:
447     LLVM_FALLTHROUGH;
448   case dwarf::DW_FORM_strp_sup:
449     LLVM_FALLTHROUGH;
450   case dwarf::DW_FORM_ref_sup:
451     switch (AP->OutStreamer->getContext().getDwarfFormat()) {
452     case dwarf::DWARF32:
453       return 4;
454     case dwarf::DWARF64:
455       return 8;
456     }
457     llvm_unreachable("Invalid DWARF format");
458   case dwarf::DW_FORM_GNU_str_index:
459     LLVM_FALLTHROUGH;
460   case dwarf::DW_FORM_GNU_addr_index:
461     LLVM_FALLTHROUGH;
462   case dwarf::DW_FORM_ref_udata:
463     LLVM_FALLTHROUGH;
464   case dwarf::DW_FORM_udata:
465     return getULEB128Size(Integer);
466   case dwarf::DW_FORM_sdata:
467     return getSLEB128Size(Integer);
468   case dwarf::DW_FORM_addr:
469     return AP->getPointerSize();
470   default: llvm_unreachable("DIE Value form not supported yet");
471   }
472 }
473
474 LLVM_DUMP_METHOD
475 void DIEInteger::print(raw_ostream &O) const {
476   O << "Int: " << (int64_t)Integer << "  0x";
477   O.write_hex(Integer);
478 }
479
480 //===----------------------------------------------------------------------===//
481 // DIEExpr Implementation
482 //===----------------------------------------------------------------------===//
483
484 /// EmitValue - Emit expression value.
485 ///
486 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
487   AP->EmitDebugValue(Expr, SizeOf(AP, Form));
488 }
489
490 /// SizeOf - Determine size of expression value in bytes.
491 ///
492 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
493   if (Form == dwarf::DW_FORM_data4) return 4;
494   if (Form == dwarf::DW_FORM_sec_offset) return 4;
495   if (Form == dwarf::DW_FORM_strp) return 4;
496   return AP->getPointerSize();
497 }
498
499 LLVM_DUMP_METHOD
500 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
501
502 //===----------------------------------------------------------------------===//
503 // DIELabel Implementation
504 //===----------------------------------------------------------------------===//
505
506 /// EmitValue - Emit label value.
507 ///
508 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
509   AP->EmitLabelReference(Label, SizeOf(AP, Form),
510                          Form == dwarf::DW_FORM_strp ||
511                              Form == dwarf::DW_FORM_sec_offset ||
512                              Form == dwarf::DW_FORM_ref_addr ||
513                              Form == dwarf::DW_FORM_data4);
514 }
515
516 /// SizeOf - Determine size of label value in bytes.
517 ///
518 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
519   if (Form == dwarf::DW_FORM_data4) return 4;
520   if (Form == dwarf::DW_FORM_sec_offset) return 4;
521   if (Form == dwarf::DW_FORM_strp) return 4;
522   return AP->getPointerSize();
523 }
524
525 LLVM_DUMP_METHOD
526 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
527
528 //===----------------------------------------------------------------------===//
529 // DIEDelta Implementation
530 //===----------------------------------------------------------------------===//
531
532 /// EmitValue - Emit delta value.
533 ///
534 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
535   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
536 }
537
538 /// SizeOf - Determine size of delta value in bytes.
539 ///
540 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
541   if (Form == dwarf::DW_FORM_data4) return 4;
542   if (Form == dwarf::DW_FORM_sec_offset) return 4;
543   if (Form == dwarf::DW_FORM_strp) return 4;
544   return AP->getPointerSize();
545 }
546
547 LLVM_DUMP_METHOD
548 void DIEDelta::print(raw_ostream &O) const {
549   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
550 }
551
552 //===----------------------------------------------------------------------===//
553 // DIEString Implementation
554 //===----------------------------------------------------------------------===//
555
556 /// EmitValue - Emit string value.
557 ///
558 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
559   assert(
560       (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
561       "Expected valid string form");
562
563   // Index of string in symbol table.
564   if (Form == dwarf::DW_FORM_GNU_str_index) {
565     DIEInteger(S.getIndex()).EmitValue(AP, Form);
566     return;
567   }
568
569   // Relocatable symbol.
570   assert(Form == dwarf::DW_FORM_strp);
571   if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
572     DIELabel(S.getSymbol()).EmitValue(AP, Form);
573     return;
574   }
575
576   // Offset into symbol table.
577   DIEInteger(S.getOffset()).EmitValue(AP, Form);
578 }
579
580 /// SizeOf - Determine size of delta value in bytes.
581 ///
582 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
583   assert(
584       (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
585       "Expected valid string form");
586
587   // Index of string in symbol table.
588   if (Form == dwarf::DW_FORM_GNU_str_index)
589     return DIEInteger(S.getIndex()).SizeOf(AP, Form);
590
591   // Relocatable symbol.
592   if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
593     return DIELabel(S.getSymbol()).SizeOf(AP, Form);
594
595   // Offset into symbol table.
596   return DIEInteger(S.getOffset()).SizeOf(AP, Form);
597 }
598
599 LLVM_DUMP_METHOD
600 void DIEString::print(raw_ostream &O) const {
601   O << "String: " << S.getString();
602 }
603
604 //===----------------------------------------------------------------------===//
605 // DIEInlineString Implementation
606 //===----------------------------------------------------------------------===//
607 void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
608   if (Form == dwarf::DW_FORM_string) {
609     for (char ch : S)
610       AP->EmitInt8(ch);
611     AP->EmitInt8(0);
612     return;
613   }
614   llvm_unreachable("Expected valid string form");
615 }
616
617 unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
618   // Emit string bytes + NULL byte.
619   return S.size() + 1;
620 }
621
622 LLVM_DUMP_METHOD
623 void DIEInlineString::print(raw_ostream &O) const {
624   O << "InlineString: " << S;
625 }
626
627 //===----------------------------------------------------------------------===//
628 // DIEEntry Implementation
629 //===----------------------------------------------------------------------===//
630
631 /// EmitValue - Emit debug information entry offset.
632 ///
633 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
634
635   switch (Form) {
636   case dwarf::DW_FORM_ref1:
637   case dwarf::DW_FORM_ref2:
638   case dwarf::DW_FORM_ref4:
639   case dwarf::DW_FORM_ref8:
640     AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form));
641     return;
642
643   case dwarf::DW_FORM_ref_udata:
644     AP->EmitULEB128(Entry->getOffset());
645     return;
646
647   case dwarf::DW_FORM_ref_addr: {
648     // Get the absolute offset for this DIE within the debug info/types section.
649     unsigned Addr = Entry->getDebugSectionOffset();
650     if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
651       const DwarfDebug *DD = AP->getDwarfDebug();
652       if (DD)
653         assert(!DD->useSplitDwarf() &&
654                "TODO: dwo files can't have relocations.");
655       const DIEUnit *Unit = Entry->getUnit();
656       assert(Unit && "CUDie should belong to a CU.");
657       MCSection *Section = Unit->getSection();
658       if (Section) {
659         const MCSymbol *SectionSym = Section->getBeginSymbol();
660         AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
661         return;
662       }
663     }
664     AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form));
665     return;
666   }
667   default:
668     llvm_unreachable("Improper form for DIE reference");
669   }
670 }
671
672 unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
673   switch (Form) {
674   case dwarf::DW_FORM_ref1:
675     return 1;
676   case dwarf::DW_FORM_ref2:
677     return 2;
678   case dwarf::DW_FORM_ref4:
679     return 4;
680   case dwarf::DW_FORM_ref8:
681     return 8;
682   case dwarf::DW_FORM_ref_udata:
683     return getULEB128Size(Entry->getOffset());
684   case dwarf::DW_FORM_ref_addr:
685     if (AP->getDwarfVersion() == 2)
686       return AP->getPointerSize();
687     switch (AP->OutStreamer->getContext().getDwarfFormat()) {
688     case dwarf::DWARF32:
689       return 4;
690     case dwarf::DWARF64:
691       return 8;
692     }
693     llvm_unreachable("Invalid DWARF format");
694
695   default:
696     llvm_unreachable("Improper form for DIE reference");
697   }
698 }
699
700 LLVM_DUMP_METHOD
701 void DIEEntry::print(raw_ostream &O) const {
702   O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
703 }
704
705 //===----------------------------------------------------------------------===//
706 // DIELoc Implementation
707 //===----------------------------------------------------------------------===//
708
709 /// ComputeSize - calculate the size of the location expression.
710 ///
711 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
712   if (!Size) {
713     for (const auto &V : values())
714       Size += V.SizeOf(AP);
715   }
716
717   return Size;
718 }
719
720 /// EmitValue - Emit location data.
721 ///
722 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
723   switch (Form) {
724   default: llvm_unreachable("Improper form for block");
725   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
726   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
727   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
728   case dwarf::DW_FORM_block:
729   case dwarf::DW_FORM_exprloc:
730     Asm->EmitULEB128(Size); break;
731   }
732
733   for (const auto &V : values())
734     V.EmitValue(Asm);
735 }
736
737 /// SizeOf - Determine size of location data in bytes.
738 ///
739 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
740   switch (Form) {
741   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
742   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
743   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
744   case dwarf::DW_FORM_block:
745   case dwarf::DW_FORM_exprloc:
746     return Size + getULEB128Size(Size);
747   default: llvm_unreachable("Improper form for block");
748   }
749 }
750
751 LLVM_DUMP_METHOD
752 void DIELoc::print(raw_ostream &O) const {
753   printValues(O, *this, "ExprLoc", Size, 5);
754 }
755
756 //===----------------------------------------------------------------------===//
757 // DIEBlock Implementation
758 //===----------------------------------------------------------------------===//
759
760 /// ComputeSize - calculate the size of the block.
761 ///
762 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
763   if (!Size) {
764     for (const auto &V : values())
765       Size += V.SizeOf(AP);
766   }
767
768   return Size;
769 }
770
771 /// EmitValue - Emit block data.
772 ///
773 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
774   switch (Form) {
775   default: llvm_unreachable("Improper form for block");
776   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
777   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
778   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
779   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
780   }
781
782   for (const auto &V : values())
783     V.EmitValue(Asm);
784 }
785
786 /// SizeOf - Determine size of block data in bytes.
787 ///
788 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
789   switch (Form) {
790   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
791   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
792   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
793   case dwarf::DW_FORM_block:  return Size + getULEB128Size(Size);
794   default: llvm_unreachable("Improper form for block");
795   }
796 }
797
798 LLVM_DUMP_METHOD
799 void DIEBlock::print(raw_ostream &O) const {
800   printValues(O, *this, "Blk", Size, 5);
801 }
802
803 //===----------------------------------------------------------------------===//
804 // DIELocList Implementation
805 //===----------------------------------------------------------------------===//
806
807 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
808   if (Form == dwarf::DW_FORM_data4)
809     return 4;
810   if (Form == dwarf::DW_FORM_sec_offset)
811     return 4;
812   return AP->getPointerSize();
813 }
814
815 /// EmitValue - Emit label value.
816 ///
817 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
818   DwarfDebug *DD = AP->getDwarfDebug();
819   MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
820   AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
821 }
822
823 LLVM_DUMP_METHOD
824 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }