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
impl RegVal
sourcepub fn less_than_signed(&self, other: RegVal) -> bool
pub fn less_than_signed(&self, other: RegVal) -> bool
Compares two RegVal
s, interpreting them both as signed integers.
sourcepub fn less_than_unsigned(&self, other: RegVal) -> bool
pub fn less_than_unsigned(&self, other: RegVal) -> bool
Compares two RegVal
s, interpreting them both as unsigned integers.
sourcepub fn greater_than_equal_to_signed(&self, other: RegVal) -> bool
pub fn greater_than_equal_to_signed(&self, other: RegVal) -> bool
Compares two RegVal
s, interpreting them both as signed integers.
sourcepub fn greater_than_equal_to_unsigned(&self, other: RegVal) -> bool
pub fn greater_than_equal_to_unsigned(&self, other: RegVal) -> bool
Compares two RegVal
s, interpreting them both as unsigned integers.
sourcepub fn shift_left_logical(&self, other: RegVal) -> RegVal
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.
sourcepub fn shift_right_logical(&self, other: RegVal) -> RegVal
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.
sourcepub fn shift_right_arithmetic(&self, other: RegVal) -> RegVal
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.
pub fn from_u32(val: u32) -> RegVal
pub fn from_i32(val: i32) -> RegVal
pub fn from_addr(addr: Addr) -> RegVal
pub fn to_u32(self) -> u32
pub fn to_i32(self) -> i32
pub fn to_addr(self) -> Addr
Trait Implementations§
source§impl AddAssign<RegVal> for Addr
impl AddAssign<RegVal> for Addr
source§fn add_assign(&mut self, offset: RegVal)
fn add_assign(&mut self, offset: RegVal)
+=
operation. Read more