Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
5.0 KiB

  1. //===-- llvm/MC/MCDisassembler.h - Disassembler interface -------*- 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. #ifndef LLVM_MC_MCDISASSEMBLER_H
  10. #define LLVM_MC_MCDISASSEMBLER_H
  11. #include "llvm-c/Disassembler.h"
  12. #include "llvm/Support/DataTypes.h"
  13. namespace llvm {
  14. class MCInst;
  15. class MCSubtargetInfo;
  16. class MemoryObject;
  17. class raw_ostream;
  18. class MCContext;
  19. /// MCDisassembler - Superclass for all disassemblers. Consumes a memory region
  20. /// and provides an array of assembly instructions.
  21. class MCDisassembler {
  22. public:
  23. /// Ternary decode status. Most backends will just use Fail and
  24. /// Success, however some have a concept of an instruction with
  25. /// understandable semantics but which is architecturally
  26. /// incorrect. An example of this is ARM UNPREDICTABLE instructions
  27. /// which are disassemblable but cause undefined behaviour.
  28. ///
  29. /// Because it makes sense to disassemble these instructions, there
  30. /// is a "soft fail" failure mode that indicates the MCInst& is
  31. /// valid but architecturally incorrect.
  32. ///
  33. /// The enum numbers are deliberately chosen such that reduction
  34. /// from Success->SoftFail ->Fail can be done with a simple
  35. /// bitwise-AND:
  36. ///
  37. /// LEFT & TOP = | Success Unpredictable Fail
  38. /// --------------+-----------------------------------
  39. /// Success | Success Unpredictable Fail
  40. /// Unpredictable | Unpredictable Unpredictable Fail
  41. /// Fail | Fail Fail Fail
  42. ///
  43. /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
  44. /// Success, SoftFail, Fail respectively.
  45. enum DecodeStatus {
  46. Fail = 0,
  47. SoftFail = 1,
  48. Success = 3
  49. };
  50. /// Constructor - Performs initial setup for the disassembler.
  51. MCDisassembler(const MCSubtargetInfo &STI) : GetOpInfo(0), SymbolLookUp(0),
  52. DisInfo(0), Ctx(0),
  53. STI(STI), CommentStream(0) {}
  54. virtual ~MCDisassembler();
  55. /// getInstruction - Returns the disassembly of a single instruction.
  56. ///
  57. /// @param instr - An MCInst to populate with the contents of the
  58. /// instruction.
  59. /// @param size - A value to populate with the size of the instruction, or
  60. /// the number of bytes consumed while attempting to decode
  61. /// an invalid instruction.
  62. /// @param region - The memory object to use as a source for machine code.
  63. /// @param address - The address, in the memory space of region, of the first
  64. /// byte of the instruction.
  65. /// @param vStream - The stream to print warnings and diagnostic messages on.
  66. /// @param cStream - The stream to print comments and annotations on.
  67. /// @return - MCDisassembler::Success if the instruction is valid,
  68. /// MCDisassembler::SoftFail if the instruction was
  69. /// disassemblable but invalid,
  70. /// MCDisassembler::Fail if the instruction was invalid.
  71. virtual DecodeStatus getInstruction(MCInst& instr,
  72. uint64_t& size,
  73. const MemoryObject &region,
  74. uint64_t address,
  75. raw_ostream &vStream,
  76. raw_ostream &cStream) const = 0;
  77. private:
  78. //
  79. // Hooks for symbolic disassembly via the public 'C' interface.
  80. //
  81. // The function to get the symbolic information for operands.
  82. LLVMOpInfoCallback GetOpInfo;
  83. // The function to lookup a symbol name.
  84. LLVMSymbolLookupCallback SymbolLookUp;
  85. // The pointer to the block of symbolic information for above call back.
  86. void *DisInfo;
  87. // The assembly context for creating symbols and MCExprs in place of
  88. // immediate operands when there is symbolic information.
  89. MCContext *Ctx;
  90. protected:
  91. // Subtarget information, for instruction decoding predicates if required.
  92. const MCSubtargetInfo &STI;
  93. public:
  94. void setupForSymbolicDisassembly(LLVMOpInfoCallback getOpInfo,
  95. LLVMSymbolLookupCallback symbolLookUp,
  96. void *disInfo,
  97. MCContext *ctx) {
  98. GetOpInfo = getOpInfo;
  99. SymbolLookUp = symbolLookUp;
  100. DisInfo = disInfo;
  101. Ctx = ctx;
  102. }
  103. LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; }
  104. LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const {
  105. return SymbolLookUp;
  106. }
  107. void *getDisInfoBlock() const { return DisInfo; }
  108. MCContext *getMCContext() const { return Ctx; }
  109. // Marked mutable because we cache it inside the disassembler, rather than
  110. // having to pass it around as an argument through all the autogenerated code.
  111. mutable raw_ostream *CommentStream;
  112. };
  113. } // namespace llvm
  114. #endif