Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DIE.cpp
35271 views
//===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Data structures for DWARF info entries.9//10//===----------------------------------------------------------------------===//1112#include "llvm/CodeGen/DIE.h"13#include "DwarfCompileUnit.h"14#include "DwarfDebug.h"15#include "llvm/CodeGen/AsmPrinter.h"16#include "llvm/Config/llvm-config.h"17#include "llvm/MC/MCAsmInfo.h"18#include "llvm/MC/MCStreamer.h"19#include "llvm/MC/MCSymbol.h"20#include "llvm/Support/Debug.h"21#include "llvm/Support/ErrorHandling.h"22#include "llvm/Support/Format.h"23#include "llvm/Support/LEB128.h"24#include "llvm/Support/raw_ostream.h"25using namespace llvm;2627#define DEBUG_TYPE "dwarfdebug"2829//===----------------------------------------------------------------------===//30// DIEAbbrevData Implementation31//===----------------------------------------------------------------------===//3233/// Profile - Used to gather unique data for the abbreviation folding set.34///35void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {36// Explicitly cast to an integer type for which FoldingSetNodeID has37// overloads. Otherwise MSVC 2010 thinks this call is ambiguous.38ID.AddInteger(unsigned(Attribute));39ID.AddInteger(unsigned(Form));40if (Form == dwarf::DW_FORM_implicit_const)41ID.AddInteger(Value);42}4344//===----------------------------------------------------------------------===//45// DIEAbbrev Implementation46//===----------------------------------------------------------------------===//4748/// Profile - Used to gather unique data for the abbreviation folding set.49///50void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {51ID.AddInteger(unsigned(Tag));52ID.AddInteger(unsigned(Children));5354// For each attribute description.55for (const DIEAbbrevData &D : Data)56D.Profile(ID);57}5859/// Emit - Print the abbreviation using the specified asm printer.60///61void DIEAbbrev::Emit(const AsmPrinter *AP) const {62// Emit its Dwarf tag type.63AP->emitULEB128(Tag, dwarf::TagString(Tag).data());6465// Emit whether it has children DIEs.66AP->emitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());6768// For each attribute description.69for (const DIEAbbrevData &AttrData : Data) {70// Emit attribute type.71AP->emitULEB128(AttrData.getAttribute(),72dwarf::AttributeString(AttrData.getAttribute()).data());7374// Emit form type.75#ifndef NDEBUG76// Could be an assertion, but this way we can see the failing form code77// easily, which helps track down where it came from.78if (!dwarf::isValidFormForVersion(AttrData.getForm(),79AP->getDwarfVersion())) {80LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())81<< " for DWARF version " << AP->getDwarfVersion()82<< "\n");83llvm_unreachable("Invalid form for specified DWARF version");84}85#endif86AP->emitULEB128(AttrData.getForm(),87dwarf::FormEncodingString(AttrData.getForm()).data());8889// Emit value for DW_FORM_implicit_const.90if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)91AP->emitSLEB128(AttrData.getValue());92}9394// Mark end of abbreviation.95AP->emitULEB128(0, "EOM(1)");96AP->emitULEB128(0, "EOM(2)");97}9899LLVM_DUMP_METHOD100void DIEAbbrev::print(raw_ostream &O) const {101O << "Abbreviation @"102<< format("0x%lx", (long)(intptr_t)this)103<< " "104<< dwarf::TagString(Tag)105<< " "106<< dwarf::ChildrenString(Children)107<< '\n';108109for (const DIEAbbrevData &D : Data) {110O << " " << dwarf::AttributeString(D.getAttribute()) << " "111<< dwarf::FormEncodingString(D.getForm());112113if (D.getForm() == dwarf::DW_FORM_implicit_const)114O << " " << D.getValue();115116O << '\n';117}118}119120#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)121LLVM_DUMP_METHOD void DIEAbbrev::dump() const {122print(dbgs());123}124#endif125126//===----------------------------------------------------------------------===//127// DIEAbbrevSet Implementation128//===----------------------------------------------------------------------===//129130DIEAbbrevSet::~DIEAbbrevSet() {131for (DIEAbbrev *Abbrev : Abbreviations)132Abbrev->~DIEAbbrev();133}134135DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {136137FoldingSetNodeID ID;138DIEAbbrev Abbrev = Die.generateAbbrev();139Abbrev.Profile(ID);140141void *InsertPos;142if (DIEAbbrev *Existing =143AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {144Die.setAbbrevNumber(Existing->getNumber());145return *Existing;146}147148// Move the abbreviation to the heap and assign a number.149DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));150Abbreviations.push_back(New);151New->setNumber(Abbreviations.size());152Die.setAbbrevNumber(Abbreviations.size());153154// Store it for lookup.155AbbreviationsSet.InsertNode(New, InsertPos);156return *New;157}158159void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {160if (!Abbreviations.empty()) {161// Start the debug abbrev section.162AP->OutStreamer->switchSection(Section);163AP->emitDwarfAbbrevs(Abbreviations);164}165}166167//===----------------------------------------------------------------------===//168// DIE Implementation169//===----------------------------------------------------------------------===//170171DIE *DIE::getParent() const { return dyn_cast_if_present<DIE *>(Owner); }172173DIEAbbrev DIE::generateAbbrev() const {174DIEAbbrev Abbrev(Tag, hasChildren());175for (const DIEValue &V : values())176if (V.getForm() == dwarf::DW_FORM_implicit_const)177Abbrev.AddImplicitConstAttribute(V.getAttribute(),178V.getDIEInteger().getValue());179else180Abbrev.AddAttribute(V.getAttribute(), V.getForm());181return Abbrev;182}183184uint64_t DIE::getDebugSectionOffset() const {185const DIEUnit *Unit = getUnit();186assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");187return Unit->getDebugSectionOffset() + getOffset();188}189190const DIE *DIE::getUnitDie() const {191const DIE *p = this;192while (p) {193if (p->getTag() == dwarf::DW_TAG_compile_unit ||194p->getTag() == dwarf::DW_TAG_skeleton_unit ||195p->getTag() == dwarf::DW_TAG_type_unit)196return p;197p = p->getParent();198}199return nullptr;200}201202DIEUnit *DIE::getUnit() const {203const DIE *UnitDie = getUnitDie();204if (UnitDie)205return dyn_cast_if_present<DIEUnit *>(UnitDie->Owner);206return nullptr;207}208209DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {210// Iterate through all the attributes until we find the one we're211// looking for, if we can't find it return NULL.212for (const auto &V : values())213if (V.getAttribute() == Attribute)214return V;215return DIEValue();216}217218LLVM_DUMP_METHOD219static void printValues(raw_ostream &O, const DIEValueList &Values,220StringRef Type, unsigned Size, unsigned IndentCount) {221O << Type << ": Size: " << Size << "\n";222223unsigned I = 0;224const std::string Indent(IndentCount, ' ');225for (const auto &V : Values.values()) {226O << Indent;227O << "Blk[" << I++ << "]";228O << " " << dwarf::FormEncodingString(V.getForm()) << " ";229V.print(O);230O << "\n";231}232}233234LLVM_DUMP_METHOD235void DIE::print(raw_ostream &O, unsigned IndentCount) const {236const std::string Indent(IndentCount, ' ');237O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)238<< ", Offset: " << Offset << ", Size: " << Size << "\n";239240O << Indent << dwarf::TagString(getTag()) << " "241<< dwarf::ChildrenString(hasChildren()) << "\n";242243IndentCount += 2;244for (const auto &V : values()) {245O << Indent;246O << dwarf::AttributeString(V.getAttribute());247O << " " << dwarf::FormEncodingString(V.getForm()) << " ";248V.print(O);249O << "\n";250}251IndentCount -= 2;252253for (const auto &Child : children())254Child.print(O, IndentCount + 4);255256O << "\n";257}258259#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)260LLVM_DUMP_METHOD void DIE::dump() const {261print(dbgs());262}263#endif264265unsigned DIE::computeOffsetsAndAbbrevs(const dwarf::FormParams &FormParams,266DIEAbbrevSet &AbbrevSet,267unsigned CUOffset) {268// Unique the abbreviation and fill in the abbreviation number so this DIE269// can be emitted.270const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);271272// Set compile/type unit relative offset of this DIE.273setOffset(CUOffset);274275// Add the byte size of the abbreviation code.276CUOffset += getULEB128Size(getAbbrevNumber());277278// Add the byte size of all the DIE attribute values.279for (const auto &V : values())280CUOffset += V.sizeOf(FormParams);281282// Let the children compute their offsets and abbreviation numbers.283if (hasChildren()) {284(void)Abbrev;285assert(Abbrev.hasChildren() && "Children flag not set");286287for (auto &Child : children())288CUOffset =289Child.computeOffsetsAndAbbrevs(FormParams, AbbrevSet, CUOffset);290291// Each child chain is terminated with a zero byte, adjust the offset.292CUOffset += sizeof(int8_t);293}294295// Compute the byte size of this DIE and all of its children correctly. This296// is needed so that top level DIE can help the compile unit set its length297// correctly.298setSize(CUOffset - getOffset());299return CUOffset;300}301302//===----------------------------------------------------------------------===//303// DIEUnit Implementation304//===----------------------------------------------------------------------===//305DIEUnit::DIEUnit(dwarf::Tag UnitTag) : Die(UnitTag) {306Die.Owner = this;307assert((UnitTag == dwarf::DW_TAG_compile_unit ||308UnitTag == dwarf::DW_TAG_skeleton_unit ||309UnitTag == dwarf::DW_TAG_type_unit ||310UnitTag == dwarf::DW_TAG_partial_unit) &&311"expected a unit TAG");312}313314void DIEValue::emitValue(const AsmPrinter *AP) const {315switch (Ty) {316case isNone:317llvm_unreachable("Expected valid DIEValue");318#define HANDLE_DIEVALUE(T) \319case is##T: \320getDIE##T().emitValue(AP, Form); \321break;322#include "llvm/CodeGen/DIEValue.def"323}324}325326unsigned DIEValue::sizeOf(const dwarf::FormParams &FormParams) const {327switch (Ty) {328case isNone:329llvm_unreachable("Expected valid DIEValue");330#define HANDLE_DIEVALUE(T) \331case is##T: \332return getDIE##T().sizeOf(FormParams, Form);333#include "llvm/CodeGen/DIEValue.def"334}335llvm_unreachable("Unknown DIE kind");336}337338LLVM_DUMP_METHOD339void DIEValue::print(raw_ostream &O) const {340switch (Ty) {341case isNone:342llvm_unreachable("Expected valid DIEValue");343#define HANDLE_DIEVALUE(T) \344case is##T: \345getDIE##T().print(O); \346break;347#include "llvm/CodeGen/DIEValue.def"348}349}350351#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)352LLVM_DUMP_METHOD void DIEValue::dump() const {353print(dbgs());354}355#endif356357//===----------------------------------------------------------------------===//358// DIEInteger Implementation359//===----------------------------------------------------------------------===//360361/// EmitValue - Emit integer of appropriate size.362///363void DIEInteger::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {364switch (Form) {365case dwarf::DW_FORM_implicit_const:366case dwarf::DW_FORM_flag_present:367// Emit something to keep the lines and comments in sync.368// FIXME: Is there a better way to do this?369Asm->OutStreamer->addBlankLine();370return;371case dwarf::DW_FORM_flag:372case dwarf::DW_FORM_ref1:373case dwarf::DW_FORM_data1:374case dwarf::DW_FORM_strx1:375case dwarf::DW_FORM_addrx1:376case dwarf::DW_FORM_ref2:377case dwarf::DW_FORM_data2:378case dwarf::DW_FORM_strx2:379case dwarf::DW_FORM_addrx2:380case dwarf::DW_FORM_strx3:381case dwarf::DW_FORM_addrx3:382case dwarf::DW_FORM_strp:383case dwarf::DW_FORM_ref4:384case dwarf::DW_FORM_data4:385case dwarf::DW_FORM_ref_sup4:386case dwarf::DW_FORM_strx4:387case dwarf::DW_FORM_addrx4:388case dwarf::DW_FORM_ref8:389case dwarf::DW_FORM_ref_sig8:390case dwarf::DW_FORM_data8:391case dwarf::DW_FORM_ref_sup8:392case dwarf::DW_FORM_GNU_ref_alt:393case dwarf::DW_FORM_GNU_strp_alt:394case dwarf::DW_FORM_line_strp:395case dwarf::DW_FORM_sec_offset:396case dwarf::DW_FORM_strp_sup:397case dwarf::DW_FORM_addr:398case dwarf::DW_FORM_ref_addr:399Asm->OutStreamer->emitIntValue(Integer,400sizeOf(Asm->getDwarfFormParams(), Form));401return;402case dwarf::DW_FORM_GNU_str_index:403case dwarf::DW_FORM_GNU_addr_index:404case dwarf::DW_FORM_ref_udata:405case dwarf::DW_FORM_strx:406case dwarf::DW_FORM_addrx:407case dwarf::DW_FORM_rnglistx:408case dwarf::DW_FORM_udata:409Asm->emitULEB128(Integer);410return;411case dwarf::DW_FORM_sdata:412Asm->emitSLEB128(Integer);413return;414default: llvm_unreachable("DIE Value form not supported yet");415}416}417418/// sizeOf - Determine size of integer value in bytes.419///420unsigned DIEInteger::sizeOf(const dwarf::FormParams &FormParams,421dwarf::Form Form) const {422if (std::optional<uint8_t> FixedSize =423dwarf::getFixedFormByteSize(Form, FormParams))424return *FixedSize;425426switch (Form) {427case dwarf::DW_FORM_GNU_str_index:428case dwarf::DW_FORM_GNU_addr_index:429case dwarf::DW_FORM_ref_udata:430case dwarf::DW_FORM_strx:431case dwarf::DW_FORM_addrx:432case dwarf::DW_FORM_rnglistx:433case dwarf::DW_FORM_udata:434return getULEB128Size(Integer);435case dwarf::DW_FORM_sdata:436return getSLEB128Size(Integer);437default: llvm_unreachable("DIE Value form not supported yet");438}439}440441LLVM_DUMP_METHOD442void DIEInteger::print(raw_ostream &O) const {443O << "Int: " << (int64_t)Integer << " 0x";444O.write_hex(Integer);445}446447//===----------------------------------------------------------------------===//448// DIEExpr Implementation449//===----------------------------------------------------------------------===//450451/// EmitValue - Emit expression value.452///453void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {454AP->emitDebugValue(Expr, sizeOf(AP->getDwarfFormParams(), Form));455}456457/// SizeOf - Determine size of expression value in bytes.458///459unsigned DIEExpr::sizeOf(const dwarf::FormParams &FormParams,460dwarf::Form Form) const {461switch (Form) {462case dwarf::DW_FORM_data4:463return 4;464case dwarf::DW_FORM_data8:465return 8;466case dwarf::DW_FORM_sec_offset:467return FormParams.getDwarfOffsetByteSize();468default:469llvm_unreachable("DIE Value form not supported yet");470}471}472473LLVM_DUMP_METHOD474void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }475476//===----------------------------------------------------------------------===//477// DIELabel Implementation478//===----------------------------------------------------------------------===//479480/// EmitValue - Emit label value.481///482void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {483bool IsSectionRelative = Form != dwarf::DW_FORM_addr;484AP->emitLabelReference(Label, sizeOf(AP->getDwarfFormParams(), Form),485IsSectionRelative);486}487488/// sizeOf - Determine size of label value in bytes.489///490unsigned DIELabel::sizeOf(const dwarf::FormParams &FormParams,491dwarf::Form Form) const {492switch (Form) {493case dwarf::DW_FORM_data4:494return 4;495case dwarf::DW_FORM_data8:496return 8;497case dwarf::DW_FORM_sec_offset:498case dwarf::DW_FORM_strp:499return FormParams.getDwarfOffsetByteSize();500case dwarf::DW_FORM_addr:501return FormParams.AddrSize;502default:503llvm_unreachable("DIE Value form not supported yet");504}505}506507LLVM_DUMP_METHOD508void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }509510//===----------------------------------------------------------------------===//511// DIEBaseTypeRef Implementation512//===----------------------------------------------------------------------===//513514void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {515uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();516assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");517AP->emitULEB128(Offset, nullptr, ULEB128PadSize);518}519520unsigned DIEBaseTypeRef::sizeOf(const dwarf::FormParams &, dwarf::Form) const {521return ULEB128PadSize;522}523524LLVM_DUMP_METHOD525void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }526527//===----------------------------------------------------------------------===//528// DIEDelta Implementation529//===----------------------------------------------------------------------===//530531/// EmitValue - Emit delta value.532///533void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {534AP->emitLabelDifference(LabelHi, LabelLo,535sizeOf(AP->getDwarfFormParams(), Form));536}537538/// SizeOf - Determine size of delta value in bytes.539///540unsigned DIEDelta::sizeOf(const dwarf::FormParams &FormParams,541dwarf::Form Form) const {542switch (Form) {543case dwarf::DW_FORM_data4:544return 4;545case dwarf::DW_FORM_data8:546return 8;547case dwarf::DW_FORM_sec_offset:548return FormParams.getDwarfOffsetByteSize();549default:550llvm_unreachable("DIE Value form not supported yet");551}552}553554LLVM_DUMP_METHOD555void DIEDelta::print(raw_ostream &O) const {556O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();557}558559//===----------------------------------------------------------------------===//560// DIEString Implementation561//===----------------------------------------------------------------------===//562563/// EmitValue - Emit string value.564///565void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {566// Index of string in symbol table.567switch (Form) {568case dwarf::DW_FORM_GNU_str_index:569case dwarf::DW_FORM_strx:570case dwarf::DW_FORM_strx1:571case dwarf::DW_FORM_strx2:572case dwarf::DW_FORM_strx3:573case dwarf::DW_FORM_strx4:574DIEInteger(S.getIndex()).emitValue(AP, Form);575return;576case dwarf::DW_FORM_strp:577if (AP->doesDwarfUseRelocationsAcrossSections())578DIELabel(S.getSymbol()).emitValue(AP, Form);579else580DIEInteger(S.getOffset()).emitValue(AP, Form);581return;582default:583llvm_unreachable("Expected valid string form");584}585}586587/// sizeOf - Determine size of delta value in bytes.588///589unsigned DIEString::sizeOf(const dwarf::FormParams &FormParams,590dwarf::Form Form) const {591// Index of string in symbol table.592switch (Form) {593case dwarf::DW_FORM_GNU_str_index:594case dwarf::DW_FORM_strx:595case dwarf::DW_FORM_strx1:596case dwarf::DW_FORM_strx2:597case dwarf::DW_FORM_strx3:598case dwarf::DW_FORM_strx4:599return DIEInteger(S.getIndex()).sizeOf(FormParams, Form);600case dwarf::DW_FORM_strp:601if (FormParams.DwarfUsesRelocationsAcrossSections)602return DIELabel(S.getSymbol()).sizeOf(FormParams, Form);603return DIEInteger(S.getOffset()).sizeOf(FormParams, Form);604default:605llvm_unreachable("Expected valid string form");606}607}608609LLVM_DUMP_METHOD610void DIEString::print(raw_ostream &O) const {611O << "String: " << S.getString();612}613614//===----------------------------------------------------------------------===//615// DIEInlineString Implementation616//===----------------------------------------------------------------------===//617void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {618if (Form == dwarf::DW_FORM_string) {619AP->OutStreamer->emitBytes(S);620AP->emitInt8(0);621return;622}623llvm_unreachable("Expected valid string form");624}625626unsigned DIEInlineString::sizeOf(const dwarf::FormParams &, dwarf::Form) const {627// Emit string bytes + NULL byte.628return S.size() + 1;629}630631LLVM_DUMP_METHOD632void DIEInlineString::print(raw_ostream &O) const {633O << "InlineString: " << S;634}635636//===----------------------------------------------------------------------===//637// DIEEntry Implementation638//===----------------------------------------------------------------------===//639640/// EmitValue - Emit debug information entry offset.641///642void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {643644switch (Form) {645case dwarf::DW_FORM_ref1:646case dwarf::DW_FORM_ref2:647case dwarf::DW_FORM_ref4:648case dwarf::DW_FORM_ref8:649AP->OutStreamer->emitIntValue(Entry->getOffset(),650sizeOf(AP->getDwarfFormParams(), Form));651return;652653case dwarf::DW_FORM_ref_udata:654AP->emitULEB128(Entry->getOffset());655return;656657case dwarf::DW_FORM_ref_addr: {658// Get the absolute offset for this DIE within the debug info/types section.659uint64_t Addr = Entry->getDebugSectionOffset();660if (const MCSymbol *SectionSym =661Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {662AP->emitLabelPlusOffset(SectionSym, Addr,663sizeOf(AP->getDwarfFormParams(), Form), true);664return;665}666667AP->OutStreamer->emitIntValue(Addr, sizeOf(AP->getDwarfFormParams(), Form));668return;669}670default:671llvm_unreachable("Improper form for DIE reference");672}673}674675unsigned DIEEntry::sizeOf(const dwarf::FormParams &FormParams,676dwarf::Form Form) const {677switch (Form) {678case dwarf::DW_FORM_ref1:679return 1;680case dwarf::DW_FORM_ref2:681return 2;682case dwarf::DW_FORM_ref4:683return 4;684case dwarf::DW_FORM_ref8:685return 8;686case dwarf::DW_FORM_ref_udata:687return getULEB128Size(Entry->getOffset());688case dwarf::DW_FORM_ref_addr:689return FormParams.getRefAddrByteSize();690691default:692llvm_unreachable("Improper form for DIE reference");693}694}695696LLVM_DUMP_METHOD697void DIEEntry::print(raw_ostream &O) const {698O << format("Die: 0x%lx", (long)(intptr_t)&Entry);699}700701//===----------------------------------------------------------------------===//702// DIELoc Implementation703//===----------------------------------------------------------------------===//704705unsigned DIELoc::computeSize(const dwarf::FormParams &FormParams) const {706if (!Size) {707for (const auto &V : values())708Size += V.sizeOf(FormParams);709}710711return Size;712}713714/// EmitValue - Emit location data.715///716void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {717switch (Form) {718default: llvm_unreachable("Improper form for block");719case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;720case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;721case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;722case dwarf::DW_FORM_block:723case dwarf::DW_FORM_exprloc:724Asm->emitULEB128(Size);725break;726}727728for (const auto &V : values())729V.emitValue(Asm);730}731732/// sizeOf - Determine size of location data in bytes.733///734unsigned DIELoc::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {735switch (Form) {736case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);737case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);738case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);739case dwarf::DW_FORM_block:740case dwarf::DW_FORM_exprloc:741return Size + getULEB128Size(Size);742default: llvm_unreachable("Improper form for block");743}744}745746LLVM_DUMP_METHOD747void DIELoc::print(raw_ostream &O) const {748printValues(O, *this, "ExprLoc", Size, 5);749}750751//===----------------------------------------------------------------------===//752// DIEBlock Implementation753//===----------------------------------------------------------------------===//754755unsigned DIEBlock::computeSize(const dwarf::FormParams &FormParams) const {756if (!Size) {757for (const auto &V : values())758Size += V.sizeOf(FormParams);759}760761return Size;762}763764/// EmitValue - Emit block data.765///766void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {767switch (Form) {768default: llvm_unreachable("Improper form for block");769case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;770case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;771case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;772case dwarf::DW_FORM_exprloc:773case dwarf::DW_FORM_block:774Asm->emitULEB128(Size);775break;776case dwarf::DW_FORM_string: break;777case dwarf::DW_FORM_data16: break;778}779780for (const auto &V : values())781V.emitValue(Asm);782}783784/// sizeOf - Determine size of block data in bytes.785///786unsigned DIEBlock::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {787switch (Form) {788case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);789case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);790case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);791case dwarf::DW_FORM_exprloc:792case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);793case dwarf::DW_FORM_data16: return 16;794default: llvm_unreachable("Improper form for block");795}796}797798LLVM_DUMP_METHOD799void DIEBlock::print(raw_ostream &O) const {800printValues(O, *this, "Blk", Size, 5);801}802803//===----------------------------------------------------------------------===//804// DIELocList Implementation805//===----------------------------------------------------------------------===//806807unsigned DIELocList::sizeOf(const dwarf::FormParams &FormParams,808dwarf::Form Form) const {809switch (Form) {810case dwarf::DW_FORM_loclistx:811return getULEB128Size(Index);812case dwarf::DW_FORM_data4:813assert(FormParams.Format != dwarf::DWARF64 &&814"DW_FORM_data4 is not suitable to emit a pointer to a location list "815"in the 64-bit DWARF format");816return 4;817case dwarf::DW_FORM_data8:818assert(FormParams.Format == dwarf::DWARF64 &&819"DW_FORM_data8 is not suitable to emit a pointer to a location list "820"in the 32-bit DWARF format");821return 8;822case dwarf::DW_FORM_sec_offset:823return FormParams.getDwarfOffsetByteSize();824default:825llvm_unreachable("DIE Value form not supported yet");826}827}828829/// EmitValue - Emit label value.830///831void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {832if (Form == dwarf::DW_FORM_loclistx) {833AP->emitULEB128(Index);834return;835}836DwarfDebug *DD = AP->getDwarfDebug();837MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;838AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());839}840841LLVM_DUMP_METHOD842void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }843844//===----------------------------------------------------------------------===//845// DIEAddrOffset Implementation846//===----------------------------------------------------------------------===//847848unsigned DIEAddrOffset::sizeOf(const dwarf::FormParams &FormParams,849dwarf::Form) const {850return Addr.sizeOf(FormParams, dwarf::DW_FORM_addrx) +851Offset.sizeOf(FormParams, dwarf::DW_FORM_data4);852}853854/// EmitValue - Emit label value.855///856void DIEAddrOffset::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {857Addr.emitValue(AP, dwarf::DW_FORM_addrx);858Offset.emitValue(AP, dwarf::DW_FORM_data4);859}860861LLVM_DUMP_METHOD862void DIEAddrOffset::print(raw_ostream &O) const {863O << "AddrOffset: ";864Addr.print(O);865O << " + ";866Offset.print(O);867}868869870