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