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");
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;
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);
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);