xref: /llvm-project/llvm/tools/llvm-exegesis/lib/DisassemblerHelper.h (revision 39fc67b8af707f6bdcbcfbec1e17f14ffbaeecb8)
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 
24 #include <memory>
25 
26 namespace llvm {
27 namespace exegesis {
28 
29 // A helper class for decoding and printing machine instructions.
30 class DisassemblerHelper {
31 public:
32   DisassemblerHelper(const LLVMState &State);
33 
printInst(const MCInst * MI,raw_ostream & OS)34   void printInst(const MCInst *MI, raw_ostream &OS) const {
35     const auto &STI = State_.getSubtargetInfo();
36     InstPrinter_->printInst(MI, 0, "", STI, OS);
37   }
38 
decodeInst(MCInst & MI,uint64_t & MISize,ArrayRef<uint8_t> Bytes)39   bool decodeInst(MCInst &MI, uint64_t &MISize, ArrayRef<uint8_t> Bytes) const {
40     return Disasm_->getInstruction(MI, MISize, Bytes, 0, nulls());
41   }
42 
43 private:
44   const LLVMState &State_;
45   std::unique_ptr<MCContext> Context_;
46   std::unique_ptr<MCAsmInfo> AsmInfo_;
47   std::unique_ptr<MCInstPrinter> InstPrinter_;
48   std::unique_ptr<MCDisassembler> Disasm_;
49 };
50 
51 } // namespace exegesis
52 } // namespace llvm
53 
54 #endif // LLVM_TOOLS_LLVM_EXEGESIS_DISASSEMBLER_HELPER_H
55