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 LLVM_MC_MCDISASSEMBLER_H
10 #define LLVM_MC_MCDISASSEMBLER_H
12 #include "llvm-c/Disassembler.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/MC/MCSymbolizer.h"
15 #include "llvm/Support/DataTypes.h"
20 class MCSubtargetInfo;
24 /// Superclass for all disassemblers. Consumes a memory region and provides an
25 /// array of assembly instructions.
26 class MCDisassembler {
28 /// Ternary decode status. Most backends will just use Fail and
29 /// Success, however some have a concept of an instruction with
30 /// understandable semantics but which is architecturally
31 /// incorrect. An example of this is ARM UNPREDICTABLE instructions
32 /// which are disassemblable but cause undefined behaviour.
34 /// Because it makes sense to disassemble these instructions, there
35 /// is a "soft fail" failure mode that indicates the MCInst& is
36 /// valid but architecturally incorrect.
38 /// The enum numbers are deliberately chosen such that reduction
39 /// from Success->SoftFail ->Fail can be done with a simple
42 /// LEFT & TOP = | Success Unpredictable Fail
43 /// --------------+-----------------------------------
44 /// Success | Success Unpredictable Fail
45 /// Unpredictable | Unpredictable Unpredictable Fail
46 /// Fail | Fail Fail Fail
48 /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
49 /// Success, SoftFail, Fail respectively.
56 MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
57 : Ctx(Ctx), STI(STI), Symbolizer(), CommentStream(nullptr) {}
59 virtual ~MCDisassembler();
61 /// Returns the disassembly of a single instruction.
63 /// \param Instr - An MCInst to populate with the contents of the
65 /// \param Size - A value to populate with the size of the instruction, or
66 /// the number of bytes consumed while attempting to decode
67 /// an invalid instruction.
68 /// \param Address - The address, in the memory space of region, of the first
69 /// byte of the instruction.
70 /// \param VStream - The stream to print warnings and diagnostic messages on.
71 /// \param CStream - The stream to print comments and annotations on.
72 /// \return - MCDisassembler::Success if the instruction is valid,
73 /// MCDisassembler::SoftFail if the instruction was
74 /// disassemblable but invalid,
75 /// MCDisassembler::Fail if the instruction was invalid.
76 virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
77 ArrayRef<uint8_t> Bytes, uint64_t Address,
79 raw_ostream &CStream) const = 0;
85 // Subtarget information, for instruction decoding predicates if required.
86 const MCSubtargetInfo &STI;
87 std::unique_ptr<MCSymbolizer> Symbolizer;
90 // Helpers around MCSymbolizer
91 bool tryAddingSymbolicOperand(MCInst &Inst,
93 uint64_t Address, bool IsBranch,
94 uint64_t Offset, uint64_t InstSize) const;
96 void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const;
98 /// Set \p Symzer as the current symbolizer.
99 /// This takes ownership of \p Symzer, and deletes the previously set one.
100 void setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer);
102 MCContext& getContext() const { return Ctx; }
104 const MCSubtargetInfo& getSubtargetInfo() const { return STI; }
106 // Marked mutable because we cache it inside the disassembler, rather than
107 // having to pass it around as an argument through all the autogenerated code.
108 mutable raw_ostream *CommentStream;