Skip to content

Commit e479656

Browse files
author
Bar Soloveychik
committed
fixed format and handle invalid register
1 parent db47d2d commit e479656

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

lldb/source/Plugins/Instruction/RISCV/EmulateInstructionRISCV.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ static std::optional<RegisterInfo>
117117
GPREncodingToRegisterInfo(EmulateInstructionRISCV &emulator,
118118
uint32_t reg_encode) {
119119
uint32_t lldb_reg = GPREncodingToLLDB(reg_encode);
120+
if (lldb_reg == LLDB_INVALID_REGNUM)
121+
return std::nullopt;
120122
return emulator.GetRegisterInfo(eRegisterKindLLDB, lldb_reg);
121123
}
122124

@@ -243,7 +245,7 @@ Load(EmulateInstructionRISCV &emulator, I inst, uint64_t (*extend)(E)) {
243245
if (!addr)
244246
return false;
245247

246-
// Set up context for the load operation, similar to ARM64
248+
// Set up context for the load operation, similar to ARM64.
247249
EmulateInstructionRISCV::Context context;
248250

249251
// Get register info for base register
@@ -253,7 +255,7 @@ Load(EmulateInstructionRISCV &emulator, I inst, uint64_t (*extend)(E)) {
253255
if (!reg_info_rs1)
254256
return false;
255257

256-
// Set context type based on whether this is a stack-based load
258+
// Set context type based on whether this is a stack-based load.
257259
if (inst.rs1.rs == RISCV_GPR_SP)
258260
context.type = EmulateInstruction::eContextPopRegisterOffStack;
259261
else
@@ -262,7 +264,7 @@ Load(EmulateInstructionRISCV &emulator, I inst, uint64_t (*extend)(E)) {
262264
// Set the context address information
263265
context.SetAddress(*addr);
264266

265-
// Read from memory with context and write to register
267+
// Read from memory with context and write to register.
266268
bool success = false;
267269
uint64_t value =
268270
emulator.ReadMemoryUnsigned(context, *addr, sizeof(T), 0, &success);
@@ -279,10 +281,10 @@ Store(EmulateInstructionRISCV &emulator, I inst) {
279281
if (!addr)
280282
return false;
281283

282-
// Set up context for the store operation, similar to ARM64
284+
// Set up context for the store operation, similar to ARM64.
283285
EmulateInstructionRISCV::Context context;
284286

285-
// Get register info for source and base registers
287+
// Get register info for source and base registers.
286288
std::optional<RegisterInfo> reg_info_rs1 =
287289
GPREncodingToRegisterInfo(emulator, inst.rs1.rs);
288290
std::optional<RegisterInfo> reg_info_rs2 =
@@ -291,14 +293,14 @@ Store(EmulateInstructionRISCV &emulator, I inst) {
291293
if (!reg_info_rs1 || !reg_info_rs2)
292294
return false;
293295

294-
// Set context type based on whether this is a stack-based store
295-
if (inst.rs1.rs == RISCV_GPR_SP) // x2 is the stack pointer in RISC-V
296+
// Set context type based on whether this is a stack-based store.
297+
if (inst.rs1.rs == RISCV_GPR_SP)
296298
context.type = EmulateInstruction::eContextPushRegisterOnStack;
297299
else
298300
context.type = EmulateInstruction::eContextRegisterStore;
299301

300302
// Set the context to show which register is being stored to which base
301-
// register + offset
303+
// register + offset.
302304
context.SetRegisterToRegisterPlusOffset(*reg_info_rs2, *reg_info_rs1,
303305
SignExt(inst.imm));
304306

@@ -803,7 +805,7 @@ class Executor {
803805
inst.rs1.ReadI64(m_emu),
804806
[&](int64_t rs1) {
805807
int64_t result = rs1 + int64_t(SignExt(inst.imm));
806-
// Check if this is a stack pointer adjustment
808+
// Check if this is a stack pointer adjustment.
807809
if (inst.rd.rd == RISCV_GPR_SP &&
808810
inst.rs1.rs == RISCV_GPR_SP) {
809811
EmulateInstruction::Context context;
@@ -816,8 +818,8 @@ class Executor {
816818
return m_emu.WriteRegister(context, eRegisterKindLLDB,
817819
sp_lldb_reg, registerValue);
818820
}
819-
// Check if this is setting up the frame pointer
820-
// addi fp, sp, imm -> fp = sp + imm (frame pointer setup)
821+
// Check if this is setting up the frame pointer.
822+
// addi fp, sp, imm -> fp = sp + imm (frame pointer setup).
821823
if (inst.rd.rd == RISCV_GPR_FP &&
822824
inst.rs1.rs == RISCV_GPR_SP) {
823825
EmulateInstruction::Context context;
@@ -834,7 +836,7 @@ class Executor {
834836
return m_emu.WriteRegister(context, eRegisterKindLLDB,
835837
fp_lldb_reg, registerValue);
836838
}
837-
// Regular ADDI instruction
839+
// Regular ADDI instruction.
838840
return inst.rd.Write(m_emu, result);
839841
})
840842
.value_or(false);
@@ -1843,34 +1845,34 @@ EmulateInstructionRISCV::GetRegisterInfo(RegisterKind reg_kind,
18431845
bool EmulateInstructionRISCV::SetInstruction(const Opcode &opcode,
18441846
const Address &inst_addr,
18451847
Target *target) {
1846-
// Call the base class implementation
1848+
// Call the base class implementation.
18471849
if (!EmulateInstruction::SetInstruction(opcode, inst_addr, target))
18481850
return false;
18491851

1850-
// Extract instruction data from the opcode
1852+
// Extract instruction data from the opcode.
18511853
uint32_t inst_data = 0;
18521854
const void *opcode_data = m_opcode.GetOpcodeBytes();
18531855
if (!opcode_data)
18541856
return false;
18551857

18561858
if (m_opcode.GetByteSize() == 2) {
1857-
// 16-bit compressed instruction
1859+
// 16-bit compressed instruction.
18581860
const uint16_t *data = static_cast<const uint16_t *>(opcode_data);
18591861
inst_data = *data;
18601862
} else if (m_opcode.GetByteSize() == 4) {
1861-
// 32-bit instruction
1863+
// 32-bit instruction.
18621864
const uint32_t *data = static_cast<const uint32_t *>(opcode_data);
18631865
inst_data = *data;
18641866
} else {
18651867
return false;
18661868
}
18671869

1868-
// Decode the instruction
1870+
// Decode the instruction.
18691871
auto decoded_inst = Decode(inst_data);
18701872
if (!decoded_inst)
18711873
return false;
18721874

1873-
// Store the decoded result
1875+
// Store the decoded result.
18741876
m_decoded = *decoded_inst;
18751877
return true;
18761878
}

lldb/unittests/Instruction/RISCV/TestRiscvInstEmulation.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void TestRiscvInstEmulation::TearDownTestCase() {
4646

4747
TEST_F(TestRiscvInstEmulation, TestSimpleRiscvFunction) {
4848
ArchSpec arch("riscv64-unknown-linux-gnu");
49-
// Enable compressed instruction support (RVC extension)
49+
// Enable compressed instruction support (RVC extension).
5050
arch.SetFlags(ArchSpec::eRISCV_rvc);
5151
std::unique_ptr<UnwindAssemblyInstEmulation> engine(
5252
static_cast<UnwindAssemblyInstEmulation *>(
@@ -114,7 +114,7 @@ TEST_F(TestRiscvInstEmulation, TestSimpleRiscvFunction) {
114114
EXPECT_TRUE(engine->GetNonCallSiteUnwindPlanFromAssembly(
115115
sample_range, data, sizeof(data), unwind_plan));
116116

117-
// CFA=sp+0 => fp=<same> ra=<same>
117+
// CFA=sp+0 => fp=<same> ra=<same>.
118118
row = unwind_plan.GetRowForFunctionOffset(0);
119119
EXPECT_EQ(0, row->GetOffset());
120120
EXPECT_TRUE(row->GetCFAValue().GetRegisterNumber() == gpr_sp_riscv);
@@ -127,7 +127,7 @@ TEST_F(TestRiscvInstEmulation, TestSimpleRiscvFunction) {
127127
EXPECT_TRUE(row->GetRegisterInfo(gpr_ra_riscv, regloc));
128128
EXPECT_TRUE(regloc.IsSame());
129129

130-
// CFA=sp+16 => fp=<same> ra=<same>
130+
// CFA=sp+16 => fp=<same> ra=<same>.
131131
row = unwind_plan.GetRowForFunctionOffset(2);
132132
EXPECT_EQ(2, row->GetOffset());
133133
EXPECT_TRUE(row->GetCFAValue().GetRegisterNumber() == gpr_sp_riscv);
@@ -140,7 +140,7 @@ TEST_F(TestRiscvInstEmulation, TestSimpleRiscvFunction) {
140140
EXPECT_TRUE(row->GetRegisterInfo(gpr_ra_riscv, regloc));
141141
EXPECT_TRUE(regloc.IsSame());
142142

143-
// CFA=sp+16 => fp=<same> ra=[CFA-8]
143+
// CFA=sp+16 => fp=<same> ra=[CFA-8].
144144
row = unwind_plan.GetRowForFunctionOffset(4);
145145
EXPECT_EQ(4, row->GetOffset());
146146
EXPECT_TRUE(row->GetCFAValue().GetRegisterNumber() == gpr_sp_riscv);
@@ -185,4 +185,4 @@ TEST_F(TestRiscvInstEmulation, TestSimpleRiscvFunction) {
185185
EXPECT_TRUE(row->GetRegisterInfo(gpr_ra_riscv, regloc));
186186
EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
187187
EXPECT_EQ(-8, regloc.GetOffset());
188-
}
188+
}

0 commit comments

Comments
 (0)