[Lldb-commits] [lldb] 0577a9f - [LLDB] Change EmulateInstriction::ReadRegister to return Optional

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 12 01:37:45 PDT 2022


Author: David Spickett
Date: 2022-10-12T08:37:36Z
New Revision: 0577a9f8d06b97bf8b4d9aa171096e0797e4f5d1

URL: https://github.com/llvm/llvm-project/commit/0577a9f8d06b97bf8b4d9aa171096e0797e4f5d1
DIFF: https://github.com/llvm/llvm-project/commit/0577a9f8d06b97bf8b4d9aa171096e0797e4f5d1.diff

LOG: [LLDB] Change EmulateInstriction::ReadRegister to return Optional

Making it easier to understand and harder to misuse.
This only applies to the ReadRegister(const RegisterInfo &reg_info) variant.

Depends on D135671

Reviewed By: clayborg

Differential Revision: https://reviews.llvm.org/D135672

Added: 
    

Modified: 
    lldb/include/lldb/Core/EmulateInstruction.h
    lldb/source/Core/EmulateInstruction.cpp
    lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
    lldb/source/Plugins/Instruction/MIPS/EmulateInstructionMIPS.cpp
    lldb/source/Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/EmulateInstruction.h b/lldb/include/lldb/Core/EmulateInstruction.h
index 68b40673faab2..65b7982c36a0b 100644
--- a/lldb/include/lldb/Core/EmulateInstruction.h
+++ b/lldb/include/lldb/Core/EmulateInstruction.h
@@ -388,7 +388,7 @@ class EmulateInstruction : public PluginInterface {
                                        uint32_t reg_num, std::string &reg_name);
 
   // RegisterInfo variants
-  bool ReadRegister(const RegisterInfo &reg_info, RegisterValue &reg_value);
+  llvm::Optional<RegisterValue> ReadRegister(const RegisterInfo &reg_info);
 
   uint64_t ReadRegisterUnsigned(const RegisterInfo &reg_info,
                                 uint64_t fail_value, bool *success_ptr);

