llvm-project/llvm/tools/llvm-exegesis/lib/DisassemblerHelper.cpp
Pavel Kosov 39fc67b8af [llvm-exegesis] Factor out DisassemblerHelper from the Analysis class
As part of preparing the reports, the Analysis class needs to print
machine instructions in a disassembled form. For this purpose, the class
has four fields (namely Context_, AsmInfo_, InstPrinter_ and Disasm_).
All the constructor of the Analysis class does is conditionally
initializing these four fields.

This commit factors out the logic for decoding machine code and printing
it in an assembler form into a separate DisassemblerHelper class.

~~

Huawei RRI, OS Lab

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D147156
2023-04-04 09:17:50 +03:00

36 lines
1.4 KiB
C++

//===-- DisassemblerHelper.cpp ----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DisassemblerHelper.h"
#include "llvm/MC/TargetRegistry.h"
namespace llvm {
namespace exegesis {
DisassemblerHelper::DisassemblerHelper(const LLVMState &State) : State_(State) {
MCTargetOptions MCOptions;
const auto &TM = State.getTargetMachine();
const auto &Triple = TM.getTargetTriple();
AsmInfo_.reset(TM.getTarget().createMCAsmInfo(State_.getRegInfo(),
Triple.str(), MCOptions));
InstPrinter_.reset(TM.getTarget().createMCInstPrinter(
Triple, 0 /*default variant*/, *AsmInfo_, State_.getInstrInfo(),
State_.getRegInfo()));
Context_ = std::make_unique<MCContext>(
Triple, AsmInfo_.get(), &State_.getRegInfo(), &State_.getSubtargetInfo());
Disasm_.reset(TM.getTarget().createMCDisassembler(State_.getSubtargetInfo(),
*Context_));
assert(Disasm_ && "cannot create MCDisassembler. missing call to "
"InitializeXXXTargetDisassembler ?");
}
} // namespace exegesis
} // namespace llvm