Disassembler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===------------- Disassembler.h - LLVM Disassembler -----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the interface for the Disassembly library's disassembler
  11. // context. The disassembler is responsible for producing strings for
  12. // individual instructions according to a given architecture and disassembly
  13. // syntax.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
  17. #define LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
  18. #include "llvm-c/Disassembler.h"
  19. #include "llvm/ADT/SmallString.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <string>
  22. namespace llvm {
  23. class MCContext;
  24. class MCAsmInfo;
  25. class MCDisassembler;
  26. class MCInstPrinter;
  27. class MCInstrInfo;
  28. class MCRegisterInfo;
  29. class MCSubtargetInfo;
  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, const MCAsmInfo *mAI,
  83. const MCRegisterInfo *mRI,
  84. const MCSubtargetInfo *mSI,
  85. const MCInstrInfo *mII,
  86. llvm::MCContext *ctx, const MCDisassembler *disAsm,
  87. MCInstPrinter *iP) : TripleName(tripleName),
  88. DisInfo(disInfo), TagType(tagType), GetOpInfo(getOpInfo),
  89. SymbolLookUp(symbolLookUp), TheTarget(theTarget),
  90. Options(0),
  91. CommentStream(CommentsToEmit) {
  92. MAI.reset(mAI);
  93. MRI.reset(mRI);
  94. MSI.reset(mSI);
  95. MII.reset(mII);
  96. Ctx.reset(ctx);
  97. DisAsm.reset(disAsm);
  98. IP.reset(iP);
  99. }
  100. const std::string &getTripleName() const { return TripleName; }
  101. void *getDisInfo() const { return DisInfo; }
  102. int getTagType() const { return TagType; }
  103. LLVMOpInfoCallback getGetOpInfo() const { return GetOpInfo; }
  104. LLVMSymbolLookupCallback getSymbolLookupCallback() const {
  105. return SymbolLookUp;
  106. }
  107. const Target *getTarget() const { return TheTarget; }
  108. const MCDisassembler *getDisAsm() const { return DisAsm.get(); }
  109. const MCAsmInfo *getAsmInfo() const { return MAI.get(); }
  110. const MCInstrInfo *getInstrInfo() const { return MII.get(); }
  111. const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); }
  112. const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); }
  113. MCInstPrinter *getIP() { return IP.get(); }
  114. void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); }
  115. uint64_t getOptions() const { return Options; }
  116. void addOptions(uint64_t Options) { this->Options |= Options; }
  117. StringRef getCPU() const { return CPU; }
  118. void setCPU(const char *CPU) { this->CPU = CPU; }
  119. };
  120. } // namespace llvm
  121. #endif