[Lldb-commits] [lldb] 16e17ca - [lldb] Refactor Scalar::TruncOrExtendTo

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Jun 24 07:08:12 PDT 2020


Author: Pavel Labath
Date: 2020-06-24T15:58:33+02:00
New Revision: 16e17ca16a89daa099fd2726d92f08a49870e5a9

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

LOG: [lldb] Refactor Scalar::TruncOrExtendTo

The "type" argument to the function is mostly useless -- the only
interesting aspect of it is signedness. Pass signedness directly and
compute the value of bits and signedness fields -- that's exactly
what the single caller of this function does.

Added: 
    

Modified: 
    lldb/include/lldb/Utility/Scalar.h
    lldb/source/Expression/DWARFExpression.cpp
    lldb/source/Utility/Scalar.cpp
    lldb/unittests/Expression/DWARFExpressionTest.cpp
    lldb/unittests/Utility/ScalarTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/Scalar.h b/lldb/include/lldb/Utility/Scalar.h
index 1865a34775ee..203ba565b223 100644
--- a/lldb/include/lldb/Utility/Scalar.h
+++ b/lldb/include/lldb/Utility/Scalar.h
@@ -130,8 +130,8 @@ class Scalar {
     return (m_type >= e_sint) && (m_type <= e_long_double);
   }
 
-  /// Convert integer to \p type, limited to \p bits size.
-  void TruncOrExtendTo(Scalar::Type type, uint16_t bits);
+  /// Convert to an integer with \p bits and the given signedness.
+  void TruncOrExtendTo(uint16_t bits, bool sign);
 
   bool Promote(Scalar::Type type);
 

diff  --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp
index 3bcadbe9ed4a..6050c1922564 100644
--- a/lldb/source/Expression/DWARFExpression.cpp
+++ b/lldb/source/Expression/DWARFExpression.cpp
@@ -2346,8 +2346,8 @@ bool DWARFExpression::Evaluate(
         return false;
       }
       const uint64_t die_offset = opcodes.GetULEB128(&offset);
-      Scalar::Type type = Scalar::e_void;
       uint64_t bit_size;
