Skip to content

Commit

Permalink
perf(parser constant): change RISCVImmediate to i32
Browse files Browse the repository at this point in the history
  • Loading branch information
GenshinImpactStarts committed Apr 16, 2024
1 parent 261bc31 commit 015d84b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src-tauri/src/interface/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ where
data:\n",
)?;
for (i, d) in self.data.iter().enumerate() {
write!(f, "{:3} {}\n", i, d.to_string())?;
write!(f, "{:3} {}\n", i + 1, d.to_string())?;
}
f.write_str("text:\n")?;
for (i, t) in self.text.iter().enumerate() {
write!(f, "{:3} {}\n", i, t.to_string())?;
write!(f, "{:3} {}\n", i + 1, t.to_string())?;
}
Ok(())
}
Expand Down Expand Up @@ -102,7 +102,7 @@ where
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParserResultText::Text(inst) => {
write!(f, "{:3}: {:?}", inst.line, inst.op)?;
write!(f, "{:3}: {:?}", inst.line + 1, inst.op)?;
for opd in &inst.opd {
write!(f, " {:?}", opd)?;
}
Expand Down
27 changes: 12 additions & 15 deletions src-tauri/src/modules/riscv/basic/parser/oplist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn reg<T: ParserRISCVRegisterTrait + 'static>(reg: T) -> RISCVOpdSetAimOpd {
}
// --------------------imm-------------------------
pub fn imm_i(imm: i128) -> RISCVOpdSetAimOpd {
RISCVOpdSetAimOpd::Val(ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(imm)))
RISCVOpdSetAimOpd::Val(ParserRISCVInstOpd::Imm(imm as ParserRISCVImmediate))
}
// --------------------idx-------------------------
pub fn idx(idx: usize) -> RISCVOpdSetAimOpd {
Expand All @@ -71,26 +71,18 @@ pub fn idx_handler(
) -> RISCVOpdSetAimOpd {
RISCVOpdSetAimOpd::Idx(RISCVOpdSetAimOpdIdx { idx, handler })
}
// used with idx_handler_high, i = u20 << 12 + i12, get the i12 imm
pub fn idx_handler_low(opd: ParserRISCVInstOpd) -> ParserRISCVInstOpd {
if let ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(i)) = opd {
if i & 0x800 != 0 {
ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(-(i & 0x7ff)))
} else {
ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(i & 0x7ff))
}
if let ParserRISCVInstOpd::Imm(i) = opd {
ParserRISCVInstOpd::Imm((i & 0x7ff) | -(i & 0x800))
} else {
opd
}
}
// used with idx_handler_low, i = u20 << 12 + i12, get the u20 imm
pub fn idx_handler_high(opd: ParserRISCVInstOpd) -> ParserRISCVInstOpd {
if let ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(i)) = opd {
if i & 0x800 != 0 {
ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(
((i as u32 + 0x0000_1000) >> 12) as i128,
))
} else {
ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(((i as u32) >> 12) as i128))
}
if let ParserRISCVInstOpd::Imm(i) = opd {
ParserRISCVInstOpd::Imm((i >> 12) + ((i & 0x800) >> 11))
} else {
opd
}
Expand Down Expand Up @@ -118,18 +110,23 @@ pub fn expect_csr(last_opd: RISCVExpectToken) -> Vec<RISCVExpectToken> {
pub fn basic_op(op: ParserRISCVInstOp, opds: Vec<RISCVOpdSetAimOpd>) -> RISCVOpdSetAim {
RISCVOpdSetAim { op, opds }
}
// basic_op op idx(0) idx(2)
pub fn basic_op_02(op: ParserRISCVInstOp) -> RISCVOpdSetAim {
basic_op(op, vec![idx(0), idx(2)])
}
// basic_op op idx(2) idx(0)
pub fn basic_op_20(op: ParserRISCVInstOp) -> RISCVOpdSetAim {
basic_op(op, vec![idx(2), idx(0)])
}
// basic_op op idx(0) idx(2) idx(4)
pub fn basic_op_024(op: ParserRISCVInstOp) -> RISCVOpdSetAim {
basic_op(op, vec![idx(0), idx(2), idx(4)])
}
// basic_op op idx(0) idx(4) idx(2)
pub fn basic_op_042(op: ParserRISCVInstOp) -> RISCVOpdSetAim {
basic_op(op, vec![idx(0), idx(4), idx(2)])
}
// basic_op op idx(2) idx(0) idx(4)
pub fn basic_op_204(op: ParserRISCVInstOp) -> RISCVOpdSetAim {
basic_op(op, vec![idx(2), idx(0), idx(4)])
}
Expand Down
4 changes: 1 addition & 3 deletions src-tauri/src/modules/riscv/basic/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@ impl RISCVParser {
stash_label_name.push(String::new());
}
RISCVToken::ImmediateInt(val) => {
stash_opd.push(Some(ParserRISCVInstOpd::Imm(ParserRISCVImmediate::Int(
val,
))));
stash_opd.push(Some(ParserRISCVInstOpd::Imm(val as ParserRISCVImmediate)));
stash_label_name.push(String::new());
}
RISCVToken::Symbol(Symbol::Label(lbl)) => {
Expand Down
6 changes: 1 addition & 5 deletions src-tauri/src/modules/riscv/rv32i/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ pub enum RV32IInstruction {
Xori,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RISCVImmediate {
Int(i128),
Float(f64),
}
pub type RISCVImmediate = i32;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RISCVCsr {}
Expand Down

0 comments on commit 015d84b

Please sign in to comment.