Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder { /* private fields */ }
Expand description

Decodes instructions for a base ISA with a chosen set of extensions.

Decoder::new enables every supported extension that does not conflict with another (Extensions::DEFAULT), as the free functions such as decode do. Presets such as Decoder::RVA23U64, Decoder::with_only and Decoder::from_march choose the extensions of a particular target, so that instructions from other extensions are shown as .insn (or without aliases, such as lpad, that need them).

use disc_v::{Decoder, Extension, Isa};

// czero.eqz a0,a1,a2
assert_eq!(Decoder::new(Isa::Rv64).decode(0, 0x0ec5d533).to_string(), "czero.eqz a0,a1,a2");
assert_eq!(Decoder::RV64GC.decode(0, 0x0ec5d533).to_string(), ".insn 4, 0x0ec5d533");

let dec = Decoder::RV64GC.with(Extension::Zicond).unwrap();
assert_eq!(dec.decode(0, 0x0ec5d533).to_string(), "czero.eqz a0,a1,a2");

let dec = Decoder::from_march("rv64gc_zicond").unwrap();
assert_eq!(dec, Decoder::RV64GC.with(Extension::Zicond).unwrap());

Implementations§

Source§

impl Decoder

Source

pub const RV32GC: Decoder

RV32GC: RV32I with IMAFD, Zicsr, Zifencei and C.

Source

pub const RV64GC: Decoder

RV64GC: RV64I with IMAFD, Zicsr, Zifencei and C.

Source

pub const RVA23U64: Decoder

The RVA23U64 profile, for user-mode code.

Source

pub const RVA23S64: Decoder

The RVA23S64 profile, which adds the supervisor-mode and hypervisor instructions to RVA23U64.

Source

pub const fn new(isa: Isa) -> Decoder

A decoder for isa with every supported extension that does not conflict with another (Extensions::DEFAULT).

Source

pub fn with_only( isa: Isa, extensions: impl Into<Extensions>, ) -> Result<Decoder, Conflict>

A decoder for isa with exactly extensions (and the extensions they imply), or an error if two of them conflict.

use disc_v::{Decoder, Extension, Isa};

let dec = Decoder::with_only(Isa::Rv32, [Extension::M, Extension::C]).unwrap();
assert_eq!(dec.decode(0, 0x02b50533).to_string(), "mul a0,a0,a1");
assert_eq!(dec.decode(0, 0x00b57553).to_string(), ".insn 4, 0x00b57553"); // fadd.s
Source

pub fn with(self, ext: Extension) -> Result<Decoder, Conflict>

The decoder with ext (and the extensions it implies) enabled, or an error if it conflicts with an extension already enabled.

Source

pub fn try_with(self, ext: Extension) -> Decoder

The decoder with ext (and the extensions it implies) enabled, or the decoder unchanged if ext conflicts with an extension already enabled.

Source

pub fn without(self, ext: Extension) -> Decoder

The decoder with ext (and the extensions that imply it) disabled.

Source

pub fn isa(&self) -> Isa

The base ISA.

Source

pub fn extensions(&self) -> Extensions

The enabled extensions.

Source

pub fn decode(&self, pc: u64, inst: u64) -> Instruction

Decodes the instruction inst, located at address pc. See decode.

Source

pub fn decode_bytes(&self, pc: u64, bytes: &[u8]) -> Option<Instruction>

Decodes the instruction at the start of bytes, located at address pc. See decode_bytes.

Source

pub fn disassemble<'a>(&self, pc: u64, bytes: &'a [u8]) -> Disassembler<'a> ⓘ

Disassembles bytes, the first of which is at address pc. See disassemble.

Source

pub fn from_march(march: &str) -> Result<Decoder, MarchError<'_>>

A decoder for an ISA string, as passed to compilers with -march, such as "rv64gc" or "rv32imac_zicsr_zba_zbb".

Version numbers (rv64i2p1) are ignored, as are extensions that define no instructions (such as Zkt, Sstc or Zvl128b). Some extensions are decoded as a larger extension that contains them: Zmmul as M, Zaamo and Zalrsc as A, the Zve embedded vector extensions as V, and Sha as H. The Zce, Zk, Zkn and Zks bundles are expanded.

Unknown supervisor- and machine-level extensions (names starting with s) are also ignored, since nearly all of them define no instructions, while unknown z and x extensions are errors.

use disc_v::{Decoder, MarchError};

assert_eq!(Decoder::from_march("rv64gc"), Ok(Decoder::RV64GC));
assert_eq!(
    Decoder::from_march("rv64gc_xventanacondops"),
    Err(MarchError::UnsupportedExtension("xventanacondops")),
);

Trait Implementations§

Source§

impl Clone for Decoder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Decoder

Source§

impl Debug for Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Decoder

Source§

impl Hash for Decoder

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Decoder

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Decoder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.