Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring to improve type safety #803

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions fuel-vm/src/constraints/reg_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ use core::ops::{
use fuel_asm::{
PanicReason,
RegId,
RegisterId,
Word,
};

use crate::consts::{
VM_REGISTER_COUNT,
VM_REGISTER_PROGRAM_COUNT,
VM_REGISTER_SYSTEM_COUNT,
use crate::{
consts::{
VM_REGISTER_COUNT,
VM_REGISTER_PROGRAM_COUNT,
VM_REGISTER_SYSTEM_COUNT,
},
interpreter::register::verify_register_user_writable,
};

#[cfg(test)]
Expand All @@ -37,14 +39,6 @@ pub struct Reg<'r, const INDEX: u8>(&'r Word);
pub struct WriteRegKey(usize);

impl WriteRegKey {
/// Create a new writable register key if the index is within the bounds
/// of the writable registers.
pub fn new(k: impl Into<usize>) -> Result<Self, PanicReason> {
let k = k.into();
is_register_writable(&k)?;
Ok(Self(k))
}

/// Translate this key from an absolute register index
/// to a program register index.
///
Expand All @@ -55,18 +49,6 @@ impl WriteRegKey {
}
}

/// Check that the register is above the system registers and below the total
/// number of registers.
pub(crate) fn is_register_writable(r: &RegisterId) -> Result<(), PanicReason> {
const W_USIZE: usize = RegId::WRITABLE.to_u8() as usize;
const RANGE: core::ops::Range<usize> = W_USIZE..(W_USIZE + VM_REGISTER_PROGRAM_COUNT);
if RANGE.contains(r) {
Ok(())
} else {
Err(PanicReason::ReservedRegisterNotWritable)
}
}

impl<'r, const INDEX: u8> RegMut<'r, INDEX> {
/// Create a new mutable register reference.
pub fn new(reg: &'r mut Word) -> Self {
Expand Down Expand Up @@ -368,11 +350,12 @@ impl<'a> From<ProgramRegisters<'a>> for ProgramRegistersRef<'a> {
}
}

impl TryFrom<RegisterId> for WriteRegKey {
impl TryFrom<RegId> for WriteRegKey {
type Error = PanicReason;

fn try_from(r: RegisterId) -> Result<Self, Self::Error> {
Self::new(r)
fn try_from(r: RegId) -> Result<Self, Self::Error> {
verify_register_user_writable(r)?;
Ok(Self(r.to_u8() as usize))
}
}

Expand Down
11 changes: 1 addition & 10 deletions fuel-vm/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mod receipts;

mod debug;
mod ecal;
pub(crate) mod register;

use crate::profiler::Profiler;

Expand Down Expand Up @@ -224,16 +225,6 @@ impl<M: AsMut<MemoryInstance>, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal> {
}

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal> {
/// Returns the current state of the registers
pub const fn registers(&self) -> &[Word] {
&self.registers
}

/// Returns mutable access to the registers
pub fn registers_mut(&mut self) -> &mut [Word] {
&mut self.registers
}

pub(crate) fn call_stack(&self) -> &[CallFrame] {
self.frames.as_slice()
}
Expand Down
16 changes: 8 additions & 8 deletions fuel-vm/src/interpreter/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::{
error::SimpleResult,
};

use fuel_asm::PanicReason;
use fuel_types::{
RegisterId,
Word,
use fuel_asm::{
PanicReason,
RegId,
};
use fuel_types::Word;

mod muldiv;
mod wideint;
Expand All @@ -26,7 +26,7 @@ where
/// Stores the overflowed wrapped value into RegId::OF
pub(crate) fn alu_capture_overflow<F, B, C>(
&mut self,
ra: RegisterId,
ra: RegId,
f: F,
b: B,
c: C,
Expand All @@ -48,7 +48,7 @@ where
/// Set RegId::OF to true and zero the result register if overflow occurred.
pub(crate) fn alu_boolean_overflow<F, B, C>(
&mut self,
ra: RegisterId,
ra: RegId,
f: F,
b: B,
c: C,
Expand All @@ -69,7 +69,7 @@ where

pub(crate) fn alu_error<F, B, C>(
&mut self,
ra: RegisterId,
ra: RegId,
f: F,
b: B,
c: C,
Expand All @@ -89,7 +89,7 @@ where
alu_error(dest, flag.as_ref(), common, f, b, c, err_bool)
}

pub(crate) fn alu_set(&mut self, ra: RegisterId, b: Word) -> SimpleResult<()> {
pub(crate) fn alu_set(&mut self, ra: RegId, b: Word) -> SimpleResult<()> {
let (SystemRegisters { of, err, pc, .. }, mut w) =
split_registers(&mut self.registers);
let dest = &mut w[ra.try_into()?];
Expand Down
10 changes: 5 additions & 5 deletions fuel-vm/src/interpreter/alu/muldiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::{
error::SimpleResult,
};

use fuel_asm::PanicReason;
use fuel_types::{
RegisterId,
Word,
use fuel_asm::{
PanicReason,
RegId,
};
use fuel_types::Word;

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
where
Expand All @@ -22,7 +22,7 @@ where
/// Stores the overflowed wrapped value into RegId::OF
pub(crate) fn alu_muldiv(
&mut self,
ra: RegisterId,
ra: RegId,
lhs: Word,
rhs: Word,
divider: Word,
Expand Down
8 changes: 3 additions & 5 deletions fuel-vm/src/interpreter/alu/wideint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use ethnum::U256;
use fuel_asm::{
wideint::*,
PanicReason,
RegId,
};
use fuel_types::{
RegisterId,
Word,
};
use fuel_types::Word;

use super::super::{
internal::inc_pc,
Expand Down Expand Up @@ -70,7 +68,7 @@ macro_rules! wideint_ops {
{
pub(crate) fn [<alu_wideint_cmp_ $t:lower>](
&mut self,
ra: RegisterId,
ra: RegId,
b: Word,
c: Word,
args: CompareArgs,
Expand Down
13 changes: 4 additions & 9 deletions fuel-vm/src/interpreter/blob.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fuel_asm::{
RegisterId,
RegId,
Word,
};
use fuel_storage::StorageSize;
Expand All @@ -18,12 +18,9 @@ use crate::{
use super::{
internal::inc_pc,
memory::copy_from_slice_zero_fill,
split_registers,
GetRegMut,
Interpreter,
Memory,
SystemRegisters,
WriteRegKey,
};

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
Expand All @@ -34,7 +31,7 @@ where
{
pub(crate) fn blob_size(
&mut self,
dst: RegisterId,
dst: RegId,
blob_id_ptr: Word,
) -> IoResult<(), S::DataError> {
let gas_cost = self
Expand All @@ -51,10 +48,8 @@ where
.ok_or(PanicReason::BlobNotFound)?;

self.dependent_gas_charge_without_base(gas_cost, size as Word)?;
let (SystemRegisters { pc, .. }, mut w) = split_registers(&mut self.registers);
let result = &mut w[WriteRegKey::try_from(dst)?];
*result = size as Word;
Ok(inc_pc(pc)?)
*self.write_user_register(dst)? = size as Word;
Ok(self.inc_pc()?)
}

pub(crate) fn blob_load_data(
Expand Down
Loading
Loading