[lldb] Improve RegisterValue::GetScalarValue
authorPavel Labath <pavel@labath.sk>
Tue, 3 Nov 2020 15:15:15 +0000 (16:15 +0100)
committerPavel Labath <pavel@labath.sk>
Wed, 4 Nov 2020 13:53:06 +0000 (14:53 +0100)
Using Scalar::SetValueFromData, we make the code simpler, handle
big-endian systems properly, _and_ avoid type aliasing issues.

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

index 7f545c2..7848f78 100644 (file)
@@ -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:
index bbefb96..9fce707 100644 (file)
@@ -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
 }