blob: 25d17dafb576d75e5228a6420569b190bda98979 [file] [log] [blame]
Kevin Enderbyf3070dc2011-03-28 18:25:07 +00001//===------------- Disassembler.h - LLVM Disassembler -----------*- 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//
10// This file defines the interface for the Disassembly library's disassembler
11// context. The disassembler is responsible for producing strings for
12// individual instructions according to a given architecture and disassembly
13// syntax.
14//
15//===----------------------------------------------------------------------===//
Sebastian Redl1b86ea82011-04-24 15:46:56 +000016
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000017#ifndef LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
18#define LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
Sebastian Redl1b86ea82011-04-24 15:46:56 +000019
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000020#include "llvm-c/Disassembler.h"
Owen Anderson233f1302011-09-15 18:37:20 +000021#include "llvm/ADT/SmallString.h"
Benjamin Kramer391be792016-01-27 19:29:56 +000022#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCDisassembler/MCDisassembler.h"
25#include "llvm/MC/MCInstPrinter.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCSubtargetInfo.h"
Owen Anderson233f1302011-09-15 18:37:20 +000029#include "llvm/Support/raw_ostream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000030#include <string>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000031#include <utility>
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000032
33namespace llvm {
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000034class Target;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000035
36//
37// This is the disassembler context returned by LLVMCreateDisasm().
38//
39class LLVMDisasmContext {
40private:
41 //
42 // The passed parameters when the disassembler context is created.
43 //
44 // The TripleName for this disassembler.
45 std::string TripleName;
46 // The pointer to the caller's block of symbolic information.
47 void *DisInfo;
48 // The Triple specific symbolic information type returned by GetOpInfo.
49 int TagType;
50 // The function to get the symbolic information for operands.
51 LLVMOpInfoCallback GetOpInfo;
52 // The function to look up a symbol name.
53 LLVMSymbolLookupCallback SymbolLookUp;
54 //
55 // The objects created and saved by LLVMCreateDisasm() then used by
56 // LLVMDisasmInstruction().
57 //
58 // The LLVM target corresponding to the disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +000059 // FIXME: using std::unique_ptr<const llvm::Target> causes a malloc error
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000060 // when this LLVMDisasmContext is deleted.
61 const Target *TheTarget;
62 // The assembly information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000063 std::unique_ptr<const llvm::MCAsmInfo> MAI;
Evan Chengd60fa58b2011-07-18 20:57:22 +000064 // The register information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000065 std::unique_ptr<const llvm::MCRegisterInfo> MRI;
Sean Callanane804b5b2012-04-06 18:21:09 +000066 // The subtarget information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000067 std::unique_ptr<const llvm::MCSubtargetInfo> MSI;
Sean Callanane804b5b2012-04-06 18:21:09 +000068 // The instruction information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000069 std::unique_ptr<const llvm::MCInstrInfo> MII;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000070 // The assembly context for creating symbols and MCExprs.
Ahmed Charles56440fd2014-03-06 05:51:42 +000071 std::unique_ptr<const llvm::MCContext> Ctx;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000072 // The disassembler for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000073 std::unique_ptr<const llvm::MCDisassembler> DisAsm;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000074 // The instruction printer for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000075 std::unique_ptr<llvm::MCInstPrinter> IP;
Quentin Colombet5f09cb02013-10-02 22:07:57 +000076 // The options used to set up the disassembler.
77 uint64_t Options;
Quentin Colombet76e55572013-10-03 17:51:49 +000078 // The CPU string.
79 std::string CPU;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000080
81public:
Owen Anderson233f1302011-09-15 18:37:20 +000082 // Comment stream and backing vector.
83 SmallString<128> CommentsToEmit;
84 raw_svector_ostream CommentStream;
85
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000086 LLVMDisasmContext(std::string tripleName, void *disInfo, int tagType,
Kevin Enderby9377a522011-04-11 18:08:50 +000087 LLVMOpInfoCallback getOpInfo,
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000088 LLVMSymbolLookupCallback symbolLookUp,
89 const Target *theTarget, const MCAsmInfo *mAI,
Benjamin Kramer82de7d32016-05-27 14:27:24 +000090 const MCRegisterInfo *mRI, const MCSubtargetInfo *mSI,
91 const MCInstrInfo *mII, llvm::MCContext *ctx,
92 const MCDisassembler *disAsm, MCInstPrinter *iP)
93 : TripleName(std::move(tripleName)), DisInfo(disInfo), TagType(tagType),
94 GetOpInfo(getOpInfo), SymbolLookUp(symbolLookUp), TheTarget(theTarget),
95 Options(0), CommentStream(CommentsToEmit) {
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000096 MAI.reset(mAI);
Evan Chengd60fa58b2011-07-18 20:57:22 +000097 MRI.reset(mRI);
Sean Callanane804b5b2012-04-06 18:21:09 +000098 MSI.reset(mSI);
99 MII.reset(mII);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000100 Ctx.reset(ctx);
101 DisAsm.reset(disAsm);
102 IP.reset(iP);
103 }
Benjamin Kramer3f87e3b2012-06-06 20:45:10 +0000104 const std::string &getTripleName() const { return TripleName; }
105 void *getDisInfo() const { return DisInfo; }
106 int getTagType() const { return TagType; }
107 LLVMOpInfoCallback getGetOpInfo() const { return GetOpInfo; }
108 LLVMSymbolLookupCallback getSymbolLookupCallback() const {
109 return SymbolLookUp;
110 }
111 const Target *getTarget() const { return TheTarget; }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000112 const MCDisassembler *getDisAsm() const { return DisAsm.get(); }
Owen Anderson233f1302011-09-15 18:37:20 +0000113 const MCAsmInfo *getAsmInfo() const { return MAI.get(); }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000114 const MCInstrInfo *getInstrInfo() const { return MII.get(); }
115 const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); }
116 const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000117 MCInstPrinter *getIP() { return IP.get(); }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000118 void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); }
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000119 uint64_t getOptions() const { return Options; }
120 void addOptions(uint64_t Options) { this->Options |= Options; }
Quentin Colombet76e55572013-10-03 17:51:49 +0000121 StringRef getCPU() const { return CPU; }
122 void setCPU(const char *CPU) { this->CPU = CPU; }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000123};
124
125} // namespace llvm
Sebastian Redl1b86ea82011-04-24 15:46:56 +0000126
127#endif