]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/Disassembler/llvm/DisassemblerLLVMC.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Plugins / Disassembler / llvm / DisassemblerLLVMC.h
1 //===-- DisassemblerLLVMC.h -------------------------------------*- 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 #ifndef liblldb_DisassemblerLLVMC_h_
11 #define liblldb_DisassemblerLLVMC_h_
12
13 // C Includes
14 // C++ Includes
15 #include <memory>
16 #include <mutex>
17 #include <string>
18
19 // Other libraries and framework includes
20 #include "llvm-c/Disassembler.h"
21
22 // Project includes
23 #include "lldb/Core/Address.h"
24 #include "lldb/Core/Disassembler.h"
25 #include "lldb/Core/PluginManager.h"
26
27 // Opaque references to C++ Objects in LLVM's MC.
28 namespace llvm {
29 class MCContext;
30 class MCInst;
31 class MCInstrInfo;
32 class MCRegisterInfo;
33 class MCDisassembler;
34 class MCInstPrinter;
35 class MCAsmInfo;
36 class MCSubtargetInfo;
37 } // namespace llvm
38
39 class InstructionLLVMC;
40
41 class DisassemblerLLVMC : public lldb_private::Disassembler {
42   // Since we need to make two actual MC Disassemblers for ARM (ARM & THUMB),
43   // and there's a bit of goo to set up and own
44   // in the MC disassembler world, I added this class to manage the actual
45   // disassemblers.
46   class LLVMCDisassembler {
47   public:
48     LLVMCDisassembler(const char *triple, const char *cpu,
49                       const char *features_str, unsigned flavor,
50                       DisassemblerLLVMC &owner);
51
52     ~LLVMCDisassembler();
53
54     uint64_t GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len,
55                        lldb::addr_t pc, llvm::MCInst &mc_inst);
56     void PrintMCInst(llvm::MCInst &mc_inst, std::string &inst_string,
57                      std::string &comments_string);
58     void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style);
59     bool CanBranch(llvm::MCInst &mc_inst);
60     bool HasDelaySlot(llvm::MCInst &mc_inst);
61     bool IsCall(llvm::MCInst &mc_inst);
62     bool IsValid() { return m_is_valid; }
63
64   private:
65     bool m_is_valid;
66     std::unique_ptr<llvm::MCContext> m_context_ap;
67     std::unique_ptr<llvm::MCAsmInfo> m_asm_info_ap;
68     std::unique_ptr<llvm::MCSubtargetInfo> m_subtarget_info_ap;
69     std::unique_ptr<llvm::MCInstrInfo> m_instr_info_ap;
70     std::unique_ptr<llvm::MCRegisterInfo> m_reg_info_ap;
71     std::unique_ptr<llvm::MCInstPrinter> m_instr_printer_ap;
72     std::unique_ptr<llvm::MCDisassembler> m_disasm_ap;
73   };
74
75 public:
76   DisassemblerLLVMC(const lldb_private::ArchSpec &arch,
77                     const char *flavor /* = NULL */);
78
79   ~DisassemblerLLVMC() override;
80
81   //------------------------------------------------------------------
82   // Static Functions
83   //------------------------------------------------------------------
84   static void Initialize();
85
86   static void Terminate();
87
88   static lldb_private::ConstString GetPluginNameStatic();
89
90   static lldb_private::Disassembler *
91   CreateInstance(const lldb_private::ArchSpec &arch, const char *flavor);
92
93   size_t DecodeInstructions(const lldb_private::Address &base_addr,
94                             const lldb_private::DataExtractor &data,
95                             lldb::offset_t data_offset, size_t num_instructions,
96                             bool append, bool data_from_file) override;
97
98   //------------------------------------------------------------------
99   // PluginInterface protocol
100   //------------------------------------------------------------------
101   lldb_private::ConstString GetPluginName() override;
102
103   uint32_t GetPluginVersion() override;
104
105 protected:
106   friend class InstructionLLVMC;
107
108   bool FlavorValidForArchSpec(const lldb_private::ArchSpec &arch,
109                               const char *flavor) override;
110
111   bool IsValid() {
112     return (m_disasm_ap.get() != NULL && m_disasm_ap->IsValid());
113   }
114
115   int OpInfo(uint64_t PC, uint64_t Offset, uint64_t Size, int TagType,
116              void *TagBug);
117
118   const char *SymbolLookup(uint64_t ReferenceValue, uint64_t *ReferenceType,
119                            uint64_t ReferencePC, const char **ReferenceName);
120
121   static int OpInfoCallback(void *DisInfo, uint64_t PC, uint64_t Offset,
122                             uint64_t Size, int TagType, void *TagBug);
123
124   static const char *SymbolLookupCallback(void *DisInfo,
125                                           uint64_t ReferenceValue,
126                                           uint64_t *ReferenceType,
127                                           uint64_t ReferencePC,
128                                           const char **ReferenceName);
129
130   void Lock(InstructionLLVMC *inst,
131             const lldb_private::ExecutionContext *exe_ctx) {
132     m_mutex.lock();
133     m_inst = inst;
134     m_exe_ctx = exe_ctx;
135   }
136
137   void Unlock() {
138     m_inst = NULL;
139     m_exe_ctx = NULL;
140     m_mutex.unlock();
141   }
142
143   const lldb_private::ExecutionContext *m_exe_ctx;
144   InstructionLLVMC *m_inst;
145   std::mutex m_mutex;
146   bool m_data_from_file;
147
148   std::unique_ptr<LLVMCDisassembler> m_disasm_ap;
149   std::unique_ptr<LLVMCDisassembler> m_alternate_disasm_ap;
150 };
151
152 #endif // liblldb_DisassemblerLLVM_h_