From b9ebac61854f52703232217c39b1d91cb0498310 Mon Sep 17 00:00:00 2001 From: Artemis Rosman <73006620+rozukke@users.noreply.github.com> Date: Mon, 16 Sep 2024 00:57:14 +1000 Subject: [PATCH 1/2] Add traps and usability fixes --- src/lexer/mod.rs | 6 ++++-- src/main.rs | 43 +++++++++++++++++++++++++++++-------------- src/parser.rs | 2 ++ src/runtime.rs | 32 +++++++++++++++++++++++--------- src/symbol.rs | 2 ++ 5 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index bf08134..7639bad 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -97,7 +97,7 @@ pub fn tokenize(input: &'static str) -> impl Iterator> + '_ /// Test if a character is considered to be whitespace, including commas. pub(crate) fn is_whitespace(c: char) -> bool { - char::is_ascii_whitespace(&c) || c == ',' + char::is_ascii_whitespace(&c) || matches!(c, ',' | ':') } pub(crate) fn is_reg_num(c: char) -> bool { @@ -340,13 +340,15 @@ impl Cursor<'_> { use TokenKind::Trap; use TrapKind::*; match ident { + "trap" => Trap(Generic), "getc" => Trap(Getc), "out" => Trap(Out), "puts" => Trap(Puts), "in" => Trap(In), "putsp" => Trap(Putsp), "halt" => Trap(Halt), - "trap" => Trap(Generic), + "putn" => Trap(Putn), + "reg" => Trap(Reg), _ => TokenKind::Label, } } diff --git a/src/main.rs b/src/main.rs index 4f7b2e5..ec1a5ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,11 @@ enum Command { /// Destination to output .lc3 file dest: Option, }, + /// Check a `.asm` file without running or outputting binary + Check { + /// File to check + name: PathBuf, + }, /// Remove compilation artifacts for specified source Clean { /// `.asm` file to try remove artifacts for @@ -63,7 +68,7 @@ fn main() -> miette::Result<()> { let contents: &'static str = Box::leak(Box::new(fs::read_to_string(&name).into_diagnostic()?)); println!( - "{:>12} {}", + "{:>12} target {}", "Assembling".green().bold(), name.to_str().unwrap() ); @@ -72,16 +77,12 @@ fn main() -> miette::Result<()> { let mut air = parser.parse()?; air.backpatch()?; // Run file - println!( - "{:>12} {}", - "Running".green().bold(), - name.to_str().unwrap() - ); + println!("{:>12} binary", "Running".green().bold()); let mut program = RunState::try_from(air)?; program.run(); println!( - "{:>12} {}", - "Finished".green().bold(), + "{:>12} target {}", + "Completed".green().bold(), name.to_str().unwrap() ); Ok(()) @@ -91,7 +92,7 @@ fn main() -> miette::Result<()> { let contents: &'static str = Box::leak(Box::new(fs::read_to_string(&name).into_diagnostic()?)); println!( - "{:>12} {}", + "{:>12} target {}", "Assembling".green().bold(), name.to_str().unwrap() ); @@ -114,11 +115,7 @@ fn main() -> miette::Result<()> { for i in 0..air.len() { file.write(&air.get(i).emit()?.to_be_bytes()); } - println!( - "{:>12} {}", - "Finished".green().bold(), - name.to_str().unwrap() - ); + println!("{:>12} binary", "Finished".green().bold(),); println!( "{:>12} {}", "Saved to".green().bold(), @@ -126,6 +123,24 @@ fn main() -> miette::Result<()> { ); Ok(()) } + Command::Check { name } => { + let contents: &'static str = + Box::leak(Box::new(fs::read_to_string(&name).into_diagnostic()?)); + println!( + "{:>12} target {}", + "Checking".green().bold(), + name.to_str().unwrap() + ); + // Process asm + let parser = lace::AsmParser::new(&contents)?; + let mut air = parser.parse()?; + air.backpatch()?; + for stmt in air { + let _ = stmt.emit()?; + } + println!("{:>12} with 0 errors", "Finished".green().bold(),); + Ok(()) + } Command::Clean { name } => todo!(), Command::Watch { name } => todo!(), Command::Fmt { name } => todo!(), diff --git a/src/parser.rs b/src/parser.rs index 0cd0aa1..8d93bc1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -313,6 +313,8 @@ impl AsmParser { TrapKind::In => 0x23, TrapKind::Putsp => 0x24, TrapKind::Halt => 0x25, + TrapKind::Putn => 0x26, + TrapKind::Reg => 0x27, } as u8; Ok(AirStmt::Trap { trap_vect }) diff --git a/src/runtime.rs b/src/runtime.rs index f609f24..2e2cb0e 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -143,7 +143,7 @@ impl RunState { // imm Self::s_ext(instr, 5) }; - let res = val1 + val2; + let res = val1.wrapping_add(val2); self.set_flags(res); *self.reg(dr) = res; } @@ -161,7 +161,7 @@ impl RunState { // imm Self::s_ext(instr, 5) }; - let res = val1 + val2; + let res = val1 & val2; self.set_flags(res); *self.reg(dr) = res; } @@ -192,14 +192,14 @@ impl RunState { fn ld(&mut self, instr: u16) { let dr = (instr >> 9) & 0b111; - let val = *self.mem(self.pc + Self::s_ext(instr, 9)); + let val = *self.mem(self.pc.wrapping_add(Self::s_ext(instr, 9))); *self.reg(dr) = val; self.set_flags(val); } fn ldi(&mut self, instr: u16) { let dr = (instr >> 9) & 0b111; - let ptr = *self.mem(self.pc + Self::s_ext(instr, 9)); + let ptr = *self.mem(self.pc.wrapping_add(Self::s_ext(instr, 9))); let val = *self.mem(ptr); *self.reg(dr) = val; self.set_flags(val); @@ -209,14 +209,14 @@ impl RunState { let dr = (instr >> 9) & 0b111; let br = (instr >> 6) & 0b111; let ptr = *self.reg(br); - let val = *self.mem(ptr + Self::s_ext(instr, 6)); + let val = *self.mem(ptr.wrapping_add(Self::s_ext(instr, 6))); *self.reg(dr) = val; self.set_flags(val); } fn lea(&mut self, instr: u16) { let dr = (instr >> 9) & 0b111; - let val = self.pc + Self::s_ext(instr, 9); + let val = self.pc.wrapping_add(Self::s_ext(instr, 9)); *self.reg(dr) = val; self.set_flags(val); } @@ -236,13 +236,13 @@ impl RunState { fn st(&mut self, instr: u16) { let sr = (instr >> 9) & 0b111; let val = *self.reg(sr); - *self.mem(self.pc + Self::s_ext(instr, 9)) = val; + *self.mem(self.pc.wrapping_add(Self::s_ext(instr, 9))) = val; } fn sti(&mut self, instr: u16) { let sr = (instr >> 9) & 0b111; let val = *self.reg(sr); - let ptr = *self.mem(self.pc + Self::s_ext(instr, 9)); + let ptr = *self.mem(self.pc.wrapping_add(Self::s_ext(instr, 9))); *self.mem(ptr) = val; } @@ -250,7 +250,7 @@ impl RunState { let sr = (instr >> 9) & 0b111; let br = (instr >> 6) & 0b111; let ptr = *self.reg(br); - *self.mem(ptr + Self::s_ext(instr, 6)); + *self.mem(ptr.wrapping_add(Self::s_ext(instr, 6))); } fn trap(&mut self, instr: u16) { @@ -297,6 +297,20 @@ impl RunState { self.pc = u16::MAX; println!("\n{:>12}", "Halted".cyan()); } + // putn + 0x26 => { + let val = *self.reg(0); + println!("{val}"); + } + // reg + 0x27 => { + println!("\n------ Registers ------"); + for (i, reg) in self.reg.iter().enumerate() { + println!("r{i}: {reg:.>#19}"); + // println!("r{i}: {reg:.>#19b}"); + } + println!("-----------------------"); + } // unknown _ => panic!("You called a trap with an unknown vector of {}", trap_vect), } diff --git a/src/symbol.rs b/src/symbol.rs index f5d616b..a208517 100644 --- a/src/symbol.rs +++ b/src/symbol.rs @@ -239,6 +239,8 @@ pub enum TrapKind { Puts, Out, Getc, + Putn, + Reg, } #[derive(Clone, Copy, PartialEq, Eq, Debug)] From f3258f51833fb16e5e22ec256fecd7e0c52d6549 Mon Sep 17 00:00:00 2001 From: Artemis Rosman <73006620+rozukke@users.noreply.github.com> Date: Mon, 16 Sep 2024 00:57:33 +1000 Subject: [PATCH 2/2] Update README.md with install instructions --- README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 000e6df..85b6031 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,51 @@ [![Creator rozukke](https://img.shields.io/badge/Creator-rozukke-f497af.svg)](https://github.com/rozukke) [![Made with Rust](https://img.shields.io/badge/Made%20with-Rust-b7410e.svg)](https://www.rust-lang.org) -`lace` is an all-in-one **LC3** (Little Computer 3) assembly toolchain. It will support fancy convenience features like `watch`ing, -interpreting files directly from `.asm`, a formatter and (probably) running with a copy of the original OS. +`lace` is an all-in-one **LC3** (Little Computer 3) assembly toolchain. `lace` currently supports compiling, checking and +running LC3 assembly files. It will support fancy convenience features like `watch`ing, a formatter and (probably) running +with a copy of the original OS. **Some features are missing!** -## Current features -- It assembles! The `compile` command is minimally functional. You may need an LC3 VM in order to run the resulting file. +## Commands +- `run`: assemble and run a file - all in one command. +- `compile`: creates a binary file with a `.lc3` extension. Note that `lace` cannot currently run these files, so you may need +a different LC3 virtual machine to test them out until that is implemented. +- `check`: verifies that your file is correct without outputting it or running it. +- `watch`: **(work in progress)** runs `check` for a specified file while you develop. Neat! +- `fmt`: **(planned)** formats your `.asm` file to fit my arbitrary style guide. +- `clean`: **(planned)** used to clean debug artifacts that will be implemented in the future. -## MacOS/Linux -TODO: Proper installation instructions -For now, please use `cargo` to get set up. Many features are missing, so watch out! +## Traps +There are a few extra traps that should make debugging a lot nicer! Please note that they will not perform as expected when you run +your binaries with other virtual machines. +- `putn`: print the contents of `r0` to console. This isn't supposed to be easy, and you should probably learn why! +- `reg`: print the contents of every register to console. Currently only supports u16 formatting. -## Windows -TODO: figure out how Windows would work at all +## Work in progress +There are several features and fixes under development: +- Input traps +- Showing multiple errors per compilation +- Watching a file +- File formatting +- Debug symbols +- A step-through debugger (big one!) +Check the repo for updates as it is under active development. + +## Installation +For now, please use `cargo` to get set up, as there are no official releases. Check in often for the latest updates! If you are +unsure what `cargo` is, check out [this help page](https://doc.rust-lang.org/cargo/getting-started/installation.html). +To install, follow the steps below: +```sh +git clone https://github.com/rozukke/lace.git +cd lace +cargo install --path . +``` +You should, as a result, have the `lace` binary available in your PATH. + +## License +Copyright (c) 2024 Artemis Rosman + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.