Disassembler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===------------- Disassembler.h - LLVM Disassembler -----------*- 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. // This file defines the interface for the Disassembly library's disassembler
  10. // context. The disassembler is responsible for producing strings for
  11. // individual instructions according to a given architecture and disassembly
  12. // syntax.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
  16. #define LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
  17. #include "llvm-c/DisassemblerTypes.h"
  18. #include "llvm/ADT/SmallString.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/MCInstrInfo.h"
  24. #include "llvm/MC/MCRegisterInfo.h"
  25. #include "llvm/MC/MCSubtargetInfo.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <string>
  28. #include <utility>
  29. namespace llvm {
  30. class Target;
  31. //
  32. // This is the disassembler context returned by LLVMCreateDisasm().
  33. //
  34. class LLVMDisasmContext {
  35. private:
  36. //
  37. // The passed parameters when the disassembler context is created.
  38. //
  39. // The TripleName for this disassembler.
  40. std::string TripleName;
  41. // The pointer to the caller's block of symbolic information.
  42. void *DisInfo;
  43. // The Triple specific symbolic information type returned by GetOpInfo.
  44. int TagType;
  45. // The function to get the symbolic information for operands.
  46. LLVMOpInfoCallback GetOpInfo;
  47. // The function to look up a symbol name.
  48. LLVMSymbolLookupCallback SymbolLookUp;
  49. //
  50. // The objects created and saved by LLVMCreateDisasm() then used by
  51. // LLVMDisasmInstruction().
  52. //
  53. // The LLVM target corresponding to the disassembler.
  54. // FIXME: using std::unique_ptr<const llvm::Target> causes a malloc error
  55. // when this LLVMDisasmContext is deleted.
  56. const Target *TheTarget;
  57. // The assembly information for the target architecture.
  58. std::unique_ptr<const llvm::MCAsmInfo> MAI;
  59. // The register information for the target architecture.
  60. std::unique_ptr<const llvm::MCRegisterInfo> MRI;
  61. // The subtarget information for the target architecture.
  62. std::unique_ptr<const llvm::MCSubtargetInfo> MSI;
  63. // The instruction information for the target architecture.
  64. std::unique_ptr<const llvm::MCInstrInfo> MII;
  65. // The assembly context for creating symbols and MCExprs.
  66. std::unique_ptr<const llvm::MCContext> Ctx;
  67. // The disassembler for the target architecture.
  68. std::unique_ptr<const llvm::MCDisassembler> DisAsm;
  69. // The instruction printer for the target architecture.
  70. std::unique_ptr<llvm::MCInstPrinter> IP;
  71. // The options used to set up the disassembler.
  72. uint64_t Options;
  73. // The CPU string.
  74. std::string CPU;
  75. public:
  76. // Comment stream and backing vector.
  77. SmallString<128> CommentsToEmit;
  78. raw_svector_ostream CommentStream;
  79. LLVMDisasmContext(std::string TripleName, void *DisInfo, int TagType,
  80. LLVMOpInfoCallback GetOpInfo,
  81. LLVMSymbolLookupCallback SymbolLookUp,
  82. const Target *TheTarget,
  83. std::unique_ptr<const MCAsmInfo> &&MAI,
  84. std::unique_ptr<const MCRegisterInfo> &&MRI,
  85. std::unique_ptr<const MCSubtargetInfo> &&MSI,
  86. std::unique_ptr<const MCInstrInfo> &&MII,
  87. std::unique_ptr<const llvm::MCContext> &&Ctx,
  88. std::unique_ptr<const MCDisassembler> &&DisAsm,
  89. std::unique_ptr<MCInstPrinter> &&IP)
  90. : TripleName(std::move(TripleName)), DisInfo(DisInfo), TagType(TagType),
  91. GetOpInfo(GetOpInfo), SymbolLookUp(SymbolLookUp), TheTarget(TheTarget),
  92. MAI(std::move(MAI)), MRI(std::move(MRI)), MSI(std::move(MSI)),
  93. MII(std::move(MII)), Ctx(std::move(Ctx)), DisAsm(std::move(DisAsm)),
  94. IP(std::move(IP)), Options(0), CommentStream(CommentsToEmit) {}
  95. const std::string &getTripleName() const { return TripleName; }
  96. void *getDisInfo() const { return DisInfo; }
  97. int getTagType() const { return TagType; }
  98. LLVMOpInfoCallback getGetOpInfo() const { return GetOpInfo; }
  99. LLVMSymbolLookupCallback getSymbolLookupCallback() const {
  100. return SymbolLookUp;
  101. }
  102. const Target *getTarget() const { return TheTarget; }
  103. const MCDisassembler *getDisAsm() const { return DisAsm.get(); }
  104. const MCAsmInfo *getAsmInfo() const { return MAI.get(); }
  105. const MCInstrInfo *getInstrInfo() const { return MII.get(); }
  106. const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); }
  107. const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); }
  108. MCInstPrinter *getIP() { return IP.get(); }
  109. void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); }
  110. uint64_t getOptions() const { return Options; }
  111. void addOptions(uint64_t Options) { this->Options |= Options; }
  112. StringRef getCPU() const { return CPU; }
  113. void setCPU(const char *CPU) { this->CPU = CPU; }
  114. };
  115. } // namespace llvm
  116. #endif