From f468a02ad46a4172b2e7373ad961f873b4c19e0f Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 19 Aug 2023 23:46:47 +0200 Subject: [PATCH] bye stuff --- Cargo.lock | 55 +----------------------------- hbvm/Cargo.toml | 4 +-- hbvm/src/lib.rs | 1 + hbvm/src/mem/addr.rs | 14 ++++---- hbvm/src/mem/mod.rs | 22 ++++++++---- hbvm/src/mem/softpaging/mapping.rs | 16 +++++---- hbvm/src/utils.rs | 38 +++++++++++++++++++++ hbvm/src/value.rs | 4 +-- 8 files changed, 76 insertions(+), 78 deletions(-) create mode 100644 hbvm/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 47ef3c9d..fd50d298 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,25 +47,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "convert_case" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" - -[[package]] -name = "derive_more" -version = "0.99.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" -dependencies = [ - "convert_case", - "proc-macro2", - "quote", - "rustc_version", - "syn 1.0.109", -] - [[package]] name = "fnv" version = "1.0.7" @@ -113,9 +94,7 @@ version = "0.1.0" name = "hbvm" version = "0.1.0" dependencies = [ - "derive_more", "hbbytecode", - "static_assertions", ] [[package]] @@ -168,7 +147,7 @@ dependencies = [ "proc-macro2", "quote", "regex-syntax", - "syn 2.0.29", + "syn", ] [[package]] @@ -216,38 +195,6 @@ version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" -[[package]] -name = "rustc_version" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" -dependencies = [ - "semver", -] - -[[package]] -name = "semver" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" - -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.29" diff --git a/hbvm/Cargo.toml b/hbvm/Cargo.toml index d9c87cbf..e8302d24 100644 --- a/hbvm/Cargo.toml +++ b/hbvm/Cargo.toml @@ -12,6 +12,4 @@ alloc = [] nightly = [] [dependencies] -derive_more = "0.99" -hbbytecode.path = "../hbbytecode" -static_assertions = "1.0" +hbbytecode.path = "../hbbytecode" diff --git a/hbvm/src/lib.rs b/hbvm/src/lib.rs index 9651262a..5da24b1d 100644 --- a/hbvm/src/lib.rs +++ b/hbvm/src/lib.rs @@ -24,6 +24,7 @@ pub mod value; mod bmc; mod vmrun; +mod utils; use {bmc::BlockCopier, value::Value}; diff --git a/hbvm/src/mem/addr.rs b/hbvm/src/mem/addr.rs index cd0bbd39..ea95ac7a 100644 --- a/hbvm/src/mem/addr.rs +++ b/hbvm/src/mem/addr.rs @@ -1,13 +1,11 @@ //! Virtual(?) memory address -use { - core::{fmt::Debug, ops}, - derive_more::Display, -}; +use core::{fmt::Debug, ops}; + +use crate::utils::impl_display; /// Memory address -#[derive(Clone, Copy, Display, PartialEq, Eq, PartialOrd, Ord)] -#[display(fmt = "{_0:x}")] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct Address(u64); impl Address { /// A null address @@ -49,6 +47,10 @@ impl Address { } } +impl_display!(for Address => + |Address(a)| "{a:0x}" +); + impl ops::Add for Address { type Output = Self; diff --git a/hbvm/src/mem/mod.rs b/hbvm/src/mem/mod.rs index 0d9522be..e134706a 100644 --- a/hbvm/src/mem/mod.rs +++ b/hbvm/src/mem/mod.rs @@ -5,7 +5,8 @@ pub mod softpaging; mod addr; pub use addr::Address; -use {derive_more::Display, hbbytecode::ProgramVal}; + +use {crate::utils::impl_display, hbbytecode::ProgramVal}; /// Load-store memory access pub trait Memory { @@ -45,17 +46,21 @@ pub trait Memory { } /// Unhandled load access trap -#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)] -#[display(fmt = "Load access error at address {_0}")] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct LoadError(pub Address); +impl_display!(for LoadError => + |LoadError(a)| "Load access error at address {a}", +); /// Unhandled store access trap -#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)] -#[display(fmt = "Store access error at address {_0}")] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct StoreError(pub Address); +impl_display!(for StoreError => + |StoreError(a)| "Load access error at address {a}", +); /// Reason to access memory -#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum MemoryAccessReason { /// Memory was accessed for load (read) Load, @@ -63,6 +68,11 @@ pub enum MemoryAccessReason { Store, } +impl_display!(for MemoryAccessReason => match { + Self::Load => "Load"; + Self::Store => "Store"; +}); + impl From for crate::VmRunError { fn from(value: LoadError) -> Self { Self::LoadAccessEx(value.0) diff --git a/hbvm/src/mem/softpaging/mapping.rs b/hbvm/src/mem/softpaging/mapping.rs index e07c0dd3..67f65a1e 100644 --- a/hbvm/src/mem/softpaging/mapping.rs +++ b/hbvm/src/mem/softpaging/mapping.rs @@ -1,6 +1,6 @@ //! Automatic memory mapping -use crate::mem::addr::Address; +use crate::{mem::addr::Address, utils::impl_display}; use { super::{ @@ -9,7 +9,6 @@ use { PageSize, SoftPagedMem, }, alloc::boxed::Box, - derive_more::Display, }; impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> { @@ -143,22 +142,25 @@ impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> { } /// Error mapping -#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum MapError { /// Entry was already mapped - #[display(fmt = "There is already a page mapped on specified address")] AlreadyMapped, /// When walking a page entry was /// encounterd. - #[display(fmt = "There was a page mapped on the way instead of node")] PageOnNode, } +impl_display!(for MapError => match { + Self::AlreadyMapped => "There is already a page mapped on specified address"; + Self::PageOnNode => "There was a page mapped on the way instead of node"; +}); + /// There was no entry in page table to unmap /// /// No worry, don't panic, nothing bad has happened, /// but if you are 120% sure there should be something, /// double-check your addresses. -#[derive(Clone, Copy, Display, Debug)] -#[display(fmt = "There was no entry to unmap")] +#[derive(Clone, Copy, Debug)] pub struct NothingToUnmap; +impl_display!(for NothingToUnmap => "There is no entry to unmap"); diff --git a/hbvm/src/utils.rs b/hbvm/src/utils.rs new file mode 100644 index 00000000..3350c1da --- /dev/null +++ b/hbvm/src/utils.rs @@ -0,0 +1,38 @@ +macro_rules! impl_display { + (for $ty:ty => $(|$selfty:pat_param|)? $fmt:literal $(, $($param:expr),+)? $(,)?) => { + impl ::core::fmt::Display for $ty { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + $(let $selfty = self;)? + write!(f, $fmt, $($param),*) + } + } + }; + + (for $ty:ty => $str:literal) => { + impl ::core::fmt::Display for $ty { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + f.write_str($str) + } + } + }; + + (for $ty:ty => match {$( + $bind:pat => $fmt:literal $(,$($param:expr),* $(,)?)?; + )*}) => { + impl ::core::fmt::Display for $ty { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + match self { + $( + $bind => write!(f, $fmt, $($($param),*)?) + ),* + } + } + } + } +} + +macro_rules! static_assert_eq(($l:expr, $r:expr $(,)?) => { + const _: [(); ($l != $r) as usize] = []; +}); + +pub(crate) use {impl_display, static_assert_eq}; diff --git a/hbvm/src/value.rs b/hbvm/src/value.rs index 7401057e..b72b72b5 100644 --- a/hbvm/src/value.rs +++ b/hbvm/src/value.rs @@ -26,7 +26,7 @@ macro_rules! value_def { } } - static_assertions::const_assert_eq!( + crate::utils::static_assert_eq!( core::mem::size_of::<$ty>(), core::mem::size_of::(), ); @@ -70,7 +70,7 @@ mod private { } value_def!(u64, i64, f64); -static_assertions::const_assert_eq!(core::mem::size_of::(), 8); +crate::utils::static_assert_eq!(core::mem::size_of::(), 8); impl core::fmt::Debug for Value { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {