Skip to content

[MC][NFC] Allow MCInstrAnalysis to store state #65479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions llvm/include/llvm/MC/MCInstrAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ class MCInstrAnalysis {
MCInstrAnalysis(const MCInstrInfo *Info) : Info(Info) {}
virtual ~MCInstrAnalysis() = default;

/// Clear the internal state. See updateState for more information.
virtual void resetState() {}

/// Update internal state with \p Inst at \p Addr.
///
/// For some types of analyses, inspecting a single instruction is not
/// sufficient. Some examples are auipc/jalr pairs on RISC-V or adrp/ldr pairs
/// on AArch64. To support inspecting multiple instructions, targets may keep
/// track of an internal state while analysing instructions. Clients should
/// call updateState for every instruction which allows later calls to one of
/// the analysis functions to take previous instructions into account.
/// Whenever state becomes irrelevant (e.g., when starting to disassemble a
/// new function), clients should call resetState to clear it.
virtual void updateState(const MCInst &Inst, uint64_t Addr) {}

virtual bool isBranch(const MCInst &Inst) const {
return Info->get(Inst.getOpcode()).isBranch();
}
Expand Down
21 changes: 16 additions & 5 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ class DisassemblerTarget {
std::unique_ptr<const MCSubtargetInfo> SubtargetInfo;
std::shared_ptr<MCContext> Context;
std::unique_ptr<MCDisassembler> DisAsm;
std::shared_ptr<const MCInstrAnalysis> InstrAnalysis;
std::shared_ptr<MCInstrAnalysis> InstrAnalysis;
std::shared_ptr<MCInstPrinter> InstPrinter;
PrettyPrinter *Printer;

Expand Down Expand Up @@ -1265,14 +1265,19 @@ collectBBAddrMapLabels(const std::unordered_map<uint64_t, BBAddrMap> &AddrToBBAd
}
}

static void collectLocalBranchTargets(
ArrayRef<uint8_t> Bytes, const MCInstrAnalysis *MIA, MCDisassembler *DisAsm,
MCInstPrinter *IP, const MCSubtargetInfo *STI, uint64_t SectionAddr,
uint64_t Start, uint64_t End, std::unordered_map<uint64_t, std::string> &Labels) {
static void
collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, MCInstrAnalysis *MIA,
MCDisassembler *DisAsm, MCInstPrinter *IP,
const MCSubtargetInfo *STI, uint64_t SectionAddr,
uint64_t Start, uint64_t End,
std::unordered_map<uint64_t, std::string> &Labels) {
// So far only supports PowerPC and X86.
if (!STI->getTargetTriple().isPPC() && !STI->getTargetTriple().isX86())
return;

if (MIA)
MIA->resetState();

Labels.clear();
unsigned LabelCount = 0;
Start += SectionAddr;
Expand All @@ -1298,6 +1303,7 @@ static void collectLocalBranchTargets(
!Labels.count(Target) &&
!(STI->getTargetTriple().isPPC() && Target == Index))
Labels[Target] = ("L" + Twine(LabelCount++)).str();
MIA->updateState(Inst, Index);
}
Index += Size;
}
Expand Down Expand Up @@ -1939,6 +1945,9 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
BBAddrMapLabels);
}

if (DT->InstrAnalysis)
DT->InstrAnalysis->resetState();

while (Index < End) {
// ARM and AArch64 ELF binaries can interleave data and text in the
// same section. We rely on the markers introduced to understand what
Expand Down Expand Up @@ -2155,6 +2164,8 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
if (TargetOS == &CommentStream)
*TargetOS << "\n";
}

DT->InstrAnalysis->updateState(Inst, SectionAddr + Index);
}
}

Expand Down