+      bool sign;
       if (die_offset == 0) {
         // The generic type has the size of an address on the target
         // machine and an unspecified signedness. Scalar has no
@@ -2357,13 +2357,13 @@ bool DWARFExpression::Evaluate(
             error_ptr->SetErrorString("No module");
           return false;
         }
+        sign = false;
         bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
         if (!bit_size) {
           if (error_ptr)
             error_ptr->SetErrorString("unspecified architecture");
           return false;
         }
-        type = Scalar::GetBestTypeForBitSize(bit_size, false);
       } else {
         // Retrieve the type DIE that the value is being converted to.
         // FIXME: the constness has annoying ripple effects.
@@ -2386,11 +2386,11 @@ bool DWARFExpression::Evaluate(
         switch (encoding) {
         case DW_ATE_signed:
         case DW_ATE_signed_char:
-          type = Scalar::GetBestTypeForBitSize(bit_size, true);
+          sign = true;
           break;
         case DW_ATE_unsigned:
         case DW_ATE_unsigned_char:
-          type = Scalar::GetBestTypeForBitSize(bit_size, false);
+          sign = false;
           break;
         default:
           if (error_ptr)
@@ -2398,13 +2398,8 @@ bool DWARFExpression::Evaluate(
           return false;
         }
       }
-      if (type == Scalar::e_void) {
-        if (error_ptr)
-          error_ptr->SetErrorString("Unsupported pointer size");
-        return false;
-      }
       Scalar &top = stack.back().ResolveValue(exe_ctx);
-      top.TruncOrExtendTo(type, bit_size);
+      top.TruncOrExtendTo(bit_size, sign);
       break;
     }
 

diff  --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index 759e2a7b56fc..748dd5541908 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -327,29 +327,9 @@ Scalar::Type Scalar::GetBestTypeForBitSize(size_t bit_size, bool sign) {
   return Scalar::e_void;
 }
 
-void Scalar::TruncOrExtendTo(Scalar::Type type, uint16_t bits) {
-  switch (type) {
-  case e_sint:
-  case e_slong:
-  case e_slonglong:
-  case e_sint128:
-  case e_sint256:
-  case e_sint512:
-    m_integer = m_integer.sextOrTrunc(bits);
-    break;
-  case e_uint:
-  case e_ulong:
-  case e_ulonglong:
-  case e_uint128:
-  case e_uint256:
-  case e_uint512:
-    m_integer = m_integer.zextOrTrunc(bits);
-    break;
-  default:
-    llvm_unreachable("Promoting a Scalar to a specific number of bits is only "
-                     "supported for integer types.");
-  }
-  m_type = type;
+void Scalar::TruncOrExtendTo(uint16_t bits, bool sign) {
+  m_integer = sign ? m_integer.sextOrTrunc(bits) : m_integer.zextOrTrunc(bits);
+  m_type = GetBestTypeForBitSize(bits, sign);
 }
 
 bool Scalar::Promote(Scalar::Type type) {

diff  --git a/lldb/unittests/Expression/DWARFExpressionTest.cpp b/lldb/unittests/Expression/DWARFExpressionTest.cpp
index 590ac2fa8338..572c585c6bfd 100644
--- a/lldb/unittests/Expression/DWARFExpressionTest.cpp
+++ b/lldb/unittests/Expression/DWARFExpressionTest.cpp
@@ -61,35 +61,8 @@ class DWARFExpressionTester : public YAMLModuleTester {
 
 /// Unfortunately Scalar's operator==() is really picky.
 static Scalar GetScalar(unsigned bits, uint64_t value, bool sign) {
-  Scalar scalar;
-  auto type = Scalar::GetBestTypeForBitSize(bits, sign);
-  switch (type) {
-  case Scalar::e_sint:
-    scalar = Scalar((int)value);
-    break;
-  case Scalar::e_slong:
-    scalar = Scalar((long)value);
-    break;
-  case Scalar::e_slonglong:
-    scalar = Scalar((long long)value);
-    break;
-  case Scalar::e_uint:
-    scalar = Scalar((unsigned int)value);
-    break;
-  case Scalar::e_ulong:
-    scalar = Scalar((unsigned long)value);
-    break;
-  case Scalar::e_ulonglong:
-    scalar = Scalar((unsigned long long)value);
-    break;
-  default:
-    llvm_unreachable("not implemented");
-  }
-  scalar.TruncOrExtendTo(type, bits);
-  if (sign)
-    scalar.MakeSigned();
-  else
-    scalar.MakeUnsigned();
+  Scalar scalar(value);
+  scalar.TruncOrExtendTo(bits, sign);
   return scalar;
 }
 

diff  --git a/lldb/unittests/Utility/ScalarTest.cpp b/lldb/unittests/Utility/ScalarTest.cpp
index baf1de98c121..be23320a045a 100644
--- a/lldb/unittests/Utility/ScalarTest.cpp
+++ b/lldb/unittests/Utility/ScalarTest.cpp
@@ -313,3 +313,15 @@ TEST(ScalarTest, Scalar_512) {
   EXPECT_EQ(S.GetType(), Scalar::e_sint512);
   EXPECT_EQ(S.GetByteSize(), 64U);
 }
+
+TEST(ScalarTest, TruncOrExtendTo) {
+  Scalar S(0xffff);
+  S.TruncOrExtendTo(12, true);
+  EXPECT_EQ(S.ULong(), 0xfffu);
+  S.TruncOrExtendTo(20, true);
+  EXPECT_EQ(S.ULong(), 0xfffffu);
+  S.TruncOrExtendTo(24, false);
+  EXPECT_EQ(S.ULong(), 0x0fffffu);
+  S.TruncOrExtendTo(16, false);
+  EXPECT_EQ(S.ULong(), 0xffffu);
+}


        


More information about the lldb-commits mailing list