Index: lldb/unittests/Process/Utility/CMakeLists.txt =================================================================== --- lldb/unittests/Process/Utility/CMakeLists.txt +++ lldb/unittests/Process/Utility/CMakeLists.txt @@ -15,9 +15,10 @@ ${NETBSD_SOURCES}) add_lldb_unittest(ProcessUtilityTests - RegisterContextTest.cpp + DynamicRegisterInfoTest.cpp LinuxProcMapsTest.cpp MemoryTagManagerAArch64MTETest.cpp + RegisterContextTest.cpp ${PLATFORM_SOURCES} LINK_LIBS Index: lldb/unittests/Process/Utility/DynamicRegisterInfoTest.cpp =================================================================== --- /dev/null +++ lldb/unittests/Process/Utility/DynamicRegisterInfoTest.cpp @@ -0,0 +1,98 @@ +//===-- DynamicRegisterInfoTest.cpp ---------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "Plugins/Process/Utility/DynamicRegisterInfo.h" + +#include "lldb/Utility/ArchSpec.h" + +using namespace lldb_private; + +static uint32_t AddRegister(DynamicRegisterInfo &info, const char *name, + uint32_t byte_size, + std::vector value_regs = {}, + std::vector invalidate_regs = {}) { + ConstString group{"group"}; + static uint32_t regnum = 0; + + struct RegisterInfo new_reg { + name, nullptr, byte_size, LLDB_INVALID_INDEX32, lldb::eEncodingUint, + lldb::eFormatUnsigned, + {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, regnum, + regnum}, + nullptr, nullptr, nullptr, 0 + }; + + if (!value_regs.empty()) { + value_regs.push_back(LLDB_INVALID_REGNUM); + new_reg.value_regs = value_regs.data(); + } + if (!invalidate_regs.empty()) { + invalidate_regs.push_back(LLDB_INVALID_REGNUM); + new_reg.invalidate_regs = invalidate_regs.data(); + } + + info.AddRegister(new_reg, group); + return regnum++; +} + +TEST(DynamicRegisterInfoTest, finalize_regs) { + DynamicRegisterInfo info; + + // Add regular registers + uint32_t b1 = AddRegister(info, "b1", 8); + uint32_t b2 = AddRegister(info, "b2", 8); + + // Add a few sub-registers + AddRegister(info, "s1", 4, {b1}); + AddRegister(info, "s2", 4, {b2}); + + // Add a register with invalidate_regs + uint32_t i1 = AddRegister(info, "i1", 8, {}, {b1}); + + // Add a register with indirect invalidate regs to be expanded + // TODO: why is it done conditionally to value_regs? + AddRegister(info, "i2", 4, {b2}, {i1}); + + info.Finalize(lldb_private::ArchSpec()); + + const RegisterInfo *added_reg = info.GetRegisterInfoAtIndex(0); + ASSERT_NE(added_reg, nullptr); + EXPECT_EQ(added_reg->byte_offset, 0U); + + added_reg = info.GetRegisterInfoAtIndex(1); + ASSERT_NE(added_reg, nullptr); + EXPECT_EQ(added_reg->byte_offset, 8U); + + added_reg = info.GetRegisterInfoAtIndex(2); + ASSERT_NE(added_reg, nullptr); + EXPECT_EQ(added_reg->byte_offset, 0U); + EXPECT_EQ(added_reg->value_regs[0], b1); + EXPECT_EQ(added_reg->value_regs[1], LLDB_INVALID_REGNUM); + + added_reg = info.GetRegisterInfoAtIndex(3); + ASSERT_NE(added_reg, nullptr); + EXPECT_EQ(added_reg->byte_offset, 8U); + EXPECT_EQ(added_reg->value_regs[0], b2); + EXPECT_EQ(added_reg->value_regs[1], LLDB_INVALID_REGNUM); + + added_reg = info.GetRegisterInfoAtIndex(4); + ASSERT_NE(added_reg, nullptr); + EXPECT_EQ(added_reg->byte_offset, 16U); + EXPECT_EQ(added_reg->invalidate_regs[0], b1); + EXPECT_EQ(added_reg->invalidate_regs[1], LLDB_INVALID_REGNUM); + + added_reg = info.GetRegisterInfoAtIndex(5); + ASSERT_NE(added_reg, nullptr); + EXPECT_EQ(added_reg->byte_offset, 8U); + EXPECT_EQ(added_reg->invalidate_regs[0], b1); + EXPECT_EQ(added_reg->invalidate_regs[1], i1); + EXPECT_EQ(added_reg->invalidate_regs[2], LLDB_INVALID_REGNUM); +}