1251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//===-- llvm/MC/MCDisassembler.h - Disassembler interface -------*- C++ -*-===//
2251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//
3251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//                     The LLVM Compiler Infrastructure
4251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//
5251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan// This file is distributed under the University of Illinois Open Source
6251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan// License. See LICENSE.TXT for details.
7251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//
8251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//===----------------------------------------------------------------------===//
9674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_MC_MCDISASSEMBLER_H
10674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_MC_MCDISASSEMBLER_H
11251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan
12bd3327654b5708f1ba92aff3ab25b1bbf5034797Kevin Enderby#include "llvm-c/Disassembler.h"
132c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include "llvm/MC/MCRelocationInfo.h"
1436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/MC/MCSymbolizer.h"
15255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/Support/DataTypes.h"
16251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan
17251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanannamespace llvm {
18639aa87bee77fe2d83f0978ae1eea53e49def324Jim Grosbach
19251ef612a812ac99edeab6c08a752bf8ca220921Sean Callananclass MCInst;
20b950585cc5a0d665e9accfe5ce490cd269756f2eJames Molloyclass MCSubtargetInfo;
21251ef612a812ac99edeab6c08a752bf8ca220921Sean Callananclass MemoryObject;
22251ef612a812ac99edeab6c08a752bf8ca220921Sean Callananclass raw_ostream;
23bd3327654b5708f1ba92aff3ab25b1bbf5034797Kevin Enderbyclass MCContext;
24639aa87bee77fe2d83f0978ae1eea53e49def324Jim Grosbach
25251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan/// MCDisassembler - Superclass for all disassemblers.  Consumes a memory region
26251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan///   and provides an array of assembly instructions.
27251ef612a812ac99edeab6c08a752bf8ca220921Sean Callananclass MCDisassembler {
28251ef612a812ac99edeab6c08a752bf8ca220921Sean Callananpublic:
2983e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// Ternary decode status. Most backends will just use Fail and
3083e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// Success, however some have a concept of an instruction with
3183e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// understandable semantics but which is architecturally
3283e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// incorrect. An example of this is ARM UNPREDICTABLE instructions
3383e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// which are disassemblable but cause undefined behaviour.
3483e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///
3583e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// Because it makes sense to disassemble these instructions, there
3683e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// is a "soft fail" failure mode that indicates the MCInst& is
3783e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// valid but architecturally incorrect.
3883e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///
3983e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// The enum numbers are deliberately chosen such that reduction
4083e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// from Success->SoftFail ->Fail can be done with a simple
4183e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// bitwise-AND:
4283e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///
4383e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///   LEFT & TOP =  | Success       Unpredictable   Fail
4483e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///   --------------+-----------------------------------
4583e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///   Success       | Success       Unpredictable   Fail
4683e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///   Unpredictable | Unpredictable Unpredictable   Fail
4783e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///   Fail          | Fail          Fail            Fail
4883e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///
4983e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
5083e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// Success, SoftFail, Fail respectively.
5183e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  enum DecodeStatus {
5283e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson    Fail = 0,
5383e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson    SoftFail = 1,
5483e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson    Success = 3
5583e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  };
5683e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson
57251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  /// Constructor     - Performs initial setup for the disassembler.
58dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
59dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    : Ctx(Ctx), STI(STI), Symbolizer(), CommentStream(nullptr) {}
60639aa87bee77fe2d83f0978ae1eea53e49def324Jim Grosbach
61251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  virtual ~MCDisassembler();
62639aa87bee77fe2d83f0978ae1eea53e49def324Jim Grosbach
63251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  /// getInstruction  - Returns the disassembly of a single instruction.
64251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  ///
65639aa87bee77fe2d83f0978ae1eea53e49def324Jim Grosbach  /// @param instr    - An MCInst to populate with the contents of the
66251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  ///                   instruction.
67251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  /// @param size     - A value to populate with the size of the instruction, or
68251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  ///                   the number of bytes consumed while attempting to decode
69251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  ///                   an invalid instruction.
70251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  /// @param region   - The memory object to use as a source for machine code.
71251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  /// @param address  - The address, in the memory space of region, of the first
72251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  ///                   byte of the instruction.
73251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  /// @param vStream  - The stream to print warnings and diagnostic messages on.
7498c5ddabca1debf935a07d14d0cbc9732374bdb8Owen Anderson  /// @param cStream  - The stream to print comments and annotations on.
7583e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  /// @return         - MCDisassembler::Success if the instruction is valid,
76639aa87bee77fe2d83f0978ae1eea53e49def324Jim Grosbach  ///                   MCDisassembler::SoftFail if the instruction was
7783e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///                                            disassemblable but invalid,
7883e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  ///                   MCDisassembler::Fail if the instruction was invalid.
7983e3f67fb68d497b600da83a62f000fcce7868a9Owen Anderson  virtual DecodeStatus  getInstruction(MCInst& instr,
80251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan                                       uint64_t& size,
81adef06a71458ded0716935a61b3d43d164d4df12Derek Schuff                                       const MemoryObject &region,
82251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan                                       uint64_t address,
8398c5ddabca1debf935a07d14d0cbc9732374bdb8Owen Anderson                                       raw_ostream &vStream,
8498c5ddabca1debf935a07d14d0cbc9732374bdb8Owen Anderson                                       raw_ostream &cStream) const = 0;
85de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombetprivate:
86dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  MCContext &Ctx;
879899f70a7406d632c82849978bf6981f1ee4ccb5Sean Callanan
88b950585cc5a0d665e9accfe5ce490cd269756f2eJames Molloyprotected:
89b950585cc5a0d665e9accfe5ce490cd269756f2eJames Molloy  // Subtarget information, for instruction decoding predicates if required.
90b950585cc5a0d665e9accfe5ce490cd269756f2eJames Molloy  const MCSubtargetInfo &STI;
9136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<MCSymbolizer> Symbolizer;
922c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
93bd3327654b5708f1ba92aff3ab25b1bbf5034797Kevin Enderbypublic:
942c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  // Helpers around MCSymbolizer
952c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  bool tryAddingSymbolicOperand(MCInst &Inst,
962c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha                                int64_t Value,
972c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha                                uint64_t Address, bool IsBranch,
982c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha                                uint64_t Offset, uint64_t InstSize) const;
992c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
1002c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const;
1012c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
1022c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  /// Set \p Symzer as the current symbolizer.
1032c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  /// This takes ownership of \p Symzer, and deletes the previously set one.
10436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer);
1052c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
106dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  MCContext& getContext() const { return Ctx; }
107de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombet
10862ab26548fee37422fb90daaac22fc1140be797dArtyom Skrobov  const MCSubtargetInfo& getSubtargetInfo() const { return STI; }
10962ab26548fee37422fb90daaac22fc1140be797dArtyom Skrobov
1109e5887b17e634b98f7c1cf0ee4f25c218097d08eKevin Enderby  // Marked mutable because we cache it inside the disassembler, rather than
1119e5887b17e634b98f7c1cf0ee4f25c218097d08eKevin Enderby  // having to pass it around as an argument through all the autogenerated code.
1129e5887b17e634b98f7c1cf0ee4f25c218097d08eKevin Enderby  mutable raw_ostream *CommentStream;
1139899f70a7406d632c82849978bf6981f1ee4ccb5Sean Callanan};
114251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan
115251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan} // namespace llvm
116251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan
117251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan#endif
118