Skip to content

Commit

Permalink
[BOLT][NFC] Move printDebugInfo out of BC::printInstruction
Browse files Browse the repository at this point in the history
Simplify `BinaryContext::printInstruction`.

Reviewed By: ayermolo

Differential Revision: https://reviews.llvm.org/D127561
  • Loading branch information
aaupov committed Jun 11, 2022
1 parent a838043 commit 7dee646
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bolt/include/bolt/Core/BinaryContext.h
Expand Up @@ -239,7 +239,7 @@ class BinaryContext {
Optional<DWARFUnit *> getDWOCU(uint64_t DWOId);

/// Returns DWOContext if it exists.
DWARFContext *getDWOContext();
DWARFContext *getDWOContext() const;

/// Get Number of DWOCUs in a map.
uint32_t getNumDWOCUs() { return DWOCUs.size(); }
Expand Down
61 changes: 33 additions & 28 deletions bolt/lib/Core/BinaryContext.cpp
Expand Up @@ -1411,7 +1411,7 @@ Optional<DWARFUnit *> BinaryContext::getDWOCU(uint64_t DWOId) {
return Iter->second;
}

DWARFContext *BinaryContext::getDWOContext() {
DWARFContext *BinaryContext::getDWOContext() const {
if (DWOCUs.empty())
return nullptr;
return &DWOCUs.begin()->second->getContext();
Expand Down Expand Up @@ -1673,6 +1673,36 @@ bool BinaryContext::isMarker(const SymbolRef &Symbol) const {
return getMarkerType(Symbol) != MarkerSymType::NONE;
}

static void printDebugInfo(raw_ostream &OS, const MCInst &Instruction,
const BinaryFunction *Function,
DWARFContext *DwCtx) {
DebugLineTableRowRef RowRef =
DebugLineTableRowRef::fromSMLoc(Instruction.getLoc());
if (RowRef == DebugLineTableRowRef::NULL_ROW)
return;

const DWARFDebugLine::LineTable *LineTable;
if (Function && Function->getDWARFUnit() &&
Function->getDWARFUnit()->getOffset() == RowRef.DwCompileUnitIndex) {
LineTable = Function->getDWARFLineTable();
} else {
LineTable = DwCtx->getLineTableForUnit(
DwCtx->getCompileUnitForOffset(RowRef.DwCompileUnitIndex));
}
assert(LineTable && "line table expected for instruction with debug info");

const DWARFDebugLine::Row &Row = LineTable->Rows[RowRef.RowIndex - 1];
StringRef FileName = "";
if (Optional<const char *> FName =
dwarf::toString(LineTable->Prologue.FileNames[Row.File - 1].Name))
FileName = *FName;
OS << " # debug line " << FileName << ":" << Row.Line;
if (Row.Column)
OS << ":" << Row.Column;
if (Row.Discriminator)
OS << " discriminator:" << Row.Discriminator;
}

void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
uint64_t Offset,
const BinaryFunction *Function,
Expand Down Expand Up @@ -1720,33 +1750,8 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,

MIB->printAnnotations(Instruction, OS);

if (opts::PrintDebugInfo) {
DebugLineTableRowRef RowRef =
DebugLineTableRowRef::fromSMLoc(Instruction.getLoc());
if (RowRef != DebugLineTableRowRef::NULL_ROW) {
const DWARFDebugLine::LineTable *LineTable;
if (Function && Function->getDWARFUnit() &&
Function->getDWARFUnit()->getOffset() == RowRef.DwCompileUnitIndex) {
LineTable = Function->getDWARFLineTable();
} else {
LineTable = DwCtx->getLineTableForUnit(
DwCtx->getCompileUnitForOffset(RowRef.DwCompileUnitIndex));
}
assert(LineTable &&
"line table expected for instruction with debug info");

const DWARFDebugLine::Row &Row = LineTable->Rows[RowRef.RowIndex - 1];
StringRef FileName = "";
if (Optional<const char *> FName =
dwarf::toString(LineTable->Prologue.FileNames[Row.File - 1].Name))
FileName = *FName;
OS << " # debug line " << FileName << ":" << Row.Line;
if (Row.Column)
OS << ":" << Row.Column;
if (Row.Discriminator)
OS << " discriminator:" << Row.Discriminator;
}
}
if (opts::PrintDebugInfo)
printDebugInfo(OS, Instruction, Function, DwCtx.get());

if ((opts::PrintRelocations || PrintRelocations) && Function) {
const uint64_t Size = computeCodeSize(&Instruction, &Instruction + 1);
Expand Down

0 comments on commit 7dee646

Please sign in to comment.