blob: 87ba029f59ede22a11930ba14ed266db7ba53886 [file] [log] [blame]
Sean Callanan95e5c632012-02-17 00:53:45 +00001//===-- DisassemblerLLVMC.cpp -----------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sean Callanan95e5c632012-02-17 00:53:45 +00006//
7//===----------------------------------------------------------------------===//
8
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00009#include "DisassemblerLLVMC.h"
10
Sean Callanan95e5c632012-02-17 00:53:45 +000011#include "llvm-c/Disassembler.h"
Benjamin Kramer79dad1d2016-01-26 16:45:00 +000012#include "llvm/ADT/SmallString.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000013#include "llvm/MC/MCAsmInfo.h"
14#include "llvm/MC/MCContext.h"
Benjamin Kramer79dad1d2016-01-26 16:45:00 +000015#include "llvm/MC/MCDisassembler/MCDisassembler.h"
16#include "llvm/MC/MCDisassembler/MCExternalSymbolizer.h"
17#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000018#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCInstPrinter.h"
20#include "llvm/MC/MCInstrInfo.h"
21#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCSubtargetInfo.h"
23#include "llvm/Support/ErrorHandling.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "llvm/Support/ScopedPrinter.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000025#include "llvm/Support/TargetRegistry.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000026#include "llvm/Support/TargetSelect.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000027
Sean Callanan95e5c632012-02-17 00:53:45 +000028#include "lldb/Core/Address.h"
Greg Clayton1f746072012-08-29 21:13:06 +000029#include "lldb/Core/Module.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000030#include "lldb/Symbol/SymbolContext.h"
31#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000034#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000035#include "lldb/Target/StackFrame.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000036#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000037#include "lldb/Utility/DataExtractor.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000038#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000039#include "lldb/Utility/RegularExpression.h"
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +000040#include "lldb/Utility/Stream.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000041
42using namespace lldb;
43using namespace lldb_private;
44
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +000045class DisassemblerLLVMC::MCDisasmInstance {
46public:
47 static std::unique_ptr<MCDisasmInstance>
48 Create(const char *triple, const char *cpu, const char *features_str,
49 unsigned flavor, DisassemblerLLVMC &owner);
50
51 ~MCDisasmInstance() = default;
52
53 uint64_t GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len,
54 lldb::addr_t pc, llvm::MCInst &mc_inst) const;
55 void PrintMCInst(llvm::MCInst &mc_inst, std::string &inst_string,
56 std::string &comments_string);
57 void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style);
58 bool CanBranch(llvm::MCInst &mc_inst) const;
59 bool HasDelaySlot(llvm::MCInst &mc_inst) const;
60 bool IsCall(llvm::MCInst &mc_inst) const;
61
62private:
63 MCDisasmInstance(std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up,
64 std::unique_ptr<llvm::MCRegisterInfo> &&reg_info_up,
65 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
66 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
67 std::unique_ptr<llvm::MCContext> &&context_up,
68 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
69 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up);
70
71 std::unique_ptr<llvm::MCInstrInfo> m_instr_info_up;
72 std::unique_ptr<llvm::MCRegisterInfo> m_reg_info_up;
73 std::unique_ptr<llvm::MCSubtargetInfo> m_subtarget_info_up;
74 std::unique_ptr<llvm::MCAsmInfo> m_asm_info_up;
75 std::unique_ptr<llvm::MCContext> m_context_up;
76 std::unique_ptr<llvm::MCDisassembler> m_disasm_up;
77 std::unique_ptr<llvm::MCInstPrinter> m_instr_printer_up;
78};
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080class InstructionLLVMC : public lldb_private::Instruction {
Sean Callanan95e5c632012-02-17 00:53:45 +000081public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 InstructionLLVMC(DisassemblerLLVMC &disasm,
83 const lldb_private::Address &address,
84 AddressClass addr_class)
85 : Instruction(address, addr_class),
86 m_disasm_wp(std::static_pointer_cast<DisassemblerLLVMC>(
87 disasm.shared_from_this())),
88 m_does_branch(eLazyBoolCalculate), m_has_delay_slot(eLazyBoolCalculate),
89 m_is_call(eLazyBoolCalculate), m_is_valid(false),
90 m_using_file_addr(false) {}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 ~InstructionLLVMC() override = default;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 bool DoesBranch() override {
95 if (m_does_branch == eLazyBoolCalculate) {
Raphael Isemann49254212018-08-28 15:31:01 +000096 DisassemblerScope disasm(*this);
97 if (disasm) {
Greg Claytonba812f42012-05-10 02:52:23 +000098 DataExtractor data;
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 if (m_opcode.GetData(data)) {
100 bool is_alternate_isa;
101 lldb::addr_t pc = m_address.GetFileAddress();
Greg Claytonba812f42012-05-10 02:52:23 +0000102
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000103 DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr =
Raphael Isemann49254212018-08-28 15:31:01 +0000104 GetDisasmToUse(is_alternate_isa, disasm);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 const uint8_t *opcode_data = data.GetDataStart();
106 const size_t opcode_data_len = data.GetByteSize();
107 llvm::MCInst inst;
108 const size_t inst_size =
109 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
110 // Be conservative, if we didn't understand the instruction, say it
111 // might branch...
112 if (inst_size == 0)
113 m_does_branch = eLazyBoolYes;
114 else {
115 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
116 if (can_branch)
117 m_does_branch = eLazyBoolYes;
118 else
119 m_does_branch = eLazyBoolNo;
120 }
Greg Claytonba812f42012-05-10 02:52:23 +0000121 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000123 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 return m_does_branch == eLazyBoolYes;
125 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 bool HasDelaySlot() override {
128 if (m_has_delay_slot == eLazyBoolCalculate) {
Raphael Isemann49254212018-08-28 15:31:01 +0000129 DisassemblerScope disasm(*this);
130 if (disasm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 DataExtractor data;
132 if (m_opcode.GetData(data)) {
133 bool is_alternate_isa;
134 lldb::addr_t pc = m_address.GetFileAddress();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000135
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000136 DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr =
Raphael Isemann49254212018-08-28 15:31:01 +0000137 GetDisasmToUse(is_alternate_isa, disasm);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 const uint8_t *opcode_data = data.GetDataStart();
139 const size_t opcode_data_len = data.GetByteSize();
140 llvm::MCInst inst;
141 const size_t inst_size =
142 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
143 // if we didn't understand the instruction, say it doesn't have a
144 // delay slot...
145 if (inst_size == 0)
146 m_has_delay_slot = eLazyBoolNo;
147 else {
148 const bool has_delay_slot = mc_disasm_ptr->HasDelaySlot(inst);
149 if (has_delay_slot)
150 m_has_delay_slot = eLazyBoolYes;
151 else
152 m_has_delay_slot = eLazyBoolNo;
153 }
Sean Callanan4740a732016-09-06 04:48:36 +0000154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 }
156 }
157 return m_has_delay_slot == eLazyBoolYes;
158 }
159
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000160 DisassemblerLLVMC::MCDisasmInstance *GetDisasmToUse(bool &is_alternate_isa) {
Raphael Isemann49254212018-08-28 15:31:01 +0000161 DisassemblerScope disasm(*this);
162 return GetDisasmToUse(is_alternate_isa, disasm);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 }
164
165 size_t Decode(const lldb_private::Disassembler &disassembler,
166 const lldb_private::DataExtractor &data,
167 lldb::offset_t data_offset) override {
168 // All we have to do is read the opcode which can be easy for some
169 // architectures
170 bool got_op = false;
Raphael Isemann49254212018-08-28 15:31:01 +0000171 DisassemblerScope disasm(*this);
172 if (disasm) {
173 const ArchSpec &arch = disasm->GetArchitecture();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 const lldb::ByteOrder byte_order = data.GetByteOrder();
175
176 const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize();
177 const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize();
178 if (min_op_byte_size == max_op_byte_size) {
179 // Fixed size instructions, just read that amount of data.
180 if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size))
181 return false;
182
183 switch (min_op_byte_size) {
184 case 1:
185 m_opcode.SetOpcode8(data.GetU8(&data_offset), byte_order);
186 got_op = true;
187 break;
188
189 case 2:
190 m_opcode.SetOpcode16(data.GetU16(&data_offset), byte_order);
191 got_op = true;
192 break;
193
194 case 4:
195 m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order);
196 got_op = true;
197 break;
198
199 case 8:
200 m_opcode.SetOpcode64(data.GetU64(&data_offset), byte_order);
201 got_op = true;
202 break;
203
204 default:
205 m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size),
206 min_op_byte_size);
207 got_op = true;
208 break;
209 }
210 }
211 if (!got_op) {
212 bool is_alternate_isa = false;
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000213 DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr =
Raphael Isemann49254212018-08-28 15:31:01 +0000214 GetDisasmToUse(is_alternate_isa, disasm);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215
216 const llvm::Triple::ArchType machine = arch.GetMachine();
217 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb) {
218 if (machine == llvm::Triple::thumb || is_alternate_isa) {
219 uint32_t thumb_opcode = data.GetU16(&data_offset);
220 if ((thumb_opcode & 0xe000) != 0xe000 ||
221 ((thumb_opcode & 0x1800u) == 0)) {
222 m_opcode.SetOpcode16(thumb_opcode, byte_order);
223 m_is_valid = true;
224 } else {
225 thumb_opcode <<= 16;
226 thumb_opcode |= data.GetU16(&data_offset);
227 m_opcode.SetOpcode16_2(thumb_opcode, byte_order);
228 m_is_valid = true;
229 }
230 } else {
231 m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order);
232 m_is_valid = true;
233 }
234 } else {
235 // The opcode isn't evenly sized, so we need to actually use the llvm
236 // disassembler to parse it and get the size.
237 uint8_t *opcode_data =
238 const_cast<uint8_t *>(data.PeekData(data_offset, 1));
239 const size_t opcode_data_len = data.BytesLeft(data_offset);
240 const addr_t pc = m_address.GetFileAddress();
241 llvm::MCInst inst;
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 const size_t inst_size =
244 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 if (inst_size == 0)
246 m_opcode.Clear();
247 else {
248 m_opcode.SetOpcodeBytes(opcode_data, inst_size);
249 m_is_valid = true;
250 }
251 }
252 }
253 return m_opcode.GetByteSize();
254 }
255 return 0;
256 }
257
258 void AppendComment(std::string &description) {
259 if (m_comment.empty())
260 m_comment.swap(description);
261 else {
262 m_comment.append(", ");
263 m_comment.append(description);
264 }
265 }
266
267 void CalculateMnemonicOperandsAndComment(
268 const lldb_private::ExecutionContext *exe_ctx) override {
269 DataExtractor data;
270 const AddressClass address_class = GetAddressClass();
271
272 if (m_opcode.GetData(data)) {
273 std::string out_string;
274 std::string comment_string;
275
Raphael Isemann49254212018-08-28 15:31:01 +0000276 DisassemblerScope disasm(*this, exe_ctx);
277 if (disasm) {
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000278 DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000280 if (address_class == AddressClass::eCodeAlternateISA)
Raphael Isemann49254212018-08-28 15:31:01 +0000281 mc_disasm_ptr = disasm->m_alternate_disasm_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 else
Raphael Isemann49254212018-08-28 15:31:01 +0000283 mc_disasm_ptr = disasm->m_disasm_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284
285 lldb::addr_t pc = m_address.GetFileAddress();
286 m_using_file_addr = true;
287
Raphael Isemann49254212018-08-28 15:31:01 +0000288 const bool data_from_file = disasm->m_data_from_file;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 bool use_hex_immediates = true;
290 Disassembler::HexImmediateStyle hex_style = Disassembler::eHexStyleC;
291
292 if (exe_ctx) {
293 Target *target = exe_ctx->GetTargetPtr();
294 if (target) {
295 use_hex_immediates = target->GetUseHexImmediates();
296 hex_style = target->GetHexImmediateStyle();
297
298 if (!data_from_file) {
299 const lldb::addr_t load_addr = m_address.GetLoadAddress(target);
300 if (load_addr != LLDB_INVALID_ADDRESS) {
301 pc = load_addr;
302 m_using_file_addr = false;
303 }
304 }
305 }
306 }
307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 const uint8_t *opcode_data = data.GetDataStart();
309 const size_t opcode_data_len = data.GetByteSize();
310 llvm::MCInst inst;
311 size_t inst_size =
312 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
313
314 if (inst_size > 0) {
315 mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style);
316 mc_disasm_ptr->PrintMCInst(inst, out_string, comment_string);
317
318 if (!comment_string.empty()) {
319 AppendComment(comment_string);
320 }
321 }
322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 if (inst_size == 0) {
324 m_comment.assign("unknown opcode");
325 inst_size = m_opcode.GetByteSize();
326 StreamString mnemonic_strm;
327 lldb::offset_t offset = 0;
328 lldb::ByteOrder byte_order = data.GetByteOrder();
329 switch (inst_size) {
330 case 1: {
331 const uint8_t uval8 = data.GetU8(&offset);
332 m_opcode.SetOpcode8(uval8, byte_order);
333 m_opcode_name.assign(".byte");
334 mnemonic_strm.Printf("0x%2.2x", uval8);
335 } break;
336 case 2: {
337 const uint16_t uval16 = data.GetU16(&offset);
338 m_opcode.SetOpcode16(uval16, byte_order);
339 m_opcode_name.assign(".short");
340 mnemonic_strm.Printf("0x%4.4x", uval16);
341 } break;
342 case 4: {
343 const uint32_t uval32 = data.GetU32(&offset);
344 m_opcode.SetOpcode32(uval32, byte_order);
345 m_opcode_name.assign(".long");
346 mnemonic_strm.Printf("0x%8.8x", uval32);
347 } break;
348 case 8: {
349 const uint64_t uval64 = data.GetU64(&offset);
350 m_opcode.SetOpcode64(uval64, byte_order);
351 m_opcode_name.assign(".quad");
352 mnemonic_strm.Printf("0x%16.16" PRIx64, uval64);
353 } break;
354 default:
355 if (inst_size == 0)
356 return;
357 else {
358 const uint8_t *bytes = data.PeekData(offset, inst_size);
Konrad Kleine248a1302019-05-23 11:14:47 +0000359 if (bytes == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 return;
361 m_opcode_name.assign(".byte");
362 m_opcode.SetOpcodeBytes(bytes, inst_size);
363 mnemonic_strm.Printf("0x%2.2x", bytes[0]);
364 for (uint32_t i = 1; i < inst_size; ++i)
365 mnemonic_strm.Printf(" 0x%2.2x", bytes[i]);
366 }
367 break;
368 }
Zachary Turnerc1564272016-11-16 21:15:24 +0000369 m_mnemonics = mnemonic_strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 return;
371 } else {
372 if (m_does_branch == eLazyBoolCalculate) {
373 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
374 if (can_branch)
375 m_does_branch = eLazyBoolYes;
376 else
377 m_does_branch = eLazyBoolNo;
378 }
379 }
380
Zachary Turner95eae422016-09-21 16:01:28 +0000381 static RegularExpression s_regex(
382 llvm::StringRef("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383
Jonas Devlieghere3af3f1e2019-08-16 21:25:36 +0000384 llvm::SmallVector<llvm::StringRef, 4> matches;
Zachary Turner95eae422016-09-21 16:01:28 +0000385 if (s_regex.Execute(out_string, &matches)) {
Jonas Devlieghere3af3f1e2019-08-16 21:25:36 +0000386 m_opcode_name = matches[1].str();
387 m_mnemonics = matches[2].str();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 }
389 }
390 }
391 }
392
393 bool IsValid() const { return m_is_valid; }
394
395 bool UsingFileAddress() const { return m_using_file_addr; }
396 size_t GetByteSize() const { return m_opcode.GetByteSize(); }
397
Raphael Isemann49254212018-08-28 15:31:01 +0000398 /// Grants exclusive access to the disassembler and initializes it with the
399 /// given InstructionLLVMC and an optional ExecutionContext.
400 class DisassemblerScope {
401 std::shared_ptr<DisassemblerLLVMC> m_disasm;
402
403 public:
404 explicit DisassemblerScope(
405 InstructionLLVMC &i,
406 const lldb_private::ExecutionContext *exe_ctx = nullptr)
407 : m_disasm(i.m_disasm_wp.lock()) {
408 m_disasm->m_mutex.lock();
409 m_disasm->m_inst = &i;
410 m_disasm->m_exe_ctx = exe_ctx;
411 }
412 ~DisassemblerScope() { m_disasm->m_mutex.unlock(); }
413
414 /// Evaluates to true if this scope contains a valid disassembler.
415 operator bool() const { return static_cast<bool>(m_disasm); }
416
417 std::shared_ptr<DisassemblerLLVMC> operator->() { return m_disasm; }
418 };
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419
420 static llvm::StringRef::const_iterator
421 ConsumeWhitespace(llvm::StringRef::const_iterator osi,
422 llvm::StringRef::const_iterator ose) {
423 while (osi != ose) {
424 switch (*osi) {
425 default:
Sean Callanan4740a732016-09-06 04:48:36 +0000426 return osi;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 case ' ':
428 case '\t':
429 break;
430 }
431 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000432 }
433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 return osi;
435 }
Sean Callanan4740a732016-09-06 04:48:36 +0000436
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 static std::pair<bool, llvm::StringRef::const_iterator>
438 ConsumeChar(llvm::StringRef::const_iterator osi, const char c,
439 llvm::StringRef::const_iterator ose) {
440 bool found = false;
441
442 osi = ConsumeWhitespace(osi, ose);
443 if (osi != ose && *osi == c) {
444 found = true;
445 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000446 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447
448 return std::make_pair(found, osi);
449 }
450
451 static std::pair<Operand, llvm::StringRef::const_iterator>
452 ParseRegisterName(llvm::StringRef::const_iterator osi,
453 llvm::StringRef::const_iterator ose) {
454 Operand ret;
455 ret.m_type = Operand::Type::Register;
456 std::string str;
457
458 osi = ConsumeWhitespace(osi, ose);
459
460 while (osi != ose) {
461 if (*osi >= '0' && *osi <= '9') {
462 if (str.empty()) {
463 return std::make_pair(Operand(), osi);
464 } else {
465 str.push_back(*osi);
Sean Callanan4740a732016-09-06 04:48:36 +0000466 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 } else if (*osi >= 'a' && *osi <= 'z') {
468 str.push_back(*osi);
469 } else {
470 switch (*osi) {
471 default:
472 if (str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000473 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 } else {
475 ret.m_register = ConstString(str);
476 return std::make_pair(ret, osi);
477 }
478 case '%':
479 if (!str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000480 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481 }
482 break;
Sean Callanan4740a732016-09-06 04:48:36 +0000483 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 }
485 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000486 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487
488 ret.m_register = ConstString(str);
489 return std::make_pair(ret, osi);
490 }
491
492 static std::pair<Operand, llvm::StringRef::const_iterator>
493 ParseImmediate(llvm::StringRef::const_iterator osi,
494 llvm::StringRef::const_iterator ose) {
495 Operand ret;
496 ret.m_type = Operand::Type::Immediate;
497 std::string str;
498 bool is_hex = false;
499
500 osi = ConsumeWhitespace(osi, ose);
501
502 while (osi != ose) {
503 if (*osi >= '0' && *osi <= '9') {
504 str.push_back(*osi);
505 } else if (*osi >= 'a' && *osi <= 'f') {
506 if (is_hex) {
507 str.push_back(*osi);
508 } else {
509 return std::make_pair(Operand(), osi);
510 }
511 } else {
512 switch (*osi) {
513 default:
514 if (str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000515 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 } else {
517 ret.m_immediate = strtoull(str.c_str(), nullptr, 0);
518 return std::make_pair(ret, osi);
519 }
520 case 'x':
521 if (!str.compare("0")) {
522 is_hex = true;
523 str.push_back(*osi);
524 } else {
Sean Callanan4740a732016-09-06 04:48:36 +0000525 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526 }
527 break;
528 case '#':
529 case '$':
530 if (!str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000531 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 }
533 break;
534 case '-':
535 if (str.empty()) {
536 ret.m_negative = true;
537 } else {
Sean Callanan4740a732016-09-06 04:48:36 +0000538 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 }
Sean Callanan4740a732016-09-06 04:48:36 +0000540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 }
542 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000543 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544
545 ret.m_immediate = strtoull(str.c_str(), nullptr, 0);
546 return std::make_pair(ret, osi);
547 }
548
549 // -0x5(%rax,%rax,2)
550 static std::pair<Operand, llvm::StringRef::const_iterator>
551 ParseIntelIndexedAccess(llvm::StringRef::const_iterator osi,
552 llvm::StringRef::const_iterator ose) {
553 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
554 ParseImmediate(osi, ose);
555 if (offset_and_iterator.first.IsValid()) {
556 osi = offset_and_iterator.second;
Sean Callanan4740a732016-09-06 04:48:36 +0000557 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558
559 bool found = false;
560 std::tie(found, osi) = ConsumeChar(osi, '(', ose);
561 if (!found) {
562 return std::make_pair(Operand(), osi);
Sean Callanan4740a732016-09-06 04:48:36 +0000563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564
565 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
566 ParseRegisterName(osi, ose);
567 if (base_and_iterator.first.IsValid()) {
568 osi = base_and_iterator.second;
569 } else {
570 return std::make_pair(Operand(), osi);
571 }
572
573 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
574 if (!found) {
575 return std::make_pair(Operand(), osi);
576 }
577
578 std::pair<Operand, llvm::StringRef::const_iterator> index_and_iterator =
579 ParseRegisterName(osi, ose);
580 if (index_and_iterator.first.IsValid()) {
581 osi = index_and_iterator.second;
582 } else {
583 return std::make_pair(Operand(), osi);
584 }
585
586 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
587 if (!found) {
588 return std::make_pair(Operand(), osi);
589 }
590
591 std::pair<Operand, llvm::StringRef::const_iterator>
592 multiplier_and_iterator = ParseImmediate(osi, ose);
593 if (index_and_iterator.first.IsValid()) {
594 osi = index_and_iterator.second;
595 } else {
596 return std::make_pair(Operand(), osi);
597 }
598
599 std::tie(found, osi) = ConsumeChar(osi, ')', ose);
600 if (!found) {
601 return std::make_pair(Operand(), osi);
602 }
603
604 Operand product;
605 product.m_type = Operand::Type::Product;
606 product.m_children.push_back(index_and_iterator.first);
607 product.m_children.push_back(multiplier_and_iterator.first);
608
609 Operand index;
610 index.m_type = Operand::Type::Sum;
611 index.m_children.push_back(base_and_iterator.first);
612 index.m_children.push_back(product);
613
614 if (offset_and_iterator.first.IsValid()) {
615 Operand offset;
616 offset.m_type = Operand::Type::Sum;
617 offset.m_children.push_back(offset_and_iterator.first);
618 offset.m_children.push_back(index);
619
620 Operand deref;
621 deref.m_type = Operand::Type::Dereference;
622 deref.m_children.push_back(offset);
623 return std::make_pair(deref, osi);
624 } else {
625 Operand deref;
626 deref.m_type = Operand::Type::Dereference;
627 deref.m_children.push_back(index);
628 return std::make_pair(deref, osi);
629 }
630 }
631
632 // -0x10(%rbp)
633 static std::pair<Operand, llvm::StringRef::const_iterator>
634 ParseIntelDerefAccess(llvm::StringRef::const_iterator osi,
635 llvm::StringRef::const_iterator ose) {
636 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
637 ParseImmediate(osi, ose);
638 if (offset_and_iterator.first.IsValid()) {
639 osi = offset_and_iterator.second;
640 }
641
642 bool found = false;
643 std::tie(found, osi) = ConsumeChar(osi, '(', ose);
644 if (!found) {
645 return std::make_pair(Operand(), osi);
646 }
647
648 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
649 ParseRegisterName(osi, ose);
650 if (base_and_iterator.first.IsValid()) {
651 osi = base_and_iterator.second;
652 } else {
653 return std::make_pair(Operand(), osi);
654 }
655
656 std::tie(found, osi) = ConsumeChar(osi, ')', ose);
657 if (!found) {
658 return std::make_pair(Operand(), osi);
659 }
660
661 if (offset_and_iterator.first.IsValid()) {
662 Operand offset;
663 offset.m_type = Operand::Type::Sum;
664 offset.m_children.push_back(offset_and_iterator.first);
665 offset.m_children.push_back(base_and_iterator.first);
666
667 Operand deref;
668 deref.m_type = Operand::Type::Dereference;
669 deref.m_children.push_back(offset);
670 return std::make_pair(deref, osi);
671 } else {
672 Operand deref;
673 deref.m_type = Operand::Type::Dereference;
674 deref.m_children.push_back(base_and_iterator.first);
675 return std::make_pair(deref, osi);
676 }
677 }
678
679 // [sp, #8]!
680 static std::pair<Operand, llvm::StringRef::const_iterator>
681 ParseARMOffsetAccess(llvm::StringRef::const_iterator osi,
682 llvm::StringRef::const_iterator ose) {
683 bool found = false;
684 std::tie(found, osi) = ConsumeChar(osi, '[', ose);
685 if (!found) {
686 return std::make_pair(Operand(), osi);
687 }
688
689 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
690 ParseRegisterName(osi, ose);
691 if (base_and_iterator.first.IsValid()) {
692 osi = base_and_iterator.second;
693 } else {
694 return std::make_pair(Operand(), osi);
695 }
696
697 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
698 if (!found) {
699 return std::make_pair(Operand(), osi);
700 }
701
702 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
703 ParseImmediate(osi, ose);
704 if (offset_and_iterator.first.IsValid()) {
705 osi = offset_and_iterator.second;
706 }
707
708 std::tie(found, osi) = ConsumeChar(osi, ']', ose);
709 if (!found) {
710 return std::make_pair(Operand(), osi);
711 }
712
713 Operand offset;
714 offset.m_type = Operand::Type::Sum;
715 offset.m_children.push_back(offset_and_iterator.first);
716 offset.m_children.push_back(base_and_iterator.first);
717
718 Operand deref;
719 deref.m_type = Operand::Type::Dereference;
720 deref.m_children.push_back(offset);
721 return std::make_pair(deref, osi);
722 }
723
724 // [sp]
725 static std::pair<Operand, llvm::StringRef::const_iterator>
726 ParseARMDerefAccess(llvm::StringRef::const_iterator osi,
727 llvm::StringRef::const_iterator ose) {
728 bool found = false;
729 std::tie(found, osi) = ConsumeChar(osi, '[', ose);
730 if (!found) {
731 return std::make_pair(Operand(), osi);
732 }
733
734 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
735 ParseRegisterName(osi, ose);
736 if (base_and_iterator.first.IsValid()) {
737 osi = base_and_iterator.second;
738 } else {
739 return std::make_pair(Operand(), osi);
740 }
741
742 std::tie(found, osi) = ConsumeChar(osi, ']', ose);
743 if (!found) {
744 return std::make_pair(Operand(), osi);
745 }
746
747 Operand deref;
748 deref.m_type = Operand::Type::Dereference;
749 deref.m_children.push_back(base_and_iterator.first);
750 return std::make_pair(deref, osi);
751 }
752
753 static void DumpOperand(const Operand &op, Stream &s) {
754 switch (op.m_type) {
755 case Operand::Type::Dereference:
756 s.PutCString("*");
757 DumpOperand(op.m_children[0], s);
758 break;
759 case Operand::Type::Immediate:
760 if (op.m_negative) {
761 s.PutCString("-");
762 }
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000763 s.PutCString(llvm::to_string(op.m_immediate));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 break;
765 case Operand::Type::Invalid:
766 s.PutCString("Invalid");
767 break;
768 case Operand::Type::Product:
769 s.PutCString("(");
770 DumpOperand(op.m_children[0], s);
771 s.PutCString("*");
772 DumpOperand(op.m_children[1], s);
773 s.PutCString(")");
774 break;
775 case Operand::Type::Register:
776 s.PutCString(op.m_register.AsCString());
777 break;
778 case Operand::Type::Sum:
779 s.PutCString("(");
780 DumpOperand(op.m_children[0], s);
781 s.PutCString("+");
782 DumpOperand(op.m_children[1], s);
783 s.PutCString(")");
784 break;
785 }
786 }
787
788 bool ParseOperands(
789 llvm::SmallVectorImpl<Instruction::Operand> &operands) override {
790 const char *operands_string = GetOperands(nullptr);
791
792 if (!operands_string) {
793 return false;
794 }
795
796 llvm::StringRef operands_ref(operands_string);
797
798 llvm::StringRef::const_iterator osi = operands_ref.begin();
799 llvm::StringRef::const_iterator ose = operands_ref.end();
800
801 while (osi != ose) {
802 Operand operand;
803 llvm::StringRef::const_iterator iter;
804
805 if ((std::tie(operand, iter) = ParseIntelIndexedAccess(osi, ose),
806 operand.IsValid()) ||
807 (std::tie(operand, iter) = ParseIntelDerefAccess(osi, ose),
808 operand.IsValid()) ||
809 (std::tie(operand, iter) = ParseARMOffsetAccess(osi, ose),
810 operand.IsValid()) ||
811 (std::tie(operand, iter) = ParseARMDerefAccess(osi, ose),
812 operand.IsValid()) ||
813 (std::tie(operand, iter) = ParseRegisterName(osi, ose),
814 operand.IsValid()) ||
815 (std::tie(operand, iter) = ParseImmediate(osi, ose),
816 operand.IsValid())) {
817 osi = iter;
818 operands.push_back(operand);
819 } else {
820 return false;
821 }
822
823 std::pair<bool, llvm::StringRef::const_iterator> found_and_iter =
824 ConsumeChar(osi, ',', ose);
825 if (found_and_iter.first) {
826 osi = found_and_iter.second;
827 }
828
829 osi = ConsumeWhitespace(osi, ose);
830 }
831
832 DisassemblerSP disasm_sp = m_disasm_wp.lock();
833
834 if (disasm_sp && operands.size() > 1) {
835 // TODO tie this into the MC Disassembler's notion of clobbers.
836 switch (disasm_sp->GetArchitecture().GetMachine()) {
837 default:
838 break;
839 case llvm::Triple::x86:
840 case llvm::Triple::x86_64:
841 operands[operands.size() - 1].m_clobbered = true;
842 break;
843 case llvm::Triple::arm:
844 operands[0].m_clobbered = true;
845 break;
846 }
847 }
848
849 if (Log *log =
850 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)) {
851 StreamString ss;
852
853 ss.Printf("[%s] expands to %zu operands:\n", operands_string,
854 operands.size());
855 for (const Operand &operand : operands) {
856 ss.PutCString(" ");
857 DumpOperand(operand, ss);
858 ss.PutCString("\n");
859 }
860
Zachary Turnerc1564272016-11-16 21:15:24 +0000861 log->PutString(ss.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 }
863
864 return true;
865 }
866
867 bool IsCall() override {
868 if (m_is_call == eLazyBoolCalculate) {
Raphael Isemann49254212018-08-28 15:31:01 +0000869 DisassemblerScope disasm(*this);
870 if (disasm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871 DataExtractor data;
872 if (m_opcode.GetData(data)) {
873 bool is_alternate_isa;
874 lldb::addr_t pc = m_address.GetFileAddress();
875
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000876 DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr =
Raphael Isemann49254212018-08-28 15:31:01 +0000877 GetDisasmToUse(is_alternate_isa, disasm);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 const uint8_t *opcode_data = data.GetDataStart();
879 const size_t opcode_data_len = data.GetByteSize();
880 llvm::MCInst inst;
881 const size_t inst_size =
882 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
883 if (inst_size == 0) {
884 m_is_call = eLazyBoolNo;
885 } else {
886 if (mc_disasm_ptr->IsCall(inst))
887 m_is_call = eLazyBoolYes;
Sean Callanan4740a732016-09-06 04:48:36 +0000888 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 m_is_call = eLazyBoolNo;
890 }
Sean Callanan4740a732016-09-06 04:48:36 +0000891 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 }
Sean Callanan4740a732016-09-06 04:48:36 +0000893 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 return m_is_call == eLazyBoolYes;
895 }
896
Sean Callanan95e5c632012-02-17 00:53:45 +0000897protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 std::weak_ptr<DisassemblerLLVMC> m_disasm_wp;
899 LazyBool m_does_branch;
900 LazyBool m_has_delay_slot;
901 LazyBool m_is_call;
902 bool m_is_valid;
903 bool m_using_file_addr;
Raphael Isemann49254212018-08-28 15:31:01 +0000904
905private:
906 DisassemblerLLVMC::MCDisasmInstance *
907 GetDisasmToUse(bool &is_alternate_isa, DisassemblerScope &disasm) {
908 is_alternate_isa = false;
909 if (disasm) {
910 if (disasm->m_alternate_disasm_up) {
911 const AddressClass address_class = GetAddressClass();
912
913 if (address_class == AddressClass::eCodeAlternateISA) {
914 is_alternate_isa = true;
915 return disasm->m_alternate_disasm_up.get();
916 }
917 }
918 return disasm->m_disasm_up.get();
919 }
920 return nullptr;
921 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000922};
923
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000924std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>
925DisassemblerLLVMC::MCDisasmInstance::Create(const char *triple, const char *cpu,
926 const char *features_str,
927 unsigned flavor,
928 DisassemblerLLVMC &owner) {
929 using Instance = std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>;
930
Zachary Turner97206d52017-05-12 04:51:55 +0000931 std::string Status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932 const llvm::Target *curr_target =
Zachary Turner97206d52017-05-12 04:51:55 +0000933 llvm::TargetRegistry::lookupTarget(triple, Status);
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000934 if (!curr_target)
935 return Instance();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000937 std::unique_ptr<llvm::MCInstrInfo> instr_info_up(
938 curr_target->createMCInstrInfo());
939 if (!instr_info_up)
940 return Instance();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000942 std::unique_ptr<llvm::MCRegisterInfo> reg_info_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 curr_target->createMCRegInfo(triple));
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000944 if (!reg_info_up)
945 return Instance();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000947 std::unique_ptr<llvm::MCSubtargetInfo> subtarget_info_up(
948 curr_target->createMCSubtargetInfo(triple, cpu, features_str));
949 if (!subtarget_info_up)
950 return Instance();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000952 std::unique_ptr<llvm::MCAsmInfo> asm_info_up(
953 curr_target->createMCAsmInfo(*reg_info_up, triple));
954 if (!asm_info_up)
955 return Instance();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000957 std::unique_ptr<llvm::MCContext> context_up(
Konrad Kleine248a1302019-05-23 11:14:47 +0000958 new llvm::MCContext(asm_info_up.get(), reg_info_up.get(), nullptr));
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000959 if (!context_up)
960 return Instance();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000962 std::unique_ptr<llvm::MCDisassembler> disasm_up(
963 curr_target->createMCDisassembler(*subtarget_info_up, *context_up));
964 if (!disasm_up)
965 return Instance();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000966
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000967 std::unique_ptr<llvm::MCRelocationInfo> rel_info_up(
968 curr_target->createMCRelocationInfo(triple, *context_up));
969 if (!rel_info_up)
970 return Instance();
971
972 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(
973 curr_target->createMCSymbolizer(
974 triple, nullptr, DisassemblerLLVMC::SymbolLookupCallback, &owner,
975 context_up.get(), std::move(rel_info_up)));
976 disasm_up->setSymbolizer(std::move(symbolizer_up));
977
978 unsigned asm_printer_variant =
979 flavor == ~0U ? asm_info_up->getAssemblerDialect() : flavor;
980
981 std::unique_ptr<llvm::MCInstPrinter> instr_printer_up(
982 curr_target->createMCInstPrinter(llvm::Triple{triple},
983 asm_printer_variant, *asm_info_up,
984 *instr_info_up, *reg_info_up));
985 if (!instr_printer_up)
986 return Instance();
987
988 return Instance(
989 new MCDisasmInstance(std::move(instr_info_up), std::move(reg_info_up),
990 std::move(subtarget_info_up), std::move(asm_info_up),
991 std::move(context_up), std::move(disasm_up),
992 std::move(instr_printer_up)));
Jim Ingham0f063ba2013-03-02 00:26:47 +0000993}
994
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +0000995DisassemblerLLVMC::MCDisasmInstance::MCDisasmInstance(
996 std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up,
997 std::unique_ptr<llvm::MCRegisterInfo> &&reg_info_up,
998 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
999 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
1000 std::unique_ptr<llvm::MCContext> &&context_up,
1001 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
1002 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up)
1003 : m_instr_info_up(std::move(instr_info_up)),
1004 m_reg_info_up(std::move(reg_info_up)),
1005 m_subtarget_info_up(std::move(subtarget_info_up)),
1006 m_asm_info_up(std::move(asm_info_up)),
1007 m_context_up(std::move(context_up)), m_disasm_up(std::move(disasm_up)),
1008 m_instr_printer_up(std::move(instr_printer_up)) {
1009 assert(m_instr_info_up && m_reg_info_up && m_subtarget_info_up &&
1010 m_asm_info_up && m_context_up && m_disasm_up && m_instr_printer_up);
1011}
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +00001012
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001013uint64_t DisassemblerLLVMC::MCDisasmInstance::GetMCInst(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc,
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001015 llvm::MCInst &mc_inst) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
1017 llvm::MCDisassembler::DecodeStatus status;
Jim Ingham0f063ba2013-03-02 00:26:47 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 uint64_t new_inst_size;
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001020 status = m_disasm_up->getInstruction(mc_inst, new_inst_size, data, pc,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 llvm::nulls(), llvm::nulls());
1022 if (status == llvm::MCDisassembler::Success)
1023 return new_inst_size;
1024 else
1025 return 0;
Jim Ingham0f063ba2013-03-02 00:26:47 +00001026}
1027
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001028void DisassemblerLLVMC::MCDisasmInstance::PrintMCInst(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 llvm::MCInst &mc_inst, std::string &inst_string,
1030 std::string &comments_string) {
1031 llvm::raw_string_ostream inst_stream(inst_string);
1032 llvm::raw_string_ostream comments_stream(comments_string);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001033
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001034 m_instr_printer_up->setCommentStream(comments_stream);
1035 m_instr_printer_up->printInst(&mc_inst, inst_stream, llvm::StringRef(),
1036 *m_subtarget_info_up);
1037 m_instr_printer_up->setCommentStream(llvm::nulls());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 comments_stream.flush();
1039
1040 static std::string g_newlines("\r\n");
1041
1042 for (size_t newline_pos = 0;
1043 (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) !=
1044 comments_string.npos;
1045 /**/) {
1046 comments_string.replace(comments_string.begin() + newline_pos,
1047 comments_string.begin() + newline_pos + 1, 1, ' ');
1048 }
Jim Ingham0f063ba2013-03-02 00:26:47 +00001049}
1050
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001051void DisassemblerLLVMC::MCDisasmInstance::SetStyle(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 bool use_hex_immed, HexImmediateStyle hex_style) {
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001053 m_instr_printer_up->setPrintImmHex(use_hex_immed);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 switch (hex_style) {
1055 case eHexStyleC:
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001056 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::C);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057 break;
1058 case eHexStyleAsm:
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001059 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::Asm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 break;
1061 }
Daniel Malead79ae052013-08-07 21:54:09 +00001062}
1063
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001064bool DisassemblerLLVMC::MCDisasmInstance::CanBranch(
1065 llvm::MCInst &mc_inst) const {
1066 return m_instr_info_up->get(mc_inst.getOpcode())
1067 .mayAffectControlFlow(mc_inst, *m_reg_info_up);
Jim Ingham0f063ba2013-03-02 00:26:47 +00001068}
1069
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001070bool DisassemblerLLVMC::MCDisasmInstance::HasDelaySlot(
1071 llvm::MCInst &mc_inst) const {
1072 return m_instr_info_up->get(mc_inst.getOpcode()).hasDelaySlot();
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00001073}
1074
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001075bool DisassemblerLLVMC::MCDisasmInstance::IsCall(llvm::MCInst &mc_inst) const {
1076 return m_instr_info_up->get(mc_inst.getOpcode()).isCall();
Sean Callanan4740a732016-09-06 04:48:36 +00001077}
1078
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079DisassemblerLLVMC::DisassemblerLLVMC(const ArchSpec &arch,
1080 const char *flavor_string)
Konrad Kleine248a1302019-05-23 11:14:47 +00001081 : Disassembler(arch, flavor_string), m_exe_ctx(nullptr), m_inst(nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 m_data_from_file(false) {
1083 if (!FlavorValidForArchSpec(arch, m_flavor.c_str())) {
1084 m_flavor.assign("default");
1085 }
1086
1087 unsigned flavor = ~0U;
1088 llvm::Triple triple = arch.GetTriple();
1089
1090 // So far the only supported flavor is "intel" on x86. The base class will
Adrian Prantl05097242018-04-30 16:49:04 +00001091 // set this correctly coming in.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 if (triple.getArch() == llvm::Triple::x86 ||
1093 triple.getArch() == llvm::Triple::x86_64) {
1094 if (m_flavor == "intel") {
1095 flavor = 1;
1096 } else if (m_flavor == "att") {
1097 flavor = 0;
Jim Ingham0f063ba2013-03-02 00:26:47 +00001098 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001100
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 ArchSpec thumb_arch(arch);
1102 if (triple.getArch() == llvm::Triple::arm) {
1103 std::string thumb_arch_name(thumb_arch.GetTriple().getArchName().str());
1104 // Replace "arm" with "thumb" so we get all thumb variants correct
1105 if (thumb_arch_name.size() > 3) {
1106 thumb_arch_name.erase(0, 3);
1107 thumb_arch_name.insert(0, "thumb");
1108 } else {
1109 thumb_arch_name = "thumbv8.2a";
Jim Ingham0f063ba2013-03-02 00:26:47 +00001110 }
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00001111 thumb_arch.GetTriple().setArchName(llvm::StringRef(thumb_arch_name));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001113
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 // If no sub architecture specified then use the most recent arm architecture
Adrian Prantl05097242018-04-30 16:49:04 +00001115 // so the disassembler will return all instruction. Without it we will see a
1116 // lot of unknow opcode in case the code uses instructions which are not
1117 // available in the oldest arm version (used when no sub architecture is
1118 // specified)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 if (triple.getArch() == llvm::Triple::arm &&
1120 triple.getSubArch() == llvm::Triple::NoSubArch)
1121 triple.setArchName("armv8.2a");
1122
Jason Molenda0dfb84c2018-09-07 01:28:48 +00001123 std::string features_str = "";
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 const char *triple_str = triple.getTriple().c_str();
1125
1126 // ARM Cortex M0-M7 devices only execute thumb instructions
1127 if (arch.IsAlwaysThumbInstructions()) {
1128 triple_str = thumb_arch.GetTriple().getTriple().c_str();
Jason Molenda0dfb84c2018-09-07 01:28:48 +00001129 features_str += "+fp-armv8,";
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 }
1131
1132 const char *cpu = "";
1133
1134 switch (arch.GetCore()) {
1135 case ArchSpec::eCore_mips32:
1136 case ArchSpec::eCore_mips32el:
1137 cpu = "mips32";
1138 break;
1139 case ArchSpec::eCore_mips32r2:
1140 case ArchSpec::eCore_mips32r2el:
1141 cpu = "mips32r2";
1142 break;
1143 case ArchSpec::eCore_mips32r3:
1144 case ArchSpec::eCore_mips32r3el:
1145 cpu = "mips32r3";
1146 break;
1147 case ArchSpec::eCore_mips32r5:
1148 case ArchSpec::eCore_mips32r5el:
1149 cpu = "mips32r5";
1150 break;
1151 case ArchSpec::eCore_mips32r6:
1152 case ArchSpec::eCore_mips32r6el:
1153 cpu = "mips32r6";
1154 break;
1155 case ArchSpec::eCore_mips64:
1156 case ArchSpec::eCore_mips64el:
1157 cpu = "mips64";
1158 break;
1159 case ArchSpec::eCore_mips64r2:
1160 case ArchSpec::eCore_mips64r2el:
1161 cpu = "mips64r2";
1162 break;
1163 case ArchSpec::eCore_mips64r3:
1164 case ArchSpec::eCore_mips64r3el:
1165 cpu = "mips64r3";
1166 break;
1167 case ArchSpec::eCore_mips64r5:
1168 case ArchSpec::eCore_mips64r5el:
1169 cpu = "mips64r5";
1170 break;
1171 case ArchSpec::eCore_mips64r6:
1172 case ArchSpec::eCore_mips64r6el:
1173 cpu = "mips64r6";
1174 break;
1175 default:
1176 cpu = "";
1177 break;
1178 }
1179
Fangrui Songddb93b62019-05-16 08:37:32 +00001180 if (arch.IsMIPS()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181 uint32_t arch_flags = arch.GetFlags();
1182 if (arch_flags & ArchSpec::eMIPSAse_msa)
1183 features_str += "+msa,";
1184 if (arch_flags & ArchSpec::eMIPSAse_dsp)
1185 features_str += "+dsp,";
1186 if (arch_flags & ArchSpec::eMIPSAse_dspr2)
1187 features_str += "+dspr2,";
1188 }
1189
Omair Javaid772176d2019-05-23 00:46:34 +00001190 // If any AArch64 variant, enable the ARMv8.5 ISA with SVE extensions so we
1191 // can disassemble newer instructions.
Jason Molendaa22e9232017-12-22 00:16:04 +00001192 if (triple.getArch() == llvm::Triple::aarch64)
Omair Javaid772176d2019-05-23 00:46:34 +00001193 features_str += "+v8.5a,+sve2";
Jason Molendaa22e9232017-12-22 00:16:04 +00001194
Jason Molendaa5834862019-03-07 03:16:45 +00001195 if (triple.getArch() == llvm::Triple::aarch64
1196 && triple.getVendor() == llvm::Triple::Apple) {
1197 cpu = "apple-latest";
1198 }
1199
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001200 // We use m_disasm_up.get() to tell whether we are valid or not, so if this
Adrian Prantl05097242018-04-30 16:49:04 +00001201 // isn't good for some reason, we won't be valid and FindPlugin will fail and
1202 // we won't get used.
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001203 m_disasm_up = MCDisasmInstance::Create(triple_str, cpu, features_str.c_str(),
1204 flavor, *this);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205
1206 llvm::Triple::ArchType llvm_arch = triple.getArch();
1207
1208 // For arm CPUs that can execute arm or thumb instructions, also create a
1209 // thumb instruction disassembler.
1210 if (llvm_arch == llvm::Triple::arm) {
1211 std::string thumb_triple(thumb_arch.GetTriple().getTriple());
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001212 m_alternate_disasm_up =
Jason Molenda0dfb84c2018-09-07 01:28:48 +00001213 MCDisasmInstance::Create(thumb_triple.c_str(), "", features_str.c_str(),
1214 flavor, *this);
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001215 if (!m_alternate_disasm_up)
1216 m_disasm_up.reset();
1217
Fangrui Songddb93b62019-05-16 08:37:32 +00001218 } else if (arch.IsMIPS()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219 /* Create alternate disassembler for MIPS16 and microMIPS */
1220 uint32_t arch_flags = arch.GetFlags();
1221 if (arch_flags & ArchSpec::eMIPSAse_mips16)
1222 features_str += "+mips16,";
1223 else if (arch_flags & ArchSpec::eMIPSAse_micromips)
1224 features_str += "+micromips,";
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001225
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001226 m_alternate_disasm_up = MCDisasmInstance::Create(
1227 triple_str, cpu, features_str.c_str(), flavor, *this);
1228 if (!m_alternate_disasm_up)
1229 m_disasm_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 }
Sean Callanan95e5c632012-02-17 00:53:45 +00001231}
1232
Eugene Zelenko45a40142015-10-22 21:24:37 +00001233DisassemblerLLVMC::~DisassemblerLLVMC() = default;
1234
Kate Stoneb9c1b512016-09-06 20:57:50 +00001235Disassembler *DisassemblerLLVMC::CreateInstance(const ArchSpec &arch,
1236 const char *flavor) {
1237 if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001238 std::unique_ptr<DisassemblerLLVMC> disasm_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 new DisassemblerLLVMC(arch, flavor));
Eugene Zelenko45a40142015-10-22 21:24:37 +00001240
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001241 if (disasm_up.get() && disasm_up->IsValid())
1242 return disasm_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243 }
Konrad Kleine248a1302019-05-23 11:14:47 +00001244 return nullptr;
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +00001245}
1246
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247size_t DisassemblerLLVMC::DecodeInstructions(const Address &base_addr,
1248 const DataExtractor &data,
1249 lldb::offset_t data_offset,
1250 size_t num_instructions,
1251 bool append, bool data_from_file) {
1252 if (!append)
1253 m_instruction_list.Clear();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001254
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 if (!IsValid())
Sean Callanan95e5c632012-02-17 00:53:45 +00001256 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257
1258 m_data_from_file = data_from_file;
1259 uint32_t data_cursor = data_offset;
1260 const size_t data_byte_size = data.GetByteSize();
1261 uint32_t instructions_parsed = 0;
1262 Address inst_addr(base_addr);
1263
1264 while (data_cursor < data_byte_size &&
1265 instructions_parsed < num_instructions) {
1266
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001267 AddressClass address_class = AddressClass::eCode;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001268
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001269 if (m_alternate_disasm_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270 address_class = inst_addr.GetAddressClass();
1271
1272 InstructionSP inst_sp(
1273 new InstructionLLVMC(*this, inst_addr, address_class));
1274
1275 if (!inst_sp)
1276 break;
1277
1278 uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
1279
1280 if (inst_size == 0)
1281 break;
1282
1283 m_instruction_list.Append(inst_sp);
1284 data_cursor += inst_size;
1285 inst_addr.Slide(inst_size);
1286 instructions_parsed++;
1287 }
1288
1289 return data_cursor - data_offset;
Sean Callanan95e5c632012-02-17 00:53:45 +00001290}
1291
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292void DisassemblerLLVMC::Initialize() {
1293 PluginManager::RegisterPlugin(GetPluginNameStatic(),
1294 "Disassembler that uses LLVM MC to disassemble "
1295 "i386, x86_64, ARM, and ARM64.",
1296 CreateInstance);
Jason Molendac980fa92015-02-13 23:24:21 +00001297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 llvm::InitializeAllTargetInfos();
1299 llvm::InitializeAllTargetMCs();
1300 llvm::InitializeAllAsmParsers();
1301 llvm::InitializeAllDisassemblers();
1302}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001303
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304void DisassemblerLLVMC::Terminate() {
1305 PluginManager::UnregisterPlugin(CreateInstance);
1306}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001307
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308ConstString DisassemblerLLVMC::GetPluginNameStatic() {
1309 static ConstString g_name("llvm-mc");
1310 return g_name;
1311}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001312
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313int DisassemblerLLVMC::OpInfoCallback(void *disassembler, uint64_t pc,
1314 uint64_t offset, uint64_t size,
1315 int tag_type, void *tag_bug) {
1316 return static_cast<DisassemblerLLVMC *>(disassembler)
1317 ->OpInfo(pc, offset, size, tag_type, tag_bug);
1318}
1319
1320const char *DisassemblerLLVMC::SymbolLookupCallback(void *disassembler,
1321 uint64_t value,
1322 uint64_t *type, uint64_t pc,
1323 const char **name) {
1324 return static_cast<DisassemblerLLVMC *>(disassembler)
1325 ->SymbolLookup(value, type, pc, name);
1326}
1327
1328bool DisassemblerLLVMC::FlavorValidForArchSpec(
1329 const lldb_private::ArchSpec &arch, const char *flavor) {
1330 llvm::Triple triple = arch.GetTriple();
Konrad Kleine248a1302019-05-23 11:14:47 +00001331 if (flavor == nullptr || strcmp(flavor, "default") == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332 return true;
1333
1334 if (triple.getArch() == llvm::Triple::x86 ||
1335 triple.getArch() == llvm::Triple::x86_64) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001336 return strcmp(flavor, "intel") == 0 || strcmp(flavor, "att") == 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 } else
1338 return false;
1339}
1340
Tatyana Krasnukha6c2c08f2018-01-11 12:06:22 +00001341bool DisassemblerLLVMC::IsValid() const { return m_disasm_up.operator bool(); }
1342
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343int DisassemblerLLVMC::OpInfo(uint64_t PC, uint64_t Offset, uint64_t Size,
1344 int tag_type, void *tag_bug) {
1345 switch (tag_type) {
1346 default:
1347 break;
1348 case 1:
1349 memset(tag_bug, 0, sizeof(::LLVMOpInfo1));
1350 break;
1351 }
1352 return 0;
1353}
1354
1355const char *DisassemblerLLVMC::SymbolLookup(uint64_t value, uint64_t *type_ptr,
1356 uint64_t pc, const char **name) {
1357 if (*type_ptr) {
1358 if (m_exe_ctx && m_inst) {
1359 // std::string remove_this_prior_to_checkin;
Konrad Kleine248a1302019-05-23 11:14:47 +00001360 Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 Address value_so_addr;
1362 Address pc_so_addr;
1363 if (m_inst->UsingFileAddress()) {
1364 ModuleSP module_sp(m_inst->GetAddress().GetModule());
1365 if (module_sp) {
1366 module_sp->ResolveFileAddress(value, value_so_addr);
1367 module_sp->ResolveFileAddress(pc, pc_so_addr);
Sean Callanan95e5c632012-02-17 00:53:45 +00001368 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 } else if (target && !target->GetSectionLoadList().IsEmpty()) {
1370 target->GetSectionLoadList().ResolveLoadAddress(value, value_so_addr);
1371 target->GetSectionLoadList().ResolveLoadAddress(pc, pc_so_addr);
1372 }
Greg Claytonba812f42012-05-10 02:52:23 +00001373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 SymbolContext sym_ctx;
Zachary Turner991e4452018-10-25 20:45:19 +00001375 const SymbolContextItem resolve_scope =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 eSymbolContextFunction | eSymbolContextSymbol;
1377 if (pc_so_addr.IsValid() && pc_so_addr.GetModule()) {
1378 pc_so_addr.GetModule()->ResolveSymbolContextForAddress(
1379 pc_so_addr, resolve_scope, sym_ctx);
1380 }
1381
1382 if (value_so_addr.IsValid() && value_so_addr.GetSection()) {
1383 StreamString ss;
1384
1385 bool format_omitting_current_func_name = false;
1386 if (sym_ctx.symbol || sym_ctx.function) {
1387 AddressRange range;
1388 if (sym_ctx.GetAddressRange(resolve_scope, 0, false, range) &&
1389 range.GetBaseAddress().IsValid() &&
1390 range.ContainsLoadAddress(value_so_addr, target)) {
1391 format_omitting_current_func_name = true;
1392 }
1393 }
1394
Adrian Prantl05097242018-04-30 16:49:04 +00001395 // If the "value" address (the target address we're symbolicating) is
1396 // inside the same SymbolContext as the current instruction pc
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 // (pc_so_addr), don't print the full function name - just print it
1398 // with DumpStyleNoFunctionName style, e.g. "<+36>".
1399 if (format_omitting_current_func_name) {
1400 value_so_addr.Dump(&ss, target, Address::DumpStyleNoFunctionName,
1401 Address::DumpStyleSectionNameOffset);
1402 } else {
1403 value_so_addr.Dump(
1404 &ss, target,
1405 Address::DumpStyleResolvedDescriptionNoFunctionArguments,
1406 Address::DumpStyleSectionNameOffset);
1407 }
1408
1409 if (!ss.GetString().empty()) {
1410 // If Address::Dump returned a multi-line description, most commonly
Adrian Prantl05097242018-04-30 16:49:04 +00001411 // seen when we have multiple levels of inlined functions at an
1412 // address, only show the first line.
Zachary Turnerc1564272016-11-16 21:15:24 +00001413 std::string str = ss.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414 size_t first_eol_char = str.find_first_of("\r\n");
1415 if (first_eol_char != std::string::npos) {
1416 str.erase(first_eol_char);
1417 }
Zachary Turnerc1564272016-11-16 21:15:24 +00001418 m_inst->AppendComment(str);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419 }
1420 }
1421 }
1422 }
1423
1424 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
Konrad Kleine248a1302019-05-23 11:14:47 +00001425 *name = nullptr;
1426 return nullptr;
Sean Callanan95e5c632012-02-17 00:53:45 +00001427}
1428
Sean Callanan95e5c632012-02-17 00:53:45 +00001429// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430ConstString DisassemblerLLVMC::GetPluginName() { return GetPluginNameStatic(); }
Sean Callanan95e5c632012-02-17 00:53:45 +00001431
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432uint32_t DisassemblerLLVMC::GetPluginVersion() { return 1; }