Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / AsmPrinter / DIE.cpp
bloba07b54773039b5a9fd35ff5310c3246d94fec7b1
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 "DwarfUnit.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/Config/llvm-config.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;
34 #define DEBUG_TYPE "dwarfdebug"
36 //===----------------------------------------------------------------------===//
37 // DIEAbbrevData Implementation
38 //===----------------------------------------------------------------------===//
40 /// Profile - Used to gather unique data for the abbreviation folding set.
41 ///
42 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
43 // Explicitly cast to an integer type for which FoldingSetNodeID has
44 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
45 ID.AddInteger(unsigned(Attribute));
46 ID.AddInteger(unsigned(Form));
47 if (Form == dwarf::DW_FORM_implicit_const)
48 ID.AddInteger(Value);
51 //===----------------------------------------------------------------------===//
52 // DIEAbbrev Implementation
53 //===----------------------------------------------------------------------===//
55 /// Profile - Used to gather unique data for the abbreviation folding set.
56 ///
57 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
58 ID.AddInteger(unsigned(Tag));
59 ID.AddInteger(unsigned(Children));
61 // For each attribute description.
62 for (unsigned i = 0, N = Data.size(); i < N; ++i)
63 Data[i].Profile(ID);
66 /// Emit - Print the abbreviation using the specified asm printer.
67 ///
68 void DIEAbbrev::Emit(const AsmPrinter *AP) const {
69 // Emit its Dwarf tag type.
70 AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
72 // Emit whether it has children DIEs.
73 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
75 // For each attribute description.
76 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
77 const DIEAbbrevData &AttrData = Data[i];
79 // Emit attribute type.
80 AP->EmitULEB128(AttrData.getAttribute(),
81 dwarf::AttributeString(AttrData.getAttribute()).data());
83 // Emit form type.
84 #ifndef NDEBUG
85 // Could be an assertion, but this way we can see the failing form code
86 // easily, which helps track down where it came from.
87 if (!dwarf::isValidFormForVersion(AttrData.getForm(),
88 AP->getDwarfVersion())) {
89 LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())
90 << " for DWARF version " << AP->getDwarfVersion()
91 << "\n");
92 llvm_unreachable("Invalid form for specified DWARF version");
94 #endif
95 AP->EmitULEB128(AttrData.getForm(),
96 dwarf::FormEncodingString(AttrData.getForm()).data());
98 // Emit value for DW_FORM_implicit_const.
99 if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
100 AP->EmitSLEB128(AttrData.getValue());
103 // Mark end of abbreviation.
104 AP->EmitULEB128(0, "EOM(1)");
105 AP->EmitULEB128(0, "EOM(2)");
108 LLVM_DUMP_METHOD
109 void DIEAbbrev::print(raw_ostream &O) const {
110 O << "Abbreviation @"
111 << format("0x%lx", (long)(intptr_t)this)
112 << " "
113 << dwarf::TagString(Tag)
114 << " "
115 << dwarf::ChildrenString(Children)
116 << '\n';
118 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
119 O << " "
120 << dwarf::AttributeString(Data[i].getAttribute())
121 << " "
122 << dwarf::FormEncodingString(Data[i].getForm());
124 if (Data[i].getForm() == dwarf::DW_FORM_implicit_const)
125 O << " " << Data[i].getValue();
127 O << '\n';
131 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 LLVM_DUMP_METHOD void DIEAbbrev::dump() const {
133 print(dbgs());
135 #endif
137 //===----------------------------------------------------------------------===//
138 // DIEAbbrevSet Implementation
139 //===----------------------------------------------------------------------===//
141 DIEAbbrevSet::~DIEAbbrevSet() {
142 for (DIEAbbrev *Abbrev : Abbreviations)
143 Abbrev->~DIEAbbrev();
146 DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {
148 FoldingSetNodeID ID;
149 DIEAbbrev Abbrev = Die.generateAbbrev();
150 Abbrev.Profile(ID);
152 void *InsertPos;
153 if (DIEAbbrev *Existing =
154 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
155 Die.setAbbrevNumber(Existing->getNumber());
156 return *Existing;
159 // Move the abbreviation to the heap and assign a number.
160 DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));
161 Abbreviations.push_back(New);
162 New->setNumber(Abbreviations.size());
163 Die.setAbbrevNumber(Abbreviations.size());
165 // Store it for lookup.
166 AbbreviationsSet.InsertNode(New, InsertPos);
167 return *New;
170 void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {
171 if (!Abbreviations.empty()) {
172 // Start the debug abbrev section.
173 AP->OutStreamer->SwitchSection(Section);
174 AP->emitDwarfAbbrevs(Abbreviations);
178 //===----------------------------------------------------------------------===//
179 // DIE Implementation
180 //===----------------------------------------------------------------------===//
182 DIE *DIE::getParent() const {
183 return Owner.dyn_cast<DIE*>();
186 DIEAbbrev DIE::generateAbbrev() const {
187 DIEAbbrev Abbrev(Tag, hasChildren());
188 for (const DIEValue &V : values())
189 if (V.getForm() == dwarf::DW_FORM_implicit_const)
190 Abbrev.AddImplicitConstAttribute(V.getAttribute(),
191 V.getDIEInteger().getValue());
192 else
193 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
194 return Abbrev;
197 unsigned DIE::getDebugSectionOffset() const {
198 const DIEUnit *Unit = getUnit();
199 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
200 return Unit->getDebugSectionOffset() + getOffset();
203 const DIE *DIE::getUnitDie() const {
204 const DIE *p = this;
205 while (p) {
206 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
207 p->getTag() == dwarf::DW_TAG_type_unit)
208 return p;
209 p = p->getParent();
211 return nullptr;
214 const DIEUnit *DIE::getUnit() const {
215 const DIE *UnitDie = getUnitDie();
216 if (UnitDie)
217 return UnitDie->Owner.dyn_cast<DIEUnit*>();
218 return nullptr;
221 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
222 // Iterate through all the attributes until we find the one we're
223 // looking for, if we can't find it return NULL.
224 for (const auto &V : values())
225 if (V.getAttribute() == Attribute)
226 return V;
227 return DIEValue();
230 LLVM_DUMP_METHOD
231 static void printValues(raw_ostream &O, const DIEValueList &Values,
232 StringRef Type, unsigned Size, unsigned IndentCount) {
233 O << Type << ": Size: " << Size << "\n";
235 unsigned I = 0;
236 const std::string Indent(IndentCount, ' ');
237 for (const auto &V : Values.values()) {
238 O << Indent;
239 O << "Blk[" << I++ << "]";
240 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
241 V.print(O);
242 O << "\n";
246 LLVM_DUMP_METHOD
247 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
248 const std::string Indent(IndentCount, ' ');
249 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
250 << ", Offset: " << Offset << ", Size: " << Size << "\n";
252 O << Indent << dwarf::TagString(getTag()) << " "
253 << dwarf::ChildrenString(hasChildren()) << "\n";
255 IndentCount += 2;
256 for (const auto &V : values()) {
257 O << Indent;
258 O << dwarf::AttributeString(V.getAttribute());
259 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
260 V.print(O);
261 O << "\n";
263 IndentCount -= 2;
265 for (const auto &Child : children())
266 Child.print(O, IndentCount + 4);
268 O << "\n";
271 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
272 LLVM_DUMP_METHOD void DIE::dump() const {
273 print(dbgs());
275 #endif
277 unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter *AP,
278 DIEAbbrevSet &AbbrevSet,
279 unsigned CUOffset) {
280 // Unique the abbreviation and fill in the abbreviation number so this DIE
281 // can be emitted.
282 const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);
284 // Set compile/type unit relative offset of this DIE.
285 setOffset(CUOffset);
287 // Add the byte size of the abbreviation code.
288 CUOffset += getULEB128Size(getAbbrevNumber());
290 // Add the byte size of all the DIE attribute values.
291 for (const auto &V : values())
292 CUOffset += V.SizeOf(AP);
294 // Let the children compute their offsets and abbreviation numbers.
295 if (hasChildren()) {
296 (void)Abbrev;
297 assert(Abbrev.hasChildren() && "Children flag not set");
299 for (auto &Child : children())
300 CUOffset = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset);
302 // Each child chain is terminated with a zero byte, adjust the offset.
303 CUOffset += sizeof(int8_t);
306 // Compute the byte size of this DIE and all of its children correctly. This
307 // is needed so that top level DIE can help the compile unit set its length
308 // correctly.
309 setSize(CUOffset - getOffset());
310 return CUOffset;
313 //===----------------------------------------------------------------------===//
314 // DIEUnit Implementation
315 //===----------------------------------------------------------------------===//
316 DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
317 : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
318 AddrSize(A)
320 Die.Owner = this;
321 assert((UnitTag == dwarf::DW_TAG_compile_unit ||
322 UnitTag == dwarf::DW_TAG_type_unit ||
323 UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG");
326 void DIEValue::EmitValue(const AsmPrinter *AP) const {
327 switch (Ty) {
328 case isNone:
329 llvm_unreachable("Expected valid DIEValue");
330 #define HANDLE_DIEVALUE(T) \
331 case is##T: \
332 getDIE##T().EmitValue(AP, Form); \
333 break;
334 #include "llvm/CodeGen/DIEValue.def"
338 unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
339 switch (Ty) {
340 case isNone:
341 llvm_unreachable("Expected valid DIEValue");
342 #define HANDLE_DIEVALUE(T) \
343 case is##T: \
344 return getDIE##T().SizeOf(AP, Form);
345 #include "llvm/CodeGen/DIEValue.def"
347 llvm_unreachable("Unknown DIE kind");
350 LLVM_DUMP_METHOD
351 void DIEValue::print(raw_ostream &O) const {
352 switch (Ty) {
353 case isNone:
354 llvm_unreachable("Expected valid DIEValue");
355 #define HANDLE_DIEVALUE(T) \
356 case is##T: \
357 getDIE##T().print(O); \
358 break;
359 #include "llvm/CodeGen/DIEValue.def"
363 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
364 LLVM_DUMP_METHOD void DIEValue::dump() const {
365 print(dbgs());
367 #endif
369 //===----------------------------------------------------------------------===//
370 // DIEInteger Implementation
371 //===----------------------------------------------------------------------===//
373 /// EmitValue - Emit integer of appropriate size.
375 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
376 switch (Form) {
377 case dwarf::DW_FORM_implicit_const:
378 case dwarf::DW_FORM_flag_present:
379 // Emit something to keep the lines and comments in sync.
380 // FIXME: Is there a better way to do this?
381 Asm->OutStreamer->AddBlankLine();
382 return;
383 case dwarf::DW_FORM_flag:
384 case dwarf::DW_FORM_ref1:
385 case dwarf::DW_FORM_data1:
386 case dwarf::DW_FORM_strx1:
387 case dwarf::DW_FORM_addrx1:
388 case dwarf::DW_FORM_ref2:
389 case dwarf::DW_FORM_data2:
390 case dwarf::DW_FORM_strx2:
391 case dwarf::DW_FORM_addrx2:
392 case dwarf::DW_FORM_strx3:
393 case dwarf::DW_FORM_strp:
394 case dwarf::DW_FORM_ref4:
395 case dwarf::DW_FORM_data4:
396 case dwarf::DW_FORM_ref_sup4:
397 case dwarf::DW_FORM_strx4:
398 case dwarf::DW_FORM_addrx4:
399 case dwarf::DW_FORM_ref8:
400 case dwarf::DW_FORM_ref_sig8:
401 case dwarf::DW_FORM_data8:
402 case dwarf::DW_FORM_ref_sup8:
403 case dwarf::DW_FORM_GNU_ref_alt:
404 case dwarf::DW_FORM_GNU_strp_alt:
405 case dwarf::DW_FORM_line_strp:
406 case dwarf::DW_FORM_sec_offset:
407 case dwarf::DW_FORM_strp_sup:
408 case dwarf::DW_FORM_addr:
409 case dwarf::DW_FORM_ref_addr:
410 Asm->OutStreamer->EmitIntValue(Integer, SizeOf(Asm, Form));
411 return;
412 case dwarf::DW_FORM_GNU_str_index:
413 case dwarf::DW_FORM_GNU_addr_index:
414 case dwarf::DW_FORM_ref_udata:
415 case dwarf::DW_FORM_strx:
416 case dwarf::DW_FORM_addrx:
417 case dwarf::DW_FORM_rnglistx:
418 case dwarf::DW_FORM_udata:
419 Asm->EmitULEB128(Integer);
420 return;
421 case dwarf::DW_FORM_sdata:
422 Asm->EmitSLEB128(Integer);
423 return;
424 default: llvm_unreachable("DIE Value form not supported yet");
428 /// SizeOf - Determine size of integer value in bytes.
430 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
431 dwarf::FormParams Params = {0, 0, dwarf::DWARF32};
432 if (AP)
433 Params = {AP->getDwarfVersion(), uint8_t(AP->getPointerSize()),
434 AP->OutStreamer->getContext().getDwarfFormat()};
436 if (Optional<uint8_t> FixedSize = dwarf::getFixedFormByteSize(Form, Params))
437 return *FixedSize;
439 switch (Form) {
440 case dwarf::DW_FORM_GNU_str_index:
441 case dwarf::DW_FORM_GNU_addr_index:
442 case dwarf::DW_FORM_ref_udata:
443 case dwarf::DW_FORM_strx:
444 case dwarf::DW_FORM_addrx:
445 case dwarf::DW_FORM_rnglistx:
446 case dwarf::DW_FORM_udata:
447 return getULEB128Size(Integer);
448 case dwarf::DW_FORM_sdata:
449 return getSLEB128Size(Integer);
450 default: llvm_unreachable("DIE Value form not supported yet");
454 LLVM_DUMP_METHOD
455 void DIEInteger::print(raw_ostream &O) const {
456 O << "Int: " << (int64_t)Integer << " 0x";
457 O.write_hex(Integer);
460 //===----------------------------------------------------------------------===//
461 // DIEExpr Implementation
462 //===----------------------------------------------------------------------===//
464 /// EmitValue - Emit expression value.
466 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
467 AP->EmitDebugValue(Expr, SizeOf(AP, Form));
470 /// SizeOf - Determine size of expression value in bytes.
472 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
473 if (Form == dwarf::DW_FORM_data4) return 4;
474 if (Form == dwarf::DW_FORM_sec_offset) return 4;
475 if (Form == dwarf::DW_FORM_strp) return 4;
476 return AP->getPointerSize();
479 LLVM_DUMP_METHOD
480 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
482 //===----------------------------------------------------------------------===//
483 // DIELabel Implementation
484 //===----------------------------------------------------------------------===//
486 /// EmitValue - Emit label value.
488 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
489 AP->EmitLabelReference(Label, SizeOf(AP, Form),
490 Form == dwarf::DW_FORM_strp ||
491 Form == dwarf::DW_FORM_sec_offset ||
492 Form == dwarf::DW_FORM_ref_addr ||
493 Form == dwarf::DW_FORM_data4);
496 /// SizeOf - Determine size of label value in bytes.
498 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
499 if (Form == dwarf::DW_FORM_data4) return 4;
500 if (Form == dwarf::DW_FORM_sec_offset) return 4;
501 if (Form == dwarf::DW_FORM_strp) return 4;
502 return AP->MAI->getCodePointerSize();
505 LLVM_DUMP_METHOD
506 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
508 //===----------------------------------------------------------------------===//
509 // DIEDelta Implementation
510 //===----------------------------------------------------------------------===//
512 /// EmitValue - Emit delta value.
514 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
515 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
518 /// SizeOf - Determine size of delta value in bytes.
520 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
521 if (Form == dwarf::DW_FORM_data4) return 4;
522 if (Form == dwarf::DW_FORM_sec_offset) return 4;
523 if (Form == dwarf::DW_FORM_strp) return 4;
524 return AP->MAI->getCodePointerSize();
527 LLVM_DUMP_METHOD
528 void DIEDelta::print(raw_ostream &O) const {
529 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
532 //===----------------------------------------------------------------------===//
533 // DIEString Implementation
534 //===----------------------------------------------------------------------===//
536 /// EmitValue - Emit string value.
538 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
539 // Index of string in symbol table.
540 switch (Form) {
541 case dwarf::DW_FORM_GNU_str_index:
542 case dwarf::DW_FORM_strx:
543 case dwarf::DW_FORM_strx1:
544 case dwarf::DW_FORM_strx2:
545 case dwarf::DW_FORM_strx3:
546 case dwarf::DW_FORM_strx4:
547 DIEInteger(S.getIndex()).EmitValue(AP, Form);
548 return;
549 case dwarf::DW_FORM_strp:
550 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
551 DIELabel(S.getSymbol()).EmitValue(AP, Form);
552 else
553 DIEInteger(S.getOffset()).EmitValue(AP, Form);
554 return;
555 default:
556 llvm_unreachable("Expected valid string form");
560 /// SizeOf - Determine size of delta value in bytes.
562 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
563 // Index of string in symbol table.
564 switch (Form) {
565 case dwarf::DW_FORM_GNU_str_index:
566 case dwarf::DW_FORM_strx:
567 case dwarf::DW_FORM_strx1:
568 case dwarf::DW_FORM_strx2:
569 case dwarf::DW_FORM_strx3:
570 case dwarf::DW_FORM_strx4:
571 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
572 case dwarf::DW_FORM_strp:
573 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
574 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
575 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
576 default:
577 llvm_unreachable("Expected valid string form");
581 LLVM_DUMP_METHOD
582 void DIEString::print(raw_ostream &O) const {
583 O << "String: " << S.getString();
586 //===----------------------------------------------------------------------===//
587 // DIEInlineString Implementation
588 //===----------------------------------------------------------------------===//
589 void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
590 if (Form == dwarf::DW_FORM_string) {
591 AP->OutStreamer->EmitBytes(S);
592 AP->emitInt8(0);
593 return;
595 llvm_unreachable("Expected valid string form");
598 unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
599 // Emit string bytes + NULL byte.
600 return S.size() + 1;
603 LLVM_DUMP_METHOD
604 void DIEInlineString::print(raw_ostream &O) const {
605 O << "InlineString: " << S;
608 //===----------------------------------------------------------------------===//
609 // DIEEntry Implementation
610 //===----------------------------------------------------------------------===//
612 /// EmitValue - Emit debug information entry offset.
614 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
616 switch (Form) {
617 case dwarf::DW_FORM_ref1:
618 case dwarf::DW_FORM_ref2:
619 case dwarf::DW_FORM_ref4:
620 case dwarf::DW_FORM_ref8:
621 AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form));
622 return;
624 case dwarf::DW_FORM_ref_udata:
625 AP->EmitULEB128(Entry->getOffset());
626 return;
628 case dwarf::DW_FORM_ref_addr: {
629 // Get the absolute offset for this DIE within the debug info/types section.
630 unsigned Addr = Entry->getDebugSectionOffset();
631 if (const MCSymbol *SectionSym =
632 Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
633 AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
634 return;
637 AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form));
638 return;
640 default:
641 llvm_unreachable("Improper form for DIE reference");
645 unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
646 switch (Form) {
647 case dwarf::DW_FORM_ref1:
648 return 1;
649 case dwarf::DW_FORM_ref2:
650 return 2;
651 case dwarf::DW_FORM_ref4:
652 return 4;
653 case dwarf::DW_FORM_ref8:
654 return 8;
655 case dwarf::DW_FORM_ref_udata:
656 return getULEB128Size(Entry->getOffset());
657 case dwarf::DW_FORM_ref_addr:
658 if (AP->getDwarfVersion() == 2)
659 return AP->MAI->getCodePointerSize();
660 switch (AP->OutStreamer->getContext().getDwarfFormat()) {
661 case dwarf::DWARF32:
662 return 4;
663 case dwarf::DWARF64:
664 return 8;
666 llvm_unreachable("Invalid DWARF format");
668 default:
669 llvm_unreachable("Improper form for DIE reference");
673 LLVM_DUMP_METHOD
674 void DIEEntry::print(raw_ostream &O) const {
675 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
678 //===----------------------------------------------------------------------===//
679 // DIELoc Implementation
680 //===----------------------------------------------------------------------===//
682 /// ComputeSize - calculate the size of the location expression.
684 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
685 if (!Size) {
686 for (const auto &V : values())
687 Size += V.SizeOf(AP);
690 return Size;
693 /// EmitValue - Emit location data.
695 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
696 switch (Form) {
697 default: llvm_unreachable("Improper form for block");
698 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
699 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
700 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
701 case dwarf::DW_FORM_block:
702 case dwarf::DW_FORM_exprloc:
703 Asm->EmitULEB128(Size); break;
706 for (const auto &V : values())
707 V.EmitValue(Asm);
710 /// SizeOf - Determine size of location data in bytes.
712 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
713 switch (Form) {
714 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
715 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
716 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
717 case dwarf::DW_FORM_block:
718 case dwarf::DW_FORM_exprloc:
719 return Size + getULEB128Size(Size);
720 default: llvm_unreachable("Improper form for block");
724 LLVM_DUMP_METHOD
725 void DIELoc::print(raw_ostream &O) const {
726 printValues(O, *this, "ExprLoc", Size, 5);
729 //===----------------------------------------------------------------------===//
730 // DIEBlock Implementation
731 //===----------------------------------------------------------------------===//
733 /// ComputeSize - calculate the size of the block.
735 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
736 if (!Size) {
737 for (const auto &V : values())
738 Size += V.SizeOf(AP);
741 return Size;
744 /// EmitValue - Emit block data.
746 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
747 switch (Form) {
748 default: llvm_unreachable("Improper form for block");
749 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
750 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
751 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
752 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
753 case dwarf::DW_FORM_string: break;
754 case dwarf::DW_FORM_data16: break;
757 for (const auto &V : values())
758 V.EmitValue(Asm);
761 /// SizeOf - Determine size of block data in bytes.
763 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
764 switch (Form) {
765 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
766 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
767 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
768 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
769 case dwarf::DW_FORM_data16: return 16;
770 default: llvm_unreachable("Improper form for block");
774 LLVM_DUMP_METHOD
775 void DIEBlock::print(raw_ostream &O) const {
776 printValues(O, *this, "Blk", Size, 5);
779 //===----------------------------------------------------------------------===//
780 // DIELocList Implementation
781 //===----------------------------------------------------------------------===//
783 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
784 if (Form == dwarf::DW_FORM_data4)
785 return 4;
786 if (Form == dwarf::DW_FORM_sec_offset)
787 return 4;
788 return AP->MAI->getCodePointerSize();
791 /// EmitValue - Emit label value.
793 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
794 DwarfDebug *DD = AP->getDwarfDebug();
795 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
796 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
799 LLVM_DUMP_METHOD
800 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }