blob: d3a2dbb6da5dfc7b0deaa785d2592f93fd40dc44 [file] [log] [blame]
Kevin Enderbyf3070dc2011-03-28 18:25:07 +00001//===------------- Disassembler.h - LLVM Disassembler -----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Songf78650a2018-07-30 19:41:25 +00006//
Kevin Enderbyf3070dc2011-03-28 18:25:07 +00007//===----------------------------------------------------------------------===//
8//
Fangrui Songf78650a2018-07-30 19:41:25 +00009// This file defines the interface for the Disassembly library's disassembler
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000010// context. The disassembler is responsible for producing strings for
11// individual instructions according to a given architecture and disassembly
12// syntax.
13//
14//===----------------------------------------------------------------------===//
Sebastian Redl1b86ea82011-04-24 15:46:56 +000015
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
17#define LLVM_LIB_MC_MCDISASSEMBLER_DISASSEMBLER_H
Sebastian Redl1b86ea82011-04-24 15:46:56 +000018
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000019#include "llvm-c/Disassembler.h"
Owen Anderson233f1302011-09-15 18:37:20 +000020#include "llvm/ADT/SmallString.h"
Benjamin Kramer391be792016-01-27 19:29:56 +000021#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 Anderson233f1302011-09-15 18:37:20 +000028#include "llvm/Support/raw_ostream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000029#include <string>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000030#include <utility>
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000031
32namespace llvm {
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,
Benjamin Kramer82de7d32016-05-27 14:27:24 +000089 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 Enderbyf3070dc2011-03-28 18:25:07 +000095 MAI.reset(mAI);
Evan Chengd60fa58b2011-07-18 20:57:22 +000096 MRI.reset(mRI);
Sean Callanane804b5b2012-04-06 18:21:09 +000097 MSI.reset(mSI);
98 MII.reset(mII);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000099 Ctx.reset(ctx);
100 DisAsm.reset(disAsm);
101 IP.reset(iP);
102 }
Benjamin Kramer3f87e3b2012-06-06 20:45:10 +0000103 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 Enderbyf3070dc2011-03-28 18:25:07 +0000111 const MCDisassembler *getDisAsm() const { return DisAsm.get(); }
Owen Anderson233f1302011-09-15 18:37:20 +0000112 const MCAsmInfo *getAsmInfo() const { return MAI.get(); }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000113 const MCInstrInfo *getInstrInfo() const { return MII.get(); }
114 const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); }
115 const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000116 MCInstPrinter *getIP() { return IP.get(); }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000117 void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); }
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000118 uint64_t getOptions() const { return Options; }
119 void addOptions(uint64_t Options) { this->Options |= Options; }
Quentin Colombet76e55572013-10-03 17:51:49 +0000120 StringRef getCPU() const { return CPU; }
121 void setCPU(const char *CPU) { this->CPU = CPU; }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000122};
123
124} // namespace llvm
Sebastian Redl1b86ea82011-04-24 15:46:56 +0000125
126#endif