1 //===-- llvm/MC/MCDisassembler.h - Disassembler interface -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #ifndef MCDISASSEMBLER_H
10 #define MCDISASSEMBLER_H
12 #include "llvm/Support/DataTypes.h"
13 #include "llvm-c/Disassembler.h"
18 class MCSubtargetInfo;
25 /// MCDisassembler - Superclass for all disassemblers. Consumes a memory region
26 /// and provides an array of assembly instructions.
27 class MCDisassembler {
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.
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.
39 /// The enum numbers are deliberately chosen such that reduction
40 /// from Success->SoftFail ->Fail can be done with a simple
43 /// LEFT & TOP = | Success Unpredictable Fail
44 /// --------------+-----------------------------------
45 /// Success | Success Unpredictable Fail
46 /// Unpredictable | Unpredictable Unpredictable Fail
47 /// Fail | Fail Fail Fail
49 /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
50 /// Success, SoftFail, Fail respectively.
57 /// Constructor - Performs initial setup for the disassembler.
58 MCDisassembler(const MCSubtargetInfo &STI) : GetOpInfo(0), SymbolLookUp(0),
60 STI(STI), CommentStream(0) {}
62 virtual ~MCDisassembler();
64 /// getInstruction - Returns the disassembly of a single instruction.
66 /// @param instr - An MCInst to populate with the contents of the
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,
85 raw_ostream &cStream) const = 0;
87 /// getEDInfo - Returns the enhanced instruction information corresponding to
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 EDInstInfo *getEDInfo() const { return (EDInstInfo*)0; }
97 // Hooks for symbolic disassembly via the public 'C' interface.
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.
105 // The assembly context for creating symbols and MCExprs in place of
106 // immediate operands when there is symbolic information.
109 // Subtarget information, for instruction decoding predicates if required.
110 const MCSubtargetInfo &STI;
113 void setupForSymbolicDisassembly(LLVMOpInfoCallback getOpInfo,
114 LLVMSymbolLookupCallback symbolLookUp,
117 GetOpInfo = getOpInfo;
118 SymbolLookUp = symbolLookUp;
122 LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; }
123 LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const {
126 void *getDisInfoBlock() const { return DisInfo; }
127 MCContext *getMCContext() const { return Ctx; }
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;