Skip to content

Commit

Permalink
Remove the dependency between lib/DebugInfoDWARF and MC.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D134817
  • Loading branch information
rastogishubham committed Dec 15, 2022
1 parent 5b5b3d7 commit 7dde942
Show file tree
Hide file tree
Showing 23 changed files with 240 additions and 222 deletions.
3 changes: 1 addition & 2 deletions lldb/source/Expression/DWARFExpression.cpp
Expand Up @@ -70,8 +70,7 @@ void DWARFExpression::UpdateValue(uint64_t const_value,
void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level,
ABI *abi) const {
llvm::DWARFExpression(m_data.GetAsLLVM(), m_data.GetAddressByteSize())
.print(s->AsRawOstream(), llvm::DIDumpOptions(),
abi ? &abi->GetMCRegisterInfo() : nullptr, nullptr);
.print(s->AsRawOstream(), llvm::DIDumpOptions(), nullptr);
}

RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; }
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Symbol/UnwindPlan.cpp
Expand Up @@ -84,7 +84,7 @@ static void DumpDWARFExpr(Stream &s, llvm::ArrayRef<uint8_t> expr, Thread *threa
llvm::DataExtractor data(expr, order_and_width->first == eByteOrderLittle,
order_and_width->second);
llvm::DWARFExpression(data, order_and_width->second, llvm::dwarf::DWARF32)
.print(s.AsRawOstream(), llvm::DIDumpOptions(), nullptr, nullptr);
.print(s.AsRawOstream(), llvm::DIDumpOptions(), nullptr);
} else
s.PutCString("dwarf-expr");
}
Expand Down
2 changes: 1 addition & 1 deletion lldb/unittests/Symbol/PostfixExpressionTest.cpp
Expand Up @@ -159,7 +159,7 @@ static std::string ParseAndGenerateDWARF(llvm::StringRef expr) {
std::string result;
llvm::raw_string_ostream os(result);
llvm::DWARFExpression(extractor, addr_size, llvm::dwarf::DWARF32)
.print(os, llvm::DIDumpOptions(), nullptr, nullptr);
.print(os, llvm::DIDumpOptions(), nullptr);
return std::move(os.str());
}

