Skip to content

Commit

Permalink
emul-visibility: Reduce visibility of decoder details (#513)
Browse files Browse the repository at this point in the history
Co-authored-by: Aurélien Nicolas <[email protected]>
  • Loading branch information
naure and Aurélien Nicolas authored Oct 31, 2024
1 parent 3a6ddcd commit 39fd9d3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
2 changes: 1 addition & 1 deletion ceno_emul/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod vm_state;
pub use vm_state::VMState;

mod rv32im;
pub use rv32im::{DecodedInstruction, EmuContext, InsnCategory, InsnCodes, InsnKind};
pub use rv32im::{DecodedInstruction, EmuContext, InsnCodes, InsnKind};

mod elf;
pub use elf::Program;
Expand Down
39 changes: 13 additions & 26 deletions ceno_emul/src/rv32im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub enum TrapCause {
pub struct DecodedInstruction {
insn: u32,
top_bit: u32,
// The bit fields of the instruction encoding, regardless of the instruction format.
func7: u32,
rs2: u32,
rs1: u32,
Expand All @@ -110,7 +111,7 @@ pub struct DecodedInstruction {
}

#[derive(Clone, Copy, Debug)]
pub enum InsnCategory {
enum InsnCategory {
Compute,
Branch,
Load,
Expand Down Expand Up @@ -236,11 +237,6 @@ impl DecodedInstruction {
}
}

/// Get the funct3 field, regardless of the instruction format.
pub fn funct3(&self) -> u32 {
self.func3
}

/// Get the funct3 field, or zero if the instruction does not use funct3.
pub fn funct3_or_zero(&self) -> u32 {
match self.codes().format {
Expand Down Expand Up @@ -275,18 +271,13 @@ impl DecodedInstruction {
}
}

/// Get the funct7 field, regardless of the instruction format.
pub fn funct7(&self) -> u32 {
self.func7
}

/// Get the decoded immediate, or 2^shift, or the funct7 field, depending on the instruction format.
pub fn imm_or_funct7(&self) -> u32 {
match self.codes().format {
R => self.func7,
I => match self.codes().kind {
// decode the shift as a multiplication/division by 1 << immediate
SLLI | SRLI | SRAI => 1 << (self.imm_i() & 0x1f),
SLLI | SRLI | SRAI => 1 << self.imm_shamt(),
_ => self.imm_i(),
},
S => self.imm_s(),
Expand All @@ -308,35 +299,31 @@ impl DecodedInstruction {
}
}

pub fn sign_bit(&self) -> u32 {
self.top_bit
}

pub fn codes(&self) -> InsnCodes {
FastDecodeTable::get().lookup(self)
}

pub fn kind(&self) -> (InsnCategory, InsnKind) {
let i = FastDecodeTable::get().lookup(self);
(i.category, i.kind)
}

pub fn imm_b(&self) -> u32 {
fn imm_b(&self) -> u32 {
(self.top_bit * 0xfffff000)
| ((self.rd & 1) << 11)
| ((self.func7 & 0x3f) << 5)
| (self.rd & 0x1e)
}

pub fn imm_i(&self) -> u32 {
fn imm_i(&self) -> u32 {
(self.top_bit * 0xfffff000) | (self.func7 << 5) | self.rs2
}

pub fn imm_s(&self) -> u32 {
/// Shift amount field of SLLI, SRLI, SRAI.
fn imm_shamt(&self) -> u32 {
self.rs2
}

fn imm_s(&self) -> u32 {
(self.top_bit * 0xfffff000) | (self.func7 << 5) | self.rd
}

pub fn imm_j(&self) -> u32 {
fn imm_j(&self) -> u32 {
(self.top_bit * 0xfff00000)
| (self.rs1 << 15)
| (self.func3 << 12)
Expand All @@ -345,7 +332,7 @@ impl DecodedInstruction {
| (self.rs2 & 0x1e)
}

pub fn imm_u(&self) -> u32 {
fn imm_u(&self) -> u32 {
self.insn & 0xfffff000
}
}
Expand Down
2 changes: 1 addition & 1 deletion ceno_emul/tests/test_vm_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn test_vm_trace() -> Result<()> {
assert_eq!(ctx.peek_register(2), x2);
assert_eq!(ctx.peek_register(3), x3);

let ops: Vec<InsnKind> = steps.iter().map(|step| step.insn().kind().1).collect();
let ops: Vec<InsnKind> = steps.iter().map(|step| step.insn().codes().kind).collect();
assert_eq!(ops, expected_ops_fibonacci_20());

assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/scheme/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn test_single_add_instance_e2e() {
let mut add_records = vec![];
let mut halt_records = vec![];
all_records.into_iter().for_each(|record| {
let kind = record.insn().kind().1;
let kind = record.insn().codes().kind;
match kind {
ADD => add_records.push(record),
EANY => {
Expand Down

0 comments on commit 39fd9d3

Please sign in to comment.