From 6759fbd2abd3b09224f603f3dab06ef0157a0480 Mon Sep 17 00:00:00 2001 From: MunirG05 Date: Tue, 11 Jul 2023 14:03:25 +0530 Subject: [PATCH 1/3] tried to shove the timer back in --- hbasm/src/macros.rs | 1 - hbvm/src/vm/mem/mod.rs | 12 +++++++----- hbvm/src/vm/mem/paging.rs | 19 ++++++++++--------- hbvm/src/vm/mod.rs | 19 +++++++++++++++++-- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/hbasm/src/macros.rs b/hbasm/src/macros.rs index d041ab2..0a3c93b 100644 --- a/hbasm/src/macros.rs +++ b/hbasm/src/macros.rs @@ -66,7 +66,6 @@ macro_rules! impl_asm { }; } - pub(super) use {impl_asm, impl_asm_opcodes}; #[allow(clippy::single_component_path_imports)] diff --git a/hbvm/src/vm/mem/mod.rs b/hbvm/src/vm/mem/mod.rs index dd92875..65f5800 100644 --- a/hbvm/src/vm/mem/mod.rs +++ b/hbvm/src/vm/mem/mod.rs @@ -2,11 +2,13 @@ pub mod paging; -use self::paging::{PageTable, Permission, PtEntry}; -use super::{trap::HandleTrap, VmRunError}; -use alloc::boxed::Box; -use core::mem::MaybeUninit; -use derive_more::Display; +use { + self::paging::{PageTable, Permission, PtEntry}, + super::{trap::HandleTrap, VmRunError}, + alloc::boxed::Box, + core::mem::MaybeUninit, + derive_more::Display, +}; /// HoleyBytes virtual memory #[derive(Clone, Debug)] diff --git a/hbvm/src/vm/mem/paging.rs b/hbvm/src/vm/mem/paging.rs index b1979eb..c621ab4 100644 --- a/hbvm/src/vm/mem/paging.rs +++ b/hbvm/src/vm/mem/paging.rs @@ -1,12 +1,14 @@ //! Page table and associated structures implementation -use core::{ - fmt::Debug, - mem::MaybeUninit, - ops::{Index, IndexMut}, - slice::SliceIndex, +use { + core::{ + fmt::Debug, + mem::MaybeUninit, + ops::{Index, IndexMut}, + slice::SliceIndex, + }, + delegate::delegate, }; -use delegate::delegate; /// Page entry permission #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -30,7 +32,7 @@ pub enum Permission { pub struct PtEntry(u64); impl PtEntry { /// Create new - /// + /// /// # Safety /// - `ptr` has to point to valid data and shall not be deallocated /// troughout the entry lifetime @@ -61,7 +63,6 @@ impl Debug for PtEntry { } } - /// Page table #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(align(4096))] @@ -145,7 +146,7 @@ impl Default for PageTable { #[repr(C, align(4096))] pub union PtPointedData { /// Node - next page table - pub pt: PageTable, + pub pt: PageTable, /// Leaf pub page: u8, } diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 0f6fa2f..8b975bf 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -97,6 +97,10 @@ pub struct Vm<'a, T> { /// Program program: &'a [u8], + + pub last_tick_count: u32, + pub tick_callback: Option u32>, + pub tick_limit: u32, } impl<'a, T: HandleTrap> Vm<'a, T> { @@ -111,6 +115,9 @@ impl<'a, T: HandleTrap> Vm<'a, T> { traph, pc: 0, program, + last_tick_count: 0, + tick_callback: None, + tick_limit: 32, } } @@ -123,12 +130,12 @@ impl<'a, T: HandleTrap> Vm<'a, T> { /// Execute program /// /// Program can return [`VmRunError`] if a trap handling failed - pub fn run(&mut self) -> Result<(), VmRunError> { + pub fn run(&mut self) -> Result { use hbbytecode::opcode::*; loop { // Fetch instruction let Some(&opcode) = self.program.get(self.pc) - else { return Ok(()) }; + else { return Ok(2) }; // Big match unsafe { @@ -318,7 +325,15 @@ impl<'a, T: HandleTrap> Vm<'a, T> { } } } + if self.tick_callback.is_some() { + let ret = self.tick_callback.unwrap()(); + if (ret - self.last_tick_count) >= self.tick_limit { + return Ok(0); + } + } + return Ok(1); } + } /// Read register From b72f0afe8412308d55d3a2a12e33d783a235f629 Mon Sep 17 00:00:00 2001 From: MunirG05 Date: Tue, 11 Jul 2023 14:38:20 +0530 Subject: [PATCH 2/3] add fancy errors --- Cargo.lock | 23 +++++++++++++++++++++++ hbasm/Cargo.toml | 1 + hbasm/src/main.rs | 39 +++++++++++++++++++++++++++++---------- hbvm/src/vm/mod.rs | 19 ++----------------- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 32c620d..e77351e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,16 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56fc6cf8dc8c4158eed8649f9b8b0ea1518eb62b544fe9490d66fa0b349eafe9" +[[package]] +name = "ariadne" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72fe02fc62033df9ba41cba57ee19acf5e742511a140c7dbc3a873e19a19a1bd" +dependencies = [ + "unicode-width", + "yansi", +] + [[package]] name = "beef" version = "0.5.2" @@ -90,6 +100,7 @@ dependencies = [ name = "hbasm" version = "0.1.0" dependencies = [ + "ariadne", "hashbrown 0.14.0", "hbbytecode", "lasso", @@ -250,8 +261,20 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/hbasm/Cargo.toml b/hbasm/Cargo.toml index 755d32d..1f98378 100644 --- a/hbasm/Cargo.toml +++ b/hbasm/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" hbbytecode = { path = "../hbbytecode" } paste = "1.0" hashbrown = "0.14.0" +ariadne = "0.3.0" [dependencies.lasso] version = "0.7" diff --git a/hbasm/src/main.rs b/hbasm/src/main.rs index 046e92f..3126ffb 100644 --- a/hbasm/src/main.rs +++ b/hbasm/src/main.rs @@ -1,6 +1,9 @@ -use std::{ - error::Error, - io::{stdin, stdout, Read, Write}, +use { + ariadne::{ColorGenerator, Label, Report, ReportKind, Source}, + std::{ + error::Error, + io::{stdin, Read}, + }, }; fn main() -> Result<(), Box> { @@ -8,14 +11,30 @@ fn main() -> Result<(), Box> { stdin().read_to_string(&mut code)?; let mut buf = vec![]; + if let Err(e) = hbasm::text::assembly(&code, &mut buf) { - eprintln!( - "Error {:?} at {:?} (`{}`)", - e.kind, - e.span.clone(), - &code[e.span], - ); + let mut colors = ColorGenerator::new(); + + let e_code = match e.kind { + hbasm::text::ErrorKind::UnexpectedToken => 1, + hbasm::text::ErrorKind::InvalidToken => 2, + hbasm::text::ErrorKind::UnexpectedEnd => 3, + hbasm::text::ErrorKind::InvalidSymbol => 4, + }; + let a = colors.next(); + + Report::build(ReportKind::Error, "engine_internal", 12) + .with_code(e_code) + .with_message(format!("{:?}", e.kind)) + .with_label( + Label::new(("engine_internal", e.span.clone())) + .with_message(format!("{:?}", e.kind)) + .with_color(a), + ) + .finish() + .eprint(("engine_internal", Source::from(&code))) + .unwrap(); } - stdout().write_all(&buf)?; + Ok(()) } diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 8b975bf..0f6fa2f 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -97,10 +97,6 @@ pub struct Vm<'a, T> { /// Program program: &'a [u8], - - pub last_tick_count: u32, - pub tick_callback: Option u32>, - pub tick_limit: u32, } impl<'a, T: HandleTrap> Vm<'a, T> { @@ -115,9 +111,6 @@ impl<'a, T: HandleTrap> Vm<'a, T> { traph, pc: 0, program, - last_tick_count: 0, - tick_callback: None, - tick_limit: 32, } } @@ -130,12 +123,12 @@ impl<'a, T: HandleTrap> Vm<'a, T> { /// Execute program /// /// Program can return [`VmRunError`] if a trap handling failed - pub fn run(&mut self) -> Result { + pub fn run(&mut self) -> Result<(), VmRunError> { use hbbytecode::opcode::*; loop { // Fetch instruction let Some(&opcode) = self.program.get(self.pc) - else { return Ok(2) }; + else { return Ok(()) }; // Big match unsafe { @@ -325,15 +318,7 @@ impl<'a, T: HandleTrap> Vm<'a, T> { } } } - if self.tick_callback.is_some() { - let ret = self.tick_callback.unwrap()(); - if (ret - self.last_tick_count) >= self.tick_limit { - return Ok(0); - } - } - return Ok(1); } - } /// Read register From e25a89d56dc0b5cefd0d1efbc8295ddcd18dc697 Mon Sep 17 00:00:00 2001 From: MunirG05 Date: Tue, 11 Jul 2023 14:54:49 +0530 Subject: [PATCH 3/3] the design is very human --- hbasm/src/main.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/hbasm/src/main.rs b/hbasm/src/main.rs index 3126ffb..d59a873 100644 --- a/hbasm/src/main.rs +++ b/hbasm/src/main.rs @@ -21,14 +21,20 @@ fn main() -> Result<(), Box> { hbasm::text::ErrorKind::UnexpectedEnd => 3, hbasm::text::ErrorKind::InvalidSymbol => 4, }; + let message = match e.kind { + hbasm::text::ErrorKind::UnexpectedToken => "This token is not expected!", + hbasm::text::ErrorKind::InvalidToken => "The token is not valid!", + hbasm::text::ErrorKind::UnexpectedEnd => "The assembler reached the end of input unexpectedly!", + hbasm::text::ErrorKind::InvalidSymbol => "This referenced symbol doesn't have a corresponding label!", + }; let a = colors.next(); - Report::build(ReportKind::Error, "engine_internal", 12) + Report::build(ReportKind::Error, "engine_internal", e.span.clone().start) .with_code(e_code) .with_message(format!("{:?}", e.kind)) .with_label( Label::new(("engine_internal", e.span.clone())) - .with_message(format!("{:?}", e.kind)) + .with_message(message) .with_color(a), ) .finish()