MCDisassembler.h revision 639aa87bee77fe2d83f0978ae1eea53e49def324
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), SymbolLookUp(0),
59                                               DisInfo(0), Ctx(0),
60                                               STI(STI), CommentStream(0) {}
61
62  virtual ~MCDisassembler();
63
64  /// getInstruction  - Returns the disassembly of a single instruction.
65  ///
66  /// @param instr    - An MCInst to populate with the contents of the
67  ///                   instruction.
68  /// @param size     - A value to populate with the size of the instruction, or
69  ///                   the number of bytes consumed while attempting to decode
70  ///                   an invalid instruction.
71  /// @param region   - The memory object to use as a source for machine code.
72  /// @param address  - The address, in the memory space of region, of the first
73  ///                   byte of the instruction.
74  /// @param vStream  - The stream to print warnings and diagnostic messages on.
75  /// @param cStream  - The stream to print comments and annotations on.
76  /// @return         - MCDisassembler::Success if the instruction is valid,
77  ///                   MCDisassembler::SoftFail if the instruction was
78  ///                                            disassemblable but invalid,
79  ///                   MCDisassembler::Fail if the instruction was invalid.
80  virtual DecodeStatus  getInstruction(MCInst& instr,
81                                       uint64_t& size,
82                                       const MemoryObject &region,
83                                       uint64_t address,
84                                       raw_ostream &vStream,
85                                       raw_ostream &cStream) const = 0;
86
87  /// getEDInfo - Returns the enhanced instruction information corresponding to
88  ///   the disassembler.
89  ///
90  /// @return         - An array of instruction information, with one entry for
91  ///                   each MCInst opcode this disassembler returns.
92  ///                   NULL if there is no info for this target.
93  virtual const EDInstInfo   *getEDInfo() const { return (EDInstInfo*)0; }
94
95private:
96  //
97  // Hooks for symbolic disassembly via the public 'C' interface.
98  //
99  // The function to get the symbolic information for operands.
100  LLVMOpInfoCallback GetOpInfo;
101  // The function to lookup a symbol name.
102  LLVMSymbolLookupCallback SymbolLookUp;
103  // The pointer to the block of symbolic information for above call back.
104  void *DisInfo;
105  // The assembly context for creating symbols and MCExprs in place of
106  // immediate operands when there is symbolic information.
107  MCContext *Ctx;
108protected:
109  // Subtarget information, for instruction decoding predicates if required.
110  const MCSubtargetInfo &STI;
111
112public:
113  void setupForSymbolicDisassembly(LLVMOpInfoCallback getOpInfo,
114                                   LLVMSymbolLookupCallback symbolLookUp,
115                                   void *disInfo,
116                                   MCContext *ctx) {
117    GetOpInfo = getOpInfo;
118    SymbolLookUp = symbolLookUp;
119    DisInfo = disInfo;
120    Ctx = ctx;
121  }
122  LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; }
123  LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const {
124    return SymbolLookUp;
125  }
126  void *getDisInfoBlock() const { return DisInfo; }
127  MCContext *getMCContext() const { return Ctx; }
128
129  // Marked mutable because we cache it inside the disassembler, rather than
130  // having to pass it around as an argument through all the autogenerated code.
131  mutable raw_ostream *CommentStream;
132};
133
134} // namespace llvm
135
136#endif
137