blob: 6359146d81d85b81744c525c05ce6e6fbf1fafc7 [file] [log] [blame]
Sean Callanan95e5c632012-02-17 00:53:45 +00001//===-- 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
Eugene Zelenko45a40142015-10-22 21:24:37 +000013// C Includes
14// C++ Includes
15#include <memory>
Jim Ingham0f063ba2013-03-02 00:26:47 +000016#include <string>
Sean Callanan95e5c632012-02-17 00:53:45 +000017
Eugene Zelenko45a40142015-10-22 21:24:37 +000018// Other libraries and framework includes
Sean Callanan95e5c632012-02-17 00:53:45 +000019#include "llvm-c/Disassembler.h"
20
Eugene Zelenko45a40142015-10-22 21:24:37 +000021// Project includes
22#include "lldb/Core/Address.h"
23#include "lldb/Core/Disassembler.h"
24#include "lldb/Core/PluginManager.h"
25#include "lldb/Host/Mutex.h"
26
Jim Ingham0f063ba2013-03-02 00:26:47 +000027// Opaque references to C++ Objects in LLVM's MC.
28namespace llvm
29{
30 class MCContext;
31 class MCInst;
32 class MCInstrInfo;
33 class MCRegisterInfo;
34 class MCDisassembler;
35 class MCInstPrinter;
36 class MCAsmInfo;
37 class MCSubtargetInfo;
Eugene Zelenko45a40142015-10-22 21:24:37 +000038} // namespace llvm
Sean Callanan95e5c632012-02-17 00:53:45 +000039
40class InstructionLLVMC;
41
42class DisassemblerLLVMC : public lldb_private::Disassembler
43{
Jim Ingham0f063ba2013-03-02 00:26:47 +000044 // Since we need to make two actual MC Disassemblers for ARM (ARM & THUMB), and there's a bit of goo to set up and own
45 // in the MC disassembler world, I added this class to manage the actual disassemblers.
46 class LLVMCDisassembler
47 {
48 public:
Jaydeep Patil501a7812015-07-16 03:51:55 +000049 LLVMCDisassembler (const char *triple, const char *cpu, const char *features_str, unsigned flavor, DisassemblerLLVMC &owner);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000050
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +000051 ~LLVMCDisassembler();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000052
Greg Clayton3faf47c2013-03-28 23:42:53 +000053 uint64_t GetMCInst (const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc, llvm::MCInst &mc_inst);
Sean Callanand38f4d22015-12-11 19:10:04 +000054 void PrintMCInst (llvm::MCInst &mc_inst, std::string &inst_string, std::string &comments_string);
Daniel Malead79ae052013-08-07 21:54:09 +000055 void SetStyle (bool use_hex_immed, HexImmediateStyle hex_style);
Jim Ingham0f063ba2013-03-02 00:26:47 +000056 bool CanBranch (llvm::MCInst &mc_inst);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000057 bool HasDelaySlot (llvm::MCInst &mc_inst);
Jim Ingham0f063ba2013-03-02 00:26:47 +000058 bool IsValid()
59 {
60 return m_is_valid;
61 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000062
Jim Ingham0f063ba2013-03-02 00:26:47 +000063 private:
Daniel Malead79ae052013-08-07 21:54:09 +000064 bool m_is_valid;
Greg Clayton7b0992d2013-04-18 22:45:39 +000065 std::unique_ptr<llvm::MCContext> m_context_ap;
66 std::unique_ptr<llvm::MCAsmInfo> m_asm_info_ap;
67 std::unique_ptr<llvm::MCSubtargetInfo> m_subtarget_info_ap;
68 std::unique_ptr<llvm::MCInstrInfo> m_instr_info_ap;
69 std::unique_ptr<llvm::MCRegisterInfo> m_reg_info_ap;
70 std::unique_ptr<llvm::MCInstPrinter> m_instr_printer_ap;
71 std::unique_ptr<llvm::MCDisassembler> m_disasm_ap;
Jim Ingham0f063ba2013-03-02 00:26:47 +000072 };
73
Sean Callanan95e5c632012-02-17 00:53:45 +000074public:
Eugene Zelenko45a40142015-10-22 21:24:37 +000075 DisassemblerLLVMC(const lldb_private::ArchSpec &arch, const char *flavor /* = NULL */);
76
77 ~DisassemblerLLVMC() override;
78
Sean Callanan95e5c632012-02-17 00:53:45 +000079 //------------------------------------------------------------------
80 // Static Functions
81 //------------------------------------------------------------------
82 static void
83 Initialize();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000084
Sean Callanan95e5c632012-02-17 00:53:45 +000085 static void
86 Terminate();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000087
Greg Clayton57abc5d2013-05-10 21:47:16 +000088 static lldb_private::ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +000089 GetPluginNameStatic();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000090
Sean Callanan95e5c632012-02-17 00:53:45 +000091 static lldb_private::Disassembler *
Jim Ingham0f063ba2013-03-02 00:26:47 +000092 CreateInstance(const lldb_private::ArchSpec &arch, const char *flavor);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000093
Eugene Zelenko45a40142015-10-22 21:24:37 +000094 size_t
95 DecodeInstructions(const lldb_private::Address &base_addr,
96 const lldb_private::DataExtractor& data,
97 lldb::offset_t data_offset,
98 size_t num_instructions,
99 bool append,
100 bool data_from_file) override;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000101
Sean Callanan95e5c632012-02-17 00:53:45 +0000102 //------------------------------------------------------------------
103 // PluginInterface protocol
104 //------------------------------------------------------------------
Eugene Zelenko45a40142015-10-22 21:24:37 +0000105 lldb_private::ConstString
106 GetPluginName() override;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000107
Eugene Zelenko45a40142015-10-22 21:24:37 +0000108 uint32_t
109 GetPluginVersion() override;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000110
Sean Callanan95e5c632012-02-17 00:53:45 +0000111protected:
112 friend class InstructionLLVMC;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000113
Eugene Zelenko45a40142015-10-22 21:24:37 +0000114 bool
115 FlavorValidForArchSpec(const lldb_private::ArchSpec &arch, const char *flavor) override;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000116
Sean Callanan95e5c632012-02-17 00:53:45 +0000117 bool
118 IsValid()
119 {
Jim Ingham0f063ba2013-03-02 00:26:47 +0000120 return (m_disasm_ap.get() != NULL && m_disasm_ap->IsValid());
Sean Callanan95e5c632012-02-17 00:53:45 +0000121 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000122
Sean Callanan95e5c632012-02-17 00:53:45 +0000123 int OpInfo(uint64_t PC,
124 uint64_t Offset,
125 uint64_t Size,
126 int TagType,
127 void *TagBug);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000128
Sean Callanan95e5c632012-02-17 00:53:45 +0000129 const char *SymbolLookup (uint64_t ReferenceValue,
130 uint64_t *ReferenceType,
131 uint64_t ReferencePC,
132 const char **ReferenceName);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000133
Sean Callanan95e5c632012-02-17 00:53:45 +0000134 static int OpInfoCallback (void *DisInfo,
135 uint64_t PC,
136 uint64_t Offset,
137 uint64_t Size,
138 int TagType,
139 void *TagBug);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000140
Sean Callanan95e5c632012-02-17 00:53:45 +0000141 static const char *SymbolLookupCallback(void *DisInfo,
142 uint64_t ReferenceValue,
143 uint64_t *ReferenceType,
144 uint64_t ReferencePC,
145 const char **ReferenceName);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000146
147 void Lock(InstructionLLVMC *inst,
Greg Claytonba812f42012-05-10 02:52:23 +0000148 const lldb_private::ExecutionContext *exe_ctx)
Sean Callanan95e5c632012-02-17 00:53:45 +0000149 {
150 m_mutex.Lock();
Sean Callanan95e5c632012-02-17 00:53:45 +0000151 m_inst = inst;
Greg Claytonba812f42012-05-10 02:52:23 +0000152 m_exe_ctx = exe_ctx;
Sean Callanan95e5c632012-02-17 00:53:45 +0000153 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000154
Sean Callanan95e5c632012-02-17 00:53:45 +0000155 void Unlock()
156 {
Sean Callanan95e5c632012-02-17 00:53:45 +0000157 m_inst = NULL;
Greg Claytonba812f42012-05-10 02:52:23 +0000158 m_exe_ctx = NULL;
Sean Callanan95e5c632012-02-17 00:53:45 +0000159 m_mutex.Unlock();
160 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000161
Greg Claytonba812f42012-05-10 02:52:23 +0000162 const lldb_private::ExecutionContext *m_exe_ctx;
163 InstructionLLVMC *m_inst;
164 lldb_private::Mutex m_mutex;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000165 bool m_data_from_file;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000166
Greg Clayton7b0992d2013-04-18 22:45:39 +0000167 std::unique_ptr<LLVMCDisassembler> m_disasm_ap;
168 std::unique_ptr<LLVMCDisassembler> m_alternate_disasm_ap;
Sean Callanan95e5c632012-02-17 00:53:45 +0000169};
170
Eugene Zelenko45a40142015-10-22 21:24:37 +0000171#endif // liblldb_DisassemblerLLVM_h_