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

Add runtime #16

Merged
merged 2 commits into from
Sep 15, 2024
Merged
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
38 changes: 36 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fxhash = "0.2.1"

[dev-dependencies]
assert_cmd = "2.0.14"
predicates = "3.1.2"

[[bin]]
name = "lace"
Expand Down
26 changes: 26 additions & 0 deletions src/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ impl Air {
}
}

impl IntoIterator for Air {
type Item = AsmLine;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.ast.into_iter()
}
}

/// Single LC3 statement. Has optional labels.
#[derive(PartialEq, Eq, Debug)]
pub enum AirStmt {
Expand Down Expand Up @@ -106,6 +115,12 @@ pub enum AirStmt {
src_reg: Register,
dest_label: Label,
},
/// Store value in src_reg at dest_label + offset
StoreOffs {
src_reg: Register,
dest_reg: Register,
offset: u8,
},
/// A raw value created during preprocessing
RawWord { val: RawWord },
/// Jump to address at index trap_vect of the trap table
Expand Down Expand Up @@ -283,6 +298,17 @@ impl AsmLine {
raw |= self.bit_offs(dest_label, 9)?;
Ok(raw)
}
AirStmt::StoreOffs {
src_reg,
dest_reg,
offset,
} => {
let mut raw = 0x7000;
raw |= (*src_reg as u16) << 9;
raw |= (*dest_reg as u16) << 6;
raw |= *offset as u16;
Ok(raw)
}
AirStmt::RawWord { val: bytes } => Ok(bytes.0),
AirStmt::Trap { trap_vect } => Ok(0xF000 | *trap_vect as u16),
}
Expand Down
1 change: 1 addition & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ impl Cursor<'_> {
"rti" => Instr(Rti),
"st" => Instr(St),
"sti" => Instr(Sti),
"str" => Instr(Str),
_ => TokenKind::Label,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ pub use parser::AsmParser;

pub mod air;
pub use air::Air;
mod runtime;
pub use runtime::RunState;

mod error;
mod lexer;
mod runtime;
mod symbol;
56 changes: 41 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use clap::{Parser, Subcommand};
use colored::Colorize;
use miette::{GraphicalTheme, IntoDiagnostic, MietteHandlerOpts, Result};

use lace::AsmParser;
use lace::{AsmParser, RunState};

/// Lace is a complete & convenient assembler toolchain for the LC3 assembly language.
#[derive(Parser)]
Expand All @@ -19,18 +19,14 @@ struct Args {
#[command(subcommand)]
command: Option<Command>,

/// Run commands in debug mode
#[arg(long)]
debug: bool,
/// Quickly provide a `.asm` file to run
path: Option<PathBuf>,
}

#[derive(Subcommand)]
enum Command {
/// Run text `.asm` or binary `.lc3` file directly and output to terminal
Run {
/// Map the LC3 operating system ROM to memory to handle traps and interrupts
#[arg(short, long)]
os: bool,
/// .asm file to run
name: PathBuf,
},
Expand Down Expand Up @@ -63,7 +59,33 @@ fn main() -> miette::Result<()> {

if let Some(command) = args.command {
match command {
Command::Run { os, name } => todo!(),
Command::Run { name } => {
let contents: &'static str =
Box::leak(Box::new(fs::read_to_string(&name).into_diagnostic()?));
println!(
"{:>12} {}",
"Assembling".green().bold(),
name.to_str().unwrap()
);
// Process asm
let parser = lace::AsmParser::new(&contents)?;
let mut air = parser.parse()?;
air.backpatch()?;
// Run file
println!(
"{:>12} {}",
"Running".green().bold(),
name.to_str().unwrap()
);
let mut program = RunState::try_from(air)?;
program.run();
println!(
"{:>12} {}",
"Finished".green().bold(),
name.to_str().unwrap()
);
Ok(())
}
Command::Compile { name, dest } => {
// Available until end of program
let contents: &'static str =
Expand Down Expand Up @@ -109,10 +131,14 @@ fn main() -> miette::Result<()> {
Command::Fmt { name } => todo!(),
}
} else {
println!("\n~ lace v{VERSION} - Copyright (c) 2024 Artemis Rosman ~");
println!("{}", LOGO.truecolor(255, 183, 197).bold());
println!("{SHORT_INFO}");
std::process::exit(0);
if let Some(path) = args.path {
todo!("Should allow for running files with no subcommand")
} else {
println!("\n~ lace v{VERSION} - Copyright (c) 2024 Artemis Rosman ~");
println!("{}", LOGO.truecolor(255, 183, 197).bold());
println!("{SHORT_INFO}");
std::process::exit(0);
}
}
}

Expand All @@ -131,9 +157,9 @@ x .d88"
"% ^Y" ^Y' "P' "YP'"#;

const SHORT_INFO: &str = r"
Welcome to lace (from LAIS - LC3 Assembler & Interpreter System), an all-in-one toolchain
for working with LC3 assembly code. Please use `-h` or `--help` to access
the usage instructions and documentation.
Welcome to lace (from LAIS - LC3 Assembler & Interpreter System),
an all-in-one toolchain for working with LC3 assembly code.
Please use `-h` or `--help` to access the usage instructions and documentation.
";

const SHORT_HELP: &str = r"
Expand Down
10 changes: 10 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ impl AsmParser {
dest_label,
})
}
InstrKind::Str => {
let src_reg = self.expect_reg()?;
let dest_reg = self.expect_reg()?;
let offset = self.expect_lit(Bits::Signed(6))? as u8;
Ok(AirStmt::StoreOffs {
src_reg,
dest_reg,
offset,
})
}
}
}

Expand Down
Loading
Loading