Skip to main content

asmkit/riscv/
mod.rs

1#[doc(hidden)]
2pub(crate) mod arch_traits;
3pub mod assembler;
4#[doc(hidden)]
5// Generated instruction APIs retain spec prose and operand-expression shape.
6#[allow(clippy::doc_lazy_continuation)]
7mod emitter;
8pub(crate) mod instapi;
9#[allow(dead_code)]
10pub(crate) mod instdb;
11#[allow(
12    clippy::doc_lazy_continuation,
13    clippy::identity_op,
14    clippy::needless_borrow
15)]
16#[allow(dead_code)]
17pub(crate) mod opcodes;
18pub mod operands;
19
20pub use crate::core::operand::imm;
21pub use assembler::*;
22pub use emitter::*;
23pub use instapi::query_rw_info;
24pub use instdb::{ALL_CPU_FEATURES, CPU_FEATURE_COUNT, CPU_FEATURE_NAMES, CpuFeature};
25pub use opcodes::Opcode;
26pub use operands::*;
27pub use regs::*;
28
29/// Unstable validation tooling; this is not a stable instruction-encoding API.
30#[cfg(feature = "validation")]
31#[doc(hidden)]
32pub mod coverage {
33    pub use super::instdb::{ANY, FP, GP, IMM, INST_INFO_TABLE, SIGNATURE_TABLE, VEC};
34    pub use super::opcodes::{ALL_OPCODES, Encoding, OPCODE_STR, OPCODE_XLEN};
35}
36
37/// Return the length (in bytes) of an instruction given the low 16 bits of it.
38///
39/// The current spec reserves a bit pattern for instructions of length >= 192 bits, but for
40/// simplicity this function just returns 24 in that case. The largest instructions currently
41/// defined are 4 bytes so it will likely be a long time until this difference matters.
42pub fn instruction_length(i: u16) -> usize {
43    if i & 0b11 != 0b11 {
44        2
45    } else if i & 0b11100 != 0b11100 {
46        4
47    } else if i & 0b111111 == 0b011111 {
48        6
49    } else if i & 0b1111111 == 0b011111 {
50        8
51    } else {
52        10 + 2 * ((i >> 12) & 0b111) as usize
53    }
54}