1 //===-- EmulateInstructionARM64.cpp ------------------------------*- C++-*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "EmulateInstructionARM64.h"
11 
12 #include <stdlib.h>
13 
14 #include "lldb/Core/Address.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Symbol/UnwindPlan.h"
17 #include "lldb/Utility/ArchSpec.h"
18 #include "lldb/Utility/ConstString.h"
19 #include "lldb/Utility/RegisterValue.h"
20 #include "lldb/Utility/Stream.h"
21 
22 #include "Plugins/Process/Utility/ARMDefines.h"
23 #include "Plugins/Process/Utility/ARMUtils.h"
24 #include "Plugins/Process/Utility/lldb-arm64-register-enums.h"
25 
26 #define GPR_OFFSET(idx) ((idx)*8)
27 #define GPR_OFFSET_NAME(reg) 0
28 #define FPU_OFFSET(idx) ((idx)*16)
29 #define FPU_OFFSET_NAME(reg) 0
30 #define EXC_OFFSET_NAME(reg) 0
31 #define DBG_OFFSET_NAME(reg) 0
32 #define DBG_OFFSET_NAME(reg) 0
33 #define DEFINE_DBG(re, y)                                                      \
34   "na", nullptr, 8, 0, lldb::eEncodingUint, lldb::eFormatHex,                  \
35       {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,          \
36        LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},                              \
37       nullptr, nullptr, nullptr, 0
38 
39 #define DECLARE_REGISTER_INFOS_ARM64_STRUCT
40 
41 #include "Plugins/Process/Utility/RegisterInfos_arm64.h"
42 
43 #include "llvm/ADT/STLExtras.h"
44 #include "llvm/Support/MathExtras.h"
45 
46 #include "Plugins/Process/Utility/InstructionUtils.h"
47 
48 using namespace lldb;
49 using namespace lldb_private;
50 
51 static bool LLDBTableGetRegisterInfo(uint32_t reg_num, RegisterInfo &reg_info) {
52   if (reg_num >= llvm::array_lengthof(g_register_infos_arm64_le))
53     return false;
54   reg_info = g_register_infos_arm64_le[reg_num];
55   return true;
56 }
57 
58 #define No_VFP 0
59 #define VFPv1 (1u << 1)
60 #define VFPv2 (1u << 2)
61 #define VFPv3 (1u << 3)
62 #define AdvancedSIMD (1u << 4)
63 
64 #define VFPv1_ABOVE (VFPv1 | VFPv2 | VFPv3 | AdvancedSIMD)
65 #define VFPv2_ABOVE (VFPv2 | VFPv3 | AdvancedSIMD)
66 #define VFPv2v3 (VFPv2 | VFPv3)
67 
68 #define UInt(x) ((uint64_t)x)
69 #define SInt(x) ((int64_t)x)
70 #define bit bool
71 #define boolean bool
72 #define integer int64_t
73 
74 static inline bool IsZero(uint64_t x) { return x == 0; }
75 
76 static inline uint64_t NOT(uint64_t x) { return ~x; }
77 
78 // LSL()
79 // =====
80 
81 static inline uint64_t LSL(uint64_t x, integer shift) {
82   if (shift == 0)
83     return x;
84   return x << shift;
85 }
86 
87 // AddWithCarry()
88 // ===============
89 static inline uint64_t
90 AddWithCarry(uint32_t N, uint64_t x, uint64_t y, bit carry_in,
91              EmulateInstructionARM64::ProcState &proc_state) {
92   uint64_t unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
93   int64_t signed_sum = SInt(x) + SInt(y) + UInt(carry_in);
94   uint64_t result = unsigned_sum;
95   if (N < 64)
96     result = Bits64(result, N - 1, 0);
97   proc_state.N = Bit64(result, N - 1);
98   proc_state.Z = IsZero(result);
99   proc_state.C = UInt(result) == unsigned_sum;
100   proc_state.V = SInt(result) == signed_sum;
101   return result;
102 }
103 
104 // ConstrainUnpredictable()
105 // ========================
106 
107 EmulateInstructionARM64::ConstraintType
108 ConstrainUnpredictable(EmulateInstructionARM64::Unpredictable which) {
109   EmulateInstructionARM64::ConstraintType result =
110       EmulateInstructionARM64::Constraint_UNKNOWN;
111   switch (which) {
112   case EmulateInstructionARM64::Unpredictable_WBOVERLAP:
113   case EmulateInstructionARM64::Unpredictable_LDPOVERLAP:
114     // TODO: don't know what to really do here? Pseudo code says:
115     // set result to one of above Constraint behaviours or UNDEFINED
116     break;
117   }
118   return result;
119 }
120 
121 //----------------------------------------------------------------------
122 //
123 // EmulateInstructionARM implementation
124 //
125 //----------------------------------------------------------------------
126 
127 void EmulateInstructionARM64::Initialize() {
128   PluginManager::RegisterPlugin(GetPluginNameStatic(),
129                                 GetPluginDescriptionStatic(), CreateInstance);
130 }
131 
132 void EmulateInstructionARM64::Terminate() {
133   PluginManager::UnregisterPlugin(CreateInstance);
134 }
135 
136 ConstString EmulateInstructionARM64::GetPluginNameStatic() {
137   ConstString g_plugin_name("lldb.emulate-instruction.arm64");
138   return g_plugin_name;
139 }
140 
141 lldb_private::ConstString EmulateInstructionARM64::GetPluginName() {
142   static ConstString g_plugin_name("EmulateInstructionARM64");
143   return g_plugin_name;
144 }
145 
146 const char *EmulateInstructionARM64::GetPluginDescriptionStatic() {
147   return "Emulate instructions for the ARM64 architecture.";
148 }
149 
150 EmulateInstruction *
151 EmulateInstructionARM64::CreateInstance(const ArchSpec &arch,
152                                         InstructionType inst_type) {
153   if (EmulateInstructionARM64::SupportsEmulatingInstructionsOfTypeStatic(
154           inst_type)) {
155     if (arch.GetTriple().getArch() == llvm::Triple::aarch64) {
156       return new EmulateInstructionARM64(arch);
157     }
158   }
159 
160   return NULL;
161 }
162 
163 bool EmulateInstructionARM64::SetTargetTriple(const ArchSpec &arch) {
164   if (arch.GetTriple().getArch() == llvm::Triple::arm)
165     return true;
166   else if (arch.GetTriple().getArch() == llvm::Triple::thumb)
167     return true;
168 
169   return false;
170 }
171 
172 bool EmulateInstructionARM64::GetRegisterInfo(RegisterKind reg_kind,
173                                               uint32_t reg_num,
174                                               RegisterInfo &reg_info) {
175   if (reg_kind == eRegisterKindGeneric) {
176     switch (reg_num) {
177     case LLDB_REGNUM_GENERIC_PC:
178       reg_kind = eRegisterKindLLDB;
179       reg_num = gpr_pc_arm64;
180       break;
181     case LLDB_REGNUM_GENERIC_SP:
182       reg_kind = eRegisterKindLLDB;
183       reg_num = gpr_sp_arm64;
184       break;
185     case LLDB_REGNUM_GENERIC_FP:
186       reg_kind = eRegisterKindLLDB;
187       reg_num = gpr_fp_arm64;
188       break;
189     case LLDB_REGNUM_GENERIC_RA:
190       reg_kind = eRegisterKindLLDB;
191       reg_num = gpr_lr_arm64;
192       break;
193     case LLDB_REGNUM_GENERIC_FLAGS:
194       reg_kind = eRegisterKindLLDB;
195       reg_num = gpr_cpsr_arm64;
196       break;
197 
198     default:
199       return false;
200     }
201   }
202 
203   if (reg_kind == eRegisterKindLLDB)
204     return LLDBTableGetRegisterInfo(reg_num, reg_info);
205   return false;
206 }
207 
208 EmulateInstructionARM64::Opcode *
209 EmulateInstructionARM64::GetOpcodeForInstruction(const uint32_t opcode) {
210   static EmulateInstructionARM64::Opcode g_opcodes[] = {
211       //----------------------------------------------------------------------
212       // Prologue instructions
213       //----------------------------------------------------------------------
214 
215       // push register(s)
216       {0xff000000, 0xd1000000, No_VFP,
217        &EmulateInstructionARM64::EmulateADDSUBImm,
218        "SUB  <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"},
219       {0xff000000, 0xf1000000, No_VFP,
220        &EmulateInstructionARM64::EmulateADDSUBImm,
221        "SUBS  <Xd>, <Xn|SP>, #<imm> {, <shift>}"},
222       {0xff000000, 0x91000000, No_VFP,
223        &EmulateInstructionARM64::EmulateADDSUBImm,
224        "ADD  <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}"},
225       {0xff000000, 0xb1000000, No_VFP,
226        &EmulateInstructionARM64::EmulateADDSUBImm,
227        "ADDS  <Xd>, <Xn|SP>, #<imm> {, <shift>}"},
228 
229       {0xff000000, 0x51000000, No_VFP,
230        &EmulateInstructionARM64::EmulateADDSUBImm,
231        "SUB  <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"},
232       {0xff000000, 0x71000000, No_VFP,
233        &EmulateInstructionARM64::EmulateADDSUBImm,
234        "SUBS  <Wd>, <Wn|WSP>, #<imm> {, <shift>}"},
235       {0xff000000, 0x11000000, No_VFP,
236        &EmulateInstructionARM64::EmulateADDSUBImm,
237        "ADD  <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}"},
238       {0xff000000, 0x31000000, No_VFP,
239        &EmulateInstructionARM64::EmulateADDSUBImm,
240        "ADDS  <Wd>, <Wn|WSP>, #<imm> {, <shift>}"},
241 
242       {0xffc00000, 0x29000000, No_VFP,
243        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
244        "STP  <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"},
245       {0xffc00000, 0xa9000000, No_VFP,
246        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
247        "STP  <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"},
248       {0xffc00000, 0x2d000000, No_VFP,
249        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
250        "STP  <St>, <St2>, [<Xn|SP>{, #<imm>}]"},
251       {0xffc00000, 0x6d000000, No_VFP,
252        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
253        "STP  <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"},
254       {0xffc00000, 0xad000000, No_VFP,
255        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
256        "STP  <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"},
257 
258       {0xffc00000, 0x29800000, No_VFP,
259        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
260        "STP  <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
261       {0xffc00000, 0xa9800000, No_VFP,
262        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
263        "STP  <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
264       {0xffc00000, 0x2d800000, No_VFP,
265        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
266        "STP  <St>, <St2>, [<Xn|SP>, #<imm>]!"},
267       {0xffc00000, 0x6d800000, No_VFP,
268        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
269        "STP  <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
270       {0xffc00000, 0xad800000, No_VFP,
271        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
272        "STP  <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
273 
274       {0xffc00000, 0x28800000, No_VFP,
275        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
276        "STP  <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
277       {0xffc00000, 0xa8800000, No_VFP,
278        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
279        "STP  <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
280       {0xffc00000, 0x2c800000, No_VFP,
281        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
282        "STP  <St>, <St2>, [<Xn|SP>, #<imm>]!"},
283       {0xffc00000, 0x6c800000, No_VFP,
284        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
285        "STP  <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
286       {0xffc00000, 0xac800000, No_VFP,
287        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
288        "STP  <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
289 
290       {0xffc00000, 0x29400000, No_VFP,
291        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
292        "LDP  <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]"},
293       {0xffc00000, 0xa9400000, No_VFP,
294        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
295        "LDP  <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]"},
296       {0xffc00000, 0x2d400000, No_VFP,
297        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
298        "LDP  <St>, <St2>, [<Xn|SP>{, #<imm>}]"},
299       {0xffc00000, 0x6d400000, No_VFP,
300        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
301        "LDP  <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]"},
302       {0xffc00000, 0xad400000, No_VFP,
303        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_OFF>,
304        "LDP  <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]"},
305 
306       {0xffc00000, 0x29c00000, No_VFP,
307        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
308        "LDP  <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
309       {0xffc00000, 0xa9c00000, No_VFP,
310        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
311        "LDP  <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
312       {0xffc00000, 0x2dc00000, No_VFP,
313        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
314        "LDP  <St>, <St2>, [<Xn|SP>, #<imm>]!"},
315       {0xffc00000, 0x6dc00000, No_VFP,
316        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
317        "LDP  <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
318       {0xffc00000, 0xadc00000, No_VFP,
319        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_PRE>,
320        "LDP  <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
321 
322       {0xffc00000, 0x28c00000, No_VFP,
323        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
324        "LDP  <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!"},
325       {0xffc00000, 0xa8c00000, No_VFP,
326        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
327        "LDP  <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!"},
328       {0xffc00000, 0x2cc00000, No_VFP,
329        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
330        "LDP  <St>, <St2>, [<Xn|SP>, #<imm>]!"},
331       {0xffc00000, 0x6cc00000, No_VFP,
332        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
333        "LDP  <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!"},
334       {0xffc00000, 0xacc00000, No_VFP,
335        &EmulateInstructionARM64::EmulateLDPSTP<AddrMode_POST>,
336        "LDP  <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!"},
337 
338       {0xffe00c00, 0xb8000400, No_VFP,
339        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
340        "STR <Wt>, [<Xn|SP>], #<simm>"},
341       {0xffe00c00, 0xf8000400, No_VFP,
342        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
343        "STR <Xt>, [<Xn|SP>], #<simm>"},
344       {0xffe00c00, 0xb8000c00, No_VFP,
345        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
346        "STR <Wt>, [<Xn|SP>, #<simm>]!"},
347       {0xffe00c00, 0xf8000c00, No_VFP,
348        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
349        "STR <Xt>, [<Xn|SP>, #<simm>]!"},
350       {0xffc00000, 0xb9000000, No_VFP,
351        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
352        "STR <Wt>, [<Xn|SP>{, #<pimm>}]"},
353       {0xffc00000, 0xf9000000, No_VFP,
354        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
355        "STR <Xt>, [<Xn|SP>{, #<pimm>}]"},
356 
357       {0xffe00c00, 0xb8400400, No_VFP,
358        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
359        "LDR <Wt>, [<Xn|SP>], #<simm>"},
360       {0xffe00c00, 0xf8400400, No_VFP,
361        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_POST>,
362        "LDR <Xt>, [<Xn|SP>], #<simm>"},
363       {0xffe00c00, 0xb8400c00, No_VFP,
364        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
365        "LDR <Wt>, [<Xn|SP>, #<simm>]!"},
366       {0xffe00c00, 0xf8400c00, No_VFP,
367        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_PRE>,
368        "LDR <Xt>, [<Xn|SP>, #<simm>]!"},
369       {0xffc00000, 0xb9400000, No_VFP,
370        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
371        "LDR <Wt>, [<Xn|SP>{, #<pimm>}]"},
372       {0xffc00000, 0xf9400000, No_VFP,
373        &EmulateInstructionARM64::EmulateLDRSTRImm<AddrMode_OFF>,
374        "LDR <Xt>, [<Xn|SP>{, #<pimm>}]"},
375 
376       {0xfc000000, 0x14000000, No_VFP, &EmulateInstructionARM64::EmulateB,
377        "B <label>"},
378       {0xff000010, 0x54000000, No_VFP, &EmulateInstructionARM64::EmulateBcond,
379        "B.<cond> <label>"},
380       {0x7f000000, 0x34000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ,
381        "CBZ <Wt>, <label>"},
382       {0x7f000000, 0x35000000, No_VFP, &EmulateInstructionARM64::EmulateCBZ,
383        "CBNZ <Wt>, <label>"},
384       {0x7f000000, 0x36000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ,
385        "TBZ <R><t>, #<imm>, <label>"},
386       {0x7f000000, 0x37000000, No_VFP, &EmulateInstructionARM64::EmulateTBZ,
387        "TBNZ <R><t>, #<imm>, <label>"},
388 
389   };
390   static const size_t k_num_arm_opcodes = llvm::array_lengthof(g_opcodes);
391 
392   for (size_t i = 0; i < k_num_arm_opcodes; ++i) {
393     if ((g_opcodes[i].mask & opcode) == g_opcodes[i].value)
394       return &g_opcodes[i];
395   }
396   return nullptr;
397 }
398 
399 bool EmulateInstructionARM64::ReadInstruction() {
400   bool success = false;
401   m_addr = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC,
402                                 LLDB_INVALID_ADDRESS, &success);
403   if (success) {
404     Context read_inst_context;
405     read_inst_context.type = eContextReadOpcode;
406     read_inst_context.SetNoArgs();
407     m_opcode.SetOpcode32(
408         ReadMemoryUnsigned(read_inst_context, m_addr, 4, 0, &success),
409         GetByteOrder());
410   }
411   if (!success)
412     m_addr = LLDB_INVALID_ADDRESS;
413   return success;
414 }
415 
416 bool EmulateInstructionARM64::EvaluateInstruction(uint32_t evaluate_options) {
417   const uint32_t opcode = m_opcode.GetOpcode32();
418   Opcode *opcode_data = GetOpcodeForInstruction(opcode);
419   if (opcode_data == NULL)
420     return false;
421 
422   // printf ("opcode template for 0x%8.8x: %s\n", opcode, opcode_data->name);
423   const bool auto_advance_pc =
424       evaluate_options & eEmulateInstructionOptionAutoAdvancePC;
425   m_ignore_conditions =
426       evaluate_options & eEmulateInstructionOptionIgnoreConditions;
427 
428   bool success = false;
429   //    if (m_opcode_cpsr == 0 || m_ignore_conditions == false)
430   //    {
431   //        m_opcode_cpsr = ReadRegisterUnsigned (eRegisterKindLLDB,
432   //                                              gpr_cpsr_arm64,
433   //                                              0,
434   //                                              &success);
435   //    }
436 
437   // Only return false if we are unable to read the CPSR if we care about
438   // conditions
439   if (success == false && m_ignore_conditions == false)
440     return false;
441 
442   uint32_t orig_pc_value = 0;
443   if (auto_advance_pc) {
444     orig_pc_value =
445         ReadRegisterUnsigned(eRegisterKindLLDB, gpr_pc_arm64, 0, &success);
446     if (!success)
447       return false;
448   }
449 
450   // Call the Emulate... function.
451   success = (this->*opcode_data->callback)(opcode);
452   if (!success)
453     return false;
454 
455   if (auto_advance_pc) {
456     uint32_t new_pc_value =
457         ReadRegisterUnsigned(eRegisterKindLLDB, gpr_pc_arm64, 0, &success);
458     if (!success)
459       return false;
460 
461     if (auto_advance_pc && (new_pc_value == orig_pc_value)) {
462       EmulateInstruction::Context context;
463       context.type = eContextAdvancePC;
464       context.SetNoArgs();
465       if (!WriteRegisterUnsigned(context, eRegisterKindLLDB, gpr_pc_arm64,
466                                  orig_pc_value + 4))
467         return false;
468     }
469   }
470   return true;
471 }
472 
473 bool EmulateInstructionARM64::CreateFunctionEntryUnwind(
474     UnwindPlan &unwind_plan) {
475   unwind_plan.Clear();
476   unwind_plan.SetRegisterKind(eRegisterKindLLDB);
477 
478   UnwindPlan::RowSP row(new UnwindPlan::Row);
479 
480   // Our previous Call Frame Address is the stack pointer
481   row->GetCFAValue().SetIsRegisterPlusOffset(gpr_sp_arm64, 0);
482 
483   unwind_plan.AppendRow(row);
484   unwind_plan.SetSourceName("EmulateInstructionARM64");
485   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
486   unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
487   unwind_plan.SetReturnAddressRegister(gpr_lr_arm64);
488   return true;
489 }
490 
491 uint32_t EmulateInstructionARM64::GetFramePointerRegisterNumber() const {
492   if (m_arch.GetTriple().isAndroid())
493     return LLDB_INVALID_REGNUM; // Don't use frame pointer on android
494 
495   return gpr_fp_arm64;
496 }
497 
498 bool EmulateInstructionARM64::UsingAArch32() {
499   bool aarch32 = m_opcode_pstate.RW == 1;
500   // if !HaveAnyAArch32() then assert !aarch32;
501   // if HighestELUsingAArch32() then assert aarch32;
502   return aarch32;
503 }
504 
505 bool EmulateInstructionARM64::BranchTo(const Context &context, uint32_t N,
506                                        addr_t target) {
507 #if 0
508     // Set program counter to a new address, with a branch reason hint for
509     // possible use by hardware fetching the next instruction.
510     BranchTo(bits(N) target, BranchType branch_type)
511         Hint_Branch(branch_type);
512         if N == 32 then
513             assert UsingAArch32();
514             _PC = ZeroExtend(target);
515         else
516             assert N == 64 && !UsingAArch32();
517             // Remove the tag bits from a tagged target
518             case PSTATE.EL of
519                 when EL0, EL1
520                     if target<55> == '1' && TCR_EL1.TBI1 == '1' then
521                         target<63:56> = '11111111';
522                     if target<55> == '0' && TCR_EL1.TBI0 == '1' then
523                         target<63:56> = '00000000';
524                 when EL2
525                     if TCR_EL2.TBI == '1' then
526                         target<63:56> = '00000000';
527                 when EL3
528                     if TCR_EL3.TBI == '1' then
529                         target<63:56> = '00000000';
530         _PC = target<63:0>;
531         return;
532 #endif
533 
534   addr_t addr;
535 
536   // Hint_Branch(branch_type);
537   if (N == 32) {
538     if (!UsingAArch32())
539       return false;
540     addr = target;
541   } else if (N == 64) {
542     if (UsingAArch32())
543       return false;
544     // TODO: Remove the tag bits from a tagged target
545     addr = target;
546   } else
547     return false;
548 
549   if (!WriteRegisterUnsigned(context, eRegisterKindGeneric,
550                              LLDB_REGNUM_GENERIC_PC, addr))
551     return false;
552 
553   return true;
554 }
555 
556 bool EmulateInstructionARM64::ConditionHolds(const uint32_t cond) {
557   // If we are ignoring conditions, then always return true. this allows us to
558   // iterate over disassembly code and still emulate an instruction even if we
559   // don't have all the right bits set in the CPSR register...
560   if (m_ignore_conditions)
561     return true;
562 
563   bool result = false;
564   switch (UnsignedBits(cond, 3, 1)) {
565   case 0:
566     result = (m_opcode_pstate.Z == 1);
567     break;
568   case 1:
569     result = (m_opcode_pstate.C == 1);
570     break;
571   case 2:
572     result = (m_opcode_pstate.N == 1);
573     break;
574   case 3:
575     result = (m_opcode_pstate.V == 1);
576     break;
577   case 4:
578     result = (m_opcode_pstate.C == 1 && m_opcode_pstate.Z == 0);
579     break;
580   case 5:
581     result = (m_opcode_pstate.N == m_opcode_pstate.V);
582     break;
583   case 6:
584     result = (m_opcode_pstate.N == m_opcode_pstate.V && m_opcode_pstate.Z == 0);
585     break;
586   case 7:
587     // Always execute (cond == 0b1110, or the special 0b1111 which gives
588     // opcodes different meanings, but always means execution happens.
589     return true;
590   }
591 
592   if (cond & 1)
593     result = !result;
594   return result;
595 }
596 
597 bool EmulateInstructionARM64::EmulateADDSUBImm(const uint32_t opcode) {
598   // integer d = UInt(Rd);
599   // integer n = UInt(Rn);
600   // integer datasize = if sf == 1 then 64 else 32;
601   // boolean sub_op = (op == 1);
602   // boolean setflags = (S == 1);
603   // bits(datasize) imm;
604   //
605   // case shift of
606   //     when '00' imm = ZeroExtend(imm12, datasize);
607   //     when '01' imm = ZeroExtend(imm12 : Zeros(12), datasize);
608   //    when '1x' UNDEFINED;
609   //
610   //
611   // bits(datasize) result;
612   // bits(datasize) operand1 = if n == 31 then SP[] else X[n];
613   // bits(datasize) operand2 = imm;
614   // bits(4) nzcv;
615   // bit carry_in;
616   //
617   // if sub_op then
618   //     operand2 = NOT(operand2);
619   //     carry_in = 1;
620   // else
621   //     carry_in = 0;
622   //
623   // (result, nzcv) = AddWithCarry(operand1, operand2, carry_in);
624   //
625   // if setflags then
626   //     PSTATE.NZCV = nzcv;
627   //
628   // if d == 31 && !setflags then
629   //     SP[] = result;
630   // else
631   //     X[d] = result;
632 
633   const uint32_t sf = Bit32(opcode, 31);
634   const uint32_t op = Bit32(opcode, 30);
635   const uint32_t S = Bit32(opcode, 29);
636   const uint32_t shift = Bits32(opcode, 23, 22);
637   const uint32_t imm12 = Bits32(opcode, 21, 10);
638   const uint32_t Rn = Bits32(opcode, 9, 5);
639   const uint32_t Rd = Bits32(opcode, 4, 0);
640 
641   bool success = false;
642 
643   const uint32_t d = UInt(Rd);
644   const uint32_t n = UInt(Rn);
645   const uint32_t datasize = (sf == 1) ? 64 : 32;
646   boolean sub_op = op == 1;
647   boolean setflags = S == 1;
648   uint64_t imm;
649 
650   switch (shift) {
651   case 0:
652     imm = imm12;
653     break;
654   case 1:
655     imm = imm12 << 12;
656     break;
657   default:
658     return false; // UNDEFINED;
659   }
660   uint64_t result;
661   uint64_t operand1 =
662       ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + n, 0, &success);
663   uint64_t operand2 = imm;
664   bit carry_in;
665 
666   if (sub_op) {
667     operand2 = NOT(operand2);
668     carry_in = 1;
669     imm = -imm; // For the Register plug offset context below
670   } else {
671     carry_in = 0;
672   }
673 
674   ProcState proc_state;
675 
676   result = AddWithCarry(datasize, operand1, operand2, carry_in, proc_state);
677 
678   if (setflags) {
679     m_emulated_pstate.N = proc_state.N;
680     m_emulated_pstate.Z = proc_state.Z;
681     m_emulated_pstate.C = proc_state.C;
682     m_emulated_pstate.V = proc_state.V;
683   }
684 
685   Context context;
686   RegisterInfo reg_info_Rn;
687   if (GetRegisterInfo(eRegisterKindLLDB, n, reg_info_Rn))
688     context.SetRegisterPlusOffset(reg_info_Rn, imm);
689 
690   if (n == GetFramePointerRegisterNumber() && d == gpr_sp_arm64 && !setflags) {
691     // 'mov sp, fp' - common epilogue instruction, CFA is now in terms of the
692     // stack pointer, instead of frame pointer.
693     context.type = EmulateInstruction::eContextRestoreStackPointer;
694   } else if ((n == gpr_sp_arm64 || n == GetFramePointerRegisterNumber()) &&
695              d == gpr_sp_arm64 && !setflags) {
696     context.type = EmulateInstruction::eContextAdjustStackPointer;
697   } else if (d == GetFramePointerRegisterNumber() && n == gpr_sp_arm64 &&
698              !setflags) {
699     context.type = EmulateInstruction::eContextSetFramePointer;
700   } else {
701     context.type = EmulateInstruction::eContextImmediate;
702   }
703 
704   // If setflags && d == gpr_sp_arm64 then d = WZR/XZR. See CMN, CMP
705   if (!setflags || d != gpr_sp_arm64)
706     WriteRegisterUnsigned(context, eRegisterKindLLDB, gpr_x0_arm64 + d, result);
707 
708   return false;
709 }
710 
711 template <EmulateInstructionARM64::AddrMode a_mode>
712 bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) {
713   uint32_t opc = Bits32(opcode, 31, 30);
714   uint32_t V = Bit32(opcode, 26);
715   uint32_t L = Bit32(opcode, 22);
716   uint32_t imm7 = Bits32(opcode, 21, 15);
717   uint32_t Rt2 = Bits32(opcode, 14, 10);
718   uint32_t Rn = Bits32(opcode, 9, 5);
719   uint32_t Rt = Bits32(opcode, 4, 0);
720 
721   integer n = UInt(Rn);
722   integer t = UInt(Rt);
723   integer t2 = UInt(Rt2);
724   uint64_t idx;
725 
726   MemOp memop = L == 1 ? MemOp_LOAD : MemOp_STORE;
727   boolean vector = (V == 1);
728   // AccType acctype = AccType_NORMAL;
729   boolean is_signed = false;
730   boolean wback = a_mode != AddrMode_OFF;
731   boolean wb_unknown = false;
732   boolean rt_unknown = false;
733   integer scale;
734   integer size;
735 
736   if (opc == 3)
737     return false; // UNDEFINED
738 
739   if (vector) {
740     scale = 2 + UInt(opc);
741   } else {
742     scale = (opc & 2) ? 3 : 2;
743     is_signed = (opc & 1) != 0;
744     if (is_signed && memop == MemOp_STORE)
745       return false; // UNDEFINED
746   }
747 
748   if (!vector && wback && ((t == n) || (t2 == n))) {
749     switch (ConstrainUnpredictable(Unpredictable_WBOVERLAP)) {
750     case Constraint_UNKNOWN:
751       wb_unknown = true; // writeback is UNKNOWN
752       break;
753 
754     case Constraint_SUPPRESSWB:
755       wback = false; // writeback is suppressed
756       break;
757 
758     case Constraint_NOP:
759       memop = MemOp_NOP; // do nothing
760       wback = false;
761       break;
762 
763     case Constraint_NONE:
764       break;
765     }
766   }
767 
768   if (memop == MemOp_LOAD && t == t2) {
769     switch (ConstrainUnpredictable(Unpredictable_LDPOVERLAP)) {
770     case Constraint_UNKNOWN:
771       rt_unknown = true; // result is UNKNOWN
772       break;
773 
774     case Constraint_NOP:
775       memop = MemOp_NOP; // do nothing
776       wback = false;
777       break;
778 
779     default:
780       break;
781     }
782   }
783 
784   idx = LSL(llvm::SignExtend64<7>(imm7), scale);
785   size = (integer)1 << scale;
786   uint64_t datasize = size * 8;
787   uint64_t address;
788   uint64_t wb_address;
789 
790   RegisterValue data_Rt;
791   RegisterValue data_Rt2;
792 
793   //    if (vector)
794   //        CheckFPEnabled(false);
795 
796   RegisterInfo reg_info_base;
797   RegisterInfo reg_info_Rt;
798   RegisterInfo reg_info_Rt2;
799   if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + n, reg_info_base))
800     return false;
801 
802   if (vector) {
803     if (!GetRegisterInfo(eRegisterKindLLDB, fpu_d0_arm64 + t, reg_info_Rt))
804       return false;
805     if (!GetRegisterInfo(eRegisterKindLLDB, fpu_d0_arm64 + t2, reg_info_Rt2))
806       return false;
807   } else {
808     if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + t, reg_info_Rt))
809       return false;
810     if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + t2, reg_info_Rt2))
811       return false;
812   }
813 
814   bool success = false;
815   if (n == 31) {
816     // CheckSPAlignment();
817     address =
818         ReadRegisterUnsigned(eRegisterKindLLDB, gpr_sp_arm64, 0, &success);
819   } else
820     address =
821         ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + n, 0, &success);
822 
823   wb_address = address + idx;
824   if (a_mode != AddrMode_POST)
825     address = wb_address;
826 
827   Context context_t;
828   Context context_t2;
829 
830   uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
831   Status error;
832 
833   switch (memop) {
834   case MemOp_STORE: {
835     if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
836                                                          // based off of the sp
837                                                          // or fp register
838     {
839       context_t.type = eContextPushRegisterOnStack;
840       context_t2.type = eContextPushRegisterOnStack;
841     } else {
842       context_t.type = eContextRegisterStore;
843       context_t2.type = eContextRegisterStore;
844     }
845     context_t.SetRegisterToRegisterPlusOffset(reg_info_Rt, reg_info_base, 0);
846     context_t2.SetRegisterToRegisterPlusOffset(reg_info_Rt2, reg_info_base,
847                                                size);
848 
849     if (!ReadRegister(&reg_info_Rt, data_Rt))
850       return false;
851 
852     if (data_Rt.GetAsMemoryData(&reg_info_Rt, buffer, reg_info_Rt.byte_size,
853                                 eByteOrderLittle, error) == 0)
854       return false;
855 
856     if (!WriteMemory(context_t, address + 0, buffer, reg_info_Rt.byte_size))
857       return false;
858 
859     if (!ReadRegister(&reg_info_Rt2, data_Rt2))
860       return false;
861 
862     if (data_Rt2.GetAsMemoryData(&reg_info_Rt2, buffer, reg_info_Rt2.byte_size,
863                                  eByteOrderLittle, error) == 0)
864       return false;
865 
866     if (!WriteMemory(context_t2, address + size, buffer,
867                      reg_info_Rt2.byte_size))
868       return false;
869   } break;
870 
871   case MemOp_LOAD: {
872     if (n == 31 || n == GetFramePointerRegisterNumber()) // if this load is
873                                                          // based off of the sp
874                                                          // or fp register
875     {
876       context_t.type = eContextPopRegisterOffStack;
877       context_t2.type = eContextPopRegisterOffStack;
878     } else {
879       context_t.type = eContextRegisterLoad;
880       context_t2.type = eContextRegisterLoad;
881     }
882     context_t.SetAddress(address);
883     context_t2.SetAddress(address + size);
884 
885     if (rt_unknown)
886       memset(buffer, 'U', reg_info_Rt.byte_size);
887     else {
888       if (!ReadMemory(context_t, address, buffer, reg_info_Rt.byte_size))
889         return false;
890     }
891 
892     if (data_Rt.SetFromMemoryData(&reg_info_Rt, buffer, reg_info_Rt.byte_size,
893                                   eByteOrderLittle, error) == 0)
894       return false;
895 
896     if (!vector && is_signed && !data_Rt.SignExtend(datasize))
897       return false;
898 
899     if (!WriteRegister(context_t, &reg_info_Rt, data_Rt))
900       return false;
901 
902     if (!rt_unknown) {
903       if (!ReadMemory(context_t2, address + size, buffer,
904                       reg_info_Rt2.byte_size))
905         return false;
906     }
907 
908     if (data_Rt2.SetFromMemoryData(&reg_info_Rt2, buffer,
909                                    reg_info_Rt2.byte_size, eByteOrderLittle,
910                                    error) == 0)
911       return false;
912 
913     if (!vector && is_signed && !data_Rt2.SignExtend(datasize))
914       return false;
915 
916     if (!WriteRegister(context_t2, &reg_info_Rt2, data_Rt2))
917       return false;
918   } break;
919 
920   default:
921     break;
922   }
923 
924   if (wback) {
925     if (wb_unknown)
926       wb_address = LLDB_INVALID_ADDRESS;
927     Context context;
928     context.SetImmediateSigned(idx);
929     if (n == 31)
930       context.type = eContextAdjustStackPointer;
931     else
932       context.type = eContextAdjustBaseRegister;
933     WriteRegisterUnsigned(context, &reg_info_base, wb_address);
934   }
935   return true;
936 }
937 
938 template <EmulateInstructionARM64::AddrMode a_mode>
939 bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) {
940   uint32_t size = Bits32(opcode, 31, 30);
941   uint32_t opc = Bits32(opcode, 23, 22);
942   uint32_t n = Bits32(opcode, 9, 5);
943   uint32_t t = Bits32(opcode, 4, 0);
944 
945   bool wback;
946   bool postindex;
947   uint64_t offset;
948 
949   switch (a_mode) {
950   case AddrMode_POST:
951     wback = true;
952     postindex = true;
953     offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12));
954     break;
955   case AddrMode_PRE:
956     wback = true;
957     postindex = false;
958     offset = llvm::SignExtend64<9>(Bits32(opcode, 20, 12));
959     break;
960   case AddrMode_OFF:
961     wback = false;
962     postindex = false;
963     offset = LSL(Bits32(opcode, 21, 10), size);
964     break;
965   }
966 
967   MemOp memop;
968 
969   if (Bit32(opc, 1) == 0) {
970     memop = Bit32(opc, 0) == 1 ? MemOp_LOAD : MemOp_STORE;
971   } else {
972     memop = MemOp_LOAD;
973     if (size == 2 && Bit32(opc, 0) == 1)
974       return false;
975   }
976 
977   Status error;
978   bool success = false;
979   uint64_t address;
980   uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
981   RegisterValue data_Rt;
982 
983   if (n == 31)
984     address =
985         ReadRegisterUnsigned(eRegisterKindLLDB, gpr_sp_arm64, 0, &success);
986   else
987     address =
988         ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + n, 0, &success);
989 
990   if (!success)
991     return false;
992 
993   if (!postindex)
994     address += offset;
995 
996   RegisterInfo reg_info_base;
997   if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + n, reg_info_base))
998     return false;
999 
1000   RegisterInfo reg_info_Rt;
1001   if (!GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + t, reg_info_Rt))
1002     return false;
1003 
1004   Context context;
1005   switch (memop) {
1006   case MemOp_STORE:
1007     if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
1008                                                          // based off of the sp
1009                                                          // or fp register
1010       context.type = eContextPushRegisterOnStack;
1011     else
1012       context.type = eContextRegisterStore;
1013     context.SetRegisterToRegisterPlusOffset(reg_info_Rt, reg_info_base,
1014                                             postindex ? 0 : offset);
1015 
1016     if (!ReadRegister(&reg_info_Rt, data_Rt))
1017       return false;
1018 
1019     if (data_Rt.GetAsMemoryData(&reg_info_Rt, buffer, reg_info_Rt.byte_size,
1020                                 eByteOrderLittle, error) == 0)
1021       return false;
1022 
1023     if (!WriteMemory(context, address, buffer, reg_info_Rt.byte_size))
1024       return false;
1025     break;
1026 
1027   case MemOp_LOAD:
1028     if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
1029                                                          // based off of the sp
1030                                                          // or fp register
1031       context.type = eContextPopRegisterOffStack;
1032     else
1033       context.type = eContextRegisterLoad;
1034     context.SetAddress(address);
1035 
1036     if (!ReadMemory(context, address, buffer, reg_info_Rt.byte_size))
1037       return false;
1038 
1039     if (data_Rt.SetFromMemoryData(&reg_info_Rt, buffer, reg_info_Rt.byte_size,
1040                                   eByteOrderLittle, error) == 0)
1041       return false;
1042 
1043     if (!WriteRegister(context, &reg_info_Rt, data_Rt))
1044       return false;
1045     break;
1046   default:
1047     return false;
1048   }
1049 
1050   if (wback) {
1051     if (postindex)
1052       address += offset;
1053 
1054     if (n == 31)
1055       context.type = eContextAdjustStackPointer;
1056     else
1057       context.type = eContextAdjustBaseRegister;
1058     context.SetImmediateSigned(offset);
1059 
1060     if (!WriteRegisterUnsigned(context, &reg_info_base, address))
1061       return false;
1062   }
1063   return true;
1064 }
1065 
1066 bool EmulateInstructionARM64::EmulateB(const uint32_t opcode) {
1067 #if 0
1068     // ARM64 pseudo code...
1069     if branch_type == BranchType_CALL then X[30] = PC[] + 4;
1070     BranchTo(PC[] + offset, branch_type);
1071 #endif
1072 
1073   bool success = false;
1074 
1075   EmulateInstruction::Context context;
1076   context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1077   const uint64_t pc = ReadRegisterUnsigned(eRegisterKindGeneric,
1078                                            LLDB_REGNUM_GENERIC_PC, 0, &success);
1079   if (!success)
1080     return false;
1081 
1082   int64_t offset = llvm::SignExtend64<28>(Bits32(opcode, 25, 0) << 2);
1083   BranchType branch_type = Bit32(opcode, 31) ? BranchType_CALL : BranchType_JMP;
1084   addr_t target = pc + offset;
1085   context.SetImmediateSigned(offset);
1086 
1087   switch (branch_type) {
1088   case BranchType_CALL: {
1089     addr_t x30 = pc + 4;
1090     if (!WriteRegisterUnsigned(context, eRegisterKindLLDB, gpr_lr_arm64, x30))
1091       return false;
1092   } break;
1093   case BranchType_JMP:
1094     break;
1095   default:
1096     return false;
1097   }
1098 
1099   if (!BranchTo(context, 64, target))
1100     return false;
1101   return true;
1102 }
1103 
1104 bool EmulateInstructionARM64::EmulateBcond(const uint32_t opcode) {
1105 #if 0
1106     // ARM64 pseudo code...
1107     bits(64) offset = SignExtend(imm19:'00', 64);
1108     bits(4) condition = cond;
1109     if ConditionHolds(condition) then
1110         BranchTo(PC[] + offset, BranchType_JMP);
1111 #endif
1112 
1113   if (ConditionHolds(Bits32(opcode, 3, 0))) {
1114     bool success = false;
1115 
1116     const uint64_t pc = ReadRegisterUnsigned(
1117         eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1118     if (!success)
1119       return false;
1120 
1121     int64_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2);
1122     addr_t target = pc + offset;
1123 
1124     EmulateInstruction::Context context;
1125     context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1126     context.SetImmediateSigned(offset);
1127     if (!BranchTo(context, 64, target))
1128       return false;
1129   }
1130   return true;
1131 }
1132 
1133 bool EmulateInstructionARM64::EmulateCBZ(const uint32_t opcode) {
1134 #if 0
1135     integer t = UInt(Rt);
1136     integer datasize = if sf == '1' then 64 else 32;
1137     boolean iszero = (op == '0');
1138     bits(64) offset = SignExtend(imm19:'00', 64);
1139 
1140     bits(datasize) operand1 = X[t];
1141     if IsZero(operand1) == iszero then
1142         BranchTo(PC[] + offset, BranchType_JMP);
1143 #endif
1144 
1145   bool success = false;
1146 
1147   uint32_t t = Bits32(opcode, 4, 0);
1148   bool is_zero = Bit32(opcode, 24) == 0;
1149   int32_t offset = llvm::SignExtend64<21>(Bits32(opcode, 23, 5) << 2);
1150 
1151   const uint64_t operand =
1152       ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + t, 0, &success);
1153   if (!success)
1154     return false;
1155 
1156   if (m_ignore_conditions || ((operand == 0) == is_zero)) {
1157     const uint64_t pc = ReadRegisterUnsigned(
1158         eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1159     if (!success)
1160       return false;
1161 
1162     EmulateInstruction::Context context;
1163     context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1164     context.SetImmediateSigned(offset);
1165     if (!BranchTo(context, 64, pc + offset))
1166       return false;
1167   }
1168   return true;
1169 }
1170 
1171 bool EmulateInstructionARM64::EmulateTBZ(const uint32_t opcode) {
1172 #if 0
1173     integer t = UInt(Rt);
1174     integer datasize = if b5 == '1' then 64 else 32;
1175     integer bit_pos = UInt(b5:b40);
1176     bit bit_val = op;
1177     bits(64) offset = SignExtend(imm14:'00', 64);
1178 #endif
1179 
1180   bool success = false;
1181 
1182   uint32_t t = Bits32(opcode, 4, 0);
1183   uint32_t bit_pos = (Bit32(opcode, 31) << 6) | (Bits32(opcode, 23, 19));
1184   uint32_t bit_val = Bit32(opcode, 24);
1185   int64_t offset = llvm::SignExtend64<16>(Bits32(opcode, 18, 5) << 2);
1186 
1187   const uint64_t operand =
1188       ReadRegisterUnsigned(eRegisterKindLLDB, gpr_x0_arm64 + t, 0, &success);
1189   if (!success)
1190     return false;
1191 
1192   if (m_ignore_conditions || Bit32(operand, bit_pos) == bit_val) {
1193     const uint64_t pc = ReadRegisterUnsigned(
1194         eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1195     if (!success)
1196       return false;
1197 
1198     EmulateInstruction::Context context;
1199     context.type = EmulateInstruction::eContextRelativeBranchImmediate;
1200     context.SetImmediateSigned(offset);
1201     if (!BranchTo(context, 64, pc + offset))
1202       return false;
1203   }
1204   return true;
1205 }
1206