Skip to content

Commit

Permalink
[refactor] allow casting possible wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Alignof committed Aug 11, 2024
1 parent 659493d commit 326a1a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/decode/inst_16/c_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ pub fn parse_rs2(inst: u16, opkind: &COpcode) -> Option<usize> {
}
}

#[allow(clippy::cast_possible_wrap)]
#[allow(clippy::similar_names)]
pub fn parse_imm(inst: u16, opkind: &COpcode) -> Option<i32> {
let q0_uimm = || (inst.slice(12, 10).set(&[5, 4, 3]) | inst.slice(6, 5).set(&[2, 6])) as i32;
let q0_uimm_64 = || (inst.slice(12, 10).set(&[5, 4, 3]) | inst.slice(6, 5).set(&[7, 6])) as i32;
let q0_nzuimm = || inst.slice(12, 5).set(&[5, 4, 9, 8, 7, 6, 2, 3]) as i32;
let q1_nzuimm =
|| (inst.slice(6, 2).set(&[4, 3, 2, 1, 0]) | inst.slice(12, 12).set(&[5])) as i32;
let q0_uimm = || (inst.slice(12, 10).set(&[5, 4, 3]) | inst.slice(6, 5).set(&[2, 6]));
let q0_uimm_64 = || (inst.slice(12, 10).set(&[5, 4, 3]) | inst.slice(6, 5).set(&[7, 6]));
let q0_nzuimm = || inst.slice(12, 5).set(&[5, 4, 9, 8, 7, 6, 2, 3]);
let q1_nzuimm = || (inst.slice(6, 2).set(&[4, 3, 2, 1, 0]) | inst.slice(12, 12).set(&[5]));
let q1_nzimm = || {
let imm16 = (inst.slice(6, 2).set(&[4, 3, 2, 1, 0]) | inst.slice(12, 12).set(&[5])) as i32;
inst.to_signed_nbit(imm16, 6)
Expand Down Expand Up @@ -234,13 +234,13 @@ pub fn parse_imm(inst: u16, opkind: &COpcode) -> Option<i32> {

match opkind {
// Quadrant0
COpcode::ADDI4SPN => Some(q0_nzuimm()),
COpcode::LW | COpcode::SW => Some(q0_uimm()),
COpcode::LD | COpcode::SD => Some(q0_uimm_64()),
COpcode::ADDI4SPN => Some(q0_nzuimm() as i32),
COpcode::LW | COpcode::SW => Some(q0_uimm() as i32),
COpcode::LD | COpcode::SD => Some(q0_uimm_64() as i32),
// Quadrant1
COpcode::ADDIW | COpcode::LI | COpcode::ANDI => Some(q1_imm()),
COpcode::NOP | COpcode::ADDI => Some(q1_nzimm()),
COpcode::SRLI | COpcode::SRAI => Some(q1_nzuimm()),
COpcode::SRLI | COpcode::SRAI => Some(q1_nzuimm() as i32),
COpcode::JAL | COpcode::J => Some(q1_j_imm()),
COpcode::BEQZ | COpcode::BNEZ => Some(q1_b_imm()),
COpcode::LUI => Some(q1_lui_imm()),
Expand Down
2 changes: 1 addition & 1 deletion src/decode/inst_32/a_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ pub fn parse_rs2(inst: u32, opkind: &AOpcode) -> Option<usize> {
}
}

#[allow(clippy::unnecessary_wraps)]
#[allow(non_snake_case)]
#[allow(clippy::unnecessary_wraps, clippy::cast_possible_wrap)]
pub fn parse_imm(inst: u32, opkind: &AOpcode) -> Option<i32> {
let aq_and_rl = || inst.slice(26, 25) as i32;

Expand Down
11 changes: 6 additions & 5 deletions src/decode/inst_32/base_i.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ pub fn parse_rs2(inst: u32, opkind: &BaseIOpcode) -> Option<usize> {
}
}

#[allow(clippy::cast_possible_wrap)]
#[allow(non_snake_case)]
pub fn parse_imm(inst: u32, opkind: &BaseIOpcode, isa: Isa) -> Option<i32> {
let U_type = || (inst.slice(31, 12) << 12) as i32;
Expand All @@ -293,8 +294,8 @@ pub fn parse_imm(inst: u32, opkind: &BaseIOpcode, isa: Isa) -> Option<i32> {
]) as i32;
inst.to_signed_nbit(imm32, 21)
};
let shamt5 = || inst.slice(24, 20) as i32;
let shamt6 = || inst.slice(25, 20) as i32;
let shamt5 = || inst.slice(24, 20); // shamt = SHift AMounT
let shamt6 = || inst.slice(25, 20);

match opkind {
// u-type
Expand Down Expand Up @@ -327,10 +328,10 @@ pub fn parse_imm(inst: u32, opkind: &BaseIOpcode, isa: Isa) -> Option<i32> {
// s-type
BaseIOpcode::SD | BaseIOpcode::SB | BaseIOpcode::SH | BaseIOpcode::SW => Some(S_type()),
BaseIOpcode::SRAI | BaseIOpcode::SLLI | BaseIOpcode::SRLI => match isa {
Isa::Rv32 => Some(shamt5()), // shamt
Isa::Rv64 => Some(shamt6()),
Isa::Rv32 => Some(shamt5() as i32), // shamt
Isa::Rv64 => Some(shamt6() as i32),
},
BaseIOpcode::SLLIW | BaseIOpcode::SRLIW | BaseIOpcode::SRAIW => Some(shamt5()),
BaseIOpcode::SLLIW | BaseIOpcode::SRLIW | BaseIOpcode::SRAIW => Some(shamt5() as i32),
_ => None,
}
}
5 changes: 3 additions & 2 deletions src/decode/inst_32/zicsr_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ pub fn parse_rs2(inst: u32, opkind: &ZicsrOpcode) -> Option<usize> {
}
}

#[allow(clippy::cast_possible_wrap)]
pub fn parse_imm(inst: u32, opkind: &ZicsrOpcode) -> Option<i32> {
let uimm: i32 = inst.slice(19, 15) as i32;
let uimm: u32 = inst.slice(19, 15);
match opkind {
ZicsrOpcode::CSRRWI | ZicsrOpcode::CSRRSI | ZicsrOpcode::CSRRCI => Some(uimm),
ZicsrOpcode::CSRRWI | ZicsrOpcode::CSRRSI | ZicsrOpcode::CSRRCI => Some(uimm as i32),
_ => None,
}
}

0 comments on commit 326a1a6

Please sign in to comment.