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