blob: 6ab9e9ae26250ed4f5398c82f71f09243097894c [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
Jim Ingham0f063ba2013-03-02 00:26:47 +000013#include <string>
Sean Callanan95e5c632012-02-17 00:53:45 +000014
15#include "llvm-c/Disassembler.h"
16
Jim Ingham0f063ba2013-03-02 00:26:47 +000017// Opaque references to C++ Objects in LLVM's MC.
18namespace llvm
19{
20 class MCContext;
21 class MCInst;
22 class MCInstrInfo;
23 class MCRegisterInfo;
24 class MCDisassembler;
25 class MCInstPrinter;
26 class MCAsmInfo;
27 class MCSubtargetInfo;
28}
29
Sean Callanan95e5c632012-02-17 00:53:45 +000030#include "lldb/Core/Address.h"
31#include "lldb/Core/Disassembler.h"
32#include "lldb/Core/PluginManager.h"
33#include "lldb/Host/Mutex.h"
34
35class InstructionLLVMC;
36
37class DisassemblerLLVMC : public lldb_private::Disassembler
38{
Jim Ingham0f063ba2013-03-02 00:26:47 +000039 // 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
40 // in the MC disassembler world, I added this class to manage the actual disassemblers.
41 class LLVMCDisassembler
42 {
43 public:
44 LLVMCDisassembler (const char *triple, unsigned flavor, DisassemblerLLVMC &owner);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000045
Greg Claytone01e07b2013-04-18 18:10:51 +000046 ~LLVMCDisassembler();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000047
Greg Clayton3faf47c2013-03-28 23:42:53 +000048 uint64_t GetMCInst (const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc, llvm::MCInst &mc_inst);
Jim Ingham0f063ba2013-03-02 00:26:47 +000049 uint64_t PrintMCInst (llvm::MCInst &mc_inst, char *output_buffer, size_t out_buffer_len);
Daniel Malead79ae052013-08-07 21:54:09 +000050 void SetStyle (bool use_hex_immed, HexImmediateStyle hex_style);
Jim Ingham0f063ba2013-03-02 00:26:47 +000051 bool CanBranch (llvm::MCInst &mc_inst);
52 bool IsValid()
53 {
54 return m_is_valid;
55 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000056
Jim Ingham0f063ba2013-03-02 00:26:47 +000057 private:
Daniel Malead79ae052013-08-07 21:54:09 +000058 bool m_is_valid;
Greg Clayton7b0992d2013-04-18 22:45:39 +000059 std::unique_ptr<llvm::MCContext> m_context_ap;
60 std::unique_ptr<llvm::MCAsmInfo> m_asm_info_ap;
61 std::unique_ptr<llvm::MCSubtargetInfo> m_subtarget_info_ap;
62 std::unique_ptr<llvm::MCInstrInfo> m_instr_info_ap;
63 std::unique_ptr<llvm::MCRegisterInfo> m_reg_info_ap;
64 std::unique_ptr<llvm::MCInstPrinter> m_instr_printer_ap;
65 std::unique_ptr<llvm::MCDisassembler> m_disasm_ap;
Jim Ingham0f063ba2013-03-02 00:26:47 +000066 };
67
Sean Callanan95e5c632012-02-17 00:53:45 +000068public:
69 //------------------------------------------------------------------
70 // Static Functions
71 //------------------------------------------------------------------
72 static void
73 Initialize();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000074
Sean Callanan95e5c632012-02-17 00:53:45 +000075 static void
76 Terminate();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000077
Greg Clayton57abc5d2013-05-10 21:47:16 +000078 static lldb_private::ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +000079 GetPluginNameStatic();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000080
Sean Callanan95e5c632012-02-17 00:53:45 +000081 static lldb_private::Disassembler *
Jim Ingham0f063ba2013-03-02 00:26:47 +000082 CreateInstance(const lldb_private::ArchSpec &arch, const char *flavor);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000083
Jim Ingham0f063ba2013-03-02 00:26:47 +000084 DisassemblerLLVMC(const lldb_private::ArchSpec &arch, const char *flavor /* = NULL */);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000085
Sean Callanan95e5c632012-02-17 00:53:45 +000086 virtual
87 ~DisassemblerLLVMC();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000088
Greg Clayton3faf47c2013-03-28 23:42:53 +000089 virtual size_t
Sean Callanan95e5c632012-02-17 00:53:45 +000090 DecodeInstructions (const lldb_private::Address &base_addr,
91 const lldb_private::DataExtractor& data,
Greg Claytonc7bece562013-01-25 18:06:21 +000092 lldb::offset_t data_offset,
93 size_t num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +000094 bool append,
95 bool data_from_file);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000096
Sean Callanan95e5c632012-02-17 00:53:45 +000097 //------------------------------------------------------------------
98 // PluginInterface protocol
99 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000100 virtual lldb_private::ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000101 GetPluginName();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000102
Sean Callanan95e5c632012-02-17 00:53:45 +0000103 virtual uint32_t
104 GetPluginVersion();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000105
Sean Callanan95e5c632012-02-17 00:53:45 +0000106protected:
107 friend class InstructionLLVMC;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000108
Jim Ingham0f063ba2013-03-02 00:26:47 +0000109 virtual bool
110 FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000111
Sean Callanan95e5c632012-02-17 00:53:45 +0000112 bool
113 IsValid()
114 {
Jim Ingham0f063ba2013-03-02 00:26:47 +0000115 return (m_disasm_ap.get() != NULL && m_disasm_ap->IsValid());
Sean Callanan95e5c632012-02-17 00:53:45 +0000116 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000117
Sean Callanan95e5c632012-02-17 00:53:45 +0000118 int OpInfo(uint64_t PC,
119 uint64_t Offset,
120 uint64_t Size,
121 int TagType,
122 void *TagBug);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000123
Sean Callanan95e5c632012-02-17 00:53:45 +0000124 const char *SymbolLookup (uint64_t ReferenceValue,
125 uint64_t *ReferenceType,
126 uint64_t ReferencePC,
127 const char **ReferenceName);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000128
Sean Callanan95e5c632012-02-17 00:53:45 +0000129 static int OpInfoCallback (void *DisInfo,
130 uint64_t PC,
131 uint64_t Offset,
132 uint64_t Size,
133 int TagType,
134 void *TagBug);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000135
Sean Callanan95e5c632012-02-17 00:53:45 +0000136 static const char *SymbolLookupCallback(void *DisInfo,
137 uint64_t ReferenceValue,
138 uint64_t *ReferenceType,
139 uint64_t ReferencePC,
140 const char **ReferenceName);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000141
142 void Lock(InstructionLLVMC *inst,
Greg Claytonba812f42012-05-10 02:52:23 +0000143 const lldb_private::ExecutionContext *exe_ctx)
Sean Callanan95e5c632012-02-17 00:53:45 +0000144 {
145 m_mutex.Lock();
Sean Callanan95e5c632012-02-17 00:53:45 +0000146 m_inst = inst;
Greg Claytonba812f42012-05-10 02:52:23 +0000147 m_exe_ctx = exe_ctx;
Sean Callanan95e5c632012-02-17 00:53:45 +0000148 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000149
Sean Callanan95e5c632012-02-17 00:53:45 +0000150 void Unlock()
151 {
Sean Callanan95e5c632012-02-17 00:53:45 +0000152 m_inst = NULL;
Greg Claytonba812f42012-05-10 02:52:23 +0000153 m_exe_ctx = NULL;
Sean Callanan95e5c632012-02-17 00:53:45 +0000154 m_mutex.Unlock();
155 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000156
Greg Claytonba812f42012-05-10 02:52:23 +0000157 const lldb_private::ExecutionContext *m_exe_ctx;
158 InstructionLLVMC *m_inst;
159 lldb_private::Mutex m_mutex;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000160 bool m_data_from_file;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000161
Greg Clayton7b0992d2013-04-18 22:45:39 +0000162 std::unique_ptr<LLVMCDisassembler> m_disasm_ap;
163 std::unique_ptr<LLVMCDisassembler> m_alternate_disasm_ap;
Sean Callanan95e5c632012-02-17 00:53:45 +0000164};
165
166#endif // liblldb_DisassemblerLLVM_h_