| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 1 | //===------------- Disassembler.h - LLVM Disassembler -----------*- C++ -*-===// | 
|  | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 6 | // | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
| Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 9 | // This file defines the interface for the Disassembly library's disassembler | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 10 | // context.  The disassembler is responsible for producing strings for | 
|  | 11 | // individual instructions according to a given architecture and disassembly | 
|  | 12 | // syntax. | 
|  | 13 | // | 
|  | 14 | //===----------------------------------------------------------------------===// | 
| Sebastian Redl | 1b86ea8 | 2011-04-24 15:46:56 +0000 | [diff] [blame] | 15 |  | 
| Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 16 | #ifndef LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H | 
|  | 17 | #define LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H | 
| Sebastian Redl | 1b86ea8 | 2011-04-24 15:46:56 +0000 | [diff] [blame] | 18 |  | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 19 | #include "llvm-c/Disassembler.h" | 
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" | 
| Benjamin Kramer | 391be79 | 2016-01-27 19:29:56 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" | 
|  | 22 | #include "llvm/MC/MCContext.h" | 
|  | 23 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" | 
|  | 24 | #include "llvm/MC/MCInstPrinter.h" | 
|  | 25 | #include "llvm/MC/MCInstrInfo.h" | 
|  | 26 | #include "llvm/MC/MCRegisterInfo.h" | 
|  | 27 | #include "llvm/MC/MCSubtargetInfo.h" | 
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" | 
| Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 29 | #include <string> | 
| Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 30 | #include <utility> | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 31 |  | 
|  | 32 | namespace llvm { | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 33 | class Target; | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | // | 
|  | 36 | // This is the disassembler context returned by LLVMCreateDisasm(). | 
|  | 37 | // | 
|  | 38 | class LLVMDisasmContext { | 
|  | 39 | private: | 
|  | 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 Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 58 | // FIXME: using std::unique_ptr<const llvm::Target> causes a malloc error | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 59 | //        when this LLVMDisasmContext is deleted. | 
|  | 60 | const Target *TheTarget; | 
|  | 61 | // The assembly information for the target architecture. | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 62 | std::unique_ptr<const llvm::MCAsmInfo> MAI; | 
| Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 63 | // The register information for the target architecture. | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 64 | std::unique_ptr<const llvm::MCRegisterInfo> MRI; | 
| Sean Callanan | e804b5b | 2012-04-06 18:21:09 +0000 | [diff] [blame] | 65 | // The subtarget information for the target architecture. | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 66 | std::unique_ptr<const llvm::MCSubtargetInfo> MSI; | 
| Sean Callanan | e804b5b | 2012-04-06 18:21:09 +0000 | [diff] [blame] | 67 | // The instruction information for the target architecture. | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 68 | std::unique_ptr<const llvm::MCInstrInfo> MII; | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 69 | // The assembly context for creating symbols and MCExprs. | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 70 | std::unique_ptr<const llvm::MCContext> Ctx; | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 71 | // The disassembler for the target architecture. | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 72 | std::unique_ptr<const llvm::MCDisassembler> DisAsm; | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 73 | // The instruction printer for the target architecture. | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 74 | std::unique_ptr<llvm::MCInstPrinter> IP; | 
| Quentin Colombet | 5f09cb0 | 2013-10-02 22:07:57 +0000 | [diff] [blame] | 75 | // The options used to set up the disassembler. | 
|  | 76 | uint64_t Options; | 
| Quentin Colombet | 76e5557 | 2013-10-03 17:51:49 +0000 | [diff] [blame] | 77 | // The CPU string. | 
|  | 78 | std::string CPU; | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 79 |  | 
|  | 80 | public: | 
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 81 | // Comment stream and backing vector. | 
|  | 82 | SmallString<128> CommentsToEmit; | 
|  | 83 | raw_svector_ostream CommentStream; | 
|  | 84 |  | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 85 | LLVMDisasmContext(std::string tripleName, void *disInfo, int tagType, | 
| Kevin Enderby | 9377a52 | 2011-04-11 18:08:50 +0000 | [diff] [blame] | 86 | LLVMOpInfoCallback getOpInfo, | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 87 | LLVMSymbolLookupCallback symbolLookUp, | 
|  | 88 | const Target *theTarget, const MCAsmInfo *mAI, | 
| Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 89 | const MCRegisterInfo *mRI, const MCSubtargetInfo *mSI, | 
|  | 90 | const MCInstrInfo *mII, llvm::MCContext *ctx, | 
|  | 91 | const MCDisassembler *disAsm, MCInstPrinter *iP) | 
|  | 92 | : TripleName(std::move(tripleName)), DisInfo(disInfo), TagType(tagType), | 
|  | 93 | GetOpInfo(getOpInfo), SymbolLookUp(symbolLookUp), TheTarget(theTarget), | 
|  | 94 | Options(0), CommentStream(CommentsToEmit) { | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 95 | MAI.reset(mAI); | 
| Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 96 | MRI.reset(mRI); | 
| Sean Callanan | e804b5b | 2012-04-06 18:21:09 +0000 | [diff] [blame] | 97 | MSI.reset(mSI); | 
|  | 98 | MII.reset(mII); | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 99 | Ctx.reset(ctx); | 
|  | 100 | DisAsm.reset(disAsm); | 
|  | 101 | IP.reset(iP); | 
|  | 102 | } | 
| Benjamin Kramer | 3f87e3b | 2012-06-06 20:45:10 +0000 | [diff] [blame] | 103 | const std::string &getTripleName() const { return TripleName; } | 
|  | 104 | void *getDisInfo() const { return DisInfo; } | 
|  | 105 | int getTagType() const { return TagType; } | 
|  | 106 | LLVMOpInfoCallback getGetOpInfo() const { return GetOpInfo; } | 
|  | 107 | LLVMSymbolLookupCallback getSymbolLookupCallback() const { | 
|  | 108 | return SymbolLookUp; | 
|  | 109 | } | 
|  | 110 | const Target *getTarget() const { return TheTarget; } | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 111 | const MCDisassembler *getDisAsm() const { return DisAsm.get(); } | 
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 112 | const MCAsmInfo *getAsmInfo() const { return MAI.get(); } | 
| Kevin Enderby | 85cf531 | 2012-12-18 23:47:28 +0000 | [diff] [blame] | 113 | const MCInstrInfo *getInstrInfo() const { return MII.get(); } | 
|  | 114 | const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); } | 
|  | 115 | const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); } | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 116 | MCInstPrinter *getIP() { return IP.get(); } | 
| Kevin Enderby | 85cf531 | 2012-12-18 23:47:28 +0000 | [diff] [blame] | 117 | void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); } | 
| Quentin Colombet | 5f09cb0 | 2013-10-02 22:07:57 +0000 | [diff] [blame] | 118 | uint64_t getOptions() const { return Options; } | 
|  | 119 | void addOptions(uint64_t Options) { this->Options |= Options; } | 
| Quentin Colombet | 76e5557 | 2013-10-03 17:51:49 +0000 | [diff] [blame] | 120 | StringRef getCPU() const { return CPU; } | 
|  | 121 | void setCPU(const char *CPU) { this->CPU = CPU; } | 
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 122 | }; | 
|  | 123 |  | 
|  | 124 | } // namespace llvm | 
| Sebastian Redl | 1b86ea8 | 2011-04-24 15:46:56 +0000 | [diff] [blame] | 125 |  | 
|  | 126 | #endif |