Struct riscy::RegVal

source ·
pub struct RegVal(/* private fields */);
Expand description

A struct for representing a value that can be held inside of a general purpose register (eg, x0, x1, …, x32).

While all general purpose registers are 32-bits wide in rv32i, the interpretation of those bits depends on the operation in question. For example, some instructions (such as lw) treat registers values as signed integers, while other instructions (such as lwu) treat them as unsigned integers.

Therefore, riscy uses this type to prevent programmers from assuming one interpretation or the other, and instead, it forces them to explicitly cast any RegVal to the representation they need.

All immediate values used by the decode module (such as ImmS) can also be cast to RegVal.

// get the value of the first argument register, `a0` (also known as `x10`)
let value = riscv.reg(10);
println!("a0 = {} (as an unsigned integer)", u32::from(value));
println!("a0 = {} (as an   signed integer)", i32::from(value));

Several operations which correspond to register-register instructions (eg, add, xor), are provided as either methods or as traits.

let value1 = RegVal::from_u32(2);
let value2 = RegVal::from_u32(3);

// addition
println!("value1 + value2 = {}", u32::from(value1 + value2));

// bitwise exclusive or
println!("value1 ^ value2 = {}", u32::from(value1 ^ value2));

// left logical shift
println!("value1 << value2 = {}", u32::from(value1.shift_left_logical(value2));

Implementations§

source§

impl RegVal

source

pub fn less_than_signed(&self, other: RegVal) -> bool

Compares two RegVals, interpreting them both as signed integers.

source

pub fn less_than_unsigned(&self, other: RegVal) -> bool

Compares two RegVals, interpreting them both as unsigned integers.

source

pub fn greater_than_equal_to_signed(&self, other: RegVal) -> bool

Compares two RegVals, interpreting them both as signed integers.

source

pub fn greater_than_equal_to_unsigned(&self, other: RegVal) -> bool

Compares two RegVals, interpreting them both as unsigned integers.

source

pub fn shift_left_logical(&self, other: RegVal) -> RegVal

Does a logical left shift of this register by the value in another register.

A logical left shift is one where the vacant bits are all set to 0.

The value in other is interpreted as an unsigned integer.

The sll instruction (shift left logical) will only ever supply small values for other. The behavior of this function is only guaranteed for values that could come from that instruction.

source

pub fn shift_right_logical(&self, other: RegVal) -> RegVal

Does a logical right shift of this register by the value in another register.

A logical right shift is one where the vacant bits are all set to 0. This is most common when the register value is interpreted as an unsigned integer, since it has the effect of dividing by 2 to the power of other.

The value in other is interpreted as an unsigned integer.

The srl instruction (shift right logical) will only ever supply small values for other. The behavior of this function is only guaranteed for values that could come from that instruction.

source

pub fn shift_right_arithmetic(&self, other: RegVal) -> RegVal

Does an arithmetic right shift of this register by the value in another register.

An arithmetic right shift is one where the vacant bits are all set to a copy of the most signfint bit. This is most common when the register value is interpreted as a signed integer, since it has the effect of dividing by 2 to the power of other, while preserving the sign.

The value in other is interpreted as an unsigned integer.

The srl instruction (shift right logical) will only ever supply small values for other. The behavior of this function is only guaranteed for values that could come from that instruction.

source

pub fn from_u32(val: u32) -> RegVal

source

pub fn from_i32(val: i32) -> RegVal

source

pub fn from_addr(addr: Addr) -> RegVal

source

pub fn to_u32(self) -> u32

source

pub fn to_i32(self) -> i32

source

pub fn to_addr(self) -> Addr

Trait Implementations§

source§

impl Add<RegVal> for Addr

§

type Output = Addr

The resulting type after applying the + operator.
source§

fn add(self, offset: RegVal) -> Self

Performs the + operation. Read more
source§

impl Add for RegVal

§

type Output = RegVal

The resulting type after applying the + operator.
source§

fn add(self, other: RegVal) -> Self

Performs the + operation. Read more
source§

impl AddAssign<RegVal> for Addr

source§

fn add_assign(&mut self, offset: RegVal)

Performs the += operation. Read more
source§

impl BitAnd for RegVal

§

type Output = RegVal

The resulting type after applying the & operator.
source§

fn bitand(self, other: RegVal) -> Self

Performs the & operation. Read more
source§

impl BitOr for RegVal

§

type Output = RegVal

The resulting type after applying the | operator.
source§

fn bitor(self, other: RegVal) -> Self

Performs the | operation. Read more
source§

impl BitXor for RegVal

§

type Output = RegVal

The resulting type after applying the ^ operator.
source§

fn bitxor(self, other: RegVal) -> Self

Performs the ^ operation. Read more
source§

impl Clone for RegVal

source§

fn clone(&self) -> RegVal

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for RegVal

source§

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

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

impl From<Addr> for RegVal

source§

fn from(addr: Addr) -> RegVal

Converts to this type from the input type.
source§

impl From<RegVal> for Addr

source§

fn from(val: RegVal) -> Addr

Converts to this type from the input type.
source§

impl From<RegVal> for i32

source§

fn from(val: RegVal) -> i32

Converts to this type from the input type.
source§

impl From<RegVal> for u32

source§

fn from(val: RegVal) -> u32

Converts to this type from the input type.
source§

impl From<i32> for RegVal

source§

fn from(val: i32) -> RegVal

Converts to this type from the input type.
source§

impl From<u32> for RegVal

source§

fn from(val: u32) -> RegVal

Converts to this type from the input type.
source§

impl Hash for RegVal

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 RegVal

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Sub for RegVal

§

type Output = RegVal

The resulting type after applying the - operator.
source§

fn sub(self, other: RegVal) -> Self

Performs the - operation. Read more
source§

impl Copy for RegVal

source§

impl Eq for RegVal

source§

impl StructuralEq for RegVal

source§

impl StructuralPartialEq for RegVal

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.