[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / CodeGen / AsmPrinter / DIE.cpp
blob619155cafe9277d1b3fc1bfd02fe76e27042a115
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.
11 //===----------------------------------------------------------------------===//
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;
28 #define DEBUG_TYPE "dwarfdebug"
30 //===----------------------------------------------------------------------===//
31 // DIEAbbrevData Implementation
32 //===----------------------------------------------------------------------===//
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);
45 //===----------------------------------------------------------------------===//
46 // DIEAbbrev Implementation
47 //===----------------------------------------------------------------------===//
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));
55 // For each attribute description.
56 for (unsigned i = 0, N = Data.size(); i < N; ++i)
57 Data[i].Profile(ID);
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());
66 // Emit whether it has children DIEs.
67 AP->emitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
69 // For each attribute description.
70 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
71 const DIEAbbrevData &AttrData = Data[i];
73 // Emit attribute type.
74 AP->emitULEB128(AttrData.getAttribute(),
75 dwarf::AttributeString(AttrData.getAttribute()).data());
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");
88 #endif
89 AP->emitULEB128(AttrData.getForm(),
90 dwarf::FormEncodingString(AttrData.getForm()).data());
92 // Emit value for DW_FORM_implicit_const.
93 if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
94 AP->emitSLEB128(AttrData.getValue());
97 // Mark end of abbreviation.
98 AP->emitULEB128(0, "EOM(1)");
99 AP->emitULEB128(0, "EOM(2)");
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';
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());
118 if (Data[i].getForm() == dwarf::DW_FORM_implicit_const)
119 O << " " << Data[i].getValue();
121 O << '\n';
125 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
126 LLVM_DUMP_METHOD void DIEAbbrev::dump() const {
127 print(dbgs());
129 #endif
131 //===----------------------------------------------------------------------===//
132 // DIEAbbrevSet Implementation
133 //===----------------------------------------------------------------------===//
135 DIEAbbrevSet::~DIEAbbrevSet() {
136 for (DIEAbbrev *Abbrev : Abbreviations)
137 Abbrev->~DIEAbbrev();
140 DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {
142 FoldingSetNodeID ID;
143 DIEAbbrev Abbrev = Die.generateAbbrev();
144 Abbrev.Profile(ID);
146 void *InsertPos;
147 if (DIEAbbrev *Existing =
148 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
149 Die.setAbbrevNumber(Existing->getNumber());
150 return *Existing;
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());
159 // Store it for lookup.
160 AbbreviationsSet.InsertNode(New, InsertPos);
161 return *New;
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);
172 //===----------------------------------------------------------------------===//
173 // DIE Implementation
174 //===----------------------------------------------------------------------===//
176 DIE *DIE::getParent() const { return dyn_cast_if_present<DIE *>(Owner); }
178 DIEAbbrev DIE::generateAbbrev() const {
179 DIEAbbrev Abbrev(Tag, hasChildren());
180 for (const DIEValue &V : values())
181 if (V.getForm() == dwarf::DW_FORM_implicit_const)
182 Abbrev.AddImplicitConstAttribute(V.getAttribute(),
183 V.getDIEInteger().getValue());
184 else
185 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
186 return Abbrev;
189 uint64_t DIE::getDebugSectionOffset() const {
190 const DIEUnit *Unit = getUnit();
191 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
192 return Unit->getDebugSectionOffset() + getOffset();
195 const DIE *DIE::getUnitDie() const {
196 const DIE *p = this;
197 while (p) {
198 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
199 p->getTag() == dwarf::DW_TAG_skeleton_unit ||
200 p->getTag() == dwarf::DW_TAG_type_unit)
201 return p;
202 p = p->getParent();
204 return nullptr;
207 DIEUnit *DIE::getUnit() const {
208 const DIE *UnitDie = getUnitDie();
209 if (UnitDie)
210 return dyn_cast_if_present<DIEUnit *>(UnitDie->Owner);
211 return nullptr;
214 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
215 // Iterate through all the attributes until we find the one we're
216 // looking for, if we can't find it return NULL.
217 for (const auto &V : values())
218 if (V.getAttribute() == Attribute)
219 return V;
220 return DIEValue();
223 LLVM_DUMP_METHOD
224 static void printValues(raw_ostream &O, const DIEValueList &Values,
225 StringRef Type, unsigned Size, unsigned IndentCount) {
226 O << Type << ": Size: " << Size << "\n";
228 unsigned I = 0;
229 const std::string Indent(IndentCount, ' ');
230 for (const auto &V : Values.values()) {
231 O << Indent;
232 O << "Blk[" << I++ << "]";
233 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
234 V.print(O);
235 O << "\n";
239 LLVM_DUMP_METHOD
240 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
241 const std::string Indent(IndentCount, ' ');
242 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
243 << ", Offset: " << Offset << ", Size: " << Size << "\n";
245 O << Indent << dwarf::TagString(getTag()) << " "
246 << dwarf::ChildrenString(hasChildren()) << "\n";
248 IndentCount += 2;
249 for (const auto &V : values()) {
250 O << Indent;
251 O << dwarf::AttributeString(V.getAttribute());
252 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
253 V.print(O);
254 O << "\n";
256 IndentCount -= 2;
258 for (const auto &Child : children())
259 Child.print(O, IndentCount + 4);
261 O << "\n";
264 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
265 LLVM_DUMP_METHOD void DIE::dump() const {
266 print(dbgs());
268 #endif
270 unsigned DIE::computeOffsetsAndAbbrevs(const dwarf::FormParams &FormParams,
271 DIEAbbrevSet &AbbrevSet,
272 unsigned CUOffset) {
273 // Unique the abbreviation and fill in the abbreviation number so this DIE
274 // can be emitted.
275 const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);
277 // Set compile/type unit relative offset of this DIE.
278 setOffset(CUOffset);
280 // Add the byte size of the abbreviation code.
281 CUOffset += getULEB128Size(getAbbrevNumber());
283 // Add the byte size of all the DIE attribute values.
284 for (const auto &V : values())
285 CUOffset += V.sizeOf(FormParams);
287 // Let the children compute their offsets and abbreviation numbers.
288 if (hasChildren()) {
289 (void)Abbrev;
290 assert(Abbrev.hasChildren() && "Children flag not set");
292 for (auto &Child : children())
293 CUOffset =
294 Child.computeOffsetsAndAbbrevs(FormParams, AbbrevSet, CUOffset);
296 // Each child chain is terminated with a zero byte, adjust the offset.
297 CUOffset += sizeof(int8_t);
300 // Compute the byte size of this DIE and all of its children correctly. This
301 // is needed so that top level DIE can help the compile unit set its length
302 // correctly.
303 setSize(CUOffset - getOffset());
304 return CUOffset;
307 //===----------------------------------------------------------------------===//
308 // DIEUnit Implementation
309 //===----------------------------------------------------------------------===//
310 DIEUnit::DIEUnit(dwarf::Tag UnitTag) : Die(UnitTag) {
311 Die.Owner = this;
312 assert((UnitTag == dwarf::DW_TAG_compile_unit ||
313 UnitTag == dwarf::DW_TAG_skeleton_unit ||
314 UnitTag == dwarf::DW_TAG_type_unit ||
315 UnitTag == dwarf::DW_TAG_partial_unit) &&
316 "expected a unit TAG");
319 void DIEValue::emitValue(const AsmPrinter *AP) const {
320 switch (Ty) {
321 case isNone:
322 llvm_unreachable("Expected valid DIEValue");
323 #define HANDLE_DIEVALUE(T) \
324 case is##T: \
325 getDIE##T().emitValue(AP, Form); \
326 break;
327 #include "llvm/CodeGen/DIEValue.def"
331 unsigned DIEValue::sizeOf(const dwarf::FormParams &FormParams) const {
332 switch (Ty) {
333 case isNone:
334 llvm_unreachable("Expected valid DIEValue");
335 #define HANDLE_DIEVALUE(T) \
336 case is##T: \
337 return getDIE##T().sizeOf(FormParams, Form);
338 #include "llvm/CodeGen/DIEValue.def"
340 llvm_unreachable("Unknown DIE kind");
343 LLVM_DUMP_METHOD
344 void DIEValue::print(raw_ostream &O) const {
345 switch (Ty) {
346 case isNone:
347 llvm_unreachable("Expected valid DIEValue");
348 #define HANDLE_DIEVALUE(T) \
349 case is##T: \
350 getDIE##T().print(O); \
351 break;
352 #include "llvm/CodeGen/DIEValue.def"
356 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
357 LLVM_DUMP_METHOD void DIEValue::dump() const {
358 print(dbgs());
360 #endif
362 //===----------------------------------------------------------------------===//
363 // DIEInteger Implementation
364 //===----------------------------------------------------------------------===//
366 /// EmitValue - Emit integer of appropriate size.
368 void DIEInteger::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
369 switch (Form) {
370 case dwarf::DW_FORM_implicit_const:
371 case dwarf::DW_FORM_flag_present:
372 // Emit something to keep the lines and comments in sync.
373 // FIXME: Is there a better way to do this?
374 Asm->OutStreamer->addBlankLine();
375 return;
376 case dwarf::DW_FORM_flag:
377 case dwarf::DW_FORM_ref1:
378 case dwarf::DW_FORM_data1:
379 case dwarf::DW_FORM_strx1:
380 case dwarf::DW_FORM_addrx1:
381 case dwarf::DW_FORM_ref2:
382 case dwarf::DW_FORM_data2:
383 case dwarf::DW_FORM_strx2:
384 case dwarf::DW_FORM_addrx2:
385 case dwarf::DW_FORM_strx3:
386 case dwarf::DW_FORM_addrx3:
387 case dwarf::DW_FORM_strp:
388 case dwarf::DW_FORM_ref4:
389 case dwarf::DW_FORM_data4:
390 case dwarf::DW_FORM_ref_sup4:
391 case dwarf::DW_FORM_strx4:
392 case dwarf::DW_FORM_addrx4:
393 case dwarf::DW_FORM_ref8:
394 case dwarf::DW_FORM_ref_sig8:
395 case dwarf::DW_FORM_data8:
396 case dwarf::DW_FORM_ref_sup8:
397 case dwarf::DW_FORM_GNU_ref_alt:
398 case dwarf::DW_FORM_GNU_strp_alt:
399 case dwarf::DW_FORM_line_strp:
400 case dwarf::DW_FORM_sec_offset:
401 case dwarf::DW_FORM_strp_sup:
402 case dwarf::DW_FORM_addr:
403 case dwarf::DW_FORM_ref_addr:
404 Asm->OutStreamer->emitIntValue(Integer,
405 sizeOf(Asm->getDwarfFormParams(), Form));
406 return;
407 case dwarf::DW_FORM_GNU_str_index:
408 case dwarf::DW_FORM_GNU_addr_index:
409 case dwarf::DW_FORM_ref_udata:
410 case dwarf::DW_FORM_strx:
411 case dwarf::DW_FORM_addrx:
412 case dwarf::DW_FORM_rnglistx:
413 case dwarf::DW_FORM_udata:
414 Asm->emitULEB128(Integer);
415 return;
416 case dwarf::DW_FORM_sdata:
417 Asm->emitSLEB128(Integer);
418 return;
419 default: llvm_unreachable("DIE Value form not supported yet");
423 /// sizeOf - Determine size of integer value in bytes.
425 unsigned DIEInteger::sizeOf(const dwarf::FormParams &FormParams,
426 dwarf::Form Form) const {
427 if (std::optional<uint8_t> FixedSize =
428 dwarf::getFixedFormByteSize(Form, FormParams))
429 return *FixedSize;
431 switch (Form) {
432 case dwarf::DW_FORM_GNU_str_index:
433 case dwarf::DW_FORM_GNU_addr_index:
434 case dwarf::DW_FORM_ref_udata:
435 case dwarf::DW_FORM_strx:
436 case dwarf::DW_FORM_addrx:
437 case dwarf::DW_FORM_rnglistx:
438 case dwarf::DW_FORM_udata:
439 return getULEB128Size(Integer);
440 case dwarf::DW_FORM_sdata:
441 return getSLEB128Size(Integer);
442 default: llvm_unreachable("DIE Value form not supported yet");
446 LLVM_DUMP_METHOD
447 void DIEInteger::print(raw_ostream &O) const {
448 O << "Int: " << (int64_t)Integer << " 0x";
449 O.write_hex(Integer);
452 //===----------------------------------------------------------------------===//
453 // DIEExpr Implementation
454 //===----------------------------------------------------------------------===//
456 /// EmitValue - Emit expression value.
458 void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
459 AP->emitDebugValue(Expr, sizeOf(AP->getDwarfFormParams(), Form));
462 /// SizeOf - Determine size of expression value in bytes.
464 unsigned DIEExpr::sizeOf(const dwarf::FormParams &FormParams,
465 dwarf::Form Form) const {
466 switch (Form) {
467 case dwarf::DW_FORM_data4:
468 return 4;
469 case dwarf::DW_FORM_data8:
470 return 8;
471 case dwarf::DW_FORM_sec_offset:
472 return FormParams.getDwarfOffsetByteSize();
473 default:
474 llvm_unreachable("DIE Value form not supported yet");
478 LLVM_DUMP_METHOD
479 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
481 //===----------------------------------------------------------------------===//
482 // DIELabel Implementation
483 //===----------------------------------------------------------------------===//
485 /// EmitValue - Emit label value.
487 void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
488 bool IsSectionRelative = Form != dwarf::DW_FORM_addr;
489 AP->emitLabelReference(Label, sizeOf(AP->getDwarfFormParams(), Form),
490 IsSectionRelative);
493 /// sizeOf - Determine size of label value in bytes.
495 unsigned DIELabel::sizeOf(const dwarf::FormParams &FormParams,
496 dwarf::Form Form) const {
497 switch (Form) {
498 case dwarf::DW_FORM_data4:
499 return 4;
500 case dwarf::DW_FORM_data8:
501 return 8;
502 case dwarf::DW_FORM_sec_offset:
503 case dwarf::DW_FORM_strp:
504 return FormParams.getDwarfOffsetByteSize();
505 case dwarf::DW_FORM_addr:
506 return FormParams.AddrSize;
507 default:
508 llvm_unreachable("DIE Value form not supported yet");
512 LLVM_DUMP_METHOD
513 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
515 //===----------------------------------------------------------------------===//
516 // DIEBaseTypeRef Implementation
517 //===----------------------------------------------------------------------===//
519 void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
520 uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();
521 assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
522 AP->emitULEB128(Offset, nullptr, ULEB128PadSize);
525 unsigned DIEBaseTypeRef::sizeOf(const dwarf::FormParams &, dwarf::Form) const {
526 return ULEB128PadSize;
529 LLVM_DUMP_METHOD
530 void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }
532 //===----------------------------------------------------------------------===//
533 // DIEDelta Implementation
534 //===----------------------------------------------------------------------===//
536 /// EmitValue - Emit delta value.
538 void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
539 AP->emitLabelDifference(LabelHi, LabelLo,
540 sizeOf(AP->getDwarfFormParams(), Form));
543 /// SizeOf - Determine size of delta value in bytes.
545 unsigned DIEDelta::sizeOf(const dwarf::FormParams &FormParams,
546 dwarf::Form Form) const {
547 switch (Form) {
548 case dwarf::DW_FORM_data4:
549 return 4;
550 case dwarf::DW_FORM_data8:
551 return 8;
552 case dwarf::DW_FORM_sec_offset:
553 return FormParams.getDwarfOffsetByteSize();
554 default:
555 llvm_unreachable("DIE Value form not supported yet");
559 LLVM_DUMP_METHOD
560 void DIEDelta::print(raw_ostream &O) const {
561 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
564 //===----------------------------------------------------------------------===//
565 // DIEString Implementation
566 //===----------------------------------------------------------------------===//
568 /// EmitValue - Emit string value.
570 void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
571 // Index of string in symbol table.
572 switch (Form) {
573 case dwarf::DW_FORM_GNU_str_index:
574 case dwarf::DW_FORM_strx:
575 case dwarf::DW_FORM_strx1:
576 case dwarf::DW_FORM_strx2:
577 case dwarf::DW_FORM_strx3:
578 case dwarf::DW_FORM_strx4:
579 DIEInteger(S.getIndex()).emitValue(AP, Form);
580 return;
581 case dwarf::DW_FORM_strp:
582 if (AP->doesDwarfUseRelocationsAcrossSections())
583 DIELabel(S.getSymbol()).emitValue(AP, Form);
584 else
585 DIEInteger(S.getOffset()).emitValue(AP, Form);
586 return;
587 default:
588 llvm_unreachable("Expected valid string form");
592 /// sizeOf - Determine size of delta value in bytes.
594 unsigned DIEString::sizeOf(const dwarf::FormParams &FormParams,
595 dwarf::Form Form) const {
596 // Index of string in symbol table.
597 switch (Form) {
598 case dwarf::DW_FORM_GNU_str_index:
599 case dwarf::DW_FORM_strx:
600 case dwarf::DW_FORM_strx1:
601 case dwarf::DW_FORM_strx2:
602 case dwarf::DW_FORM_strx3:
603 case dwarf::DW_FORM_strx4:
604 return DIEInteger(S.getIndex()).sizeOf(FormParams, Form);
605 case dwarf::DW_FORM_strp:
606 if (FormParams.DwarfUsesRelocationsAcrossSections)
607 return DIELabel(S.getSymbol()).sizeOf(FormParams, Form);
608 return DIEInteger(S.getOffset()).sizeOf(FormParams, Form);
609 default:
610 llvm_unreachable("Expected valid string form");
614 LLVM_DUMP_METHOD
615 void DIEString::print(raw_ostream &O) const {
616 O << "String: " << S.getString();
619 //===----------------------------------------------------------------------===//
620 // DIEInlineString Implementation
621 //===----------------------------------------------------------------------===//
622 void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
623 if (Form == dwarf::DW_FORM_string) {
624 AP->OutStreamer->emitBytes(S);
625 AP->emitInt8(0);
626 return;
628 llvm_unreachable("Expected valid string form");
631 unsigned DIEInlineString::sizeOf(const dwarf::FormParams &, dwarf::Form) const {
632 // Emit string bytes + NULL byte.
633 return S.size() + 1;
636 LLVM_DUMP_METHOD
637 void DIEInlineString::print(raw_ostream &O) const {
638 O << "InlineString: " << S;
641 //===----------------------------------------------------------------------===//
642 // DIEEntry Implementation
643 //===----------------------------------------------------------------------===//
645 /// EmitValue - Emit debug information entry offset.
647 void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
649 switch (Form) {
650 case dwarf::DW_FORM_ref1:
651 case dwarf::DW_FORM_ref2:
652 case dwarf::DW_FORM_ref4:
653 case dwarf::DW_FORM_ref8:
654 AP->OutStreamer->emitIntValue(Entry->getOffset(),
655 sizeOf(AP->getDwarfFormParams(), Form));
656 return;
658 case dwarf::DW_FORM_ref_udata:
659 AP->emitULEB128(Entry->getOffset());
660 return;
662 case dwarf::DW_FORM_ref_addr: {
663 // Get the absolute offset for this DIE within the debug info/types section.
664 uint64_t Addr = Entry->getDebugSectionOffset();
665 if (const MCSymbol *SectionSym =
666 Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
667 AP->emitLabelPlusOffset(SectionSym, Addr,
668 sizeOf(AP->getDwarfFormParams(), Form), true);
669 return;
672 AP->OutStreamer->emitIntValue(Addr, sizeOf(AP->getDwarfFormParams(), Form));
673 return;
675 default:
676 llvm_unreachable("Improper form for DIE reference");
680 unsigned DIEEntry::sizeOf(const dwarf::FormParams &FormParams,
681 dwarf::Form Form) const {
682 switch (Form) {
683 case dwarf::DW_FORM_ref1:
684 return 1;
685 case dwarf::DW_FORM_ref2:
686 return 2;
687 case dwarf::DW_FORM_ref4:
688 return 4;
689 case dwarf::DW_FORM_ref8:
690 return 8;
691 case dwarf::DW_FORM_ref_udata:
692 return getULEB128Size(Entry->getOffset());
693 case dwarf::DW_FORM_ref_addr:
694 return FormParams.getRefAddrByteSize();
696 default:
697 llvm_unreachable("Improper form for DIE reference");
701 LLVM_DUMP_METHOD
702 void DIEEntry::print(raw_ostream &O) const {
703 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
706 //===----------------------------------------------------------------------===//
707 // DIELoc Implementation
708 //===----------------------------------------------------------------------===//
710 unsigned DIELoc::computeSize(const dwarf::FormParams &FormParams) const {
711 if (!Size) {
712 for (const auto &V : values())
713 Size += V.sizeOf(FormParams);
716 return Size;
719 /// EmitValue - Emit location data.
721 void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
722 switch (Form) {
723 default: llvm_unreachable("Improper form for block");
724 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
725 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
726 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
727 case dwarf::DW_FORM_block:
728 case dwarf::DW_FORM_exprloc:
729 Asm->emitULEB128(Size);
730 break;
733 for (const auto &V : values())
734 V.emitValue(Asm);
737 /// sizeOf - Determine size of location data in bytes.
739 unsigned DIELoc::sizeOf(const dwarf::FormParams &, 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");
751 LLVM_DUMP_METHOD
752 void DIELoc::print(raw_ostream &O) const {
753 printValues(O, *this, "ExprLoc", Size, 5);
756 //===----------------------------------------------------------------------===//
757 // DIEBlock Implementation
758 //===----------------------------------------------------------------------===//
760 unsigned DIEBlock::computeSize(const dwarf::FormParams &FormParams) const {
761 if (!Size) {
762 for (const auto &V : values())
763 Size += V.sizeOf(FormParams);
766 return Size;
769 /// EmitValue - Emit block data.
771 void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
772 switch (Form) {
773 default: llvm_unreachable("Improper form for block");
774 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
775 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
776 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
777 case dwarf::DW_FORM_exprloc:
778 case dwarf::DW_FORM_block:
779 Asm->emitULEB128(Size);
780 break;
781 case dwarf::DW_FORM_string: break;
782 case dwarf::DW_FORM_data16: break;
785 for (const auto &V : values())
786 V.emitValue(Asm);
789 /// sizeOf - Determine size of block data in bytes.
791 unsigned DIEBlock::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {
792 switch (Form) {
793 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
794 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
795 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
796 case dwarf::DW_FORM_exprloc:
797 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
798 case dwarf::DW_FORM_data16: return 16;
799 default: llvm_unreachable("Improper form for block");
803 LLVM_DUMP_METHOD
804 void DIEBlock::print(raw_ostream &O) const {
805 printValues(O, *this, "Blk", Size, 5);
808 //===----------------------------------------------------------------------===//
809 // DIELocList Implementation
810 //===----------------------------------------------------------------------===//
812 unsigned DIELocList::sizeOf(const dwarf::FormParams &FormParams,
813 dwarf::Form Form) const {
814 switch (Form) {
815 case dwarf::DW_FORM_loclistx:
816 return getULEB128Size(Index);
817 case dwarf::DW_FORM_data4:
818 assert(FormParams.Format != dwarf::DWARF64 &&
819 "DW_FORM_data4 is not suitable to emit a pointer to a location list "
820 "in the 64-bit DWARF format");
821 return 4;
822 case dwarf::DW_FORM_data8:
823 assert(FormParams.Format == dwarf::DWARF64 &&
824 "DW_FORM_data8 is not suitable to emit a pointer to a location list "
825 "in the 32-bit DWARF format");
826 return 8;
827 case dwarf::DW_FORM_sec_offset:
828 return FormParams.getDwarfOffsetByteSize();
829 default:
830 llvm_unreachable("DIE Value form not supported yet");
834 /// EmitValue - Emit label value.
836 void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
837 if (Form == dwarf::DW_FORM_loclistx) {
838 AP->emitULEB128(Index);
839 return;
841 DwarfDebug *DD = AP->getDwarfDebug();
842 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
843 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
846 LLVM_DUMP_METHOD
847 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
849 //===----------------------------------------------------------------------===//
850 // DIEAddrOffset Implementation
851 //===----------------------------------------------------------------------===//
853 unsigned DIEAddrOffset::sizeOf(const dwarf::FormParams &FormParams,
854 dwarf::Form) const {
855 return Addr.sizeOf(FormParams, dwarf::DW_FORM_addrx) +
856 Offset.sizeOf(FormParams, dwarf::DW_FORM_data4);
859 /// EmitValue - Emit label value.
861 void DIEAddrOffset::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
862 Addr.emitValue(AP, dwarf::DW_FORM_addrx);
863 Offset.emitValue(AP, dwarf::DW_FORM_data4);
866 LLVM_DUMP_METHOD
867 void DIEAddrOffset::print(raw_ostream &O) const {
868 O << "AddrOffset: ";
869 Addr.print(O);
870 O << " + ";
871 Offset.print(O);