[Lldb-commits] [lldb] ac6799d - [lldb] Improve RegisterValue::GetScalarValue

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 4 05:57:29 PST 2020


Author: Pavel Labath
Date: 2020-11-04T14:53:06+01:00
New Revision: ac6799d25a59e262191e0babcd7024537fda467e

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

LOG: [lldb] Improve RegisterValue::GetScalarValue

Using Scalar::SetValueFromData, we make the code simpler, handle
big-endian systems properly, _and_ avoid type aliasing issues.

Added: 
    

Modified: 
    lldb/source/Utility/RegisterValue.cpp
    lldb/unittests/Utility/RegisterValueTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Utility/RegisterValue.cpp b/lldb/source/Utility/RegisterValue.cpp
index 7f545c214a4c..7848f784d8f2 100644
--- a/lldb/source/Utility/RegisterValue.cpp
+++ b/lldb/source/Utility/RegisterValue.cpp
@@ -138,36 +138,10 @@ bool RegisterValue::GetScalarValue(Scalar &scalar) const {
   case eTypeInvalid:
     break;
   case eTypeBytes: {
-    switch (buffer.length) {
-    default:
-      break;
-    case 1:
-      scalar = *(const uint8_t *)buffer.bytes;
-      return true;
-    case 2:
-      scalar = *reinterpret_cast<const uint16_t *>(buffer.bytes);
-      return true;
-    case 4:
-      scalar = *reinterpret_cast<const uint32_t *>(buffer.bytes);
+    DataExtractor data(buffer.bytes, buffer.length, buffer.byte_order, 1);
+    if (scalar.SetValueFromData(data, lldb::eEncodingUint,
+	  buffer.length).Success())
       return true;
-    case 8:
-      scalar = *reinterpret_cast<const uint64_t *>(buffer.bytes);
-      return true;
-    case 16:
-    case 32:
-    case 64:
-      if (buffer.length % sizeof(uint64_t) == 0) {
-        const auto length_in_bits = buffer.length * 8;
-        const auto length_in_uint64 = buffer.length / sizeof(uint64_t);
-        scalar =
-            llvm::APInt(length_in_bits,
-                        llvm::ArrayRef<uint64_t>(
-                            reinterpret_cast<const uint64_t *>(buffer.bytes),
-                            length_in_uint64));
-        return true;
-      }
-      break;
-    }
   } break;
   case eTypeUInt8:
   case eTypeUInt16:

diff  --git a/lldb/unittests/Utility/RegisterValueTest.cpp b/lldb/unittests/Utility/RegisterValueTest.cpp
index bbefb96519b1..9fce707ff402 100644
--- a/lldb/unittests/Utility/RegisterValueTest.cpp
+++ b/lldb/unittests/Utility/RegisterValueTest.cpp
@@ -41,18 +41,16 @@ TEST(RegisterValueTest, GetScalarValue) {
   EXPECT_EQ(Get(RV(47.5L)), Scalar(47.5L));
   EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc}, lldb::eByteOrderLittle)),
             Scalar(0xccddeeff));
-  //  EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc}, lldb::eByteOrderBig)),
-  //            Scalar(0xffeeddcc));
+  EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc}, lldb::eByteOrderBig)),
+            Scalar(0xffeeddcc));
   EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66,
                     0x55, 0x44, 0x33, 0x22, 0x11, 0x00},
                    lldb::eByteOrderLittle)),
             Scalar((APInt(128, 0x0011223344556677ull) << 64) |
                    APInt(128, 0x8899aabbccddeeff)));
-#if 0
   EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66,
                     0x55, 0x44, 0x33, 0x22, 0x11, 0x00},
                    lldb::eByteOrderBig)),
             Scalar((APInt(128, 0xffeeddccbbaa9988ull) << 64) |
                    APInt(128, 0x7766554433221100)));
-#endif
 }


        


More information about the lldb-commits mailing list