blob: 4b5d1dca9759d6482b9799662386d6cfa6c436a8 [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}
Johnny Chen84d42e82011-05-21 00:55:57 +0000111static void
Johnny Chend17f8012011-05-23 18:00:40 +0000112Align(Stream *s, const char *str, size_t opcodeColWidth, size_t operandColWidth)
Johnny Chen84d42e82011-05-21 00:55:57 +0000113{
114 llvm::StringRef raw_disasm(str);
115 StripSpaces(raw_disasm);
Johnny Chend17f8012011-05-23 18:00:40 +0000116 // Split the raw disassembly into opcode and operands.
117 std::pair<llvm::StringRef, llvm::StringRef> p = raw_disasm.split('\t');
118 PadString(s, p.first, opcodeColWidth);
119 if (!p.second.empty())
120 PadString(s, p.second, operandColWidth);
Johnny Chen84d42e82011-05-21 00:55:57 +0000121}
Johnny Chen51ff2482011-05-19 01:05:37 +0000122
Chris Lattner24943d22010-06-08 16:52:24 +0000123void
Caroline Ticeaf591802011-04-05 23:22:54 +0000124InstructionLLVM::Dump
Chris Lattner24943d22010-06-08 16:52:24 +0000125(
126 Stream *s,
Greg Clayton889fbd02011-03-26 19:14:58 +0000127 uint32_t max_opcode_byte_size,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000128 bool show_address,
Greg Clayton149731c2011-03-25 18:03:16 +0000129 bool show_bytes,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000130 const lldb_private::ExecutionContext* exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000131 bool raw
132)
133{
134 const size_t opcodeColumnWidth = 7;
135 const size_t operandColumnWidth = 25;
136
Greg Clayton5c4c7462010-10-06 03:09:58 +0000137 ExecutionContextScope *exe_scope = NULL;
138 if (exe_ctx)
139 exe_scope = exe_ctx->GetBestExecutionContextScope();
140
Chris Lattner24943d22010-06-08 16:52:24 +0000141 // If we have an address, print it out
Sean Callanan91557b02010-11-10 01:38:28 +0000142 if (GetAddress().IsValid() && show_address)
Greg Clayton70436352010-06-30 23:03:03 +0000143 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000144 if (GetAddress().Dump (s,
145 exe_scope,
146 Address::DumpStyleLoadAddress,
147 Address::DumpStyleModuleWithFileAddress,
148 0))
Greg Clayton70436352010-06-30 23:03:03 +0000149 s->PutCString(": ");
150 }
Chris Lattner24943d22010-06-08 16:52:24 +0000151
152 // If we are supposed to show bytes, "bytes" will be non-NULL.
Greg Clayton149731c2011-03-25 18:03:16 +0000153 if (show_bytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000154 {
Greg Clayton149731c2011-03-25 18:03:16 +0000155 if (m_opcode.GetType() == Opcode::eTypeBytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000156 {
Greg Clayton149731c2011-03-25 18:03:16 +0000157 // x86_64 and i386 are the only ones that use bytes right now so
158 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
159 // plus a space
Greg Clayton889fbd02011-03-26 19:14:58 +0000160 if (max_opcode_byte_size > 0)
161 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
162 else
163 m_opcode.Dump (s, 15 * 3 + 1);
Greg Clayton149731c2011-03-25 18:03:16 +0000164 }
165 else
166 {
167 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
168 // plus two for padding...
Greg Clayton889fbd02011-03-26 19:14:58 +0000169 if (max_opcode_byte_size > 0)
170 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
171 else
172 m_opcode.Dump (s, 12);
Chris Lattner24943d22010-06-08 16:52:24 +0000173 }
174 }
175
Greg Claytonf15996e2011-04-07 22:46:35 +0000176 int numTokens = -1;
177
Johnny Chen80ab18e2011-05-12 22:25:53 +0000178 // FIXME!!!
179 /* Remove the following section of code related to force_raw .... */
Johnny Chend17f8012011-05-23 18:00:40 +0000180 /*
Johnny Chen80ab18e2011-05-12 22:25:53 +0000181 bool force_raw = m_arch_type == llvm::Triple::arm ||
182 m_arch_type == llvm::Triple::thumb;
Greg Claytonf15996e2011-04-07 22:46:35 +0000183 if (!raw)
Johnny Chen80ab18e2011-05-12 22:25:53 +0000184 raw = force_raw;
Johnny Chend17f8012011-05-23 18:00:40 +0000185 */
Johnny Chen80ab18e2011-05-12 22:25:53 +0000186 /* .... when we fix the edis for arm/thumb. */
Greg Claytonabe0fed2011-04-18 08:33:37 +0000187
188 if (!raw)
Greg Claytonf15996e2011-04-07 22:46:35 +0000189 numTokens = EDNumTokens(m_inst);
Chris Lattner24943d22010-06-08 16:52:24 +0000190
191 int currentOpIndex = -1;
192
Greg Claytonf15996e2011-04-07 22:46:35 +0000193 bool printTokenized = false;
194
195 if (numTokens != -1 && !raw)
Sean Callanan8541f2f2010-07-23 02:19:15 +0000196 {
197 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Claytonf15996e2011-04-07 22:46:35 +0000198
Greg Clayton5c4c7462010-10-06 03:09:58 +0000199 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
200 base_addr = GetAddress().GetLoadAddress (exe_ctx->target);
Sean Callanan8541f2f2010-07-23 02:19:15 +0000201 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton5c4c7462010-10-06 03:09:58 +0000202 base_addr = GetAddress().GetFileAddress ();
Greg Claytonf15996e2011-04-07 22:46:35 +0000203
Johnny Chen80ab18e2011-05-12 22:25:53 +0000204 lldb::addr_t PC = base_addr + EDInstByteSize(m_inst);
205
206 // When executing an ARM instruction, PC reads as the address of the
207 // current instruction plus 8. And for Thumb, it is plus 4.
208 if (m_arch_type == llvm::Triple::arm)
209 PC = base_addr + 8;
210 else if (m_arch_type == llvm::Triple::thumb)
211 PC = base_addr + 4;
212
213 RegisterReaderArg rra(PC, m_disassembler);
Johnny Chenc5272bf2011-05-12 18:48:11 +0000214
Chris Lattner24943d22010-06-08 16:52:24 +0000215 printTokenized = true;
216
217 // Handle the opcode column.
218
219 StreamString opcode;
220
221 int tokenIndex = 0;
222
223 EDTokenRef token;
224 const char *tokenStr;
225
Johnny Chenff8fea62011-05-18 22:48:41 +0000226 if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success
227 printTokenized = false;
228 else if (!EDTokenIsOpcode(token))
229 printTokenized = false;
230 else if (EDGetTokenString(&tokenStr, token)) // 0 on success
Chris Lattner24943d22010-06-08 16:52:24 +0000231 printTokenized = false;
232
Johnny Chenff8fea62011-05-18 22:48:41 +0000233 if (printTokenized)
Chris Lattner24943d22010-06-08 16:52:24 +0000234 {
Johnny Chenff8fea62011-05-18 22:48:41 +0000235 // Put the token string into our opcode string
236 opcode.PutCString(tokenStr);
Chris Lattner24943d22010-06-08 16:52:24 +0000237
Johnny Chenff8fea62011-05-18 22:48:41 +0000238 // If anything follows, it probably starts with some whitespace. Skip it.
239 if (++tokenIndex < numTokens)
240 {
241 if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success
242 printTokenized = false;
243 else if (!EDTokenIsWhitespace(token))
244 printTokenized = false;
245 }
246
247 ++tokenIndex;
Chris Lattner24943d22010-06-08 16:52:24 +0000248 }
249
Chris Lattner24943d22010-06-08 16:52:24 +0000250 // Handle the operands and the comment.
Chris Lattner24943d22010-06-08 16:52:24 +0000251 StreamString operands;
252 StreamString comment;
253
254 if (printTokenized)
255 {
Johnny Chen51ff2482011-05-19 01:05:37 +0000256 bool show_token = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000257
258 for (; tokenIndex < numTokens; ++tokenIndex)
259 {
260 if (EDGetToken(&token, m_inst, tokenIndex))
261 return;
262
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000263 int operandIndex = EDOperandIndexForToken(token);
Chris Lattner24943d22010-06-08 16:52:24 +0000264
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000265 if (operandIndex >= 0)
266 {
267 if (operandIndex != currentOpIndex)
Chris Lattner24943d22010-06-08 16:52:24 +0000268 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000269 show_token = true;
270
271 currentOpIndex = operandIndex;
272 EDOperandRef operand;
273
274 if (!EDGetOperand(&operand, m_inst, currentOpIndex))
Chris Lattner24943d22010-06-08 16:52:24 +0000275 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000276 if (EDOperandIsMemory(operand))
Chris Lattner24943d22010-06-08 16:52:24 +0000277 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000278 uint64_t operand_value;
279
280 if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, &rra))
Chris Lattner24943d22010-06-08 16:52:24 +0000281 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000282 if (EDInstIsBranch(m_inst))
Chris Lattner24943d22010-06-08 16:52:24 +0000283 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000284 operands.Printf("0x%llx ", operand_value);
285 show_token = false;
286 }
287 else
288 {
289 // Put the address value into the comment
290 comment.Printf("0x%llx ", operand_value);
291 }
Chris Lattner24943d22010-06-08 16:52:24 +0000292
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000293 lldb_private::Address so_addr;
294 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
295 {
296 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
297 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
298 }
299 else
300 {
301 Module *module = GetAddress().GetModule();
302 if (module)
Chris Lattner24943d22010-06-08 16:52:24 +0000303 {
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000304 if (module->ResolveFileAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000305 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Greg Clayton70436352010-06-30 23:03:03 +0000306 }
Johnny Chen6d61ebf2011-05-18 22:08:52 +0000307 }
308 } // EDEvaluateOperand
309 } // EDOperandIsMemory
310 } // EDGetOperand
311 } // operandIndex != currentOpIndex
312 } // operandIndex >= 0
Chris Lattner24943d22010-06-08 16:52:24 +0000313
314 if (show_token)
315 {
316 if(EDGetTokenString(&tokenStr, token))
317 {
318 printTokenized = false;
319 break;
320 }
321
322 operands.PutCString(tokenStr);
323 }
324 } // for (tokenIndex)
325
Johnny Chende5cc8c2011-05-20 17:27:37 +0000326 // FIXME!!!
327 // Workaround for llvm::tB's operands not properly parsed by ARMAsmParser.
328 if (m_arch_type == llvm::Triple::thumb && opcode.GetString() == "b") {
329 const char *inst_str;
Peter Collingbourned77c0392011-05-20 22:42:59 +0000330 const char *pos = NULL;
Johnny Chende5cc8c2011-05-20 17:27:37 +0000331 if (EDGetInstString(&inst_str, m_inst) == 0 && (pos = strstr(inst_str, "#")) != NULL) {
332 uint64_t operand_value = PC + atoi(++pos);
333 operands.Printf("0x%llx ", operand_value);
334
335 lldb_private::Address so_addr;
336 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty()) {
337 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
338 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
339 } else {
340 Module *module = GetAddress().GetModule();
341 if (module) {
342 if (module->ResolveFileAddress (operand_value, so_addr))
343 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
344 }
345 }
346 }
347 }
348 // END of workaround.
349
Johnny Chen51ff2482011-05-19 01:05:37 +0000350 // If both operands and comment are empty, we will just print out
351 // the raw disassembly.
352 if (operands.GetString().empty() && comment.GetString().empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000353 {
Johnny Chen51ff2482011-05-19 01:05:37 +0000354 const char *str;
355
356 if (EDGetInstString(&str, m_inst))
357 return;
Johnny Chend17f8012011-05-23 18:00:40 +0000358 Align(s, str, opcodeColumnWidth, operandColumnWidth);
Johnny Chen51ff2482011-05-19 01:05:37 +0000359 }
360 else
361 {
362 PadString(s, opcode.GetString(), opcodeColumnWidth);
363
364 if (comment.GetString().empty())
365 s->PutCString(operands.GetString().c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000366 else
367 {
Johnny Chen51ff2482011-05-19 01:05:37 +0000368 PadString(s, operands.GetString(), operandColumnWidth);
Chris Lattner24943d22010-06-08 16:52:24 +0000369
Johnny Chen51ff2482011-05-19 01:05:37 +0000370 s->PutCString("; ");
371 s->PutCString(comment.GetString().c_str());
372 } // else (comment.GetString().empty())
373 } // else (operands.GetString().empty() && comment.GetString().empty())
374 } // printTokenized
Chris Lattner24943d22010-06-08 16:52:24 +0000375 } // numTokens != -1
376
377 if (!printTokenized)
378 {
379 const char *str;
380
Johnny Chen51ff2482011-05-19 01:05:37 +0000381 if (EDGetInstString(&str, m_inst)) // 0 on success
Chris Lattner24943d22010-06-08 16:52:24 +0000382 return;
Johnny Chen08251ef2011-05-21 00:44:42 +0000383 if (raw)
384 s->Write(str, strlen(str) - 1);
385 else
386 {
387 // EDis fails to parse the tokens of this inst. Need to align this
Johnny Chen84d42e82011-05-21 00:55:57 +0000388 // raw disassembly's opcode with the rest of output.
Johnny Chend17f8012011-05-23 18:00:40 +0000389 Align(s, str, opcodeColumnWidth, operandColumnWidth);
Johnny Chen08251ef2011-05-21 00:44:42 +0000390 }
Chris Lattner24943d22010-06-08 16:52:24 +0000391 }
392}
393
394bool
Caroline Ticeaf591802011-04-05 23:22:54 +0000395InstructionLLVM::DoesBranch() const
Chris Lattner24943d22010-06-08 16:52:24 +0000396{
397 return EDInstIsBranch(m_inst);
398}
399
400size_t
Caroline Ticeaf591802011-04-05 23:22:54 +0000401InstructionLLVM::Decode (const Disassembler &disassembler,
402 const lldb_private::DataExtractor &data,
403 uint32_t data_offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000404{
405 if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data)))
Greg Clayton7bc39082011-03-24 23:53:38 +0000406 {
407 const int byte_size = EDInstByteSize(m_inst);
408 uint32_t offset = data_offset;
409 // Make a copy of the opcode in m_opcode
410 switch (disassembler.GetArchitecture().GetMachine())
411 {
412 case llvm::Triple::x86:
413 case llvm::Triple::x86_64:
414 m_opcode.SetOpcodeBytes (data.PeekData (data_offset, byte_size), byte_size);
415 break;
416
417 case llvm::Triple::arm:
Greg Clayton7bc39082011-03-24 23:53:38 +0000418 case llvm::Triple::thumb:
Greg Clayton149731c2011-03-25 18:03:16 +0000419 switch (byte_size)
420 {
421 case 2:
422 m_opcode.SetOpcode16 (data.GetU16 (&offset));
423 break;
424
425 case 4:
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000426 {
427 if (GetAddressClass() == eAddressClassCodeAlternateISA)
428 {
429 // If it is a 32-bit THUMB instruction, we need to swap the upper & lower halves.
430 uint32_t orig_bytes = data.GetU32 (&offset);
431 uint16_t upper_bits = (orig_bytes >> 16) & ((1u << 16) - 1);
432 uint16_t lower_bits = orig_bytes & ((1u << 16) - 1);
433 uint32_t swapped = (lower_bits << 16) | upper_bits;
434 m_opcode.SetOpcode32 (swapped);
435 }
436 else
437 m_opcode.SetOpcode32 (data.GetU32 (&offset));
438 }
Greg Clayton149731c2011-03-25 18:03:16 +0000439 break;
440
441 default:
442 assert (!"Invalid ARM opcode size");
443 break;
444 }
Greg Clayton7bc39082011-03-24 23:53:38 +0000445 break;
446
447 default:
448 assert (!"This shouldn't happen since we control the architecture we allow DisassemblerLLVM to be created for");
449 break;
450 }
451 return byte_size;
452 }
Chris Lattner24943d22010-06-08 16:52:24 +0000453 else
454 return 0;
455}
456
Chris Lattner24943d22010-06-08 16:52:24 +0000457static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000458SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000459{
Greg Clayton940b1032011-02-23 00:35:02 +0000460 switch (arch.GetMachine ())
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000461 {
Greg Clayton940b1032011-02-23 00:35:02 +0000462 case llvm::Triple::x86:
463 case llvm::Triple::x86_64:
Chris Lattner24943d22010-06-08 16:52:24 +0000464 return kEDAssemblySyntaxX86ATT;
Sean Callanand151c8a2011-03-09 01:02:51 +0000465 case llvm::Triple::arm:
Greg Clayton889fbd02011-03-26 19:14:58 +0000466 case llvm::Triple::thumb:
Sean Callanand151c8a2011-03-09 01:02:51 +0000467 return kEDAssemblySyntaxARMUAL;
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000468 default:
469 break;
470 }
Greg Claytoncf015052010-06-11 03:25:34 +0000471 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000472}
473
474Disassembler *
475DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
476{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000477 std::auto_ptr<DisassemblerLLVM> disasm_ap (new DisassemblerLLVM(arch));
478
Caroline Tice080bf612011-04-05 18:46:00 +0000479 if (disasm_ap.get() && disasm_ap->IsValid())
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000480 return disasm_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000481
Greg Claytoncf015052010-06-11 03:25:34 +0000482 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000483}
484
485DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
Greg Claytonb01000f2011-01-17 03:46:26 +0000486 Disassembler (arch),
Greg Claytonb1888f22011-03-19 01:12:21 +0000487 m_disassembler (NULL),
488 m_disassembler_thumb (NULL) // For ARM only
Chris Lattner24943d22010-06-08 16:52:24 +0000489{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000490 const std::string &arch_triple = arch.GetTriple().str();
491 if (!arch_triple.empty())
Greg Claytoncf015052010-06-11 03:25:34 +0000492 {
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000493 if (EDGetDisassembler(&m_disassembler, arch_triple.c_str(), SyntaxForArchSpec (arch)))
494 m_disassembler = NULL;
Greg Claytonb1888f22011-03-19 01:12:21 +0000495 llvm::Triple::ArchType llvm_arch = arch.GetTriple().getArch();
Greg Clayton889fbd02011-03-26 19:14:58 +0000496 // Don't have the lldb::Triple::thumb architecture here. If someone specifies
497 // "thumb" as the architecture, we want a thumb only disassembler. But if any
498 // architecture starting with "arm" if specified, we want to auto detect the
499 // arm/thumb code automatically using the AddressClass from section offset
500 // addresses.
Greg Claytonb1888f22011-03-19 01:12:21 +0000501 if (llvm_arch == llvm::Triple::arm)
502 {
503 if (EDGetDisassembler(&m_disassembler_thumb, "thumb-apple-darwin", kEDAssemblySyntaxARMUAL))
504 m_disassembler_thumb = NULL;
505 }
Greg Claytoncf015052010-06-11 03:25:34 +0000506 }
Chris Lattner24943d22010-06-08 16:52:24 +0000507}
508
509DisassemblerLLVM::~DisassemblerLLVM()
510{
511}
512
513size_t
Greg Clayton70436352010-06-30 23:03:03 +0000514DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000515(
Greg Clayton5c4c7462010-10-06 03:09:58 +0000516 const Address &base_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000517 const DataExtractor& data,
518 uint32_t data_offset,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000519 uint32_t num_instructions,
520 bool append
Chris Lattner24943d22010-06-08 16:52:24 +0000521)
522{
Greg Claytonb01000f2011-01-17 03:46:26 +0000523 if (m_disassembler == NULL)
524 return 0;
525
Chris Lattner24943d22010-06-08 16:52:24 +0000526 size_t total_inst_byte_size = 0;
527
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000528 if (!append)
529 m_instruction_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000530
531 while (data.ValidOffset(data_offset) && num_instructions)
532 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000533 Address inst_addr (base_addr);
534 inst_addr.Slide(data_offset);
Greg Claytonb1888f22011-03-19 01:12:21 +0000535
536 bool use_thumb = false;
537 // If we have a thumb disassembler, then we have an ARM architecture
538 // so we need to check what the instruction address class is to make
539 // sure we shouldn't be disassembling as thumb...
Greg Clayton889fbd02011-03-26 19:14:58 +0000540 AddressClass inst_address_class = eAddressClassInvalid;
Greg Claytonb1888f22011-03-19 01:12:21 +0000541 if (m_disassembler_thumb)
542 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000543 inst_address_class = inst_addr.GetAddressClass ();
544 if (inst_address_class == eAddressClassCodeAlternateISA)
Greg Claytonb1888f22011-03-19 01:12:21 +0000545 use_thumb = true;
546 }
Johnny Chen80ab18e2011-05-12 22:25:53 +0000547
Greg Clayton7bc39082011-03-24 23:53:38 +0000548 InstructionSP inst_sp (new InstructionLLVM (inst_addr,
Greg Clayton889fbd02011-03-26 19:14:58 +0000549 inst_address_class,
Greg Claytonabe0fed2011-04-18 08:33:37 +0000550 use_thumb ? m_disassembler_thumb : m_disassembler,
Johnny Chen1608c872011-05-18 18:22:16 +0000551 use_thumb ? llvm::Triple::thumb : m_arch.GetMachine()));
Chris Lattner24943d22010-06-08 16:52:24 +0000552
Greg Clayton889fbd02011-03-26 19:14:58 +0000553 size_t inst_byte_size = inst_sp->Decode (*this, data, data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000554
555 if (inst_byte_size == 0)
556 break;
557
Greg Clayton5c4c7462010-10-06 03:09:58 +0000558 m_instruction_list.Append (inst_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000559
560 total_inst_byte_size += inst_byte_size;
561 data_offset += inst_byte_size;
562 num_instructions--;
563 }
564
565 return total_inst_byte_size;
566}
567
568void
569DisassemblerLLVM::Initialize()
570{
571 PluginManager::RegisterPlugin (GetPluginNameStatic(),
572 GetPluginDescriptionStatic(),
573 CreateInstance);
574}
575
576void
577DisassemblerLLVM::Terminate()
578{
579 PluginManager::UnregisterPlugin (CreateInstance);
580}
581
582
583const char *
584DisassemblerLLVM::GetPluginNameStatic()
585{
Greg Clayton149731c2011-03-25 18:03:16 +0000586 return "llvm";
Chris Lattner24943d22010-06-08 16:52:24 +0000587}
588
589const char *
590DisassemblerLLVM::GetPluginDescriptionStatic()
591{
Greg Clayton149731c2011-03-25 18:03:16 +0000592 return "Disassembler that uses LLVM opcode tables to disassemble i386, x86_64 and ARM.";
Chris Lattner24943d22010-06-08 16:52:24 +0000593}
594
595//------------------------------------------------------------------
596// PluginInterface protocol
597//------------------------------------------------------------------
598const char *
599DisassemblerLLVM::GetPluginName()
600{
601 return "DisassemblerLLVM";
602}
603
604const char *
605DisassemblerLLVM::GetShortPluginName()
606{
607 return GetPluginNameStatic();
608}
609
610uint32_t
611DisassemblerLLVM::GetPluginVersion()
612{
613 return 1;
614}
615