1 //===-- Disassembler.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 "lldb/Core/Disassembler.h"
11 
12 #include "lldb/Core/AddressRange.h" // for AddressRange
13 #include "lldb/Core/Debugger.h"
14 #include "lldb/Core/EmulateInstruction.h"
15 #include "lldb/Core/Mangled.h" // for Mangled, Mangled...
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/ModuleList.h" // for ModuleList
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Core/SourceManager.h" // for SourceManager
20 #include "lldb/Host/FileSystem.h"
21 #include "lldb/Interpreter/OptionValue.h"
22 #include "lldb/Interpreter/OptionValueArray.h"
23 #include "lldb/Interpreter/OptionValueDictionary.h"
24 #include "lldb/Interpreter/OptionValueRegex.h"
25 #include "lldb/Interpreter/OptionValueString.h"
26 #include "lldb/Interpreter/OptionValueUInt64.h"
27 #include "lldb/Symbol/Function.h"
28 #include "lldb/Symbol/Symbol.h"        // for Symbol
29 #include "lldb/Symbol/SymbolContext.h" // for SymbolContext
30 #include "lldb/Target/ExecutionContext.h"
31 #include "lldb/Target/SectionLoadList.h"
32 #include "lldb/Target/StackFrame.h"
33 #include "lldb/Target/Target.h"
34 #include "lldb/Target/Thread.h" // for Thread
35 #include "lldb/Utility/DataBufferHeap.h"
36 #include "lldb/Utility/DataExtractor.h"
37 #include "lldb/Utility/RegularExpression.h"
38 #include "lldb/Utility/Status.h"
39 #include "lldb/Utility/Stream.h"       // for Stream
40 #include "lldb/Utility/StreamString.h" // for StreamString
41 #include "lldb/Utility/Timer.h"
42 #include "lldb/lldb-private-enumerations.h" // for InstructionType:...
43 #include "lldb/lldb-private-interfaces.h"   // for DisassemblerCrea...
44 #include "lldb/lldb-private-types.h"        // for RegisterInfo
45 #include "llvm/ADT/Triple.h"                // for Triple, Triple::...
46 #include "llvm/Support/Compiler.h"          // for LLVM_PRETTY_FUNC...
47 
48 #include <cstdint> // for uint32_t, UINT32...
49 #include <cstring>
50 #include <utility> // for pair
51 
52 #include <assert.h> // for assert
53 
54 #define DEFAULT_DISASM_BYTE_SIZE 32
55 
56 using namespace lldb;
57 using namespace lldb_private;
58 
59 DisassemblerSP Disassembler::FindPlugin(const ArchSpec &arch,
60                                         const char *flavor,
61                                         const char *plugin_name) {
62   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
63   Timer scoped_timer(func_cat,
64                      "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
65                      arch.GetArchitectureName(), plugin_name);
66 
67   DisassemblerCreateInstance create_callback = nullptr;
68 
69   if (plugin_name) {
70     ConstString const_plugin_name(plugin_name);
71     create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName(
72         const_plugin_name);
73     if (create_callback) {
74       DisassemblerSP disassembler_sp(create_callback(arch, flavor));
75 
76       if (disassembler_sp)
77         return disassembler_sp;
78     }
79   } else {
80     for (uint32_t idx = 0;
81          (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(
82               idx)) != nullptr;
83          ++idx) {
84       DisassemblerSP disassembler_sp(create_callback(arch, flavor));
85 
86       if (disassembler_sp)
87         return disassembler_sp;
88     }
89   }
90   return DisassemblerSP();
91 }
92 
93 DisassemblerSP Disassembler::FindPluginForTarget(const TargetSP target_sp,
94                                                  const ArchSpec &arch,
95                                                  const char *flavor,
96                                                  const char *plugin_name) {
97   if (target_sp && flavor == nullptr) {
98     // FIXME - we don't have the mechanism in place to do per-architecture
99     // settings.  But since we know that for now we only support flavors on x86
100     // & x86_64,
101     if (arch.GetTriple().getArch() == llvm::Triple::x86 ||
102         arch.GetTriple().getArch() == llvm::Triple::x86_64)
103       flavor = target_sp->GetDisassemblyFlavor();
104   }
105   return FindPlugin(arch, flavor, plugin_name);
106 }
107 
108 static void ResolveAddress(const ExecutionContext &exe_ctx, const Address &addr,
109                            Address &resolved_addr) {
110   if (!addr.IsSectionOffset()) {
111     // If we weren't passed in a section offset address range, try and resolve
112     // it to something
113     Target *target = exe_ctx.GetTargetPtr();
114     if (target) {
115       if (target->GetSectionLoadList().IsEmpty()) {
116         target->GetImages().ResolveFileAddress(addr.GetOffset(), resolved_addr);
117       } else {
118         target->GetSectionLoadList().ResolveLoadAddress(addr.GetOffset(),
119                                                         resolved_addr);
120       }
121       // We weren't able to resolve the address, just treat it as a raw address
122       if (resolved_addr.IsValid())
123         return;
124     }
125   }
126   resolved_addr = addr;
127 }
128 
129 size_t Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
130                                  const char *plugin_name, const char *flavor,
131                                  const ExecutionContext &exe_ctx,
132                                  SymbolContextList &sc_list,
133                                  uint32_t num_instructions,
134                                  bool mixed_source_and_assembly,
135                                  uint32_t num_mixed_context_lines,
136                                  uint32_t options, Stream &strm) {
137   size_t success_count = 0;
138   const size_t count = sc_list.GetSize();
139   SymbolContext sc;
140   AddressRange range;
141   const uint32_t scope =
142       eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
143   const bool use_inline_block_range = true;
144   for (size_t i = 0; i < count; ++i) {
145     if (!sc_list.GetContextAtIndex(i, sc))
146       break;
147     for (uint32_t range_idx = 0;
148          sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
149          ++range_idx) {
150       if (Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
151                       num_instructions, mixed_source_and_assembly,
152                       num_mixed_context_lines, options, strm)) {
153         ++success_count;
154         strm.EOL();
155       }
156     }
157   }
158   return success_count;
159 }
160 
161 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
162                                const char *plugin_name, const char *flavor,
163                                const ExecutionContext &exe_ctx,
164                                const ConstString &name, Module *module,
165                                uint32_t num_instructions,
166                                bool mixed_source_and_assembly,
167                                uint32_t num_mixed_context_lines,
168                                uint32_t options, Stream &strm) {
169   SymbolContextList sc_list;
170   if (name) {
171     const bool include_symbols = true;
172     const bool include_inlines = true;
173     if (module) {
174       module->FindFunctions(name, nullptr, eFunctionNameTypeAuto,
175                             include_symbols, include_inlines, true, sc_list);
176     } else if (exe_ctx.GetTargetPtr()) {
177       exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
178           name, eFunctionNameTypeAuto, include_symbols, include_inlines, false,
179           sc_list);
180     }
181   }
182 
183   if (sc_list.GetSize()) {
184     return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
185                        num_instructions, mixed_source_and_assembly,
186                        num_mixed_context_lines, options, strm);
187   }
188   return false;
189 }
190 
191 lldb::DisassemblerSP Disassembler::DisassembleRange(
192     const ArchSpec &arch, const char *plugin_name, const char *flavor,
193     const ExecutionContext &exe_ctx, const AddressRange &range,
194     bool prefer_file_cache) {
195   lldb::DisassemblerSP disasm_sp;
196   if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) {
197     disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch,
198                                                   flavor, plugin_name);
199 
200     if (disasm_sp) {
201       size_t bytes_disassembled = disasm_sp->ParseInstructions(
202           &exe_ctx, range, nullptr, prefer_file_cache);
203       if (bytes_disassembled == 0)
204         disasm_sp.reset();
205     }
206   }
207   return disasm_sp;
208 }
209 
210 lldb::DisassemblerSP
211 Disassembler::DisassembleBytes(const ArchSpec &arch, const char *plugin_name,
212                                const char *flavor, const Address &start,
213                                const void *src, size_t src_len,
214                                uint32_t num_instructions, bool data_from_file) {
215   lldb::DisassemblerSP disasm_sp;
216 
217   if (src) {
218     disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
219 
220     if (disasm_sp) {
221       DataExtractor data(src, src_len, arch.GetByteOrder(),
222                          arch.GetAddressByteSize());
223 
224       (void)disasm_sp->DecodeInstructions(start, data, 0, num_instructions,
225                                           false, data_from_file);
226     }
227   }
228 
229   return disasm_sp;
230 }
231 
232 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
233                                const char *plugin_name, const char *flavor,
234                                const ExecutionContext &exe_ctx,
235                                const AddressRange &disasm_range,
236                                uint32_t num_instructions,
237                                bool mixed_source_and_assembly,
238                                uint32_t num_mixed_context_lines,
239                                uint32_t options, Stream &strm) {
240   if (disasm_range.GetByteSize()) {
241     lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
242         exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
243 
244     if (disasm_sp) {
245       AddressRange range;
246       ResolveAddress(exe_ctx, disasm_range.GetBaseAddress(),
247                      range.GetBaseAddress());
248       range.SetByteSize(disasm_range.GetByteSize());
249       const bool prefer_file_cache = false;
250       size_t bytes_disassembled = disasm_sp->ParseInstructions(
251           &exe_ctx, range, &strm, prefer_file_cache);
252       if (bytes_disassembled == 0)
253         return false;
254 
255       return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx,
256                                num_instructions, mixed_source_and_assembly,
257                                num_mixed_context_lines, options, strm);
258     }
259   }
260   return false;
261 }
262 
263 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
264                                const char *plugin_name, const char *flavor,
265                                const ExecutionContext &exe_ctx,
266                                const Address &start_address,
267                                uint32_t num_instructions,
268                                bool mixed_source_and_assembly,
269                                uint32_t num_mixed_context_lines,
270                                uint32_t options, Stream &strm) {
271   if (num_instructions > 0) {
272     lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
273         exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
274     if (disasm_sp) {
275       Address addr;
276       ResolveAddress(exe_ctx, start_address, addr);
277       const bool prefer_file_cache = false;
278       size_t bytes_disassembled = disasm_sp->ParseInstructions(
279           &exe_ctx, addr, num_instructions, prefer_file_cache);
280       if (bytes_disassembled == 0)
281         return false;
282       return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx,
283                                num_instructions, mixed_source_and_assembly,
284                                num_mixed_context_lines, options, strm);
285     }
286   }
287   return false;
288 }
289 
290 Disassembler::SourceLine
291 Disassembler::GetFunctionDeclLineEntry(const SymbolContext &sc) {
292   SourceLine decl_line;
293   if (sc.function && sc.line_entry.IsValid()) {
294     LineEntry prologue_end_line = sc.line_entry;
295     FileSpec func_decl_file;
296     uint32_t func_decl_line;
297     sc.function->GetStartLineSourceInfo(func_decl_file, func_decl_line);
298     if (func_decl_file == prologue_end_line.file ||
299         func_decl_file == prologue_end_line.original_file) {
300       decl_line.file = func_decl_file;
301       decl_line.line = func_decl_line;
302       // TODO do we care about column on these entries?  If so, we need to
303       // plumb that through GetStartLineSourceInfo.
304       decl_line.column = 0;
305     }
306   }
307   return decl_line;
308 }
309 
310 void Disassembler::AddLineToSourceLineTables(
311     SourceLine &line,
312     std::map<FileSpec, std::set<uint32_t>> &source_lines_seen) {
313   if (line.IsValid()) {
314     auto source_lines_seen_pos = source_lines_seen.find(line.file);
315     if (source_lines_seen_pos == source_lines_seen.end()) {
316       std::set<uint32_t> lines;
317       lines.insert(line.line);
318       source_lines_seen.emplace(line.file, lines);
319     } else {
320       source_lines_seen_pos->second.insert(line.line);
321     }
322   }
323 }
324 
325 bool Disassembler::ElideMixedSourceAndDisassemblyLine(
326     const ExecutionContext &exe_ctx, const SymbolContext &sc,
327     SourceLine &line) {
328 
329   // TODO: should we also check target.process.thread.step-avoid-libraries ?
330 
331   const RegularExpression *avoid_regex = nullptr;
332 
333   // Skip any line #0 entries - they are implementation details
334   if (line.line == 0)
335     return false;
336 
337   ThreadSP thread_sp = exe_ctx.GetThreadSP();
338   if (thread_sp) {
339     avoid_regex = thread_sp->GetSymbolsToAvoidRegexp();
340   } else {
341     TargetSP target_sp = exe_ctx.GetTargetSP();
342     if (target_sp) {
343       Status error;
344       OptionValueSP value_sp = target_sp->GetDebugger().GetPropertyValue(
345           &exe_ctx, "target.process.thread.step-avoid-regexp", false, error);
346       if (value_sp && value_sp->GetType() == OptionValue::eTypeRegex) {
347         OptionValueRegex *re = value_sp->GetAsRegex();
348         if (re) {
349           avoid_regex = re->GetCurrentValue();
350         }
351       }
352     }
353   }
354   if (avoid_regex && sc.symbol != nullptr) {
355     const char *function_name =
356         sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
357             .GetCString();
358     if (function_name) {
359       RegularExpression::Match regex_match(1);
360       if (avoid_regex->Execute(function_name, &regex_match)) {
361         // skip this source line
362         return true;
363       }
364     }
365   }
366   // don't skip this source line
367   return false;
368 }
369 
370 bool Disassembler::PrintInstructions(Disassembler *disasm_ptr,
371                                      Debugger &debugger, const ArchSpec &arch,
372                                      const ExecutionContext &exe_ctx,
373                                      uint32_t num_instructions,
374                                      bool mixed_source_and_assembly,
375                                      uint32_t num_mixed_context_lines,
376                                      uint32_t options, Stream &strm) {
377   // We got some things disassembled...
378   size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
379 
380   if (num_instructions > 0 && num_instructions < num_instructions_found)
381     num_instructions_found = num_instructions;
382 
383   const uint32_t max_opcode_byte_size =
384       disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize();
385   SymbolContext sc;
386   SymbolContext prev_sc;
387   AddressRange current_source_line_range;
388   const Address *pc_addr_ptr = nullptr;
389   StackFrame *frame = exe_ctx.GetFramePtr();
390 
391   TargetSP target_sp(exe_ctx.GetTargetSP());
392   SourceManager &source_manager =
393       target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager();
394 
395   if (frame) {
396     pc_addr_ptr = &frame->GetFrameCodeAddress();
397   }
398   const uint32_t scope =
399       eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
400   const bool use_inline_block_range = false;
401 
402   const FormatEntity::Entry *disassembly_format = nullptr;
403   FormatEntity::Entry format;
404   if (exe_ctx.HasTargetScope()) {
405     disassembly_format =
406         exe_ctx.GetTargetRef().GetDebugger().GetDisassemblyFormat();
407   } else {
408     FormatEntity::Parse("${addr}: ", format);
409     disassembly_format = &format;
410   }
411 
412   // First pass: step through the list of instructions, find how long the
413   // initial addresses strings are, insert padding in the second pass so the
414   // opcodes all line up nicely.
415 
416   // Also build up the source line mapping if this is mixed source & assembly
417   // mode. Calculate the source line for each assembly instruction (eliding
418   // inlined functions which the user wants to skip).
419 
420   std::map<FileSpec, std::set<uint32_t>> source_lines_seen;
421   Symbol *previous_symbol = nullptr;
422 
423   size_t address_text_size = 0;
424   for (size_t i = 0; i < num_instructions_found; ++i) {
425     Instruction *inst =
426         disasm_ptr->GetInstructionList().GetInstructionAtIndex(i).get();
427     if (inst) {
428       const Address &addr = inst->GetAddress();
429       ModuleSP module_sp(addr.GetModule());
430       if (module_sp) {
431         const uint32_t resolve_mask = eSymbolContextFunction |
432                                       eSymbolContextSymbol |
433                                       eSymbolContextLineEntry;
434         uint32_t resolved_mask =
435             module_sp->ResolveSymbolContextForAddress(addr, resolve_mask, sc);
436         if (resolved_mask) {
437           StreamString strmstr;
438           Debugger::FormatDisassemblerAddress(disassembly_format, &sc, nullptr,
439                                               &exe_ctx, &addr, strmstr);
440           size_t cur_line = strmstr.GetSizeOfLastLine();
441           if (cur_line > address_text_size)
442             address_text_size = cur_line;
443 
444           // Add entries to our "source_lines_seen" map+set which list which
445           // sources lines occur in this disassembly session.  We will print
446           // lines of context around a source line, but we don't want to print
447           // a source line that has a line table entry of its own - we'll leave
448           // that source line to be printed when it actually occurs in the
449           // disassembly.
450 
451           if (mixed_source_and_assembly && sc.line_entry.IsValid()) {
452             if (sc.symbol != previous_symbol) {
453               SourceLine decl_line = GetFunctionDeclLineEntry(sc);
454               if (ElideMixedSourceAndDisassemblyLine(exe_ctx, sc, decl_line) ==
455                   false)
456                 AddLineToSourceLineTables(decl_line, source_lines_seen);
457             }
458             if (sc.line_entry.IsValid()) {
459               SourceLine this_line;
460               this_line.file = sc.line_entry.file;
461               this_line.line = sc.line_entry.line;
462               this_line.column = sc.line_entry.column;
463               if (ElideMixedSourceAndDisassemblyLine(exe_ctx, sc, this_line) ==
464                   false)
465                 AddLineToSourceLineTables(this_line, source_lines_seen);
466             }
467           }
468         }
469         sc.Clear(false);
470       }
471     }
472   }
473 
474   previous_symbol = nullptr;
475   SourceLine previous_line;
476   for (size_t i = 0; i < num_instructions_found; ++i) {
477     Instruction *inst =
478         disasm_ptr->GetInstructionList().GetInstructionAtIndex(i).get();
479 
480     if (inst) {
481       const Address &addr = inst->GetAddress();
482       const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
483       SourceLinesToDisplay source_lines_to_display;
484 
485       prev_sc = sc;
486 
487       ModuleSP module_sp(addr.GetModule());
488       if (module_sp) {
489         uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(
490             addr, eSymbolContextEverything, sc);
491         if (resolved_mask) {
492           if (mixed_source_and_assembly) {
493 
494             // If we've started a new function (non-inlined), print all of the
495             // source lines from the function declaration until the first line
496             // table entry - typically the opening curly brace of the function.
497             if (previous_symbol != sc.symbol) {
498               // The default disassembly format puts an extra blank line
499               // between functions - so when we're displaying the source
500               // context for a function, we don't want to add a blank line
501               // after the source context or we'll end up with two of them.
502               if (previous_symbol != nullptr)
503                 source_lines_to_display.print_source_context_end_eol = false;
504 
505               previous_symbol = sc.symbol;
506               if (sc.function && sc.line_entry.IsValid()) {
507                 LineEntry prologue_end_line = sc.line_entry;
508                 if (ElideMixedSourceAndDisassemblyLine(
509                         exe_ctx, sc, prologue_end_line) == false) {
510                   FileSpec func_decl_file;
511                   uint32_t func_decl_line;
512                   sc.function->GetStartLineSourceInfo(func_decl_file,
513                                                       func_decl_line);
514                   if (func_decl_file == prologue_end_line.file ||
515                       func_decl_file == prologue_end_line.original_file) {
516                     // Add all the lines between the function declaration and
517                     // the first non-prologue source line to the list of lines
518                     // to print.
519                     for (uint32_t lineno = func_decl_line;
520                          lineno <= prologue_end_line.line; lineno++) {
521                       SourceLine this_line;
522                       this_line.file = func_decl_file;
523                       this_line.line = lineno;
524                       source_lines_to_display.lines.push_back(this_line);
525                     }
526                     // Mark the last line as the "current" one.  Usually this
527                     // is the open curly brace.
528                     if (source_lines_to_display.lines.size() > 0)
529                       source_lines_to_display.current_source_line =
530                           source_lines_to_display.lines.size() - 1;
531                   }
532                 }
533               }
534               sc.GetAddressRange(scope, 0, use_inline_block_range,
535                                  current_source_line_range);
536             }
537 
538             // If we've left a previous source line's address range, print a
539             // new source line
540             if (!current_source_line_range.ContainsFileAddress(addr)) {
541               sc.GetAddressRange(scope, 0, use_inline_block_range,
542                                  current_source_line_range);
543 
544               if (sc != prev_sc && sc.comp_unit && sc.line_entry.IsValid()) {
545                 SourceLine this_line;
546                 this_line.file = sc.line_entry.file;
547                 this_line.line = sc.line_entry.line;
548 
549                 if (ElideMixedSourceAndDisassemblyLine(exe_ctx, sc,
550                                                        this_line) == false) {
551                   // Only print this source line if it is different from the
552                   // last source line we printed.  There may have been inlined
553                   // functions between these lines that we elided, resulting in
554                   // the same line being printed twice in a row for a
555                   // contiguous block of assembly instructions.
556                   if (this_line != previous_line) {
557 
558                     std::vector<uint32_t> previous_lines;
559                     for (uint32_t i = 0;
560                          i < num_mixed_context_lines &&
561                          (this_line.line - num_mixed_context_lines) > 0;
562                          i++) {
563                       uint32_t line =
564                           this_line.line - num_mixed_context_lines + i;
565                       auto pos = source_lines_seen.find(this_line.file);
566                       if (pos != source_lines_seen.end()) {
567                         if (pos->second.count(line) == 1) {
568                           previous_lines.clear();
569                         } else {
570                           previous_lines.push_back(line);
571                         }
572                       }
573                     }
574                     for (size_t i = 0; i < previous_lines.size(); i++) {
575                       SourceLine previous_line;
576                       previous_line.file = this_line.file;
577                       previous_line.line = previous_lines[i];
578                       auto pos = source_lines_seen.find(previous_line.file);
579                       if (pos != source_lines_seen.end()) {
580                         pos->second.insert(previous_line.line);
581                       }
582                       source_lines_to_display.lines.push_back(previous_line);
583                     }
584 
585                     source_lines_to_display.lines.push_back(this_line);
586                     source_lines_to_display.current_source_line =
587                         source_lines_to_display.lines.size() - 1;
588 
589                     for (uint32_t i = 0; i < num_mixed_context_lines; i++) {
590                       SourceLine next_line;
591                       next_line.file = this_line.file;
592                       next_line.line = this_line.line + i + 1;
593                       auto pos = source_lines_seen.find(next_line.file);
594                       if (pos != source_lines_seen.end()) {
595                         if (pos->second.count(next_line.line) == 1)
596                           break;
597                         pos->second.insert(next_line.line);
598                       }
599                       source_lines_to_display.lines.push_back(next_line);
600                     }
601                   }
602                   previous_line = this_line;
603                 }
604               }
605             }
606           }
607         } else {
608           sc.Clear(true);
609         }
610       }
611 
612       if (source_lines_to_display.lines.size() > 0) {
613         strm.EOL();
614         for (size_t idx = 0; idx < source_lines_to_display.lines.size();
615              idx++) {
616           SourceLine ln = source_lines_to_display.lines[idx];
617           const char *line_highlight = "";
618           if (inst_is_at_pc && (options & eOptionMarkPCSourceLine)) {
619             line_highlight = "->";
620           } else if (idx == source_lines_to_display.current_source_line) {
621             line_highlight = "**";
622           }
623           source_manager.DisplaySourceLinesWithLineNumbers(
624               ln.file, ln.line, ln.column, 0, 0, line_highlight, &strm);
625         }
626         if (source_lines_to_display.print_source_context_end_eol)
627           strm.EOL();
628       }
629 
630       const bool show_bytes = (options & eOptionShowBytes) != 0;
631       inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx, &sc,
632                  &prev_sc, nullptr, address_text_size);
633       strm.EOL();
634     } else {
635       break;
636     }
637   }
638 
639   return true;
640 }
641 
642 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
643                                const char *plugin_name, const char *flavor,
644                                const ExecutionContext &exe_ctx,
645                                uint32_t num_instructions,
646                                bool mixed_source_and_assembly,
647                                uint32_t num_mixed_context_lines,
648                                uint32_t options, Stream &strm) {
649   AddressRange range;
650   StackFrame *frame = exe_ctx.GetFramePtr();
651   if (frame) {
652     SymbolContext sc(
653         frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
654     if (sc.function) {
655       range = sc.function->GetAddressRange();
656     } else if (sc.symbol && sc.symbol->ValueIsAddress()) {
657       range.GetBaseAddress() = sc.symbol->GetAddressRef();
658       range.SetByteSize(sc.symbol->GetByteSize());
659     } else {
660       range.GetBaseAddress() = frame->GetFrameCodeAddress();
661     }
662 
663     if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
664       range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
665   }
666 
667   return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
668                      num_instructions, mixed_source_and_assembly,
669                      num_mixed_context_lines, options, strm);
670 }
671 
672 Instruction::Instruction(const Address &address, AddressClass addr_class)
673     : m_address(address), m_address_class(addr_class), m_opcode(),
674       m_calculated_strings(false) {}
675 
676 Instruction::~Instruction() = default;
677 
678 AddressClass Instruction::GetAddressClass() {
679   if (m_address_class == eAddressClassInvalid)
680     m_address_class = m_address.GetAddressClass();
681   return m_address_class;
682 }
683 
684 void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size,
685                        bool show_address, bool show_bytes,
686                        const ExecutionContext *exe_ctx,
687                        const SymbolContext *sym_ctx,
688                        const SymbolContext *prev_sym_ctx,
689                        const FormatEntity::Entry *disassembly_addr_format,
690                        size_t max_address_text_size) {
691   size_t opcode_column_width = 7;
692   const size_t operand_column_width = 25;
693 
694   CalculateMnemonicOperandsAndCommentIfNeeded(exe_ctx);
695 
696   StreamString ss;
697 
698   if (show_address) {
699     Debugger::FormatDisassemblerAddress(disassembly_addr_format, sym_ctx,
700                                         prev_sym_ctx, exe_ctx, &m_address, ss);
701     ss.FillLastLineToColumn(max_address_text_size, ' ');
702   }
703 
704   if (show_bytes) {
705     if (m_opcode.GetType() == Opcode::eTypeBytes) {
706       // x86_64 and i386 are the only ones that use bytes right now so pad out
707       // the byte dump to be able to always show 15 bytes (3 chars each) plus a
708       // space
709       if (max_opcode_byte_size > 0)
710         m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1);
711       else
712         m_opcode.Dump(&ss, 15 * 3 + 1);
713     } else {
714       // Else, we have ARM or MIPS which can show up to a uint32_t 0x00000000
715       // (10 spaces) plus two for padding...
716       if (max_opcode_byte_size > 0)
717         m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1);
718       else
719         m_opcode.Dump(&ss, 12);
720     }
721   }
722 
723   const size_t opcode_pos = ss.GetSizeOfLastLine();
724 
725   // The default opcode size of 7 characters is plenty for most architectures
726   // but some like arm can pull out the occasional vqrshrun.s16.  We won't get
727   // consistent column spacing in these cases, unfortunately.
728   if (m_opcode_name.length() >= opcode_column_width) {
729     opcode_column_width = m_opcode_name.length() + 1;
730   }
731 
732   ss.PutCString(m_opcode_name);
733   ss.FillLastLineToColumn(opcode_pos + opcode_column_width, ' ');
734   ss.PutCString(m_mnemonics);
735 
736   if (!m_comment.empty()) {
737     ss.FillLastLineToColumn(
738         opcode_pos + opcode_column_width + operand_column_width, ' ');
739     ss.PutCString(" ; ");
740     ss.PutCString(m_comment);
741   }
742   s->PutCString(ss.GetString());
743 }
744 
745 bool Instruction::DumpEmulation(const ArchSpec &arch) {
746   std::unique_ptr<EmulateInstruction> insn_emulator_ap(
747       EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
748   if (insn_emulator_ap) {
749     insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr);
750     return insn_emulator_ap->EvaluateInstruction(0);
751   }
752 
753   return false;
754 }
755 
756 bool Instruction::CanSetBreakpoint () {
757   return !HasDelaySlot();
758 }
759 
760 bool Instruction::HasDelaySlot() {
761   // Default is false.
762   return false;
763 }
764 
765 OptionValueSP Instruction::ReadArray(FILE *in_file, Stream *out_stream,
766                                      OptionValue::Type data_type) {
767   bool done = false;
768   char buffer[1024];
769 
770   auto option_value_sp = std::make_shared<OptionValueArray>(1u << data_type);
771 
772   int idx = 0;
773   while (!done) {
774     if (!fgets(buffer, 1023, in_file)) {
775       out_stream->Printf(
776           "Instruction::ReadArray:  Error reading file (fgets).\n");
777       option_value_sp.reset();
778       return option_value_sp;
779     }
780 
781     std::string line(buffer);
782 
783     size_t len = line.size();
784     if (line[len - 1] == '\n') {
785       line[len - 1] = '\0';
786       line.resize(len - 1);
787     }
788 
789     if ((line.size() == 1) && line[0] == ']') {
790       done = true;
791       line.clear();
792     }
793 
794     if (!line.empty()) {
795       std::string value;
796       static RegularExpression g_reg_exp(
797           llvm::StringRef("^[ \t]*([^ \t]+)[ \t]*$"));
798       RegularExpression::Match regex_match(1);
799       bool reg_exp_success = g_reg_exp.Execute(line, &regex_match);
800       if (reg_exp_success)
801         regex_match.GetMatchAtIndex(line.c_str(), 1, value);
802       else
803         value = line;
804 
805       OptionValueSP data_value_sp;
806       switch (data_type) {
807       case OptionValue::eTypeUInt64:
808         data_value_sp = std::make_shared<OptionValueUInt64>(0, 0);
809         data_value_sp->SetValueFromString(value);
810         break;
811       // Other types can be added later as needed.
812       default:
813         data_value_sp = std::make_shared<OptionValueString>(value.c_str(), "");
814         break;
815       }
816 
817       option_value_sp->GetAsArray()->InsertValue(idx, data_value_sp);
818       ++idx;
819     }
820   }
821 
822   return option_value_sp;
823 }
824 
825 OptionValueSP Instruction::ReadDictionary(FILE *in_file, Stream *out_stream) {
826   bool done = false;
827   char buffer[1024];
828 
829   auto option_value_sp = std::make_shared<OptionValueDictionary>();
830   static ConstString encoding_key("data_encoding");
831   OptionValue::Type data_type = OptionValue::eTypeInvalid;
832 
833   while (!done) {
834     // Read the next line in the file
835     if (!fgets(buffer, 1023, in_file)) {
836       out_stream->Printf(
837           "Instruction::ReadDictionary: Error reading file (fgets).\n");
838       option_value_sp.reset();
839       return option_value_sp;
840     }
841 
842     // Check to see if the line contains the end-of-dictionary marker ("}")
843     std::string line(buffer);
844 
845     size_t len = line.size();
846     if (line[len - 1] == '\n') {
847       line[len - 1] = '\0';
848       line.resize(len - 1);
849     }
850 
851     if ((line.size() == 1) && (line[0] == '}')) {
852       done = true;
853       line.clear();
854     }
855 
856     // Try to find a key-value pair in the current line and add it to the
857     // dictionary.
858     if (!line.empty()) {
859       static RegularExpression g_reg_exp(llvm::StringRef(
860           "^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$"));
861       RegularExpression::Match regex_match(2);
862 
863       bool reg_exp_success = g_reg_exp.Execute(line, &regex_match);
864       std::string key;
865       std::string value;
866       if (reg_exp_success) {
867         regex_match.GetMatchAtIndex(line.c_str(), 1, key);
868         regex_match.GetMatchAtIndex(line.c_str(), 2, value);
869       } else {
870         out_stream->Printf("Instruction::ReadDictionary: Failure executing "
871                            "regular expression.\n");
872         option_value_sp.reset();
873         return option_value_sp;
874       }
875 
876       ConstString const_key(key.c_str());
877       // Check value to see if it's the start of an array or dictionary.
878 
879       lldb::OptionValueSP value_sp;
880       assert(value.empty() == false);
881       assert(key.empty() == false);
882 
883       if (value[0] == '{') {
884         assert(value.size() == 1);
885         // value is a dictionary
886         value_sp = ReadDictionary(in_file, out_stream);
887         if (!value_sp) {
888           option_value_sp.reset();
889           return option_value_sp;
890         }
891       } else if (value[0] == '[') {
892         assert(value.size() == 1);
893         // value is an array
894         value_sp = ReadArray(in_file, out_stream, data_type);
895         if (!value_sp) {
896           option_value_sp.reset();
897           return option_value_sp;
898         }
899         // We've used the data_type to read an array; re-set the type to
900         // Invalid
901         data_type = OptionValue::eTypeInvalid;
902       } else if ((value[0] == '0') && (value[1] == 'x')) {
903         value_sp = std::make_shared<OptionValueUInt64>(0, 0);
904         value_sp->SetValueFromString(value);
905       } else {
906         size_t len = value.size();
907         if ((value[0] == '"') && (value[len - 1] == '"'))
908           value = value.substr(1, len - 2);
909         value_sp = std::make_shared<OptionValueString>(value.c_str(), "");
910       }
911 
912       if (const_key == encoding_key) {
913         // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data
914         // indicating the
915         // data type of an upcoming array (usually the next bit of data to be
916         // read in).
917         if (strcmp(value.c_str(), "uint32_t") == 0)
918           data_type = OptionValue::eTypeUInt64;
919       } else
920         option_value_sp->GetAsDictionary()->SetValueForKey(const_key, value_sp,
921                                                            false);
922     }
923   }
924 
925   return option_value_sp;
926 }
927 
928 bool Instruction::TestEmulation(Stream *out_stream, const char *file_name) {
929   if (!out_stream)
930     return false;
931 
932   if (!file_name) {
933     out_stream->Printf("Instruction::TestEmulation:  Missing file_name.");
934     return false;
935   }
936   FILE *test_file = FileSystem::Fopen(file_name, "r");
937   if (!test_file) {
938     out_stream->Printf(
939         "Instruction::TestEmulation: Attempt to open test file failed.");
940     return false;
941   }
942 
943   char buffer[256];
944   if (!fgets(buffer, 255, test_file)) {
945     out_stream->Printf(
946         "Instruction::TestEmulation: Error reading first line of test file.\n");
947     fclose(test_file);
948     return false;
949   }
950 
951   if (strncmp(buffer, "InstructionEmulationState={", 27) != 0) {
952     out_stream->Printf("Instructin::TestEmulation: Test file does not contain "
953                        "emulation state dictionary\n");
954     fclose(test_file);
955     return false;
956   }
957 
958   // Read all the test information from the test file into an
959   // OptionValueDictionary.
960 
961   OptionValueSP data_dictionary_sp(ReadDictionary(test_file, out_stream));
962   if (!data_dictionary_sp) {
963     out_stream->Printf(
964         "Instruction::TestEmulation:  Error reading Dictionary Object.\n");
965     fclose(test_file);
966     return false;
967   }
968 
969   fclose(test_file);
970 
971   OptionValueDictionary *data_dictionary =
972       data_dictionary_sp->GetAsDictionary();
973   static ConstString description_key("assembly_string");
974   static ConstString triple_key("triple");
975 
976   OptionValueSP value_sp = data_dictionary->GetValueForKey(description_key);
977 
978   if (!value_sp) {
979     out_stream->Printf("Instruction::TestEmulation:  Test file does not "
980                        "contain description string.\n");
981     return false;
982   }
983 
984   SetDescription(value_sp->GetStringValue());
985 
986   value_sp = data_dictionary->GetValueForKey(triple_key);
987   if (!value_sp) {
988     out_stream->Printf(
989         "Instruction::TestEmulation: Test file does not contain triple.\n");
990     return false;
991   }
992 
993   ArchSpec arch;
994   arch.SetTriple(llvm::Triple(value_sp->GetStringValue()));
995 
996   bool success = false;
997   std::unique_ptr<EmulateInstruction> insn_emulator_ap(
998       EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
999   if (insn_emulator_ap)
1000     success =
1001         insn_emulator_ap->TestEmulation(out_stream, arch, data_dictionary);
1002 
1003   if (success)
1004     out_stream->Printf("Emulation test succeeded.");
1005   else
1006     out_stream->Printf("Emulation test failed.");
1007 
1008   return success;
1009 }
1010 
1011 bool Instruction::Emulate(
1012     const ArchSpec &arch, uint32_t evaluate_options, void *baton,
1013     EmulateInstruction::ReadMemoryCallback read_mem_callback,
1014     EmulateInstruction::WriteMemoryCallback write_mem_callback,
1015     EmulateInstruction::ReadRegisterCallback read_reg_callback,
1016     EmulateInstruction::WriteRegisterCallback write_reg_callback) {
1017   std::unique_ptr<EmulateInstruction> insn_emulator_ap(
1018       EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
1019   if (insn_emulator_ap) {
1020     insn_emulator_ap->SetBaton(baton);
1021     insn_emulator_ap->SetCallbacks(read_mem_callback, write_mem_callback,
1022                                    read_reg_callback, write_reg_callback);
1023     insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr);
1024     return insn_emulator_ap->EvaluateInstruction(evaluate_options);
1025   }
1026 
1027   return false;
1028 }
1029 
1030 uint32_t Instruction::GetData(DataExtractor &data) {
1031   return m_opcode.GetData(data);
1032 }
1033 
1034 InstructionList::InstructionList() : m_instructions() {}
1035 
1036 InstructionList::~InstructionList() = default;
1037 
1038 size_t InstructionList::GetSize() const { return m_instructions.size(); }
1039 
1040 uint32_t InstructionList::GetMaxOpcocdeByteSize() const {
1041   uint32_t max_inst_size = 0;
1042   collection::const_iterator pos, end;
1043   for (pos = m_instructions.begin(), end = m_instructions.end(); pos != end;
1044        ++pos) {
1045     uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
1046     if (max_inst_size < inst_size)
1047       max_inst_size = inst_size;
1048   }
1049   return max_inst_size;
1050 }
1051 
1052 InstructionSP InstructionList::GetInstructionAtIndex(size_t idx) const {
1053   InstructionSP inst_sp;
1054   if (idx < m_instructions.size())
1055     inst_sp = m_instructions[idx];
1056   return inst_sp;
1057 }
1058 
1059 void InstructionList::Dump(Stream *s, bool show_address, bool show_bytes,
1060                            const ExecutionContext *exe_ctx) {
1061   const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
1062   collection::const_iterator pos, begin, end;
1063 
1064   const FormatEntity::Entry *disassembly_format = nullptr;
1065   FormatEntity::Entry format;
1066   if (exe_ctx && exe_ctx->HasTargetScope()) {
1067     disassembly_format =
1068         exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1069   } else {
1070     FormatEntity::Parse("${addr}: ", format);
1071     disassembly_format = &format;
1072   }
1073 
1074   for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
1075        pos != end; ++pos) {
1076     if (pos != begin)
1077       s->EOL();
1078     (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx,
1079                  nullptr, nullptr, disassembly_format, 0);
1080   }
1081 }
1082 
1083 void InstructionList::Clear() { m_instructions.clear(); }
1084 
1085 void InstructionList::Append(lldb::InstructionSP &inst_sp) {
1086   if (inst_sp)
1087     m_instructions.push_back(inst_sp);
1088 }
1089 
1090 uint32_t
1091 InstructionList::GetIndexOfNextBranchInstruction(uint32_t start,
1092                                                  Target &target) const {
1093   size_t num_instructions = m_instructions.size();
1094 
1095   uint32_t next_branch = UINT32_MAX;
1096   size_t i;
1097   for (i = start; i < num_instructions; i++) {
1098     if (m_instructions[i]->DoesBranch()) {
1099       next_branch = i;
1100       break;
1101     }
1102   }
1103 
1104   // Hexagon needs the first instruction of the packet with the branch. Go
1105   // backwards until we find an instruction marked end-of-packet, or until we
1106   // hit start.
1107   if (target.GetArchitecture().GetTriple().getArch() == llvm::Triple::hexagon) {
1108     // If we didn't find a branch, find the last packet start.
1109     if (next_branch == UINT32_MAX) {
1110       i = num_instructions - 1;
1111     }
1112 
1113     while (i > start) {
1114       --i;
1115 
1116       Status error;
1117       uint32_t inst_bytes;
1118       bool prefer_file_cache = false; // Read from process if process is running
1119       lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1120       target.ReadMemory(m_instructions[i]->GetAddress(), prefer_file_cache,
1121                         &inst_bytes, sizeof(inst_bytes), error, &load_addr);
1122       // If we have an error reading memory, return start
1123       if (!error.Success())
1124         return start;
1125       // check if this is the last instruction in a packet bits 15:14 will be
1126       // 11b or 00b for a duplex
1127       if (((inst_bytes & 0xC000) == 0xC000) ||
1128           ((inst_bytes & 0xC000) == 0x0000)) {
1129         // instruction after this should be the start of next packet
1130         next_branch = i + 1;
1131         break;
1132       }
1133     }
1134 
1135     if (next_branch == UINT32_MAX) {
1136       // We couldn't find the previous packet, so return start
1137       next_branch = start;
1138     }
1139   }
1140   return next_branch;
1141 }
1142 
1143 uint32_t
1144 InstructionList::GetIndexOfInstructionAtAddress(const Address &address) {
1145   size_t num_instructions = m_instructions.size();
1146   uint32_t index = UINT32_MAX;
1147   for (size_t i = 0; i < num_instructions; i++) {
1148     if (m_instructions[i]->GetAddress() == address) {
1149       index = i;
1150       break;
1151     }
1152   }
1153   return index;
1154 }
1155 
1156 uint32_t
1157 InstructionList::GetIndexOfInstructionAtLoadAddress(lldb::addr_t load_addr,
1158                                                     Target &target) {
1159   Address address;
1160   address.SetLoadAddress(load_addr, &target);
1161   return GetIndexOfInstructionAtAddress(address);
1162 }
1163 
1164 size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
1165                                        const AddressRange &range,
1166                                        Stream *error_strm_ptr,
1167                                        bool prefer_file_cache) {
1168   if (exe_ctx) {
1169     Target *target = exe_ctx->GetTargetPtr();
1170     const addr_t byte_size = range.GetByteSize();
1171     if (target == nullptr || byte_size == 0 ||
1172         !range.GetBaseAddress().IsValid())
1173       return 0;
1174 
1175     auto data_sp = std::make_shared<DataBufferHeap>(byte_size, '\0');
1176 
1177     Status error;
1178     lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1179     const size_t bytes_read = target->ReadMemory(
1180         range.GetBaseAddress(), prefer_file_cache, data_sp->GetBytes(),
1181         data_sp->GetByteSize(), error, &load_addr);
1182 
1183     if (bytes_read > 0) {
1184       if (bytes_read != data_sp->GetByteSize())
1185         data_sp->SetByteSize(bytes_read);
1186       DataExtractor data(data_sp, m_arch.GetByteOrder(),
1187                          m_arch.GetAddressByteSize());
1188       const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1189       return DecodeInstructions(range.GetBaseAddress(), data, 0, UINT32_MAX,
1190                                 false, data_from_file);
1191     } else if (error_strm_ptr) {
1192       const char *error_cstr = error.AsCString();
1193       if (error_cstr) {
1194         error_strm_ptr->Printf("error: %s\n", error_cstr);
1195       }
1196     }
1197   } else if (error_strm_ptr) {
1198     error_strm_ptr->PutCString("error: invalid execution context\n");
1199   }
1200   return 0;
1201 }
1202 
1203 size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
1204                                        const Address &start,
1205                                        uint32_t num_instructions,
1206                                        bool prefer_file_cache) {
1207   m_instruction_list.Clear();
1208 
1209   if (exe_ctx == nullptr || num_instructions == 0 || !start.IsValid())
1210     return 0;
1211 
1212   Target *target = exe_ctx->GetTargetPtr();
1213   // Calculate the max buffer size we will need in order to disassemble
1214   const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
1215 
1216   if (target == nullptr || byte_size == 0)
1217     return 0;
1218 
1219   DataBufferHeap *heap_buffer = new DataBufferHeap(byte_size, '\0');
1220   DataBufferSP data_sp(heap_buffer);
1221 
1222   Status error;
1223   lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1224   const size_t bytes_read =
1225       target->ReadMemory(start, prefer_file_cache, heap_buffer->GetBytes(),
1226                          byte_size, error, &load_addr);
1227 
1228   const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1229 
1230   if (bytes_read == 0)
1231     return 0;
1232   DataExtractor data(data_sp, m_arch.GetByteOrder(),
1233                      m_arch.GetAddressByteSize());
1234 
1235   const bool append_instructions = true;
1236   DecodeInstructions(start, data, 0, num_instructions, append_instructions,
1237                      data_from_file);
1238 
1239   return m_instruction_list.GetSize();
1240 }
1241 
1242 //----------------------------------------------------------------------
1243 // Disassembler copy constructor
1244 //----------------------------------------------------------------------
1245 Disassembler::Disassembler(const ArchSpec &arch, const char *flavor)
1246     : m_arch(arch), m_instruction_list(), m_base_addr(LLDB_INVALID_ADDRESS),
1247       m_flavor() {
1248   if (flavor == nullptr)
1249     m_flavor.assign("default");
1250   else
1251     m_flavor.assign(flavor);
1252 
1253   // If this is an arm variant that can only include thumb (T16, T32)
1254   // instructions, force the arch triple to be "thumbv.." instead of "armv..."
1255   if (arch.IsAlwaysThumbInstructions()) {
1256     std::string thumb_arch_name(arch.GetTriple().getArchName().str());
1257     // Replace "arm" with "thumb" so we get all thumb variants correct
1258     if (thumb_arch_name.size() > 3) {
1259       thumb_arch_name.erase(0, 3);
1260       thumb_arch_name.insert(0, "thumb");
1261     }
1262     m_arch.SetTriple(thumb_arch_name.c_str());
1263   }
1264 }
1265 
1266 Disassembler::~Disassembler() = default;
1267 
1268 InstructionList &Disassembler::GetInstructionList() {
1269   return m_instruction_list;
1270 }
1271 
1272 const InstructionList &Disassembler::GetInstructionList() const {
1273   return m_instruction_list;
1274 }
1275 
1276 //----------------------------------------------------------------------
1277 // Class PseudoInstruction
1278 //----------------------------------------------------------------------
1279 
1280 PseudoInstruction::PseudoInstruction()
1281     : Instruction(Address(), eAddressClassUnknown), m_description() {}
1282 
1283 PseudoInstruction::~PseudoInstruction() = default;
1284 
1285 bool PseudoInstruction::DoesBranch() {
1286   // This is NOT a valid question for a pseudo instruction.
1287   return false;
1288 }
1289 
1290 bool PseudoInstruction::HasDelaySlot() {
1291   // This is NOT a valid question for a pseudo instruction.
1292   return false;
1293 }
1294 
1295 size_t PseudoInstruction::Decode(const lldb_private::Disassembler &disassembler,
1296                                  const lldb_private::DataExtractor &data,
1297                                  lldb::offset_t data_offset) {
1298   return m_opcode.GetByteSize();
1299 }
1300 
1301 void PseudoInstruction::SetOpcode(size_t opcode_size, void *opcode_data) {
1302   if (!opcode_data)
1303     return;
1304 
1305   switch (opcode_size) {
1306   case 8: {
1307     uint8_t value8 = *((uint8_t *)opcode_data);
1308     m_opcode.SetOpcode8(value8, eByteOrderInvalid);
1309     break;
1310   }
1311   case 16: {
1312     uint16_t value16 = *((uint16_t *)opcode_data);
1313     m_opcode.SetOpcode16(value16, eByteOrderInvalid);
1314     break;
1315   }
1316   case 32: {
1317     uint32_t value32 = *((uint32_t *)opcode_data);
1318     m_opcode.SetOpcode32(value32, eByteOrderInvalid);
1319     break;
1320   }
1321   case 64: {
1322     uint64_t value64 = *((uint64_t *)opcode_data);
1323     m_opcode.SetOpcode64(value64, eByteOrderInvalid);
1324     break;
1325   }
1326   default:
1327     break;
1328   }
1329 }
1330 
1331 void PseudoInstruction::SetDescription(llvm::StringRef description) {
1332   m_description = description;
1333 }
1334 
1335 Instruction::Operand Instruction::Operand::BuildRegister(ConstString &r) {
1336   Operand ret;
1337   ret.m_type = Type::Register;
1338   ret.m_register = r;
1339   return ret;
1340 }
1341 
1342 Instruction::Operand Instruction::Operand::BuildImmediate(lldb::addr_t imm,
1343                                                           bool neg) {
1344   Operand ret;
1345   ret.m_type = Type::Immediate;
1346   ret.m_immediate = imm;
1347   ret.m_negative = neg;
1348   return ret;
1349 }
1350 
1351 Instruction::Operand Instruction::Operand::BuildImmediate(int64_t imm) {
1352   Operand ret;
1353   ret.m_type = Type::Immediate;
1354   if (imm < 0) {
1355     ret.m_immediate = -imm;
1356     ret.m_negative = true;
1357   } else {
1358     ret.m_immediate = imm;
1359     ret.m_negative = false;
1360   }
1361   return ret;
1362 }
1363 
1364 Instruction::Operand
1365 Instruction::Operand::BuildDereference(const Operand &ref) {
1366   Operand ret;
1367   ret.m_type = Type::Dereference;
1368   ret.m_children = {ref};
1369   return ret;
1370 }
1371 
1372 Instruction::Operand Instruction::Operand::BuildSum(const Operand &lhs,
1373                                                     const Operand &rhs) {
1374   Operand ret;
1375   ret.m_type = Type::Sum;
1376   ret.m_children = {lhs, rhs};
1377   return ret;
1378 }
1379 
1380 Instruction::Operand Instruction::Operand::BuildProduct(const Operand &lhs,
1381                                                         const Operand &rhs) {
1382   Operand ret;
1383   ret.m_type = Type::Product;
1384   ret.m_children = {lhs, rhs};
1385   return ret;
1386 }
1387 
1388 std::function<bool(const Instruction::Operand &)>
1389 lldb_private::OperandMatchers::MatchBinaryOp(
1390     std::function<bool(const Instruction::Operand &)> base,
1391     std::function<bool(const Instruction::Operand &)> left,
1392     std::function<bool(const Instruction::Operand &)> right) {
1393   return [base, left, right](const Instruction::Operand &op) -> bool {
1394     return (base(op) && op.m_children.size() == 2 &&
1395             ((left(op.m_children[0]) && right(op.m_children[1])) ||
1396              (left(op.m_children[1]) && right(op.m_children[0]))));
1397   };
1398 }
1399 
1400 std::function<bool(const Instruction::Operand &)>
1401 lldb_private::OperandMatchers::MatchUnaryOp(
1402     std::function<bool(const Instruction::Operand &)> base,
1403     std::function<bool(const Instruction::Operand &)> child) {
1404   return [base, child](const Instruction::Operand &op) -> bool {
1405     return (base(op) && op.m_children.size() == 1 && child(op.m_children[0]));
1406   };
1407 }
1408 
1409 std::function<bool(const Instruction::Operand &)>
1410 lldb_private::OperandMatchers::MatchRegOp(const RegisterInfo &info) {
1411   return [&info](const Instruction::Operand &op) {
1412     return (op.m_type == Instruction::Operand::Type::Register &&
1413             (op.m_register == ConstString(info.name) ||
1414              op.m_register == ConstString(info.alt_name)));
1415   };
1416 }
1417 
1418 std::function<bool(const Instruction::Operand &)>
1419 lldb_private::OperandMatchers::FetchRegOp(ConstString &reg) {
1420   return [&reg](const Instruction::Operand &op) {
1421     if (op.m_type != Instruction::Operand::Type::Register) {
1422       return false;
1423     }
1424     reg = op.m_register;
1425     return true;
1426   };
1427 }
1428 
1429 std::function<bool(const Instruction::Operand &)>
1430 lldb_private::OperandMatchers::MatchImmOp(int64_t imm) {
1431   return [imm](const Instruction::Operand &op) {
1432     return (op.m_type == Instruction::Operand::Type::Immediate &&
1433             ((op.m_negative && op.m_immediate == (uint64_t)-imm) ||
1434              (!op.m_negative && op.m_immediate == (uint64_t)imm)));
1435   };
1436 }
1437 
1438 std::function<bool(const Instruction::Operand &)>
1439 lldb_private::OperandMatchers::FetchImmOp(int64_t &imm) {
1440   return [&imm](const Instruction::Operand &op) {
1441     if (op.m_type != Instruction::Operand::Type::Immediate) {
1442       return false;
1443     }
1444     if (op.m_negative) {
1445       imm = -((int64_t)op.m_immediate);
1446     } else {
1447       imm = ((int64_t)op.m_immediate);
1448     }
1449     return true;
1450   };
1451 }
1452 
1453 std::function<bool(const Instruction::Operand &)>
1454 lldb_private::OperandMatchers::MatchOpType(Instruction::Operand::Type type) {
1455   return [type](const Instruction::Operand &op) { return op.m_type == type; };
1456 }
1457