diff  --git a/lldb/source/Core/EmulateInstruction.cpp b/lldb/source/Core/EmulateInstruction.cpp
index 73cbcc722ef64..2901609ce7d73 100644
--- a/lldb/source/Core/EmulateInstruction.cpp
+++ b/lldb/source/Core/EmulateInstruction.cpp
@@ -72,20 +72,29 @@ EmulateInstruction::FindPlugin(const ArchSpec &arch,
 
 EmulateInstruction::EmulateInstruction(const ArchSpec &arch) : m_arch(arch) {}
 
-bool EmulateInstruction::ReadRegister(const RegisterInfo &reg_info,
-                                      RegisterValue &reg_value) {
-  if (m_read_reg_callback != nullptr)
-    return m_read_reg_callback(this, m_baton, &reg_info, reg_value);
-  return false;
+llvm::Optional<RegisterValue>
+EmulateInstruction::ReadRegister(const RegisterInfo &reg_info) {
+  if (m_read_reg_callback == nullptr)
+    return {};
+
+  RegisterValue reg_value;
+  bool success = m_read_reg_callback(this, m_baton, &reg_info, reg_value);
+  if (success)
+    return reg_value;
+  return {};
 }
 
 bool EmulateInstruction::ReadRegister(lldb::RegisterKind reg_kind,
                                       uint32_t reg_num,
                                       RegisterValue &reg_value) {
   llvm::Optional<RegisterInfo> reg_info = GetRegisterInfo(reg_kind, reg_num);
-  if (reg_info)
-    return ReadRegister(*reg_info, reg_value);
-  return false;
+  if (!reg_info)
+    return false;
+
+  llvm::Optional<RegisterValue> value = ReadRegister(*reg_info);
+  if (value)
+    reg_value = *value;
+  return value.has_value();
 }
 
 uint64_t EmulateInstruction::ReadRegisterUnsigned(lldb::RegisterKind reg_kind,
@@ -103,12 +112,14 @@ uint64_t EmulateInstruction::ReadRegisterUnsigned(lldb::RegisterKind reg_kind,
 uint64_t EmulateInstruction::ReadRegisterUnsigned(const RegisterInfo &reg_info,
                                                   uint64_t fail_value,
                                                   bool *success_ptr) {
-  RegisterValue reg_value;
-  if (ReadRegister(reg_info, reg_value))
-    return reg_value.GetAsUInt64(fail_value, success_ptr);
-  if (success_ptr)
-    *success_ptr = false;
-  return fail_value;
+  llvm::Optional<RegisterValue> reg_value = ReadRegister(reg_info);
+  if (!reg_value) {
+    if (success_ptr)
+      *success_ptr = false;
+    return fail_value;
+  }
+
+  return reg_value->GetAsUInt64(fail_value, success_ptr);
 }
 
 bool EmulateInstruction::WriteRegister(const Context &context,

diff  --git a/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp b/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
index 4488b45ef8abe..3fc529ae20281 100644
--- a/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
+++ b/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
@@ -768,8 +768,6 @@ bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) {
   uint64_t address;
   uint64_t wb_address;
 
-  RegisterValue data_Rt;
-  RegisterValue data_Rt2;
   llvm::Optional<RegisterInfo> reg_info_base =
       GetRegisterInfo(eRegisterKindLLDB, gpr_x0_arm64 + n);
   if (!reg_info_base)
@@ -824,21 +822,24 @@ bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) {
     context_t2.SetRegisterToRegisterPlusOffset(*reg_info_Rt2, *reg_info_base,
                                                size);
 
-    if (!ReadRegister(*reg_info_Rt, data_Rt))
+    llvm::Optional<RegisterValue> data_Rt = ReadRegister(*reg_info_Rt);
+    if (!data_Rt)
       return false;
 
-    if (data_Rt.GetAsMemoryData(*reg_info_Rt, buffer, reg_info_Rt->byte_size,
-                                eByteOrderLittle, error) == 0)
+    if (data_Rt->GetAsMemoryData(*reg_info_Rt, buffer, reg_info_Rt->byte_size,
+                                 eByteOrderLittle, error) == 0)
       return false;
 
     if (!WriteMemory(context_t, address + 0, buffer, reg_info_Rt->byte_size))
       return false;
 
-    if (!ReadRegister(*reg_info_Rt2, data_Rt2))
+    llvm::Optional<RegisterValue> data_Rt2 = ReadRegister(*reg_info_Rt2);
+    if (!data_Rt2)
       return false;
 
-    if (data_Rt2.GetAsMemoryData(*reg_info_Rt2, buffer, reg_info_Rt2->byte_size,
-                                 eByteOrderLittle, error) == 0)
+    if (data_Rt2->GetAsMemoryData(*reg_info_Rt2, buffer,
+                                  reg_info_Rt2->byte_size, eByteOrderLittle,
+                                  error) == 0)
       return false;
 
     if (!WriteMemory(context_t2, address + size, buffer,
@@ -867,6 +868,7 @@ bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) {
         return false;
     }
 
+    RegisterValue data_Rt;
     if (data_Rt.SetFromMemoryData(*reg_info_Rt, buffer, reg_info_Rt->byte_size,
                                   eByteOrderLittle, error) == 0)
       return false;
@@ -883,6 +885,7 @@ bool EmulateInstructionARM64::EmulateLDPSTP(const uint32_t opcode) {
         return false;
     }
 
+    RegisterValue data_Rt2;
     if (data_Rt2.SetFromMemoryData(*reg_info_Rt2, buffer,
                                    reg_info_Rt2->byte_size, eByteOrderLittle,
                                    error) == 0)
@@ -956,7 +959,6 @@ bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) {
   bool success = false;
   uint64_t address;
   uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
-  RegisterValue data_Rt;
 
   if (n == 31)
     address =
@@ -983,7 +985,7 @@ bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) {
 
   Context context;
   switch (memop) {
-  case MemOp_STORE:
+  case MemOp_STORE: {
     if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
                                                          // based off of the sp
                                                          // or fp register
@@ -993,18 +995,19 @@ bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) {
     context.SetRegisterToRegisterPlusOffset(*reg_info_Rt, *reg_info_base,
                                             postindex ? 0 : offset);
 
-    if (!ReadRegister(*reg_info_Rt, data_Rt))
+    llvm::Optional<RegisterValue> data_Rt = ReadRegister(*reg_info_Rt);
+    if (!data_Rt)
       return false;
 
-    if (data_Rt.GetAsMemoryData(*reg_info_Rt, buffer, reg_info_Rt->byte_size,
-                                eByteOrderLittle, error) == 0)
+    if (data_Rt->GetAsMemoryData(*reg_info_Rt, buffer, reg_info_Rt->byte_size,
+                                 eByteOrderLittle, error) == 0)
       return false;
 
     if (!WriteMemory(context, address, buffer, reg_info_Rt->byte_size))
       return false;
-    break;
+  } break;
 
-  case MemOp_LOAD:
+  case MemOp_LOAD: {
     if (n == 31 || n == GetFramePointerRegisterNumber()) // if this store is
                                                          // based off of the sp
                                                          // or fp register
@@ -1016,13 +1019,14 @@ bool EmulateInstructionARM64::EmulateLDRSTRImm(const uint32_t opcode) {
     if (!ReadMemory(context, address, buffer, reg_info_Rt->byte_size))
       return false;
 
+    RegisterValue data_Rt;
     if (data_Rt.SetFromMemoryData(*reg_info_Rt, buffer, reg_info_Rt->byte_size,
                                   eByteOrderLittle, error) == 0)
       return false;
 
     if (!WriteRegister(context, *reg_info_Rt, data_Rt))
       return false;
-    break;
+  } break;
   default:
     return false;
   }

diff  --git a/lldb/source/Plugins/Instruction/MIPS/EmulateInstructionMIPS.cpp b/lldb/source/Plugins/Instruction/MIPS/EmulateInstructionMIPS.cpp
index 0fe65c27ebaca..9e7b73cb715a0 100644
--- a/lldb/source/Plugins/Instruction/MIPS/EmulateInstructionMIPS.cpp
+++ b/lldb/source/Plugins/Instruction/MIPS/EmulateInstructionMIPS.cpp
@@ -1259,18 +1259,19 @@ bool EmulateInstructionMIPS::Emulate_SW(llvm::MCInst &insn) {
       return false;
 
     Context context;
-    RegisterValue data_src;
     context.type = eContextPushRegisterOnStack;
     context.SetRegisterToRegisterPlusOffset(*reg_info_src, *reg_info_base, 0);
 
     uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
     Status error;
 
-    if (!ReadRegister(*reg_info_base, data_src))
+    llvm::Optional<RegisterValue> data_src = ReadRegister(*reg_info_base);
+    if (!data_src)
       return false;
 
-    if (data_src.GetAsMemoryData(*reg_info_src, buffer, reg_info_src->byte_size,
-                                 eByteOrderLittle, error) == 0)
+    if (data_src->GetAsMemoryData(*reg_info_src, buffer,
+                                  reg_info_src->byte_size, eByteOrderLittle,
+                                  error) == 0)
       return false;
 
     if (!WriteMemory(context, address, buffer, reg_info_src->byte_size))
@@ -1518,18 +1519,18 @@ bool EmulateInstructionMIPS::Emulate_SWSP(llvm::MCInst &insn) {
   if (base == dwarf_sp_mips && nonvolatile_reg_p(src)) {
     RegisterInfo reg_info_src = {};
     Context context;
-    RegisterValue data_src;
     context.type = eContextPushRegisterOnStack;
     context.SetRegisterToRegisterPlusOffset(reg_info_src, *reg_info_base, 0);
 
     uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
     Status error;
 
-    if (!ReadRegister(*reg_info_base, data_src))
+    llvm::Optional<RegisterValue> data_src = ReadRegister(*reg_info_base);
+    if (!data_src)
       return false;
 
-    if (data_src.GetAsMemoryData(reg_info_src, buffer, reg_info_src.byte_size,
-                                 eByteOrderLittle, error) == 0)
+    if (data_src->GetAsMemoryData(reg_info_src, buffer, reg_info_src.byte_size,
+                                  eByteOrderLittle, error) == 0)
       return false;
 
     if (!WriteMemory(context, address, buffer, reg_info_src.byte_size))
@@ -1600,18 +1601,19 @@ bool EmulateInstructionMIPS::Emulate_SWM16_32(llvm::MCInst &insn) {
       return false;
 
     Context context;
-    RegisterValue data_src;
     context.type = eContextPushRegisterOnStack;
     context.SetRegisterToRegisterPlusOffset(*reg_info_src, *reg_info_base, 0);
 
     uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
     Status error;
 
-    if (!ReadRegister(*reg_info_base, data_src))
+    llvm::Optional<RegisterValue> data_src = ReadRegister(*reg_info_base);
+    if (!data_src)
       return false;
 
-    if (data_src.GetAsMemoryData(*reg_info_src, buffer, reg_info_src->byte_size,
-                                 eByteOrderLittle, error) == 0)
+    if (data_src->GetAsMemoryData(*reg_info_src, buffer,
+                                  reg_info_src->byte_size, eByteOrderLittle,
+                                  error) == 0)
       return false;
 
     if (!WriteMemory(context, base_address, buffer, reg_info_src->byte_size))

diff  --git a/lldb/source/Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.cpp b/lldb/source/Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.cpp
index e6f561c08fe1c..37f235ce87663 100644
--- a/lldb/source/Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.cpp
+++ b/lldb/source/Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.cpp
@@ -1153,18 +1153,19 @@ bool EmulateInstructionMIPS64::Emulate_SD(llvm::MCInst &insn) {
   /* We look for sp based non-volatile register stores */
   if (nonvolatile_reg_p(src)) {
     Context context;
-    RegisterValue data_src;
     context.type = eContextPushRegisterOnStack;
     context.SetRegisterToRegisterPlusOffset(*reg_info_src, *reg_info_base, 0);
 
     uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
     Status error;
 
-    if (!ReadRegister(*reg_info_base, data_src))
+    llvm::Optional<RegisterValue> data_src = ReadRegister(*reg_info_base);
+    if (!data_src)
       return false;
 
-    if (data_src.GetAsMemoryData(*reg_info_src, buffer, reg_info_src->byte_size,
-                                 eByteOrderLittle, error) == 0)
+    if (data_src->GetAsMemoryData(*reg_info_src, buffer,
+                                  reg_info_src->byte_size, eByteOrderLittle,
+                                  error) == 0)
       return false;
 
     if (!WriteMemory(context, address, buffer, reg_info_src->byte_size))


        


More information about the lldb-commits mailing list