Skip to content

Commit

Permalink
Add decompression for C.NOP instruction.
Browse files Browse the repository at this point in the history
  • Loading branch information
user-0xcafe committed Feb 28, 2023
1 parent 44f2450 commit 77c25b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ mod tests {
#[test]
fn test_quadrant1() {
// C.NOP
assert_eq!(decode(0x0001).unwrap(), Addi(IType(0x00000013))); // addi a0, a0, 0

// C.ADDI
assert_eq!(decode(0x17e1).unwrap(), Addi(IType(0xff878793))); // addi a5, a5, -8
Expand Down
32 changes: 19 additions & 13 deletions src/decompress/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(super) fn decompress_addi4spn(i: u16) -> DecompressionResult {
let imm = get_imm(i, InstrFormat::Ciw).inv_permute(&[5, 4, 9, 8, 7, 6, 2, 3]);
let rd = 8 + ((i >> 2) & 0b111);

assert!(imm != 0, "imm == 0 is reserved");
assert!(imm != 0, "imm == 0 is reserved!");

Ok(build_itype(CiInstr::Addi, rd, Register::Sp as u16, imm))
}
Expand Down Expand Up @@ -42,16 +42,22 @@ pub(super) fn decompress_addi(i: u16, instruction_type: CiInstr) -> Decompressio
let imm = sign_extend16(get_imm(i, InstrFormat::Ci), 6);
let dest = (i >> 7) & 0b1_1111;

assert!(dest != 0, "dest == 0 is reserved!");
if matches!(instruction_type, CiInstr::Addi) {
assert!(imm != 0, "imm == 0 is a HINT!");
if matches!(instruction_type, CiInstr::Addiw) {
assert!(dest != 0, "dest == 0 is reserved!");

return Ok(build_itype(CiInstr::Addiw, dest, dest, imm));
}

Ok(match instruction_type {
CiInstr::Addi => build_itype(CiInstr::Addi, dest, dest, imm),
CiInstr::Addiw => build_itype(CiInstr::Addiw, dest, dest, imm),
_ => unreachable!(),
})
if dest == 0 {
// The instruction is the pseudoinstruction nop.
assert!(imm == 0, "imm != 0 is a HINT!");

Ok(build_itype(CiInstr::Addi, dest, dest, imm))
} else {
assert!(imm != 0, "imm == 0 is a HINT!");

Ok(build_itype(CiInstr::Addi, dest, dest, imm))
}
}

pub(super) fn decompress_li(i: u16) -> DecompressionResult {
Expand Down Expand Up @@ -203,9 +209,9 @@ pub(super) fn decompress_store_sp(i: u16, instruction_type: CsInstr) -> Decompre
let rs1 = Register::Sp as u16;
let rs2 = (i >> 2) & 0b1_1111;

match instruction_type {
CsInstr::Sw => Ok(build_stype(CsInstr::Sw, rs1, rs2, imm.inv_permute(&[5, 4, 3, 2, 7, 6]))),
CsInstr::Sd => Ok(build_stype(CsInstr::Sd, rs1, rs2, imm.inv_permute(&[5, 4, 3, 8, 7, 6])))
}
Ok(match instruction_type {
CsInstr::Sw => build_stype(CsInstr::Sw, rs1, rs2, imm.inv_permute(&[5, 4, 3, 2, 7, 6])),
CsInstr::Sd => build_stype(CsInstr::Sd, rs1, rs2, imm.inv_permute(&[5, 4, 3, 8, 7, 6])),
})
}
// }}}

0 comments on commit 77c25b8

Please sign in to comment.