blob: 46d0c4c3d94c0879f6e66fa22dcdca73ad285fc6 [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"
22#include "llvm/Support/raw_ostream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000023#include <string>
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000024
25namespace llvm {
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000026class MCContext;
27class MCAsmInfo;
28class MCDisassembler;
29class MCInstPrinter;
Sean Callanane804b5b2012-04-06 18:21:09 +000030class MCInstrInfo;
Evan Chengd60fa58b2011-07-18 20:57:22 +000031class MCRegisterInfo;
Sean Callanane804b5b2012-04-06 18:21:09 +000032class MCSubtargetInfo;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000033class Target;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000034
35//
36// This is the disassembler context returned by LLVMCreateDisasm().
37//
38class LLVMDisasmContext {
39private:
40 //
41 // The passed parameters when the disassembler context is created.
42 //
43 // The TripleName for this disassembler.
44 std::string TripleName;
45 // The pointer to the caller's block of symbolic information.
46 void *DisInfo;
47 // The Triple specific symbolic information type returned by GetOpInfo.
48 int TagType;
49 // The function to get the symbolic information for operands.
50 LLVMOpInfoCallback GetOpInfo;
51 // The function to look up a symbol name.
52 LLVMSymbolLookupCallback SymbolLookUp;
53 //
54 // The objects created and saved by LLVMCreateDisasm() then used by
55 // LLVMDisasmInstruction().
56 //
57 // The LLVM target corresponding to the disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +000058 // FIXME: using std::unique_ptr<const llvm::Target> causes a malloc error
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000059 // when this LLVMDisasmContext is deleted.
60 const Target *TheTarget;
61 // The assembly information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000062 std::unique_ptr<const llvm::MCAsmInfo> MAI;
Evan Chengd60fa58b2011-07-18 20:57:22 +000063 // The register information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000064 std::unique_ptr<const llvm::MCRegisterInfo> MRI;
Sean Callanane804b5b2012-04-06 18:21:09 +000065 // The subtarget information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000066 std::unique_ptr<const llvm::MCSubtargetInfo> MSI;
Sean Callanane804b5b2012-04-06 18:21:09 +000067 // The instruction information for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000068 std::unique_ptr<const llvm::MCInstrInfo> MII;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000069 // The assembly context for creating symbols and MCExprs.
Ahmed Charles56440fd2014-03-06 05:51:42 +000070 std::unique_ptr<const llvm::MCContext> Ctx;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000071 // The disassembler for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000072 std::unique_ptr<const llvm::MCDisassembler> DisAsm;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000073 // The instruction printer for the target architecture.
Ahmed Charles56440fd2014-03-06 05:51:42 +000074 std::unique_ptr<llvm::MCInstPrinter> IP;
Quentin Colombet5f09cb02013-10-02 22:07:57 +000075 // The options used to set up the disassembler.
76 uint64_t Options;
Quentin Colombet76e55572013-10-03 17:51:49 +000077 // The CPU string.
78 std::string CPU;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000079
80public:
Owen Anderson233f1302011-09-15 18:37:20 +000081 // Comment stream and backing vector.
82 SmallString<128> CommentsToEmit;
83 raw_svector_ostream CommentStream;
84
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000085 LLVMDisasmContext(std::string tripleName, void *disInfo, int tagType,
Kevin Enderby9377a522011-04-11 18:08:50 +000086 LLVMOpInfoCallback getOpInfo,
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000087 LLVMSymbolLookupCallback symbolLookUp,
88 const Target *theTarget, const MCAsmInfo *mAI,
Evan Chengd60fa58b2011-07-18 20:57:22 +000089 const MCRegisterInfo *mRI,
Sean Callanane804b5b2012-04-06 18:21:09 +000090 const MCSubtargetInfo *mSI,
91 const MCInstrInfo *mII,
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000092 llvm::MCContext *ctx, const MCDisassembler *disAsm,
93 MCInstPrinter *iP) : TripleName(tripleName),
94 DisInfo(disInfo), TagType(tagType), GetOpInfo(getOpInfo),
Owen Anderson233f1302011-09-15 18:37:20 +000095 SymbolLookUp(symbolLookUp), TheTarget(theTarget),
Quentin Colombet5f09cb02013-10-02 22:07:57 +000096 Options(0),
Owen Anderson233f1302011-09-15 18:37:20 +000097 CommentStream(CommentsToEmit) {
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000098 MAI.reset(mAI);
Evan Chengd60fa58b2011-07-18 20:57:22 +000099 MRI.reset(mRI);
Sean Callanane804b5b2012-04-06 18:21:09 +0000100 MSI.reset(mSI);
101 MII.reset(mII);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000102 Ctx.reset(ctx);
103 DisAsm.reset(disAsm);
104 IP.reset(iP);
105 }
Benjamin Kramer3f87e3b2012-06-06 20:45:10 +0000106 const std::string &getTripleName() const { return TripleName; }
107 void *getDisInfo() const { return DisInfo; }
108 int getTagType() const { return TagType; }
109 LLVMOpInfoCallback getGetOpInfo() const { return GetOpInfo; }
110 LLVMSymbolLookupCallback getSymbolLookupCallback() const {
111 return SymbolLookUp;
112 }
113 const Target *getTarget() const { return TheTarget; }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000114 const MCDisassembler *getDisAsm() const { return DisAsm.get(); }
Owen Anderson233f1302011-09-15 18:37:20 +0000115 const MCAsmInfo *getAsmInfo() const { return MAI.get(); }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000116 const MCInstrInfo *getInstrInfo() const { return MII.get(); }
117 const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); }
118 const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000119 MCInstPrinter *getIP() { return IP.get(); }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000120 void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); }
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000121 uint64_t getOptions() const { return Options; }
122 void addOptions(uint64_t Options) { this->Options |= Options; }
Quentin Colombet76e55572013-10-03 17:51:49 +0000123 StringRef getCPU() const { return CPU; }
124 void setCPU(const char *CPU) { this->CPU = CPU; }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000125};
126
127} // namespace llvm
Sebastian Redl1b86ea82011-04-24 15:46:56 +0000128
129#endif