blob: fc1de90286553621e489b8879fda8589ea07eb25 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- DisassemblerLLVM.cpp ------------------------------------*- 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#include "DisassemblerLLVM.h"
11
12#include "llvm-c/EnhancedDisassembly.h"
13
14#include "lldb/Core/Address.h"
15#include "lldb/Core/DataExtractor.h"
16#include "lldb/Core/Disassembler.h"
17#include "lldb/Core/Module.h"
18#include "lldb/Core/PluginManager.h"
19#include "lldb/Core/Stream.h"
20#include "lldb/Core/StreamString.h"
21#include "lldb/Symbol/SymbolContext.h"
22
23#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
26#include "lldb/Target/Target.h"
27
Greg Claytonb01000f2011-01-17 03:46:26 +000028#include <assert.h>
Chris Lattner24943d22010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33
Greg Claytonb1888f22011-03-19 01:12:21 +000034static int
Greg Clayton7bc39082011-03-24 23:53:38 +000035DataExtractorByteReader (uint8_t *byte, uint64_t address, void *arg)
Chris Lattner24943d22010-06-08 16:52:24 +000036{
37 DataExtractor &extractor = *((DataExtractor *)arg);
38
39 if (extractor.ValidOffset(address))
40 {
41 *byte = *(extractor.GetDataStart() + address);
42 return 0;
43 }
44 else
45 {
46 return -1;
47 }
48}
49
50namespace {
51 struct RegisterReaderArg {
52 const lldb::addr_t instructionPointer;
53 const EDDisassemblerRef disassembler;
54
55 RegisterReaderArg(lldb::addr_t ip,
56 EDDisassemblerRef dis) :
57 instructionPointer(ip),
58 disassembler(dis)
59 {
60 }
61 };
62}
63
64static int IPRegisterReader(uint64_t *value, unsigned regID, void* arg)
65{
66 uint64_t instructionPointer = ((RegisterReaderArg*)arg)->instructionPointer;
67 EDDisassemblerRef disassembler = ((RegisterReaderArg*)arg)->disassembler;
68
Greg Claytonb1888f22011-03-19 01:12:21 +000069 if (EDRegisterIsProgramCounter(disassembler, regID)) {
Chris Lattner24943d22010-06-08 16:52:24 +000070 *value = instructionPointer;
71 return 0;
72 }
73
74 return -1;
75}
76
Caroline Ticeaf591802011-04-05 23:22:54 +000077InstructionLLVM::InstructionLLVM (const Address &addr,
78 AddressClass addr_class,
Greg Claytonabe0fed2011-04-18 08:33:37 +000079 EDDisassemblerRef disassembler,
Johnny Chen80ab18e2011-05-12 22:25:53 +000080 llvm::Triple::ArchType arch_type) :
Greg Clayton889fbd02011-03-26 19:14:58 +000081 Instruction (addr, addr_class),
Greg Claytonabe0fed2011-04-18 08:33:37 +000082 m_disassembler (disassembler),
Johnny Chen80ab18e2011-05-12 22:25:53 +000083 m_arch_type (arch_type)
Chris Lattner24943d22010-06-08 16:52:24 +000084{
85}
86
Caroline Ticeaf591802011-04-05 23:22:54 +000087InstructionLLVM::~InstructionLLVM()
Chris Lattner24943d22010-06-08 16:52:24 +000088{
89}
90
91static void
92PadString(Stream *s, const std::string &str, size_t width)
93{
94 int diff = width - str.length();
95
96 if (diff > 0)
97 s->Printf("%s%*.*s", str.c_str(), diff, diff, "");
98 else
99 s->Printf("%s ", str.c_str());
100}
101
102void
Caroline Ticeaf591802011-04-05 23:22:54 +0000103InstructionLLVM::Dump
Chris Lattner24943d22010-06-08 16:52:24 +0000104(
105 Stream *s,
Greg Clayton889fbd02011-03-26 19:14:58 +0000106 uint32_t max_opcode_byte_size,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000107 bool show_address,
Greg Clayton149731c2011-03-25 18:03:16 +0000108 bool show_bytes,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000109 const lldb_private::ExecutionContext* exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000110 bool raw
111)
112{
113 const size_t opcodeColumnWidth = 7;
114 const size_t operandColumnWidth = 25;
115
Greg Clayton5c4c7462010-10-06 03:09:58 +0000116 ExecutionContextScope *exe_scope = NULL;
117 if (exe_ctx)
118 exe_scope = exe_ctx->GetBestExecutionContextScope();
119
Chris Lattner24943d22010-06-08 16:52:24 +0000120 // If we have an address, print it out
Sean Callanan91557b02010-11-10 01:38:28 +0000121 if (GetAddress().IsValid() && show_address)
Greg Clayton70436352010-06-30 23:03:03 +0000122 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000123 if (GetAddress().Dump (s,
124 exe_scope,
125 Address::DumpStyleLoadAddress,
126 Address::DumpStyleModuleWithFileAddress,
127 0))
Greg Clayton70436352010-06-30 23:03:03 +0000128 s->PutCString(": ");
129 }
Chris Lattner24943d22010-06-08 16:52:24 +0000130
131 // If we are supposed to show bytes, "bytes" will be non-NULL.
Greg Clayton149731c2011-03-25 18:03:16 +0000132 if (show_bytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000133 {
Greg Clayton149731c2011-03-25 18:03:16 +0000134 if (m_opcode.GetType() == Opcode::eTypeBytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000135 {
Greg Clayton149731c2011-03-25 18:03:16 +0000136 // x86_64 and i386 are the only ones that use bytes right now so
137 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
138 // plus a space
Greg Clayton889fbd02011-03-26 19:14:58 +0000139 if (max_opcode_byte_size > 0)
140 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
141 else
142 m_opcode.Dump (s, 15 * 3 + 1);
Greg Clayton149731c2011-03-25 18:03:16 +0000143 }
144 else
145 {
146 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
147 // plus two for padding...
Greg Clayton889fbd02011-03-26 19:14:58 +0000148 if (max_opcode_byte_size > 0)
149 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
150 else
151 m_opcode.Dump (s, 12);
Chris Lattner24943d22010-06-08 16:52:24 +0000152 }
153 }
154
Greg Claytonf15996e2011-04-07 22:46:35 +0000155 int numTokens = -1;
156
Johnny Chen80ab18e2011-05-12 22:25:53 +0000157 // FIXME!!!
158 /* Remove the following section of code related to force_raw .... */
159 bool force_raw = m_arch_type == llvm::Triple::arm ||
160 m_arch_type == llvm::Triple::thumb;
Greg Claytonf15996e2011-04-07 22:46:35 +0000161 if (!raw)
Johnny Chen80ab18e2011-05-12 22:25:53 +0000162 raw = force_raw;
163 /* .... when we fix the edis for arm/thumb. */
Greg Claytonabe0fed2011-04-18 08:33:37 +0000164
165 if (!raw)
Greg Claytonf15996e2011-04-07 22:46:35 +0000166 numTokens = EDNumTokens(m_inst);
Chris Lattner24943d22010-06-08 16:52:24 +0000167
168 int currentOpIndex = -1;
169
Greg Claytonf15996e2011-04-07 22:46:35 +0000170 bool printTokenized = false;
171
172 if (numTokens != -1 && !raw)
Sean Callanan8541f2f2010-07-23 02:19:15 +0000173 {
174 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Claytonf15996e2011-04-07 22:46:35 +0000175
Greg Clayton5c4c7462010-10-06 03:09:58 +0000176 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
177 base_addr = GetAddress().GetLoadAddress (exe_ctx->target);
Sean Callanan8541f2f2010-07-23 02:19:15 +0000178 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton5c4c7462010-10-06 03:09:58 +0000179 base_addr = GetAddress().GetFileAddress ();
Greg Claytonf15996e2011-04-07 22:46:35 +0000180
Johnny Chen80ab18e2011-05-12 22:25:53 +0000181 lldb::addr_t PC = base_addr + EDInstByteSize(m_inst);
182
183 // When executing an ARM instruction, PC reads as the address of the
184 // current instruction plus 8. And for Thumb, it is plus 4.
185 if (m_arch_type == llvm::Triple::arm)
186 PC = base_addr + 8;
187 else if (m_arch_type == llvm::Triple::thumb)
188 PC = base_addr + 4;
189
190 RegisterReaderArg rra(PC, m_disassembler);
Johnny Chenc5272bf2011-05-12 18:48:11 +0000191
Chris Lattner24943d22010-06-08 16:52:24 +0000192 printTokenized = true;
193
194 // Handle the opcode column.
195
196 StreamString opcode;
197
198 int tokenIndex = 0;
199
200 EDTokenRef token;
201 const char *tokenStr;
202
Johnny Chenff8fea62011-05-18 22:48:41 +0000203 if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success
204 printTokenized = false;
205 else if (!EDTokenIsOpcode(token))
206 printTokenized = false;
207 else if (EDGetTokenString(&tokenStr, token)) // 0 on success
Chris Lattner24943d22010-06-08 16:52:24 +0000208 printTokenized = false;
209
Johnny Chenff8fea62011-05-18 22:48:41 +0000210 if (printTokenized)
Chris Lattner24943d22010-06-08 16:52:24 +0000211 {
Johnny Chenff8fea62011-05-18 22:48:41 +0000212 // Put the token string into our opcode string
213 opcode.PutCString(tokenStr);
Chris Lattner24943d22010-06-08 16:52:24 +0000214
Johnny Chenff8fea62011-05-18 22:48:41 +0000215 // If anything follows, it probably starts with some whitespace. Skip it.
216 if (++tokenIndex < numTokens)
217 {
218 if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success
219 printTokenized = false;
220 else if (!EDTokenIsWhitespace(token))
221 printTokenized = false;
222 }
223
224 ++tokenIndex;
Chris Lattner24943d22010-06-08 16:52:24 +0000225 }
226
Chris Lattner24943d22010-06-08 16:52:24 +0000227 // Handle the operands and the comment.
Chris Lattner24943d22010-06-08 16:52:24 +0000228 StreamString operands;
229 StreamString comment;
230
231 if (printTokenized)
232 {
233 bool show_token;
234
235 for (; tokenIndex < numTokens; ++tokenIndex)
236 {
237 if (EDGetToken(&token, m_inst, tokenIndex))
238 return;
239
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000240 int operandIndex = EDOperandIndexForToken(token);
Chris Lattner24943d22010-06-08 16:52:24 +0000241
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000242 if (operandIndex >= 0)
243 {
244 if (operandIndex != currentOpIndex)
Chris Lattner24943d22010-06-08 16:52:24 +0000245 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000246 show_token = true;
247
248 currentOpIndex = operandIndex;
249 EDOperandRef operand;
250
251 if (!EDGetOperand(&operand, m_inst, currentOpIndex))
Chris Lattner24943d22010-06-08 16:52:24 +0000252 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000253 if (EDOperandIsMemory(operand))
Chris Lattner24943d22010-06-08 16:52:24 +0000254 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000255 uint64_t operand_value;
256
257 if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, &rra))
Chris Lattner24943d22010-06-08 16:52:24 +0000258 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000259 if (EDInstIsBranch(m_inst))
Chris Lattner24943d22010-06-08 16:52:24 +0000260 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000261 operands.Printf("0x%llx ", operand_value);
262 show_token = false;
263 }
264 else
265 {
266 // Put the address value into the comment
267 comment.Printf("0x%llx ", operand_value);
268 }
Chris Lattner24943d22010-06-08 16:52:24 +0000269
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000270 lldb_private::Address so_addr;
271 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
272 {
273 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
274 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
275 }
276 else
277 {
278 Module *module = GetAddress().GetModule();
279 if (module)
Chris Lattner24943d22010-06-08 16:52:24 +0000280 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000281 if (module->ResolveFileAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000282 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Greg Clayton70436352010-06-30 23:03:03 +0000283 }
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000284 }
285 } // EDEvaluateOperand
286 } // EDOperandIsMemory
287 } // EDGetOperand
288 } // operandIndex != currentOpIndex
289 } // operandIndex >= 0
Chris Lattner24943d22010-06-08 16:52:24 +0000290
291 if (show_token)
292 {
293 if(EDGetTokenString(&tokenStr, token))
294 {
295 printTokenized = false;
296 break;
297 }
298
299 operands.PutCString(tokenStr);
300 }
301 } // for (tokenIndex)
302
303 if (printTokenized)
304 {
305 if (operands.GetString().empty())
306 {
307 s->PutCString(opcode.GetString().c_str());
308 }
309 else
310 {
311 PadString(s, opcode.GetString(), opcodeColumnWidth);
312
313 if (comment.GetString().empty())
314 {
315 s->PutCString(operands.GetString().c_str());
316 }
317 else
318 {
319 PadString(s, operands.GetString(), operandColumnWidth);
320
321 s->PutCString("; ");
322 s->PutCString(comment.GetString().c_str());
323 } // else (comment.GetString().empty())
324 } // else (operands.GetString().empty())
325 } // printTokenized
326 } // for (tokenIndex)
327 } // numTokens != -1
328
329 if (!printTokenized)
330 {
331 const char *str;
332
333 if (EDGetInstString(&str, m_inst))
334 return;
335 else
Greg Claytonf15996e2011-04-07 22:46:35 +0000336 s->Write(str, strlen(str) - 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000337 }
338}
339
340bool
Caroline Ticeaf591802011-04-05 23:22:54 +0000341InstructionLLVM::DoesBranch() const
Chris Lattner24943d22010-06-08 16:52:24 +0000342{
343 return EDInstIsBranch(m_inst);
344}
345
346size_t
Caroline Ticeaf591802011-04-05 23:22:54 +0000347InstructionLLVM::Decode (const Disassembler &disassembler,
348 const lldb_private::DataExtractor &data,
349 uint32_t data_offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000350{
351 if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data)))
Greg Clayton7bc39082011-03-24 23:53:38 +0000352 {
353 const int byte_size = EDInstByteSize(m_inst);
354 uint32_t offset = data_offset;
355 // Make a copy of the opcode in m_opcode
356 switch (disassembler.GetArchitecture().GetMachine())
357 {
358 case llvm::Triple::x86:
359 case llvm::Triple::x86_64:
360 m_opcode.SetOpcodeBytes (data.PeekData (data_offset, byte_size), byte_size);
361 break;
362
363 case llvm::Triple::arm:
Greg Clayton7bc39082011-03-24 23:53:38 +0000364 case llvm::Triple::thumb:
Greg Clayton149731c2011-03-25 18:03:16 +0000365 switch (byte_size)
366 {
367 case 2:
368 m_opcode.SetOpcode16 (data.GetU16 (&offset));
369 break;
370
371 case 4:
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000372 {
373 if (GetAddressClass() == eAddressClassCodeAlternateISA)
374 {
375 // If it is a 32-bit THUMB instruction, we need to swap the upper & lower halves.
376 uint32_t orig_bytes = data.GetU32 (&offset);
377 uint16_t upper_bits = (orig_bytes >> 16) & ((1u << 16) - 1);
378 uint16_t lower_bits = orig_bytes & ((1u << 16) - 1);
379 uint32_t swapped = (lower_bits << 16) | upper_bits;
380 m_opcode.SetOpcode32 (swapped);
381 }
382 else
383 m_opcode.SetOpcode32 (data.GetU32 (&offset));
384 }
Greg Clayton149731c2011-03-25 18:03:16 +0000385 break;
386
387 default:
388 assert (!"Invalid ARM opcode size");
389 break;
390 }
Greg Clayton7bc39082011-03-24 23:53:38 +0000391 break;
392
393 default:
394 assert (!"This shouldn't happen since we control the architecture we allow DisassemblerLLVM to be created for");
395 break;
396 }
397 return byte_size;
398 }
Chris Lattner24943d22010-06-08 16:52:24 +0000399 else
400 return 0;
401}
402
Chris Lattner24943d22010-06-08 16:52:24 +0000403static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000404SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000405{
Greg Clayton940b1032011-02-23 00:35:02 +0000406 switch (arch.GetMachine ())
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000407 {
Greg Clayton940b1032011-02-23 00:35:02 +0000408 case llvm::Triple::x86:
409 case llvm::Triple::x86_64:
Chris Lattner24943d22010-06-08 16:52:24 +0000410 return kEDAssemblySyntaxX86ATT;
Sean Callanand151c8a2011-03-09 01:02:51 +0000411 case llvm::Triple::arm:
Greg Clayton889fbd02011-03-26 19:14:58 +0000412 case llvm::Triple::thumb:
Sean Callanand151c8a2011-03-09 01:02:51 +0000413 return kEDAssemblySyntaxARMUAL;
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000414 default:
415 break;
416 }
Greg Claytoncf015052010-06-11 03:25:34 +0000417 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000418}
419
420Disassembler *
421DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
422{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000423 std::auto_ptr<DisassemblerLLVM> disasm_ap (new DisassemblerLLVM(arch));
424
Caroline Tice080bf612011-04-05 18:46:00 +0000425 if (disasm_ap.get() && disasm_ap->IsValid())
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000426 return disasm_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000427
Greg Claytoncf015052010-06-11 03:25:34 +0000428 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000429}
430
431DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
Greg Claytonb01000f2011-01-17 03:46:26 +0000432 Disassembler (arch),
Greg Claytonb1888f22011-03-19 01:12:21 +0000433 m_disassembler (NULL),
434 m_disassembler_thumb (NULL) // For ARM only
Chris Lattner24943d22010-06-08 16:52:24 +0000435{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000436 const std::string &arch_triple = arch.GetTriple().str();
437 if (!arch_triple.empty())
Greg Claytoncf015052010-06-11 03:25:34 +0000438 {
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000439 if (EDGetDisassembler(&m_disassembler, arch_triple.c_str(), SyntaxForArchSpec (arch)))
440 m_disassembler = NULL;
Greg Claytonb1888f22011-03-19 01:12:21 +0000441 llvm::Triple::ArchType llvm_arch = arch.GetTriple().getArch();
Greg Clayton889fbd02011-03-26 19:14:58 +0000442 // Don't have the lldb::Triple::thumb architecture here. If someone specifies
443 // "thumb" as the architecture, we want a thumb only disassembler. But if any
444 // architecture starting with "arm" if specified, we want to auto detect the
445 // arm/thumb code automatically using the AddressClass from section offset
446 // addresses.
Greg Claytonb1888f22011-03-19 01:12:21 +0000447 if (llvm_arch == llvm::Triple::arm)
448 {
449 if (EDGetDisassembler(&m_disassembler_thumb, "thumb-apple-darwin", kEDAssemblySyntaxARMUAL))
450 m_disassembler_thumb = NULL;
451 }
Greg Claytoncf015052010-06-11 03:25:34 +0000452 }
Chris Lattner24943d22010-06-08 16:52:24 +0000453}
454
455DisassemblerLLVM::~DisassemblerLLVM()
456{
457}
458
459size_t
Greg Clayton70436352010-06-30 23:03:03 +0000460DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000461(
Greg Clayton5c4c7462010-10-06 03:09:58 +0000462 const Address &base_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000463 const DataExtractor& data,
464 uint32_t data_offset,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000465 uint32_t num_instructions,
466 bool append
Chris Lattner24943d22010-06-08 16:52:24 +0000467)
468{
Greg Claytonb01000f2011-01-17 03:46:26 +0000469 if (m_disassembler == NULL)
470 return 0;
471
Chris Lattner24943d22010-06-08 16:52:24 +0000472 size_t total_inst_byte_size = 0;
473
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000474 if (!append)
475 m_instruction_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000476
477 while (data.ValidOffset(data_offset) && num_instructions)
478 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000479 Address inst_addr (base_addr);
480 inst_addr.Slide(data_offset);
Greg Claytonb1888f22011-03-19 01:12:21 +0000481
482 bool use_thumb = false;
483 // If we have a thumb disassembler, then we have an ARM architecture
484 // so we need to check what the instruction address class is to make
485 // sure we shouldn't be disassembling as thumb...
Greg Clayton889fbd02011-03-26 19:14:58 +0000486 AddressClass inst_address_class = eAddressClassInvalid;
Greg Claytonb1888f22011-03-19 01:12:21 +0000487 if (m_disassembler_thumb)
488 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000489 inst_address_class = inst_addr.GetAddressClass ();
490 if (inst_address_class == eAddressClassCodeAlternateISA)
Greg Claytonb1888f22011-03-19 01:12:21 +0000491 use_thumb = true;
492 }
Johnny Chen80ab18e2011-05-12 22:25:53 +0000493
Greg Clayton7bc39082011-03-24 23:53:38 +0000494 InstructionSP inst_sp (new InstructionLLVM (inst_addr,
Greg Clayton889fbd02011-03-26 19:14:58 +0000495 inst_address_class,
Greg Claytonabe0fed2011-04-18 08:33:37 +0000496 use_thumb ? m_disassembler_thumb : m_disassembler,
Johnny Chen1608c872011-05-18 18:22:16 +0000497 use_thumb ? llvm::Triple::thumb : m_arch.GetMachine()));
Chris Lattner24943d22010-06-08 16:52:24 +0000498
Greg Clayton889fbd02011-03-26 19:14:58 +0000499 size_t inst_byte_size = inst_sp->Decode (*this, data, data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000500
501 if (inst_byte_size == 0)
502 break;
503
Greg Clayton5c4c7462010-10-06 03:09:58 +0000504 m_instruction_list.Append (inst_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000505
506 total_inst_byte_size += inst_byte_size;
507 data_offset += inst_byte_size;
508 num_instructions--;
509 }
510
511 return total_inst_byte_size;
512}
513
514void
515DisassemblerLLVM::Initialize()
516{
517 PluginManager::RegisterPlugin (GetPluginNameStatic(),
518 GetPluginDescriptionStatic(),
519 CreateInstance);
520}
521
522void
523DisassemblerLLVM::Terminate()
524{
525 PluginManager::UnregisterPlugin (CreateInstance);
526}
527
528
529const char *
530DisassemblerLLVM::GetPluginNameStatic()
531{
Greg Clayton149731c2011-03-25 18:03:16 +0000532 return "llvm";
Chris Lattner24943d22010-06-08 16:52:24 +0000533}
534
535const char *
536DisassemblerLLVM::GetPluginDescriptionStatic()
537{
Greg Clayton149731c2011-03-25 18:03:16 +0000538 return "Disassembler that uses LLVM opcode tables to disassemble i386, x86_64 and ARM.";
Chris Lattner24943d22010-06-08 16:52:24 +0000539}
540
541//------------------------------------------------------------------
542// PluginInterface protocol
543//------------------------------------------------------------------
544const char *
545DisassemblerLLVM::GetPluginName()
546{
547 return "DisassemblerLLVM";
548}
549
550const char *
551DisassemblerLLVM::GetShortPluginName()
552{
553 return GetPluginNameStatic();
554}
555
556uint32_t
557DisassemblerLLVM::GetPluginVersion()
558{
559 return 1;
560}
561