Expand Down
Expand Up @@ -40,7 +40,7 @@ CheckValidProgramTranslation(llvm::StringRef fpo_program,
std::string result;
llvm::raw_string_ostream os(result);
llvm::DWARFExpression(extractor, /*AddressSize=*/4, llvm::dwarf::DWARF32)
.print(os, llvm::DIDumpOptions(), nullptr, nullptr);
.print(os, llvm::DIDumpOptions(), nullptr);

// actual check
ASSERT_EQ(expected_dwarf_expression, os.str());
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/DebugInfo/DIContext.h
Expand Up @@ -199,6 +199,9 @@ struct DIDumpOptions {
bool SummarizeTypes = false;
bool Verbose = false;
bool DisplayRawContents = false;
bool IsEH = false;
std::function<llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)>
GetNameForDWARFReg;

/// Return default option set for printing a single DIE without children.
static DIDumpOptions getForSingleDIE() {
Expand Down
11 changes: 0 additions & 11 deletions llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h
Expand Up @@ -27,7 +27,6 @@

namespace llvm {

class MCRegisterInfo;
class MemoryBuffer;
class AppleAcceleratorTable;
class DWARFCompileUnit;
Expand Down Expand Up @@ -81,9 +80,6 @@ class DWARFContext : public DIContext {
std::weak_ptr<DWOFile> DWP;
bool CheckedForDWP = false;
std::string DWPName;

std::unique_ptr<MCRegisterInfo> RegInfo;

std::function<void(Error)> RecoverableErrorHandler =
WithColor::defaultErrorHandler;
std::function<void(Error)> WarningHandler = WithColor::defaultWarningHandler;
Expand Down Expand Up @@ -408,8 +404,6 @@ class DWARFContext : public DIContext {

std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath);

const MCRegisterInfo *getRegisterInfo() const { return RegInfo.get(); }

function_ref<void(Error)> getRecoverableErrorHandler() {
return RecoverableErrorHandler;
}
Expand All @@ -435,11 +429,6 @@ class DWARFContext : public DIContext {
std::function<void(Error)> WarningHandler =
WithColor::defaultWarningHandler);

/// Loads register info for the architecture of the provided object file.
/// Improves readability of dumped DWARF expressions. Requires the caller to
/// have initialized the relevant target descriptions.
Error loadRegisterInfo(const object::ObjectFile &Obj);

/// Get address size from CUs.
/// TODO: refactor compile_units() to make this const.
uint8_t getCUAddrSize();
Expand Down
24 changes: 10 additions & 14 deletions llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h
Expand Up @@ -164,7 +164,7 @@ class UnwindLocation {
/// instead of from .debug_frame. This is needed for register number
/// conversion because some register numbers differ between the two sections
/// for certain architectures like x86.
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const;
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const;

bool operator==(const UnwindLocation &RHS) const;
};
Expand Down Expand Up @@ -222,7 +222,7 @@ class RegisterLocations {
/// instead of from .debug_frame. This is needed for register number
/// conversion because some register numbers differ between the two sections
/// for certain architectures like x86.
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const;
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const;

/// Returns true if we have any register locations in this object.
bool hasLocations() const { return !Locations.empty(); }
Expand Down Expand Up @@ -303,7 +303,7 @@ class UnwindRow {
///
/// \param IndentLevel specify the indent level as an integer. The UnwindRow
/// will be output to the stream preceded by 2 * IndentLevel number of spaces.
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
unsigned IndentLevel = 0) const;
};

Expand Down Expand Up @@ -348,7 +348,7 @@ class UnwindTable {
///
/// \param IndentLevel specify the indent level as an integer. The UnwindRow
/// will be output to the stream preceded by 2 * IndentLevel number of spaces.
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
unsigned IndentLevel = 0) const;

/// Create an UnwindTable from a Common Information Entry (CIE).
Expand Down Expand Up @@ -454,8 +454,8 @@ class CFIProgram {
/// where a problem occurred in case an error is returned.
Error parse(DWARFDataExtractor Data, uint64_t *Offset, uint64_t EndOffset);

void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
bool IsEH, unsigned IndentLevel = 1) const;
void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
unsigned IndentLevel = 1) const;

void addInstruction(const Instruction &I) { Instructions.push_back(I); }

Expand Down Expand Up @@ -523,7 +523,6 @@ class CFIProgram {

/// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
void printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
const MCRegisterInfo *MRI, bool IsEH,
const Instruction &Instr, unsigned OperandIdx,
uint64_t Operand) const;
};
Expand All @@ -548,8 +547,7 @@ class FrameEntry {
CFIProgram &cfis() { return CFIs; }

/// Dump the instructions in this CFI fragment
virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
const MCRegisterInfo *MRI, bool IsEH) const = 0;
virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const = 0;

protected:
const FrameKind Kind;
Expand Down Expand Up @@ -605,8 +603,7 @@ class CIE : public FrameEntry {

uint32_t getLSDAPointerEncoding() const { return LSDAPointerEncoding; }

void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
bool IsEH) const override;
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override;

private:
/// The following fields are defined in section 6.4.1 of the DWARF standard v4
Expand Down Expand Up @@ -645,8 +642,7 @@ class FDE : public FrameEntry {
uint64_t getAddressRange() const { return AddressRange; }
std::optional<uint64_t> getLSDAAddress() const { return LSDAAddress; }

void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
bool IsEH) const override;
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override;

static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_FDE; }

Expand Down Expand Up @@ -688,7 +684,7 @@ class DWARFDebugFrame {
~DWARFDebugFrame();

/// Dump the section data into the given stream.
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI,
void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
std::optional<uint64_t> Offset) const;

/// Parse the section from raw data. \p Data is assumed to contain the whole
Expand Down
11 changes: 4 additions & 7 deletions llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h
Expand Up @@ -66,9 +66,8 @@ class DWARFLocationTable {
/// updated to point past the end of the current list).
bool dumpLocationList(uint64_t *Offset, raw_ostream &OS,
std::optional<object::SectionedAddress> BaseAddr,
const MCRegisterInfo *MRI, const DWARFObject &Obj,
DWARFUnit *U, DIDumpOptions DumpOpts,
unsigned Indent) const;
const DWARFObject &Obj, DWARFUnit *U,
DIDumpOptions DumpOpts, unsigned Indent) const;

Error visitAbsoluteLocationList(
uint64_t Offset, std::optional<object::SectionedAddress> BaseAddr,
Expand Down Expand Up @@ -109,8 +108,7 @@ class DWARFDebugLoc final : public DWARFLocationTable {
: DWARFLocationTable(std::move(Data)) {}

/// Print the location lists found within the debug_loc section.
void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo,
const DWARFObject &Obj, DIDumpOptions DumpOpts,
void dump(raw_ostream &OS, const DWARFObject &Obj, DIDumpOptions DumpOpts,
std::optional<uint64_t> Offset) const;

Error visitLocationList(
Expand All @@ -134,8 +132,7 @@ class DWARFDebugLoclists final : public DWARFLocationTable {

/// Dump all location lists within the given range.
void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS,
const MCRegisterInfo *MRI, const DWARFObject &Obj,
DIDumpOptions DumpOpts);
const DWARFObject &Obj, DIDumpOptions DumpOpts);

protected:
void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
Expand Down
13 changes: 6 additions & 7 deletions llvm/include/llvm/DebugInfo/DWARF/DWARFExpression.h
Expand Up @@ -93,8 +93,7 @@ class DWARFExpression {
uint64_t getEndOffset() const { return EndOffset; }
bool isError() const { return Error; }
bool print(raw_ostream &OS, DIDumpOptions DumpOpts,
const DWARFExpression *Expr, const MCRegisterInfo *RegInfo,
DWARFUnit *U, bool isEH) const;
const DWARFExpression *Expr, DWARFUnit *U) const;

/// Verify \p Op. Does not affect the return of \a isError().
static bool verify(const Operation &Op, DWARFUnit *U);
Expand Down Expand Up @@ -147,15 +146,16 @@ class DWARFExpression {
iterator begin() const { return iterator(this, 0); }
iterator end() const { return iterator(this, Data.getData().size()); }

void print(raw_ostream &OS, DIDumpOptions DumpOpts,
const MCRegisterInfo *RegInfo, DWARFUnit *U,
void print(raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U,
bool IsEH = false) const;

/// Print the expression in a format intended to be compact and useful to a
/// user, but not perfectly unambiguous, or capable of representing every
/// valid DWARF expression. Returns true if the expression was sucessfully
/// printed.
bool printCompact(raw_ostream &OS, const MCRegisterInfo &RegInfo);
bool printCompact(raw_ostream &OS,
std::function<StringRef(uint64_t RegNum, bool IsEH)>
GetNameForDWARFReg = nullptr);

bool verify(DWARFUnit *U);

Expand All @@ -165,8 +165,7 @@ class DWARFExpression {

static bool prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS,
DIDumpOptions DumpOpts, uint8_t Opcode,
const uint64_t Operands[2],
const MCRegisterInfo *MRI, bool isEH);
const uint64_t Operands[2]);

private:
DataExtractor Data;
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/DebugInfo/DWARF/CMakeLists.txt
Expand Up @@ -36,6 +36,5 @@ add_llvm_component_library(LLVMDebugInfoDWARF
LINK_COMPONENTS
BinaryFormat
Object
MC
Support
)
44 changes: 12 additions & 32 deletions llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
Expand Up @@ -38,7 +38,6 @@
#include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Object/Decompressor.h"
#include "llvm/Object/MachO.h"
Expand Down Expand Up @@ -309,9 +308,7 @@ DWARFContext::parseMacroOrMacinfo(MacroSecType SectionType) {
}

static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
DWARFDataExtractor Data,
const MCRegisterInfo *MRI,
const DWARFObject &Obj,
DWARFDataExtractor Data, const DWARFObject &Obj,
std::optional<uint64_t> DumpOffset) {
uint64_t Offset = 0;

Expand All @@ -330,13 +327,13 @@ static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
if (DumpOffset) {
if (DumpOffset >= Offset && DumpOffset < EndOffset) {
Offset = *DumpOffset;
Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, MRI, Obj,
Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, Obj,
nullptr, DumpOpts, /*Indent=*/0);
OS << "\n";
return;
}
} else {
Loc.dumpRange(Offset, EndOffset - Offset, OS, MRI, Obj, DumpOpts);
Loc.dumpRange(Offset, EndOffset - Offset, OS, Obj, DumpOpts);
}
Offset = EndOffset;
}
Expand Down Expand Up @@ -424,21 +421,21 @@ void DWARFContext::dump(

if (const auto *Off = shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
DObj->getLocSection().Data)) {
getDebugLoc()->dump(OS, getRegisterInfo(), *DObj, LLDumpOpts, *Off);
getDebugLoc()->dump(OS, *DObj, LLDumpOpts, *Off);
}
if (const auto *Off =
shouldDump(Explicit, ".debug_loclists", DIDT_ID_DebugLoclists,
DObj->getLoclistsSection().Data)) {
DWARFDataExtractor Data(*DObj, DObj->getLoclistsSection(), isLittleEndian(),
0);
dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off);
dumpLoclistsSection(OS, LLDumpOpts, Data, *DObj, *Off);
}
if (const auto *Off =
shouldDump(ExplicitDWO, ".debug_loclists.dwo", DIDT_ID_DebugLoclists,
DObj->getLoclistsDWOSection().Data)) {
DWARFDataExtractor Data(*DObj, DObj->getLoclistsDWOSection(),
isLittleEndian(), 0);
dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off);
dumpLoclistsSection(OS, LLDumpOpts, Data, *DObj, *Off);
}

