LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCDisassembler.h
Go to the documentation of this file.
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 
12 #include "llvm-c/Disassembler.h"
13 #include "llvm/ADT/OwningPtr.h"
14 #include "llvm/MC/MCSymbolizer.h"
16 #include "llvm/Support/DataTypes.h"
17 
18 namespace llvm {
19 
20 class MCInst;
21 class MCSubtargetInfo;
22 class MemoryObject;
23 class raw_ostream;
24 class MCContext;
25 
26 /// MCDisassembler - Superclass for all disassemblers. Consumes a memory region
27 /// and provides an array of assembly instructions.
29 public:
30  /// Ternary decode status. Most backends will just use Fail and
31  /// Success, however some have a concept of an instruction with
32  /// understandable semantics but which is architecturally
33  /// incorrect. An example of this is ARM UNPREDICTABLE instructions
34  /// which are disassemblable but cause undefined behaviour.
35  ///
36  /// Because it makes sense to disassemble these instructions, there
37  /// is a "soft fail" failure mode that indicates the MCInst& is
38  /// valid but architecturally incorrect.
39  ///
40  /// The enum numbers are deliberately chosen such that reduction
41  /// from Success->SoftFail ->Fail can be done with a simple
42  /// bitwise-AND:
43  ///
44  /// LEFT & TOP = | Success Unpredictable Fail
45  /// --------------+-----------------------------------
46  /// Success | Success Unpredictable Fail
47  /// Unpredictable | Unpredictable Unpredictable Fail
48  /// Fail | Fail Fail Fail
49  ///
50  /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
51  /// Success, SoftFail, Fail respectively.
52  enum DecodeStatus {
53  Fail = 0,
54  SoftFail = 1,
55  Success = 3
56  };
57 
58  /// Constructor - Performs initial setup for the disassembler.
59  MCDisassembler(const MCSubtargetInfo &STI) : GetOpInfo(0), SymbolLookUp(0),
60  DisInfo(0), Ctx(0),
61  STI(STI), Symbolizer(0),
62  CommentStream(0) {}
63 
64  virtual ~MCDisassembler();
65 
66  /// getInstruction - Returns the disassembly of a single instruction.
67  ///
68  /// @param instr - An MCInst to populate with the contents of the
69  /// instruction.
70  /// @param size - A value to populate with the size of the instruction, or
71  /// the number of bytes consumed while attempting to decode
72  /// an invalid instruction.
73  /// @param region - The memory object to use as a source for machine code.
74  /// @param address - The address, in the memory space of region, of the first
75  /// byte of the instruction.
76  /// @param vStream - The stream to print warnings and diagnostic messages on.
77  /// @param cStream - The stream to print comments and annotations on.
78  /// @return - MCDisassembler::Success if the instruction is valid,
79  /// MCDisassembler::SoftFail if the instruction was
80  /// disassemblable but invalid,
81  /// MCDisassembler::Fail if the instruction was invalid.
82  virtual DecodeStatus getInstruction(MCInst& instr,
83  uint64_t& size,
84  const MemoryObject &region,
85  uint64_t address,
86  raw_ostream &vStream,
87  raw_ostream &cStream) const = 0;
88 private:
89  //
90  // Hooks for symbolic disassembly via the public 'C' interface.
91  //
92  // The function to get the symbolic information for operands.
93  LLVMOpInfoCallback GetOpInfo;
94  // The function to lookup a symbol name.
95  LLVMSymbolLookupCallback SymbolLookUp;
96  // The pointer to the block of symbolic information for above call back.
97  void *DisInfo;
98  // The assembly context for creating symbols and MCExprs in place of
99  // immediate operands when there is symbolic information.
100  MCContext *Ctx;
101 
102 protected:
103  // Subtarget information, for instruction decoding predicates if required.
106 
107 public:
108  // Helpers around MCSymbolizer
109  bool tryAddingSymbolicOperand(MCInst &Inst,
110  int64_t Value,
111  uint64_t Address, bool IsBranch,
112  uint64_t Offset, uint64_t InstSize) const;
113 
114  void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const;
115 
116  /// Set \p Symzer as the current symbolizer.
117  /// This takes ownership of \p Symzer, and deletes the previously set one.
119 
120  /// Sets up an external symbolizer that uses the C API callbacks.
122  LLVMSymbolLookupCallback SymbolLookUp,
123  void *DisInfo,
124  MCContext *Ctx,
125  OwningPtr<MCRelocationInfo> &RelInfo);
126 
127  LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; }
129  return SymbolLookUp;
130  }
131  void *getDisInfoBlock() const { return DisInfo; }
132  MCContext *getMCContext() const { return Ctx; }
133 
134  const MCSubtargetInfo& getSubtargetInfo() const { return STI; }
135 
136  // Marked mutable because we cache it inside the disassembler, rather than
137  // having to pass it around as an argument through all the autogenerated code.
139 };
140 
141 } // namespace llvm
142 
143 #endif
const char *(* LLVMSymbolLookupCallback)(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
raw_ostream * CommentStream
OwningPtr< MCSymbolizer > Symbolizer
LLVMOpInfoCallback getLLVMOpInfoCallback() const
void * getDisInfoBlock() const
const MCSubtargetInfo & STI
void setSymbolizer(OwningPtr< MCSymbolizer > &Symzer)
const MCSubtargetInfo & getSubtargetInfo() const
LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const
MCDisassembler(const MCSubtargetInfo &STI)
Constructor - Performs initial setup for the disassembler.
virtual DecodeStatus getInstruction(MCInst &instr, uint64_t &size, const MemoryObject &region, uint64_t address, raw_ostream &vStream, raw_ostream &cStream) const =0
bool tryAddingSymbolicOperand(MCInst &Inst, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize) const
void setupForSymbolicDisassembly(LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx, OwningPtr< MCRelocationInfo > &RelInfo)
Sets up an external symbolizer that uses the C API callbacks.
LLVM Value Representation.
Definition: Value.h:66
void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const
int(* LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t Size, int TagType, void *TagBuf)
MCDisassembler::DecodeStatus DecodeStatus
MCContext * getMCContext() const