blob: 90e6413795e6650677b3ea1c94b0e207be989973 [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
Johnny Chen51ff2482011-05-19 01:05:37 +0000102#include "llvm/ADT/StringRef.h"
103static void
104StripSpaces(llvm::StringRef &Str)
105{
106 while (!Str.empty() && isspace(Str[0]))
107 Str = Str.substr(1);
108 while (!Str.empty() && isspace(Str.back()))
109 Str = Str.substr(0, Str.size()-1);
110}
111
Chris Lattner24943d22010-06-08 16:52:24 +0000112void
Caroline Ticeaf591802011-04-05 23:22:54 +0000113InstructionLLVM::Dump
Chris Lattner24943d22010-06-08 16:52:24 +0000114(
115 Stream *s,
Greg Clayton889fbd02011-03-26 19:14:58 +0000116 uint32_t max_opcode_byte_size,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000117 bool show_address,
Greg Clayton149731c2011-03-25 18:03:16 +0000118 bool show_bytes,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000119 const lldb_private::ExecutionContext* exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000120 bool raw
121)
122{
123 const size_t opcodeColumnWidth = 7;
124 const size_t operandColumnWidth = 25;
125
Greg Clayton5c4c7462010-10-06 03:09:58 +0000126 ExecutionContextScope *exe_scope = NULL;
127 if (exe_ctx)
128 exe_scope = exe_ctx->GetBestExecutionContextScope();
129
Chris Lattner24943d22010-06-08 16:52:24 +0000130 // If we have an address, print it out
Sean Callanan91557b02010-11-10 01:38:28 +0000131 if (GetAddress().IsValid() && show_address)
Greg Clayton70436352010-06-30 23:03:03 +0000132 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000133 if (GetAddress().Dump (s,
134 exe_scope,
135 Address::DumpStyleLoadAddress,
136 Address::DumpStyleModuleWithFileAddress,
137 0))
Greg Clayton70436352010-06-30 23:03:03 +0000138 s->PutCString(": ");
139 }
Chris Lattner24943d22010-06-08 16:52:24 +0000140
141 // If we are supposed to show bytes, "bytes" will be non-NULL.
Greg Clayton149731c2011-03-25 18:03:16 +0000142 if (show_bytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000143 {
Greg Clayton149731c2011-03-25 18:03:16 +0000144 if (m_opcode.GetType() == Opcode::eTypeBytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000145 {
Greg Clayton149731c2011-03-25 18:03:16 +0000146 // x86_64 and i386 are the only ones that use bytes right now so
147 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
148 // plus a space
Greg Clayton889fbd02011-03-26 19:14:58 +0000149 if (max_opcode_byte_size > 0)
150 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
151 else
152 m_opcode.Dump (s, 15 * 3 + 1);
Greg Clayton149731c2011-03-25 18:03:16 +0000153 }
154 else
155 {
156 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
157 // plus two for padding...
Greg Clayton889fbd02011-03-26 19:14:58 +0000158 if (max_opcode_byte_size > 0)
159 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
160 else
161 m_opcode.Dump (s, 12);
Chris Lattner24943d22010-06-08 16:52:24 +0000162 }
163 }
164
Greg Claytonf15996e2011-04-07 22:46:35 +0000165 int numTokens = -1;
166
Johnny Chen80ab18e2011-05-12 22:25:53 +0000167 // FIXME!!!
168 /* Remove the following section of code related to force_raw .... */
169 bool force_raw = m_arch_type == llvm::Triple::arm ||
170 m_arch_type == llvm::Triple::thumb;
Greg Claytonf15996e2011-04-07 22:46:35 +0000171 if (!raw)
Johnny Chen80ab18e2011-05-12 22:25:53 +0000172 raw = force_raw;
173 /* .... when we fix the edis for arm/thumb. */
Greg Claytonabe0fed2011-04-18 08:33:37 +0000174
175 if (!raw)
Greg Claytonf15996e2011-04-07 22:46:35 +0000176 numTokens = EDNumTokens(m_inst);
Chris Lattner24943d22010-06-08 16:52:24 +0000177
178 int currentOpIndex = -1;
179
Greg Claytonf15996e2011-04-07 22:46:35 +0000180 bool printTokenized = false;
181
182 if (numTokens != -1 && !raw)
Sean Callanan8541f2f2010-07-23 02:19:15 +0000183 {
184 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Claytonf15996e2011-04-07 22:46:35 +0000185
Greg Clayton5c4c7462010-10-06 03:09:58 +0000186 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
187 base_addr = GetAddress().GetLoadAddress (exe_ctx->target);
Sean Callanan8541f2f2010-07-23 02:19:15 +0000188 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton5c4c7462010-10-06 03:09:58 +0000189 base_addr = GetAddress().GetFileAddress ();
Greg Claytonf15996e2011-04-07 22:46:35 +0000190
Johnny Chen80ab18e2011-05-12 22:25:53 +0000191 lldb::addr_t PC = base_addr + EDInstByteSize(m_inst);
192
193 // When executing an ARM instruction, PC reads as the address of the
194 // current instruction plus 8. And for Thumb, it is plus 4.
195 if (m_arch_type == llvm::Triple::arm)
196 PC = base_addr + 8;
197 else if (m_arch_type == llvm::Triple::thumb)
198 PC = base_addr + 4;
199
200 RegisterReaderArg rra(PC, m_disassembler);
Johnny Chenc5272bf2011-05-12 18:48:11 +0000201
Chris Lattner24943d22010-06-08 16:52:24 +0000202 printTokenized = true;
203
204 // Handle the opcode column.
205
206 StreamString opcode;
207
208 int tokenIndex = 0;
209
210 EDTokenRef token;
211 const char *tokenStr;
212
Johnny Chenff8fea62011-05-18 22:48:41 +0000213 if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success
214 printTokenized = false;
215 else if (!EDTokenIsOpcode(token))
216 printTokenized = false;
217 else if (EDGetTokenString(&tokenStr, token)) // 0 on success
Chris Lattner24943d22010-06-08 16:52:24 +0000218 printTokenized = false;
219
Johnny Chenff8fea62011-05-18 22:48:41 +0000220 if (printTokenized)
Chris Lattner24943d22010-06-08 16:52:24 +0000221 {
Johnny Chenff8fea62011-05-18 22:48:41 +0000222 // Put the token string into our opcode string
223 opcode.PutCString(tokenStr);
Chris Lattner24943d22010-06-08 16:52:24 +0000224
Johnny Chenff8fea62011-05-18 22:48:41 +0000225 // If anything follows, it probably starts with some whitespace. Skip it.
226 if (++tokenIndex < numTokens)
227 {
228 if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success
229 printTokenized = false;
230 else if (!EDTokenIsWhitespace(token))
231 printTokenized = false;
232 }
233
234 ++tokenIndex;
Chris Lattner24943d22010-06-08 16:52:24 +0000235 }
236
Chris Lattner24943d22010-06-08 16:52:24 +0000237 // Handle the operands and the comment.
Chris Lattner24943d22010-06-08 16:52:24 +0000238 StreamString operands;
239 StreamString comment;
240
241 if (printTokenized)
242 {
Johnny Chen51ff2482011-05-19 01:05:37 +0000243 bool show_token = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000244
245 for (; tokenIndex < numTokens; ++tokenIndex)
246 {
247 if (EDGetToken(&token, m_inst, tokenIndex))
248 return;
249
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000250 int operandIndex = EDOperandIndexForToken(token);
Chris Lattner24943d22010-06-08 16:52:24 +0000251
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000252 if (operandIndex >= 0)
253 {
254 if (operandIndex != currentOpIndex)
Chris Lattner24943d22010-06-08 16:52:24 +0000255 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000256 show_token = true;
257
258 currentOpIndex = operandIndex;
259 EDOperandRef operand;
260
261 if (!EDGetOperand(&operand, m_inst, currentOpIndex))
Chris Lattner24943d22010-06-08 16:52:24 +0000262 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000263 if (EDOperandIsMemory(operand))
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000265 uint64_t operand_value;
266
267 if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, &rra))
Chris Lattner24943d22010-06-08 16:52:24 +0000268 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000269 if (EDInstIsBranch(m_inst))
Chris Lattner24943d22010-06-08 16:52:24 +0000270 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000271 operands.Printf("0x%llx ", operand_value);
272 show_token = false;
273 }
274 else
275 {
276 // Put the address value into the comment
277 comment.Printf("0x%llx ", operand_value);
278 }
Chris Lattner24943d22010-06-08 16:52:24 +0000279
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000280 lldb_private::Address so_addr;
281 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
282 {
283 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
284 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
285 }
286 else
287 {
288 Module *module = GetAddress().GetModule();
289 if (module)
Chris Lattner24943d22010-06-08 16:52:24 +0000290 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000291 if (module->ResolveFileAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000292 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Greg Clayton70436352010-06-30 23:03:03 +0000293 }
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000294 }
295 } // EDEvaluateOperand
296 } // EDOperandIsMemory
297 } // EDGetOperand
298 } // operandIndex != currentOpIndex
299 } // operandIndex >= 0
Chris Lattner24943d22010-06-08 16:52:24 +0000300
301 if (show_token)
302 {
303 if(EDGetTokenString(&tokenStr, token))
304 {
305 printTokenized = false;
306 break;
307 }
308
309 operands.PutCString(tokenStr);
310 }
311 } // for (tokenIndex)
312
Johnny Chende5cc8c2011-05-20 17:27:37 +0000313 // FIXME!!!
314 // Workaround for llvm::tB's operands not properly parsed by ARMAsmParser.
315 if (m_arch_type == llvm::Triple::thumb && opcode.GetString() == "b") {
316 const char *inst_str;
317 char *pos = NULL;
318 if (EDGetInstString(&inst_str, m_inst) == 0 && (pos = strstr(inst_str, "#")) != NULL) {
319 uint64_t operand_value = PC + atoi(++pos);
320 operands.Printf("0x%llx ", operand_value);
321
322 lldb_private::Address so_addr;
323 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty()) {
324 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
325 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
326 } else {
327 Module *module = GetAddress().GetModule();
328 if (module) {
329 if (module->ResolveFileAddress (operand_value, so_addr))
330 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
331 }
332 }
333 }
334 }
335 // END of workaround.
336
Johnny Chen51ff2482011-05-19 01:05:37 +0000337 // If both operands and comment are empty, we will just print out
338 // the raw disassembly.
339 if (operands.GetString().empty() && comment.GetString().empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000340 {
Johnny Chen51ff2482011-05-19 01:05:37 +0000341 const char *str;
342
343 if (EDGetInstString(&str, m_inst))
344 return;
345 llvm::StringRef raw_disasm(str);
346 StripSpaces(raw_disasm);
347 s->PutCString(raw_disasm.str().c_str());
348 }
349 else
350 {
351 PadString(s, opcode.GetString(), opcodeColumnWidth);
352
353 if (comment.GetString().empty())
354 s->PutCString(operands.GetString().c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000355 else
356 {
Johnny Chen51ff2482011-05-19 01:05:37 +0000357 PadString(s, operands.GetString(), operandColumnWidth);
Chris Lattner24943d22010-06-08 16:52:24 +0000358
Johnny Chen51ff2482011-05-19 01:05:37 +0000359 s->PutCString("; ");
360 s->PutCString(comment.GetString().c_str());
361 } // else (comment.GetString().empty())
362 } // else (operands.GetString().empty() && comment.GetString().empty())
363 } // printTokenized
Chris Lattner24943d22010-06-08 16:52:24 +0000364 } // numTokens != -1
365
366 if (!printTokenized)
367 {
368 const char *str;
369
Johnny Chen51ff2482011-05-19 01:05:37 +0000370 if (EDGetInstString(&str, m_inst)) // 0 on success
Chris Lattner24943d22010-06-08 16:52:24 +0000371 return;
Johnny Chen51ff2482011-05-19 01:05:37 +0000372 s->Write(str, strlen(str) - 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000373 }
374}
375
376bool
Caroline Ticeaf591802011-04-05 23:22:54 +0000377InstructionLLVM::DoesBranch() const
Chris Lattner24943d22010-06-08 16:52:24 +0000378{
379 return EDInstIsBranch(m_inst);
380}
381
382size_t
Caroline Ticeaf591802011-04-05 23:22:54 +0000383InstructionLLVM::Decode (const Disassembler &disassembler,
384 const lldb_private::DataExtractor &data,
385 uint32_t data_offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000386{
387 if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data)))
Greg Clayton7bc39082011-03-24 23:53:38 +0000388 {
389 const int byte_size = EDInstByteSize(m_inst);
390 uint32_t offset = data_offset;
391 // Make a copy of the opcode in m_opcode
392 switch (disassembler.GetArchitecture().GetMachine())
393 {
394 case llvm::Triple::x86:
395 case llvm::Triple::x86_64:
396 m_opcode.SetOpcodeBytes (data.PeekData (data_offset, byte_size), byte_size);
397 break;
398
399 case llvm::Triple::arm:
Greg Clayton7bc39082011-03-24 23:53:38 +0000400 case llvm::Triple::thumb:
Greg Clayton149731c2011-03-25 18:03:16 +0000401 switch (byte_size)
402 {
403 case 2:
404 m_opcode.SetOpcode16 (data.GetU16 (&offset));
405 break;
406
407 case 4:
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000408 {
409 if (GetAddressClass() == eAddressClassCodeAlternateISA)
410 {
411 // If it is a 32-bit THUMB instruction, we need to swap the upper & lower halves.
412 uint32_t orig_bytes = data.GetU32 (&offset);
413 uint16_t upper_bits = (orig_bytes >> 16) & ((1u << 16) - 1);
414 uint16_t lower_bits = orig_bytes & ((1u << 16) - 1);
415 uint32_t swapped = (lower_bits << 16) | upper_bits;
416 m_opcode.SetOpcode32 (swapped);
417 }
418 else
419 m_opcode.SetOpcode32 (data.GetU32 (&offset));
420 }
Greg Clayton149731c2011-03-25 18:03:16 +0000421 break;
422
423 default:
424 assert (!"Invalid ARM opcode size");
425 break;
426 }
Greg Clayton7bc39082011-03-24 23:53:38 +0000427 break;
428
429 default:
430 assert (!"This shouldn't happen since we control the architecture we allow DisassemblerLLVM to be created for");
431 break;
432 }
433 return byte_size;
434 }
Chris Lattner24943d22010-06-08 16:52:24 +0000435 else
436 return 0;
437}
438
Chris Lattner24943d22010-06-08 16:52:24 +0000439static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000440SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000441{
Greg Clayton940b1032011-02-23 00:35:02 +0000442 switch (arch.GetMachine ())
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000443 {
Greg Clayton940b1032011-02-23 00:35:02 +0000444 case llvm::Triple::x86:
445 case llvm::Triple::x86_64:
Chris Lattner24943d22010-06-08 16:52:24 +0000446 return kEDAssemblySyntaxX86ATT;
Sean Callanand151c8a2011-03-09 01:02:51 +0000447 case llvm::Triple::arm:
Greg Clayton889fbd02011-03-26 19:14:58 +0000448 case llvm::Triple::thumb:
Sean Callanand151c8a2011-03-09 01:02:51 +0000449 return kEDAssemblySyntaxARMUAL;
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000450 default:
451 break;
452 }
Greg Claytoncf015052010-06-11 03:25:34 +0000453 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000454}
455
456Disassembler *
457DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
458{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000459 std::auto_ptr<DisassemblerLLVM> disasm_ap (new DisassemblerLLVM(arch));
460
Caroline Tice080bf612011-04-05 18:46:00 +0000461 if (disasm_ap.get() && disasm_ap->IsValid())
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000462 return disasm_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000463
Greg Claytoncf015052010-06-11 03:25:34 +0000464 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000465}
466
467DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
Greg Claytonb01000f2011-01-17 03:46:26 +0000468 Disassembler (arch),
Greg Claytonb1888f22011-03-19 01:12:21 +0000469 m_disassembler (NULL),
470 m_disassembler_thumb (NULL) // For ARM only
Chris Lattner24943d22010-06-08 16:52:24 +0000471{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000472 const std::string &arch_triple = arch.GetTriple().str();
473 if (!arch_triple.empty())
Greg Claytoncf015052010-06-11 03:25:34 +0000474 {
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000475 if (EDGetDisassembler(&m_disassembler, arch_triple.c_str(), SyntaxForArchSpec (arch)))
476 m_disassembler = NULL;
Greg Claytonb1888f22011-03-19 01:12:21 +0000477 llvm::Triple::ArchType llvm_arch = arch.GetTriple().getArch();
Greg Clayton889fbd02011-03-26 19:14:58 +0000478 // Don't have the lldb::Triple::thumb architecture here. If someone specifies
479 // "thumb" as the architecture, we want a thumb only disassembler. But if any
480 // architecture starting with "arm" if specified, we want to auto detect the
481 // arm/thumb code automatically using the AddressClass from section offset
482 // addresses.
Greg Claytonb1888f22011-03-19 01:12:21 +0000483 if (llvm_arch == llvm::Triple::arm)
484 {
485 if (EDGetDisassembler(&m_disassembler_thumb, "thumb-apple-darwin", kEDAssemblySyntaxARMUAL))
486 m_disassembler_thumb = NULL;
487 }
Greg Claytoncf015052010-06-11 03:25:34 +0000488 }
Chris Lattner24943d22010-06-08 16:52:24 +0000489}
490
491DisassemblerLLVM::~DisassemblerLLVM()
492{
493}
494
495size_t
Greg Clayton70436352010-06-30 23:03:03 +0000496DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000497(
Greg Clayton5c4c7462010-10-06 03:09:58 +0000498 const Address &base_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000499 const DataExtractor& data,
500 uint32_t data_offset,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000501 uint32_t num_instructions,
502 bool append
Chris Lattner24943d22010-06-08 16:52:24 +0000503)
504{
Greg Claytonb01000f2011-01-17 03:46:26 +0000505 if (m_disassembler == NULL)
506 return 0;
507
Chris Lattner24943d22010-06-08 16:52:24 +0000508 size_t total_inst_byte_size = 0;
509
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000510 if (!append)
511 m_instruction_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000512
513 while (data.ValidOffset(data_offset) && num_instructions)
514 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000515 Address inst_addr (base_addr);
516 inst_addr.Slide(data_offset);
Greg Claytonb1888f22011-03-19 01:12:21 +0000517
518 bool use_thumb = false;
519 // If we have a thumb disassembler, then we have an ARM architecture
520 // so we need to check what the instruction address class is to make
521 // sure we shouldn't be disassembling as thumb...
Greg Clayton889fbd02011-03-26 19:14:58 +0000522 AddressClass inst_address_class = eAddressClassInvalid;
Greg Claytonb1888f22011-03-19 01:12:21 +0000523 if (m_disassembler_thumb)
524 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000525 inst_address_class = inst_addr.GetAddressClass ();
526 if (inst_address_class == eAddressClassCodeAlternateISA)
Greg Claytonb1888f22011-03-19 01:12:21 +0000527 use_thumb = true;
528 }
Johnny Chen80ab18e2011-05-12 22:25:53 +0000529
Greg Clayton7bc39082011-03-24 23:53:38 +0000530 InstructionSP inst_sp (new InstructionLLVM (inst_addr,
Greg Clayton889fbd02011-03-26 19:14:58 +0000531 inst_address_class,
Greg Claytonabe0fed2011-04-18 08:33:37 +0000532 use_thumb ? m_disassembler_thumb : m_disassembler,
Johnny Chen1608c872011-05-18 18:22:16 +0000533 use_thumb ? llvm::Triple::thumb : m_arch.GetMachine()));
Chris Lattner24943d22010-06-08 16:52:24 +0000534
Greg Clayton889fbd02011-03-26 19:14:58 +0000535 size_t inst_byte_size = inst_sp->Decode (*this, data, data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000536
537 if (inst_byte_size == 0)
538 break;
539
Greg Clayton5c4c7462010-10-06 03:09:58 +0000540 m_instruction_list.Append (inst_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000541
542 total_inst_byte_size += inst_byte_size;
543 data_offset += inst_byte_size;
544 num_instructions--;
545 }
546
547 return total_inst_byte_size;
548}
549
550void
551DisassemblerLLVM::Initialize()
552{
553 PluginManager::RegisterPlugin (GetPluginNameStatic(),
554 GetPluginDescriptionStatic(),
555 CreateInstance);
556}
557
558void
559DisassemblerLLVM::Terminate()
560{
561 PluginManager::UnregisterPlugin (CreateInstance);
562}
563
564
565const char *
566DisassemblerLLVM::GetPluginNameStatic()
567{
Greg Clayton149731c2011-03-25 18:03:16 +0000568 return "llvm";
Chris Lattner24943d22010-06-08 16:52:24 +0000569}
570
571const char *
572DisassemblerLLVM::GetPluginDescriptionStatic()
573{
Greg Clayton149731c2011-03-25 18:03:16 +0000574 return "Disassembler that uses LLVM opcode tables to disassemble i386, x86_64 and ARM.";
Chris Lattner24943d22010-06-08 16:52:24 +0000575}
576
577//------------------------------------------------------------------
578// PluginInterface protocol
579//------------------------------------------------------------------
580const char *
581DisassemblerLLVM::GetPluginName()
582{
583 return "DisassemblerLLVM";
584}
585
586const char *
587DisassemblerLLVM::GetShortPluginName()
588{
589 return GetPluginNameStatic();
590}
591
592uint32_t
593DisassemblerLLVM::GetPluginVersion()
594{
595 return 1;
596}
597