[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.
This commit is contained in:
Pavel Labath 2020-06-24 15:26:42 +02:00
parent c3b3b999ec
commit 16e17ca16a
5 changed files with 24 additions and 64 deletions

View File

@ -130,8 +130,8 @@ public:
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);

View File

@ -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;
}

View File

@ -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) {

View File

@ -61,35 +61,8 @@ public:
/// 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;
}

View File

@ -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);
}