if (const auto *Off =
Expand All @@ -450,20 +447,20 @@ void DWARFContext::dump(
if (*Off) {
uint64_t Offset = **Off;
Loc.dumpLocationList(&Offset, OS,
/*BaseAddr=*/std::nullopt, getRegisterInfo(), *DObj,
nullptr, LLDumpOpts, /*Indent=*/0);
/*BaseAddr=*/std::nullopt, *DObj, nullptr,
LLDumpOpts,
/*Indent=*/0);
OS << "\n";
} else {
Loc.dumpRange(0, Data.getData().size(), OS, getRegisterInfo(), *DObj,
LLDumpOpts);
Loc.dumpRange(0, Data.getData().size(), OS, *DObj, LLDumpOpts);
}
}

if (const std::optional<uint64_t> *Off =
shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
DObj->getFrameSection().Data)) {
if (Expected<const DWARFDebugFrame *> DF = getDebugFrame())
(*DF)->dump(OS, DumpOpts, getRegisterInfo(), *Off);
(*DF)->dump(OS, DumpOpts, *Off);
else
RecoverableErrorHandler(DF.takeError());
}
Expand All @@ -472,7 +469,7 @@ void DWARFContext::dump(
shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
DObj->getEHFrameSection().Data)) {
if (Expected<const DWARFDebugFrame *> DF = getEHFrame())
(*DF)->dump(OS, DumpOpts, getRegisterInfo(), *Off);
(*DF)->dump(OS, DumpOpts, *Off);
else
RecoverableErrorHandler(DF.takeError());
}
Expand Down Expand Up @@ -2020,23 +2017,6 @@ DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
std::move(DObj), "", RecoverableErrorHandler, WarningHandler);
}

Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
// Detect the architecture from the object file. We usually don't need OS
// info to lookup a target and create register info.
Triple TT;
TT.setArch(Triple::ArchType(Obj.getArch()));
TT.setVendor(Triple::UnknownVendor);
TT.setOS(Triple::UnknownOS);
std::string TargetLookupError;
const Target *TheTarget =
TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
if (!TargetLookupError.empty())
return createStringError(errc::invalid_argument,
TargetLookupError.c_str());
RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
return Error::success();
}

uint8_t DWARFContext::getCUAddrSize() {
// In theory, different compile units may have different address byte
// sizes, but for simplicity we just use the address byte size of the
Expand Down

0 comments on commit 7dde942

Please sign in to comment.