Skip to content

Commit

Permalink
fix: LUI and AUIPC immediates are already shifted
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Jan 8, 2025
1 parent 6116e23 commit d93f649
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,14 @@ impl<'host> Runtime<'host> {
match instruction {
// Load upper immediate
Instruction::Lui(rd, imm) => {
self.rw(rd, imm << 12);
// NOTE: the immediate is already shifted right by 12
self.rw(rd, imm);
}

// Add upper immediate to PC
Instruction::Auipc(rd, imm) => {
let value = self.state.pc.wrapping_add_signed(imm << 12);
// NOTE: the immediate is already shifted right by 12
let value = self.state.pc.wrapping_add_signed(imm);
self.rw(rd, value);
}

Expand Down Expand Up @@ -1044,7 +1046,7 @@ pub mod tests {

#[test]
fn test_lui() {
let instructions = vec![Instruction::Lui(Register::X15, 123)];
let instructions = vec![Instruction::Lui(Register::X15, 123 << 12)];
let program = Program::new(instructions, 0, 0);
let mut runtime = Runtime::new(program, None, AthenaCoreOpts::default(), None);
runtime.execute().unwrap();
Expand All @@ -1058,11 +1060,11 @@ pub mod tests {

let instructions = vec![
// Add small positive immediate
Instruction::Auipc(Register::X5, 1), // 1 << 12 = 4096
Instruction::Auipc(Register::X5, 1 << 12), // 1 << 12 = 4096
// Add larger positive immediate
Instruction::Auipc(Register::X6, 0x7FF), // 0x7FF << 12 = 0x7FF000
Instruction::Auipc(Register::X6, 0x7FF << 12), // 0x7FF << 12 = 0x7FF000
// Add maximum positive immediate
Instruction::Auipc(Register::X7, 0xFFFFF), // 0xFFFFF << 12 = 0xFFFFF000
Instruction::Auipc(Register::X7, 0xFFFFF << 12), // 0xFFFFF << 12 = 0xFFFFF000
];

let program = Program::new(instructions, 0, 0);
Expand Down

0 comments on commit d93f649

Please sign in to comment.