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