[Lldb-commits] [lldb] 7fadd70 - [lldb/Utility] Simplify Scalar::SetValueFromData
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Mon Jul 20 07:06:20 PDT 2020
Author: Pavel Labath
Date: 2020-07-20T15:56:56+02:00
New Revision: 7fadd7006932adfe65f445aad1e60fe8d7b81eec
URL: https://github.com/llvm/llvm-project/commit/7fadd7006932adfe65f445aad1e60fe8d7b81eec
DIFF: https://github.com/llvm/llvm-project/commit/7fadd7006932adfe65f445aad1e60fe8d7b81eec.diff
LOG: [lldb/Utility] Simplify Scalar::SetValueFromData
The function was fairly complicated and didn't support new bigger
integer sizes. Use llvm function for loading an APInt from memory to
write a unified implementation for all sizes.
Added:
Modified:
lldb/include/lldb/Utility/Scalar.h
lldb/source/Utility/Scalar.cpp
lldb/unittests/Utility/ScalarTest.cpp
Removed:
################################################################################
diff --git a/lldb/include/lldb/Utility/Scalar.h b/lldb/include/lldb/Utility/Scalar.h
index f215fa71c84c..0bb5c7d1b859 100644
--- a/lldb/include/lldb/Utility/Scalar.h
+++ b/lldb/include/lldb/Utility/Scalar.h
@@ -217,7 +217,7 @@ class Scalar {
Status SetValueFromCString(const char *s, lldb::Encoding encoding,
size_t byte_size);
- Status SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
+ Status SetValueFromData(const DataExtractor &data, lldb::Encoding encoding,
size_t byte_size);
static bool UIntValueIsValidForSize(uint64_t uval64, size_t total_byte_size) {
diff --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index 6c48bbde532f..e2c1f37fe529 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -1068,12 +1068,9 @@ Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
return error;
}
-Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
- size_t byte_size) {
+Status Scalar::SetValueFromData(const DataExtractor &data,
+ lldb::Encoding encoding, size_t byte_size) {
Status error;
-
- type128 int128;
- type256 int256;
switch (encoding) {
case lldb::eEncodingInvalid:
error.SetErrorString("invalid encoding");
@@ -1081,100 +1078,26 @@ Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
case lldb::eEncodingVector:
error.SetErrorString("vector encoding unsupported");
break;
- case lldb::eEncodingUint: {
- lldb::offset_t offset = 0;
-
- switch (byte_size) {
- case 1:
- operator=(data.GetU8(&offset));
- break;
- case 2:
- operator=(data.GetU16(&offset));
- break;
- case 4:
- operator=(data.GetU32(&offset));
- break;
- case 8:
- operator=(data.GetU64(&offset));
- break;
- case 16:
- if (data.GetByteOrder() == eByteOrderBig) {
- int128.x[1] = data.GetU64(&offset);
- int128.x[0] = data.GetU64(&offset);
- } else {
- int128.x[0] = data.GetU64(&offset);
- int128.x[1] = data.GetU64(&offset);
- }
- operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
- break;
- case 32:
- if (data.GetByteOrder() == eByteOrderBig) {
- int256.x[3] = data.GetU64(&offset);
- int256.x[2] = data.GetU64(&offset);
- int256.x[1] = data.GetU64(&offset);
- int256.x[0] = data.GetU64(&offset);
- } else {
- int256.x[0] = data.GetU64(&offset);
- int256.x[1] = data.GetU64(&offset);
- int256.x[2] = data.GetU64(&offset);
- int256.x[3] = data.GetU64(&offset);
- }
- operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
- break;
- default:
- error.SetErrorStringWithFormat(
- "unsupported unsigned integer byte size: %" PRIu64 "",
- static_cast<uint64_t>(byte_size));
- break;
- }
- } break;
+ case lldb::eEncodingUint:
case lldb::eEncodingSint: {
- lldb::offset_t offset = 0;
-
- switch (byte_size) {
- case 1:
- operator=(static_cast<int8_t>(data.GetU8(&offset)));
- break;
- case 2:
- operator=(static_cast<int16_t>(data.GetU16(&offset)));
- break;
- case 4:
- operator=(static_cast<int32_t>(data.GetU32(&offset)));
- break;
- case 8:
- operator=(static_cast<int64_t>(data.GetU64(&offset)));
- break;
- case 16:
- if (data.GetByteOrder() == eByteOrderBig) {
- int128.x[1] = data.GetU64(&offset);
- int128.x[0] = data.GetU64(&offset);
- } else {
- int128.x[0] = data.GetU64(&offset);
- int128.x[1] = data.GetU64(&offset);
- }
- operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
- break;
- case 32:
- if (data.GetByteOrder() == eByteOrderBig) {
- int256.x[3] = data.GetU64(&offset);
- int256.x[2] = data.GetU64(&offset);
- int256.x[1] = data.GetU64(&offset);
- int256.x[0] = data.GetU64(&offset);
- } else {
- int256.x[0] = data.GetU64(&offset);
- int256.x[1] = data.GetU64(&offset);
- int256.x[2] = data.GetU64(&offset);
- int256.x[3] = data.GetU64(&offset);
- }
- operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
- break;
- default:
- error.SetErrorStringWithFormat(
- "unsupported signed integer byte size: %" PRIu64 "",
- static_cast<uint64_t>(byte_size));
- break;
+ if (data.GetByteSize() < byte_size)
+ return Status("insufficient data");
+ Type type = GetBestTypeForBitSize(byte_size*8, encoding == lldb::eEncodingSint);
+ if (type == e_void) {
+ return Status("unsupported integer byte size: %" PRIu64 "",
+ static_cast<uint64_t>(byte_size));
}
- } break;
+ m_type = type;
+ if (data.GetByteOrder() == endian::InlHostByteOrder()) {
+ m_integer = APInt::getNullValue(8 * byte_size);
+ llvm::LoadIntFromMemory(m_integer, data.GetDataStart(), byte_size);
+ } else {
+ std::vector<uint8_t> buffer(byte_size);
+ std::copy_n(data.GetDataStart(), byte_size, buffer.rbegin());
+ llvm::LoadIntFromMemory(m_integer, buffer.data(), byte_size);
+ }
+ break;
+ }
case lldb::eEncodingIEEE754: {
lldb::offset_t offset = 0;
diff --git a/lldb/unittests/Utility/ScalarTest.cpp b/lldb/unittests/Utility/ScalarTest.cpp
index dd4683145b96..f6bc6a404c15 100644
--- a/lldb/unittests/Utility/ScalarTest.cpp
+++ b/lldb/unittests/Utility/ScalarTest.cpp
@@ -135,12 +135,8 @@ TEST(ScalarTest, GetBytes) {
Scalar f_scalar;
DataExtractor e_data(e, sizeof(e), endian::InlHostByteOrder(),
sizeof(void *));
- Status e_error =
- e_scalar.SetValueFromData(e_data, lldb::eEncodingUint, sizeof(e));
DataExtractor f_data(f, sizeof(f), endian::InlHostByteOrder(),
sizeof(void *));
- Status f_error =
- f_scalar.SetValueFromData(f_data, lldb::eEncodingUint, sizeof(f));
a_scalar.GetBytes(Storage);
ASSERT_EQ(0, memcmp(&a, Storage, sizeof(a)));
b_scalar.GetBytes(Storage);
@@ -149,14 +145,39 @@ TEST(ScalarTest, GetBytes) {
ASSERT_EQ(0, memcmp(&c, Storage, sizeof(c)));
d_scalar.GetBytes(Storage);
ASSERT_EQ(0, memcmp(&d, Storage, sizeof(d)));
- ASSERT_EQ(0, e_error.Fail());
+ ASSERT_THAT_ERROR(
+ e_scalar.SetValueFromData(e_data, lldb::eEncodingUint, sizeof(e))
+ .ToError(),
+ llvm::Succeeded());
e_scalar.GetBytes(Storage);
ASSERT_EQ(0, memcmp(e, Storage, sizeof(e)));
- ASSERT_EQ(0, f_error.Fail());
+ ASSERT_THAT_ERROR(
+ f_scalar.SetValueFromData(f_data, lldb::eEncodingUint, sizeof(f))
+ .ToError(),
+ llvm::Succeeded());
f_scalar.GetBytes(Storage);
ASSERT_EQ(0, memcmp(f, Storage, sizeof(f)));
}
+TEST(ScalarTest, SetValueFromData) {
+ uint8_t a[] = {1, 2, 3, 4};
+ Scalar s;
+ ASSERT_THAT_ERROR(
+ s.SetValueFromData(
+ DataExtractor(a, sizeof(a), lldb::eByteOrderLittle, sizeof(void *)),
+ lldb::eEncodingSint, sizeof(a))
+ .ToError(),
+ llvm::Succeeded());
+ EXPECT_EQ(0x04030201, s);
+ ASSERT_THAT_ERROR(
+ s.SetValueFromData(
+ DataExtractor(a, sizeof(a), lldb::eByteOrderBig, sizeof(void *)),
+ lldb::eEncodingSint, sizeof(a))
+ .ToError(),
+ llvm::Succeeded());
+ EXPECT_EQ(0x01020304, s);
+}
+
TEST(ScalarTest, CastOperations) {
long long a = 0xf1f2f3f4f5f6f7f8LL;
Scalar a_scalar(a);
More information about the lldb-commits
mailing list