1//===-- DisassemblerHelper.h ------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Helper class for decoding machine instructions and printing them in an
11/// assembler form.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVM_EXEGESIS_DISASSEMBLER_HELPER_H
16#define LLVM_TOOLS_LLVM_EXEGESIS_DISASSEMBLER_HELPER_H
17
18#include "LlvmState.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCDisassembler/MCDisassembler.h"
22#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCTargetOptions.h"
24
25#include <memory>
26
27namespace llvm {
28namespace exegesis {
29
30// A helper class for decoding and printing machine instructions.
31class DisassemblerHelper {
32public:
33 DisassemblerHelper(const LLVMState &State);
34
35 void printInst(const MCInst *MI, raw_ostream &OS) const {
36 const auto &STI = State_.getSubtargetInfo();
37 InstPrinter_->printInst(MI, Address: 0, Annot: "", STI, OS);
38 }
39
40 bool decodeInst(MCInst &MI, uint64_t &MISize, ArrayRef<uint8_t> Bytes) const {
41 return Disasm_->getInstruction(Instr&: MI, Size&: MISize, Bytes, Address: 0, CStream&: nulls());
42 }
43
44private:
45 const LLVMState &State_;
46 MCTargetOptions MCOptions_;
47 std::unique_ptr<MCContext> Context_;
48 std::unique_ptr<MCAsmInfo> AsmInfo_;
49 std::unique_ptr<MCInstPrinter> InstPrinter_;
50 std::unique_ptr<MCDisassembler> Disasm_;
51};
52
53} // namespace exegesis
54} // namespace llvm
55
56#endif // LLVM_TOOLS_LLVM_EXEGESIS_DISASSEMBLER_HELPER_H
57