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/ArchSpec.h"
15 #include "lldb/Core/Address.h"
16 #include "lldb/Core/ConstString.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Symbol/UnwindPlan.h"
20 
21 #include "Plugins/Process/Utility/ARMDefines.h"
22 #include "Plugins/Process/Utility/ARMUtils.h"
23 #include "Utility/ARM64_DWARF_Registers.h"
24 
25 #include "llvm/Support/MathExtras.h" // for SignExtend32 template function
26                                      // and CountTrailingZeros_32 function
27 
28 #include "Plugins/Process/Utility/InstructionUtils.h"
29 
30 using namespace lldb;
31 using namespace lldb_private;
32 
33 #define No_VFP  0
34 #define VFPv1   (1u << 1)
35 #define VFPv2   (1u << 2)
36 #define VFPv3   (1u << 3)
37 #define AdvancedSIMD (1u << 4)
38 
39 #define VFPv1_ABOVE (VFPv1 | VFPv2 | VFPv3 | AdvancedSIMD)
40 #define VFPv2_ABOVE (VFPv2 | VFPv3 | AdvancedSIMD)
41 #define VFPv2v3     (VFPv2 | VFPv3)
42 
43 #define UInt(x) ((uint64_t)x)
44 #define SInt(x) ((int64_t)x)
45 #define bit bool
46 #define boolean bool
47 #define integer int64_t
48 
49 static inline bool
50 IsZero(uint64_t x)
51 {
52     return x == 0;
53 }
54 
55 static inline uint64_t
56 NOT(uint64_t x)
57 {
58     return ~x;
59 }
60 
61 #if 0
62 // LSL_C()
63 // =======
64 static inline uint64_t
65 LSL_C (uint64_t x, integer shift, bool &carry_out)
66 {
67     assert (shift >= 0);
68     uint64_t result = x << shift;
69     carry_out = ((1ull << (64-1)) >> (shift - 1)) != 0;
70     return result;
71 }
72 #endif
73 
74 // LSL()
75 // =====
76 
77 static inline uint64_t
78 LSL(uint64_t x, integer shift)
79 {
80     if (shift == 0)
81         return x;
82     return x << shift;
83 }
84 
85 // AddWithCarry()
86 // ===============
87 static inline uint64_t
88 AddWithCarry (uint32_t N, uint64_t x, uint64_t y, bit carry_in, EmulateInstructionARM64::ProcState &proc_state)
89 {
90     uint64_t unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
91     int64_t signed_sum = SInt(x) + SInt(y) + UInt(carry_in);
92     uint64_t result = unsigned_sum;
93     if (N < 64)
94         result = Bits64 (result, N-1, 0);
95     proc_state.N = Bit64(result, N-1);
96     proc_state.Z = IsZero(result);
97     proc_state.C = UInt(result) == unsigned_sum;
98     proc_state.V = SInt(result) == signed_sum;
99     return result;
100 }
101 
102 // ConstrainUnpredictable()
103 // ========================
104 
105 EmulateInstructionARM64::ConstraintType
106 ConstrainUnpredictable (EmulateInstructionARM64::Unpredictable which)
107 {
108     EmulateInstructionARM64::ConstraintType result = EmulateInstructionARM64::Constraint_UNKNOWN;
109     switch (which)
110     {
111         case EmulateInstructionARM64::Unpredictable_WBOVERLAP:
112         case EmulateInstructionARM64::Unpredictable_LDPOVERLAP:
113             // TODO: don't know what to really do here? Pseudo code says:
114             // set result to one of above Constraint behaviours or UNDEFINED
115             break;
116     }
117     return result;
118 }
119 
120 
121 
122 //----------------------------------------------------------------------
123 //
124 // EmulateInstructionARM implementation
125 //
126 //----------------------------------------------------------------------
127 
128 void
129 EmulateInstructionARM64::Initialize ()
130 {
131     PluginManager::RegisterPlugin (GetPluginNameStatic (),
132                                    GetPluginDescriptionStatic (),
133                                    CreateInstance);
134 }
135 
136 void
137 EmulateInstructionARM64::Terminate ()
138 {
139     PluginManager::UnregisterPlugin (CreateInstance);
140 }
141 
142 ConstString
143 EmulateInstructionARM64::GetPluginNameStatic ()
144 {
145     ConstString g_plugin_name ("lldb.emulate-instruction.arm64");
146     return g_plugin_name;
147 }
148 
149 lldb_private::ConstString
150 EmulateInstructionARM64::GetPluginName()
151 {
152     static ConstString g_plugin_name ("EmulateInstructionARM64");
153     return g_plugin_name;
154 }
155 
156 const char *
157 EmulateInstructionARM64::GetPluginDescriptionStatic ()
158 {
159     return "Emulate instructions for the ARM64 architecture.";
160 }
161 
162 EmulateInstruction *
163 EmulateInstructionARM64::CreateInstance (const ArchSpec &arch, InstructionType inst_type)
164 {
165     if (EmulateInstructionARM64::SupportsEmulatingInstructionsOfTypeStatic(inst_type))
166     {
167         if (arch.GetTriple().getArch() == llvm::Triple::arm64)
168         {
169             std::auto_ptr<EmulateInstructionARM64> emulate_insn_ap (new EmulateInstructionARM64 (arch));
170             if (emulate_insn_ap.get())
171                 return emulate_insn_ap.release();
172         }
173     }
174 
175     return NULL;
176 }
177 
178 bool
179 EmulateInstructionARM64::SetTargetTriple (const ArchSpec &arch)
180 {
181     if (arch.GetTriple().getArch () == llvm::Triple::arm)
182         return true;
183     else if (arch.GetTriple().getArch () == llvm::Triple::thumb)
184         return true;
185 
186     return false;
187 }
188 
189 bool
190 EmulateInstructionARM64::GetRegisterInfo (uint32_t reg_kind, uint32_t reg_num, RegisterInfo &reg_info)
191 {
192     if (reg_kind == eRegisterKindGeneric)
193     {
194         switch (reg_num)
195         {
196             case LLDB_REGNUM_GENERIC_PC:    reg_kind = eRegisterKindDWARF; reg_num = arm64_dwarf::pc; break;
197             case LLDB_REGNUM_GENERIC_SP:    reg_kind = eRegisterKindDWARF; reg_num = arm64_dwarf::sp; break;
198             case LLDB_REGNUM_GENERIC_FP:    reg_kind = eRegisterKindDWARF; reg_num = arm64_dwarf::fp; break;
199             case LLDB_REGNUM_GENERIC_RA:    reg_kind = eRegisterKindDWARF; reg_num = arm64_dwarf::lr; break;
200             case LLDB_REGNUM_GENERIC_FLAGS:
201                 // There is no DWARF register number for the CPSR right now...
202                 reg_info.name = "cpsr";
203                 reg_info.alt_name = NULL;
204                 reg_info.byte_size = 4;
205                 reg_info.byte_offset = 0;
206                 reg_info.encoding = eEncodingUint;
207                 reg_info.format = eFormatHex;
208                 for (uint32_t i=0; i<lldb::kNumRegisterKinds; ++i)
209                     reg_info.kinds[reg_kind] = LLDB_INVALID_REGNUM;
210                 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
211                 return true;
212 
213             default: return false;
214         }
215     }
216 
217     if (reg_kind == eRegisterKindDWARF)
218         return arm64_dwarf::GetRegisterInfo(reg_num, reg_info);
219     return false;
220 }
221 
222 EmulateInstructionARM64::Opcode*
223 EmulateInstructionARM64::GetOpcodeForInstruction (const uint32_t opcode)
224 {
225     static EmulateInstructionARM64::Opcode
226     g_opcodes[] =
227     {
228         //----------------------------------------------------------------------
229         // Prologue instructions
230         //----------------------------------------------------------------------
231 
232         // push register(s)
233         { 0xff000000, 0xd1000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "SUB  <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}" },
234         { 0xff000000, 0xf1000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "SUBS  <Xd>, <Xn|SP>, #<imm> {, <shift>}" },
235         { 0xff000000, 0x91000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "ADD  <Xd|SP>, <Xn|SP>, #<imm> {, <shift>}" },
236         { 0xff000000, 0xb1000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "ADDS  <Xd>, <Xn|SP>, #<imm> {, <shift>}" },
237 
238 
239         { 0xff000000, 0x51000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "SUB  <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}" },
240         { 0xff000000, 0x71000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "SUBS  <Wd>, <Wn|WSP>, #<imm> {, <shift>}" },
241         { 0xff000000, 0x11000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "ADD  <Wd|WSP>, <Wn|WSP>, #<imm> {, <shift>}" },
242         { 0xff000000, 0x31000000, No_VFP, &EmulateInstructionARM64::Emulate_addsub_imm, "ADDS  <Wd>, <Wn|WSP>, #<imm> {, <shift>}" },
243 
244         { 0xffc00000, 0x29000000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_off, "STP  <Wt>, <Wt2>, [<Xn|SP>{, #<imm>}]" },
245         { 0xffc00000, 0xa9000000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_off, "STP  <Xt>, <Xt2>, [<Xn|SP>{, #<imm>}]" },
246         { 0xffc00000, 0x2d000000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_off, "STP  <St>, <St2>, [<Xn|SP>{, #<imm>}]" },
247         { 0xffc00000, 0x6d000000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_off, "STP  <Dt>, <Dt2>, [<Xn|SP>{, #<imm>}]" },
248         { 0xffc00000, 0xad000000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_off, "STP  <Qt>, <Qt2>, [<Xn|SP>{, #<imm>}]" },
249 
250         { 0xffc00000, 0xad800000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_pre, "STP  <Qt>, <Qt2>, [<Xn|SP>, #<imm>]!" },
251         { 0xffc00000, 0x2d800000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_pre, "STP  <St>, <St2>, [<Xn|SP>, #<imm>]!" },
252         { 0xffc00000, 0x29800000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_pre, "STP  <Wt>, <Wt2>, [<Xn|SP>, #<imm>]!" },
253         { 0xffc00000, 0x6d800000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_pre, "STP  <Dt>, <Dt2>, [<Xn|SP>, #<imm>]!" },
254         { 0xffc00000, 0xa9800000, No_VFP, &EmulateInstructionARM64::Emulate_ldstpair_pre, "STP  <Xt>, <Xt2>, [<Xn|SP>, #<imm>]!" },
255 
256     };
257     static const size_t k_num_arm_opcodes = sizeof(g_opcodes)/sizeof(EmulateInstructionARM64::Opcode);
258 
259     for (size_t i=0; i<k_num_arm_opcodes; ++i)
260     {
261         if ((g_opcodes[i].mask & opcode) == g_opcodes[i].value)
262             return &g_opcodes[i];
263     }
264     return NULL;
265 }
266 
267 bool
268 EmulateInstructionARM64::ReadInstruction ()
269 {
270     bool success = false;
271     m_addr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
272     if (success)
273     {
274         Context read_inst_context;
275         read_inst_context.type = eContextReadOpcode;
276         read_inst_context.SetNoArgs ();
277         m_opcode.SetOpcode32 (ReadMemoryUnsigned (read_inst_context, m_addr, 4, 0, &success), GetByteOrder());
278     }
279     if (!success)
280         m_addr = LLDB_INVALID_ADDRESS;
281     return success;
282 }
283 
284 
285 bool
286 EmulateInstructionARM64::EvaluateInstruction (uint32_t evaluate_options)
287 {
288     const uint32_t opcode = m_opcode.GetOpcode32();
289     Opcode *opcode_data = GetOpcodeForInstruction(opcode);
290     if (opcode_data == NULL)
291         return false;
292 
293     //printf ("opcode template for 0x%8.8x: %s\n", opcode, opcode_data->name);
294     const bool auto_advance_pc = evaluate_options & eEmulateInstructionOptionAutoAdvancePC;
295     m_ignore_conditions = evaluate_options & eEmulateInstructionOptionIgnoreConditions;
296 
297     bool success = false;
298 //    if (m_opcode_cpsr == 0 || m_ignore_conditions == false)
299 //    {
300 //        m_opcode_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric,         // use eRegisterKindDWARF is we ever get a cpsr DWARF register number
301 //                                              LLDB_REGNUM_GENERIC_FLAGS,    // use arm64_dwarf::cpsr if we ever get one
302 //                                              0,
303 //                                              &success);
304 //    }
305 
306     // Only return false if we are unable to read the CPSR if we care about conditions
307     if (success == false && m_ignore_conditions == false)
308         return false;
309 
310     uint32_t orig_pc_value = 0;
311     if (auto_advance_pc)
312     {
313         orig_pc_value = ReadRegisterUnsigned (eRegisterKindDWARF, arm64_dwarf::pc, 0, &success);
314         if (!success)
315             return false;
316     }
317 
318     // Call the Emulate... function.
319     success = (this->*opcode_data->callback) (opcode);
320     if (!success)
321         return false;
322 
323     if (auto_advance_pc)
324     {
325         uint32_t new_pc_value = ReadRegisterUnsigned (eRegisterKindDWARF, arm64_dwarf::pc, 0, &success);
326         if (!success)
327             return false;
328 
329         if (auto_advance_pc && (new_pc_value == orig_pc_value))
330         {
331             EmulateInstruction::Context context;
332             context.type = eContextAdvancePC;
333             context.SetNoArgs();
334             if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, arm64_dwarf::pc, orig_pc_value + 4))
335                 return false;
336         }
337     }
338     return true;
339 }
340 
341 bool
342 EmulateInstructionARM64::CreateFunctionEntryUnwind (UnwindPlan &unwind_plan)
343 {
344     unwind_plan.Clear();
345     unwind_plan.SetRegisterKind (eRegisterKindDWARF);
346 
347     UnwindPlan::RowSP row(new UnwindPlan::Row);
348     const bool can_replace = false;
349 
350     // Our previous Call Frame Address is the stack pointer
351     row->SetCFARegister (arm64_dwarf::sp);
352 
353     // Our previous PC is in the LR
354     row->SetRegisterLocationToRegister(arm64_dwarf::pc, arm64_dwarf::lr, can_replace);
355 
356     unwind_plan.AppendRow (row);
357 
358     // All other registers are the same.
359 
360     unwind_plan.SetSourceName ("EmulateInstructionARM64");
361     unwind_plan.SetSourcedFromCompiler (eLazyBoolNo);
362     unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolYes);
363     return true;
364 }
365 
366 
367 
368 bool
369 EmulateInstructionARM64::Emulate_addsub_imm (const uint32_t opcode)
370 {
371     // integer d = UInt(Rd);
372     // integer n = UInt(Rn);
373     // integer datasize = if sf == 1 then 64 else 32;
374     // boolean sub_op = (op == 1);
375     // boolean setflags = (S == 1);
376     // bits(datasize) imm;
377     //
378     // case shift of
379     //     when '00' imm = ZeroExtend(imm12, datasize);
380     //     when '01' imm = ZeroExtend(imm12 : Zeros(12), datasize);
381     //    when '1x' UNDEFINED;
382     //
383     //
384     // bits(datasize) result;
385     // bits(datasize) operand1 = if n == 31 then SP[] else X[n];
386     // bits(datasize) operand2 = imm;
387     // bits(4) nzcv;
388     // bit carry_in;
389     //
390     // if sub_op then
391     //     operand2 = NOT(operand2);
392     //     carry_in = 1;
393     // else
394     //     carry_in = 0;
395     //
396     // (result, nzcv) = AddWithCarry(operand1, operand2, carry_in);
397     //
398     // if setflags then
399     //     PSTATE.NZCV = nzcv;
400     //
401     // if d == 31 && !setflags then
402     //     SP[] = result;
403     // else
404     //     X[d] = result;
405 
406     const uint32_t sf = Bit32(opcode, 31);
407     const uint32_t op = Bit32(opcode, 30);
408     const uint32_t S = Bit32(opcode, 29);
409     const uint32_t shift = Bits32(opcode, 23, 22);
410     const uint32_t imm12 = Bits32(opcode, 21, 10);
411     const uint32_t Rn = Bits32(opcode, 9, 5);
412     const uint32_t Rd = Bits32(opcode, 4, 0);
413 
414     bool success = false;
415 
416     const uint32_t d = UInt(Rd);
417     const uint32_t n = UInt(Rn);
418     const uint32_t datasize = (sf == 1) ? 64 : 32;
419     boolean sub_op = op == 1;
420     boolean setflags = S == 1;
421     uint64_t imm;
422 
423     switch (shift)
424     {
425         case 0: imm = imm12; break;
426         case 1: imm = imm12 << 12; break;
427         default: return false;  // UNDEFINED;
428     }
429     uint64_t result;
430     uint64_t operand1 = ReadRegisterUnsigned (eRegisterKindDWARF, arm64_dwarf::x0 + n, 0, &success);
431     uint64_t operand2 = imm;
432     bit carry_in;
433 
434     if (sub_op)
435     {
436         operand2 = NOT(operand2);
437         carry_in = 1;
438         imm = -imm; // For the Register plug offset context below
439     }
440     else
441     {
442         carry_in = 0;
443     }
444 
445     ProcState proc_state;
446 
447     result = AddWithCarry (datasize, operand1, operand2, carry_in, proc_state);
448 
449     if (setflags)
450     {
451         m_emulated_pstate.N = proc_state.N;
452         m_emulated_pstate.Z = proc_state.Z;
453         m_emulated_pstate.C = proc_state.C;
454         m_emulated_pstate.V = proc_state.V;
455     }
456 
457     Context context;
458     RegisterInfo reg_info_Rn;
459     if (arm64_dwarf::GetRegisterInfo (n, reg_info_Rn))
460         context.SetRegisterPlusOffset (reg_info_Rn, imm);
461 
462     if ((n == arm64_dwarf::sp || n == arm64_dwarf::fp) &&
463         d == arm64_dwarf::sp &&
464         !setflags)
465     {
466         context.type = EmulateInstruction::eContextAdjustStackPointer;
467     }
468     else if (d == arm64_dwarf::fp &&
469              n == arm64_dwarf::sp &&
470              !setflags)
471     {
472         context.type = EmulateInstruction::eContextSetFramePointer;
473     }
474     else
475     {
476         context.type = EmulateInstruction::eContextImmediate;
477     }
478     WriteRegisterUnsigned (context, eRegisterKindDWARF, arm64_dwarf::x0 + d, result);
479 
480     return false;
481 }
482 
483 bool
484 EmulateInstructionARM64::Emulate_ldstpair_off (const uint32_t opcode)
485 {
486     return Emulate_ldstpair (opcode, AddrMode_OFF);
487 }
488 
489 
490 bool
491 EmulateInstructionARM64::Emulate_ldstpair_pre (const uint32_t opcode)
492 {
493     return Emulate_ldstpair (opcode, AddrMode_PRE);
494 }
495 
496 bool
497 EmulateInstructionARM64::Emulate_ldstpair (const uint32_t opcode, AddrMode a_mode)
498 {
499     uint32_t opc = Bits32(opcode, 31, 30);
500     uint32_t V = Bit32(opcode, 26);
501     uint32_t L = Bit32(opcode, 22);
502     uint32_t imm7 = Bits32(opcode, 21, 15);
503     uint32_t Rt2 = Bits32(opcode, 14, 10);
504     uint32_t Rn = Bits32(opcode, 9, 5);
505     uint32_t Rt = Bits32(opcode, 4, 0);
506 
507     integer n = UInt(Rn);
508     integer t = UInt(Rt);
509     integer t2 = UInt(Rt2);
510     uint64_t idx;
511 
512     MemOp memop = L == 1 ? MemOp_LOAD : MemOp_STORE;
513     boolean vector = (V == 1);
514     //AccType acctype = AccType_NORMAL;
515     boolean is_signed = false;
516     boolean wback = a_mode != AddrMode_OFF;
517     boolean wb_unknown = false;
518     boolean rt_unknown = false;
519     integer scale;
520     integer size;
521 
522     if (opc == 3)
523         return false; // UNDEFINED
524 
525     if (vector)
526     {
527         scale = 2 + UInt(opc);
528     }
529     else
530     {
531         scale = (opc & 2) ? 3 : 2;
532         is_signed = (opc & 1) != 0;
533         if (is_signed && memop == MemOp_STORE)
534             return false; // UNDEFINED
535     }
536 
537     if (!vector && wback && ((t == n) || (t2 == n)))
538     {
539         switch (ConstrainUnpredictable(Unpredictable_WBOVERLAP))
540         {
541             case Constraint_UNKNOWN:
542                 wb_unknown = true;  // writeback is UNKNOWN
543                 break;
544 
545             case Constraint_SUPPRESSWB:
546                 wback = false;      // writeback is suppressed
547                 break;
548 
549             case Constraint_NOP:
550                 memop = MemOp_NOP;  // do nothing
551                 wback = false;
552                 break;
553 
554             case Constraint_NONE:
555                 break;
556         }
557     }
558 
559     if (memop == MemOp_LOAD && t == t2)
560     {
561         switch (ConstrainUnpredictable(Unpredictable_LDPOVERLAP))
562         {
563             case Constraint_UNKNOWN:
564                 rt_unknown = true;  // result is UNKNOWN
565                 break;
566 
567             case Constraint_NOP:
568                 memop = MemOp_NOP;  // do nothing
569                 wback = false;
570                 break;
571 
572             default:
573                 break;
574         }
575     }
576 
577     idx = LSL(llvm::SignExtend64<7>(imm7), scale);
578     size = (integer)1 << scale;
579     uint64_t datasize = size * 8;
580     uint64_t address;
581     uint64_t wb_address;
582 
583     RegisterValue data_Rt;
584     RegisterValue data_Rt2;
585 
586     //    if (vector)
587     //        CheckFPEnabled(false);
588 
589     RegisterInfo reg_info_base;
590     RegisterInfo reg_info_Rt;
591     RegisterInfo reg_info_Rt2;
592     if (!GetRegisterInfo (eRegisterKindDWARF, arm64_dwarf::x0 + n, reg_info_base))
593         return false;
594 
595     if (vector)
596     {
597         if (!GetRegisterInfo (eRegisterKindDWARF, arm64_dwarf::v0 + n, reg_info_Rt))
598             return false;
599         if (!GetRegisterInfo (eRegisterKindDWARF, arm64_dwarf::v0 + n, reg_info_Rt2))
600             return false;
601     }
602     else
603     {
604         if (!GetRegisterInfo (eRegisterKindDWARF, arm64_dwarf::x0 + t, reg_info_Rt))
605             return false;
606         if (!GetRegisterInfo (eRegisterKindDWARF, arm64_dwarf::x0 + t2, reg_info_Rt2))
607             return false;
608     }
609 
610     bool success = false;
611     if (n == 31)
612     {
613         //CheckSPAlignment();
614         address = ReadRegisterUnsigned (eRegisterKindDWARF, arm64_dwarf::sp, 0, &success);
615     }
616     else
617         address = ReadRegisterUnsigned (eRegisterKindDWARF, arm64_dwarf::x0 + n, 0, &success);
618 
619     wb_address = address + idx;
620     if (a_mode != AddrMode_POST)
621         address = wb_address;
622 
623     Context context_t;
624     Context context_t2;
625 
626     if (n == 31 || n == 29) // if this store is based off of the sp or fp register
627     {
628         context_t.type = eContextPushRegisterOnStack;
629         context_t2.type = eContextPushRegisterOnStack;
630     }
631     else
632     {
633         context_t.type = eContextRegisterPlusOffset;
634         context_t2.type = eContextRegisterPlusOffset;
635     }
636     context_t.SetRegisterToRegisterPlusOffset (reg_info_Rt, reg_info_base, 0);
637     context_t2.SetRegisterToRegisterPlusOffset (reg_info_Rt2, reg_info_base, size);
638     uint8_t buffer [RegisterValue::kMaxRegisterByteSize];
639     Error error;
640 
641     switch (memop)
642     {
643         case MemOp_STORE:
644         {
645             if (!ReadRegister (&reg_info_Rt, data_Rt))
646                 return false;
647 
648             if (data_Rt.GetAsMemoryData(&reg_info_Rt, buffer, reg_info_Rt.byte_size, eByteOrderLittle, error) == 0)
649                 return false;
650 
651             if (!WriteMemory(context_t, address + 0, buffer, reg_info_Rt.byte_size))
652                 return false;
653 
654             if (!ReadRegister (&reg_info_Rt2, data_Rt2))
655                 return false;
656 
657             if (data_Rt2.GetAsMemoryData(&reg_info_Rt2, buffer, reg_info_Rt2.byte_size, eByteOrderLittle, error) == 0)
658                 return false;
659 
660             if (!WriteMemory(context_t2, address + size, buffer, reg_info_Rt2.byte_size))
661                 return false;
662         }
663             break;
664 
665         case MemOp_LOAD:
666         {
667             if (rt_unknown)
668                 memset (buffer, 'U', reg_info_Rt.byte_size);
669             else
670             {
671                 if (!ReadMemory (context_t, address, buffer, reg_info_Rt.byte_size))
672                     return false;
673             }
674 
675             if (data_Rt.SetFromMemoryData(&reg_info_Rt, buffer, reg_info_Rt.byte_size, eByteOrderLittle, error) == 0)
676                 return false;
677 
678             if (!vector && is_signed && !data_Rt.SignExtend (datasize))
679                 return false;
680 
681             if (!WriteRegister (context_t, &reg_info_Rt, data_Rt))
682                 return false;
683 
684             if (!rt_unknown)
685             {
686                 if (!ReadMemory (context_t2, address + size, buffer, reg_info_Rt2.byte_size))
687                     return false;
688             }
689 
690             if (data_Rt2.SetFromMemoryData(&reg_info_Rt2, buffer, reg_info_Rt2.byte_size, eByteOrderLittle, error) == 0)
691                 return false;
692 
693             if (!vector && is_signed && !data_Rt2.SignExtend (datasize))
694                 return false;
695 
696             if (!WriteRegister (context_t2, &reg_info_Rt2, data_Rt2))
697                 return false;
698         }
699             break;
700 
701         default:
702             break;
703     }
704 
705     if (wback)
706     {
707         if (wb_unknown)
708             wb_address = LLDB_INVALID_ADDRESS;
709         Context context;
710         context.SetImmediateSigned (idx);
711         if (n == 31)
712             context.type = eContextAdjustStackPointer;
713         else
714             context.type = eContextAdjustBaseRegister;
715         WriteRegisterUnsigned (context, &reg_info_base, wb_address);
716     }
717     return true;
718 }
719