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