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