From 79d5aa3cd51bad333bbffa44033f1387d2b18151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 28 Sep 2020 13:55:56 +0200 Subject: [PATCH] renaming, cleanup, fmt, move "extension module" traits and structs into a separate mod --- Cargo.lock | 108 +++++++++++++-------------- crsn/src/asm/data/mod.rs | 1 - crsn/src/asm/instr/op.rs | 50 +------------ crsn/src/asm/mod.rs | 3 +- crsn/src/asm/parse/mod.rs | 2 +- crsn/src/asm/parse/parse_cond.rs | 2 +- crsn/src/asm/parse/parse_data.rs | 2 +- crsn/src/asm/parse/parse_instr.rs | 2 +- crsn/src/asm/parse/parse_op.rs | 10 +-- crsn/src/asm/parse/parse_routines.rs | 2 +- crsn/src/builtin/defs.rs | 4 +- crsn/src/builtin/exec.rs | 15 ++-- crsn/src/builtin/parse.rs | 22 +++--- crsn/src/lib.rs | 1 + crsn/src/module/eval_res.rs | 18 +++++ crsn/src/module/mod.rs | 36 +++++++++ crsn/src/runtime/exec.rs | 6 +- crsn/src/runtime/frame.rs | 3 - crsn/src/runtime/frame/status.rs | 21 +++--- crsn/src/runtime/program.rs | 2 +- crsn/src/runtime/run_thread.rs | 13 ++-- crsn/src/runtime/run_thread/state.rs | 20 +++-- crsn_arith/src/exec.rs | 2 +- crsn_arith/src/parse.rs | 8 +- crsn_stacks/src/exec.rs | 4 +- crsn_stacks/src/parse.rs | 9 +-- launcher/src/main.rs | 48 ++++++------ 27 files changed, 205 insertions(+), 209 deletions(-) create mode 100644 crsn/src/module/eval_res.rs create mode 100644 crsn/src/module/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f7f6015..380662f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,9 +12,9 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", - "libc", - "winapi", + "hermit-abi", + "libc", + "winapi", ] [[package]] @@ -35,9 +35,9 @@ version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b" dependencies = [ - "num-integer", - "num-traits", - "time", + "num-integer", + "num-traits", + "time", ] [[package]] @@ -46,36 +46,36 @@ version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59" dependencies = [ - "atty", - "lazy_static", - "winapi", + "atty", + "lazy_static", + "winapi", ] [[package]] name = "crsn" version = "0.1.0" dependencies = [ - "anyhow", - "dyn-clonable", - "log", - "num-traits", - "sexp", - "thiserror", + "anyhow", + "dyn-clonable", + "log", + "num-traits", + "sexp", + "thiserror", ] [[package]] name = "crsn_arith" version = "0.1.0" dependencies = [ - "crsn", - "num-traits", + "crsn", + "num-traits", ] [[package]] name = "crsn_stacks" version = "0.1.0" dependencies = [ - "crsn", + "crsn", ] [[package]] @@ -84,8 +84,8 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e9232f0e607a262ceb9bd5141a3dfb3e4db6994b31989bbfd845878cba59fd4" dependencies = [ - "dyn-clonable-impl", - "dyn-clone", + "dyn-clonable-impl", + "dyn-clone", ] [[package]] @@ -94,9 +94,9 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "558e40ea573c374cf53507fd240b7ee2f5477df7cfebdb97323ec61c719399c5" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -111,20 +111,20 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c30f6d0bc6b00693347368a67d41b58f2fb851215ff1da49e90fe2c5c667151" dependencies = [ - "libc", + "libc", ] [[package]] name = "launcher" version = "0.1.0" dependencies = [ - "anyhow", - "crsn", - "crsn_arith", - "crsn_stacks", - "log", - "simple_logger", - "thiserror", + "anyhow", + "crsn", + "crsn_arith", + "crsn_stacks", + "log", + "simple_logger", + "thiserror", ] [[package]] @@ -145,7 +145,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if", ] [[package]] @@ -154,8 +154,8 @@ version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" dependencies = [ - "autocfg", - "num-traits", + "autocfg", + "num-traits", ] [[package]] @@ -164,7 +164,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" dependencies = [ - "autocfg", + "autocfg", ] [[package]] @@ -173,7 +173,7 @@ version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c" dependencies = [ - "unicode-xid", + "unicode-xid", ] [[package]] @@ -182,7 +182,7 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" dependencies = [ - "proc-macro2", + "proc-macro2", ] [[package]] @@ -197,11 +197,11 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13a53ed2efd04911c8280f2da7bf9abd350c931b86bc7f9f2386fbafbf525ff9" dependencies = [ - "atty", - "chrono", - "colored", - "log", - "winapi", + "atty", + "chrono", + "colored", + "log", + "winapi", ] [[package]] @@ -210,9 +210,9 @@ version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b" dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -221,7 +221,7 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" dependencies = [ - "thiserror-impl", + "thiserror-impl", ] [[package]] @@ -230,9 +230,9 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -241,9 +241,9 @@ version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ - "libc", - "wasi", - "winapi", + "libc", + "wasi", + "winapi", ] [[package]] @@ -264,8 +264,8 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", ] [[package]] diff --git a/crsn/src/asm/data/mod.rs b/crsn/src/asm/data/mod.rs index 6fc4265..de5957f 100644 --- a/crsn/src/asm/data/mod.rs +++ b/crsn/src/asm/data/mod.rs @@ -2,7 +2,6 @@ use std::convert::TryFrom; use std::fmt::{Debug, Display, Formatter}; use std::fmt; -use literal::Addr; pub use mask::Mask; pub use reg::Register; diff --git a/crsn/src/asm/instr/op.rs b/crsn/src/asm/instr/op.rs index 0b56f95..b339be6 100644 --- a/crsn/src/asm/instr/op.rs +++ b/crsn/src/asm/instr/op.rs @@ -1,14 +1,8 @@ use std::fmt::Debug; - - -use crate::asm::error::Error; - -use crate::asm::parse::arg_parser::ArgParser; use crate::builtin::defs::BuiltinOp; +use crate::module::{EvalRes, OpTrait}; use crate::runtime::fault::Fault; - - use crate::runtime::run_thread::{state::RunState, ThreadInfo}; /// A higher level simple opration @@ -19,48 +13,8 @@ pub enum Op { Ext(Box), } -pub trait OpTrait: Debug + Send + Sync + 'static { - fn execute(&self, ti : &ThreadInfo, state: &mut RunState) -> Result; -} - -pub enum ParseOpResult { - Parsed(Op), - Unknown(ArgParser), -} - -pub trait AsmModule: Debug + Send + 'static { - /// Get name of the module - fn name(&self) -> &'static str; - - /// Parse an op. - /// If the keyword matches and the function decides to parse the instruction, it must consume - /// the argument list and either return Ok or Err. - /// - /// If the instruction keyword is not recognized, return Unknown with the unchanged argument list. - fn parse_op(&self, keyword: &str, arg_tokens: ArgParser) -> Result; -} - - -pub type CyclesSpent = usize; - -#[derive(Debug)] -pub struct EvalRes { - pub cycles: CyclesSpent, - pub advance: i64, -} - -impl Default for EvalRes { - fn default() -> Self { - Self { - cycles: 1, - advance: 1, - } - } -} - - impl OpTrait for Op { - fn execute(&self, ti : &ThreadInfo, state: &mut RunState) -> Result { + fn execute(&self, ti: &ThreadInfo, state: &mut RunState) -> Result { match self { Op::BuiltIn(op) => { op.execute(ti, state) diff --git a/crsn/src/asm/mod.rs b/crsn/src/asm/mod.rs index 0caf02b..5830a5c 100644 --- a/crsn/src/asm/mod.rs +++ b/crsn/src/asm/mod.rs @@ -1,7 +1,6 @@ use std::sync::Arc; - -use crate::asm::instr::op::AsmModule; +use crate::module::AsmModule; use crate::runtime::program::Program; pub mod data; diff --git a/crsn/src/asm/parse/mod.rs b/crsn/src/asm/parse/mod.rs index b74e38e..a3fa226 100644 --- a/crsn/src/asm/parse/mod.rs +++ b/crsn/src/asm/parse/mod.rs @@ -5,8 +5,8 @@ use parse_routines::parse_routines; use crate::asm::error::Error; use crate::asm::instr::{Flatten, Op, Routine}; -use crate::asm::instr::op::AsmModule; use crate::asm::parse::sexp_expect::expect_list; +use crate::module::AsmModule; mod parse_cond; mod parse_instr; diff --git a/crsn/src/asm/parse/parse_cond.rs b/crsn/src/asm/parse/parse_cond.rs index 5e968bd..9d6feca 100644 --- a/crsn/src/asm/parse/parse_cond.rs +++ b/crsn/src/asm/parse/parse_cond.rs @@ -2,10 +2,10 @@ use sexp::Sexp; use crate::asm::error::Error; use crate::asm::instr::{Cond, cond, Instr}; -use crate::asm::instr::op::AsmModule; use crate::asm::parse::parse_instr::parse_instructions; use crate::asm::parse::sexp_expect::{expect_list, expect_string_atom}; use crate::asm::patches::TryRemove; +use crate::module::AsmModule; pub fn parse_cond_branch(tok: Sexp, parsers: &[Box]) -> Result<(Cond, Vec), Error> { let mut list = expect_list(Some(tok), false)?; diff --git a/crsn/src/asm/parse/parse_data.rs b/crsn/src/asm/parse/parse_data.rs index 5cad326..1d83a05 100644 --- a/crsn/src/asm/parse/parse_data.rs +++ b/crsn/src/asm/parse/parse_data.rs @@ -3,7 +3,7 @@ use std::convert::TryFrom; use sexp::{Atom, Sexp}; use crate::asm::data::{DataDisp, DstDisp, Rd, reg, SrcDisp, Wr}; -use crate::asm::data::literal::{Addr, Label}; +use crate::asm::data::literal::Label; use crate::asm::error::Error; use crate::asm::parse::sexp_expect::expect_string_atom; diff --git a/crsn/src/asm/parse/parse_instr.rs b/crsn/src/asm/parse/parse_instr.rs index d2f2ae2..be18138 100644 --- a/crsn/src/asm/parse/parse_instr.rs +++ b/crsn/src/asm/parse/parse_instr.rs @@ -2,11 +2,11 @@ use sexp::Sexp; use crate::asm::error::Error; use crate::asm::instr::Instr; -use crate::asm::instr::op::AsmModule; use crate::asm::parse::arg_parser::ArgParser; use crate::asm::parse::parse_cond::parse_cond_branch; use crate::asm::parse::sexp_expect::{expect_list, expect_string_atom}; use crate::asm::patches::SexpIsA; +use crate::module::AsmModule; use super::parse_op::parse_op; diff --git a/crsn/src/asm/parse/parse_op.rs b/crsn/src/asm/parse/parse_op.rs index d211e70..250cd68 100644 --- a/crsn/src/asm/parse/parse_op.rs +++ b/crsn/src/asm/parse/parse_op.rs @@ -1,12 +1,8 @@ - use crate::asm::error::Error; use crate::asm::instr::Op; - -use crate::asm::instr::op::{AsmModule, ParseOpResult}; use crate::asm::parse::arg_parser::ArgParser; - - use crate::builtin::parse::BuiltinOpParser; +use crate::module::{AsmModule, ParseOpRes}; pub fn parse_op(keyword: &str, mut arg_tokens: ArgParser, parsers: &[Box]) -> Result { // Include built-in instructions @@ -14,8 +10,8 @@ pub fn parse_op(keyword: &str, mut arg_tokens: ArgParser, parsers: &[Box return Ok(op), - Ok(ParseOpResult::Unknown(to_reuse)) => { + Ok(ParseOpRes::Parsed(op)) => return Ok(op), + Ok(ParseOpRes::Unknown(to_reuse)) => { if to_reuse.parsing_started() { panic!("Module \"{}\" started parsing {}, but returned Unknown!", p.name(), keyword); } diff --git a/crsn/src/asm/parse/parse_routines.rs b/crsn/src/asm/parse/parse_routines.rs index 23d4ec8..5322019 100644 --- a/crsn/src/asm/parse/parse_routines.rs +++ b/crsn/src/asm/parse/parse_routines.rs @@ -2,11 +2,11 @@ use sexp::Sexp; use crate::asm::data::literal::RoutineName; use crate::asm::error::Error; -use crate::asm::instr::op::AsmModule; use crate::asm::instr::Routine; use crate::asm::parse::parse_instr::parse_instructions; use crate::asm::parse::sexp_expect::{expect_list, expect_string_atom}; use crate::asm::patches::TryRemove; +use crate::module::AsmModule; pub fn parse_routines(routines: Vec, parsers: &[Box]) -> Result, Error> { let mut parsed = vec![]; diff --git a/crsn/src/builtin/defs.rs b/crsn/src/builtin/defs.rs index b974808..cd05c45 100644 --- a/crsn/src/builtin/defs.rs +++ b/crsn/src/builtin/defs.rs @@ -1,6 +1,6 @@ -use crate::asm::data::literal::{Label, RoutineName, DebugMsg}; -use crate::asm::instr::{Cond, Op}; use crate::asm::data::{Rd, Wr}; +use crate::asm::data::literal::{DebugMsg, Label, RoutineName}; +use crate::asm::instr::{Cond, Op}; #[derive(Debug)] pub enum BuiltinOp { diff --git a/crsn/src/builtin/exec.rs b/crsn/src/builtin/exec.rs index f323a6a..3bb8d94 100644 --- a/crsn/src/builtin/exec.rs +++ b/crsn/src/builtin/exec.rs @@ -1,14 +1,9 @@ - - - - -use crate::asm::instr::op::{OpTrait, EvalRes}; -use crate::runtime::fault::Fault; -use crate::runtime::frame::{StackFrame}; - -use crate::builtin::defs::BuiltinOp; use crate::asm::data::literal::Addr; -use crate::runtime::run_thread::{ThreadInfo, state::RunState}; +use crate::builtin::defs::BuiltinOp; +use crate::module::{EvalRes, OpTrait}; +use crate::runtime::fault::Fault; +use crate::runtime::frame::StackFrame; +use crate::runtime::run_thread::{state::RunState, ThreadInfo}; impl OpTrait for BuiltinOp { fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result { diff --git a/crsn/src/builtin/parse.rs b/crsn/src/builtin/parse.rs index da63ec4..ee056b1 100644 --- a/crsn/src/builtin/parse.rs +++ b/crsn/src/builtin/parse.rs @@ -1,14 +1,14 @@ +use sexp::{Atom, Sexp}; -use crate::asm::error::{Error}; -use crate::asm::instr::op::{AsmModule, ParseOpResult}; -use crate::asm::parse::arg_parser::ArgParser; use crate::asm::data::literal::{Label, RoutineName}; -use crate::asm::parse::sexp_expect::expect_string_atom; -use crate::asm::parse::parse_data::{parse_label, parse_rd}; +use crate::asm::error::Error; use crate::asm::instr::cond::parse_cond; use crate::asm::instr::Op; +use crate::asm::parse::arg_parser::ArgParser; +use crate::asm::parse::parse_data::{parse_label, parse_rd}; +use crate::asm::parse::sexp_expect::expect_string_atom; use crate::builtin::defs::BuiltinOp; -use sexp::{Sexp, Atom}; +use crate::module::{AsmModule, ParseOpRes}; #[derive(Debug, Clone)] pub struct BuiltinOpParser { @@ -28,8 +28,8 @@ impl AsmModule for BuiltinOpParser { "builtin" } - fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { - Ok(ParseOpResult::Parsed(Op::BuiltIn(match keyword { + fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { + Ok(ParseOpRes::Parsed(Op::BuiltIn(match keyword { "nop" => { BuiltinOp::Nop } @@ -132,10 +132,10 @@ impl AsmModule for BuiltinOpParser { let label = Label::Named(label.to_string()); BuiltinOp::FarLabel(label) } else { - return Ok(ParseOpResult::Unknown(args)); + return Ok(ParseOpRes::Unknown(args)); } } else { - return Ok(ParseOpResult::Unknown(args)); + return Ok(ParseOpRes::Unknown(args)); } } @@ -144,7 +144,7 @@ impl AsmModule for BuiltinOpParser { let label = Label::Named(label.to_string()); BuiltinOp::Label(label) } else { - return Ok(ParseOpResult::Unknown(args)); + return Ok(ParseOpRes::Unknown(args)); } } }))) diff --git a/crsn/src/lib.rs b/crsn/src/lib.rs index 6b8b0e2..8d154c1 100644 --- a/crsn/src/lib.rs +++ b/crsn/src/lib.rs @@ -6,4 +6,5 @@ pub use sexp; pub mod asm; pub mod builtin; pub mod runtime; +pub mod module; diff --git a/crsn/src/module/eval_res.rs b/crsn/src/module/eval_res.rs new file mode 100644 index 0000000..514558f --- /dev/null +++ b/crsn/src/module/eval_res.rs @@ -0,0 +1,18 @@ +/// Cycles spent executing an instruction +pub type CyclesSpent = usize; + +/// Result of execution evaluation (when Fault is not generated) +#[derive(Debug)] +pub struct EvalRes { + pub cycles: CyclesSpent, + pub advance: i64, +} + +impl Default for EvalRes { + fn default() -> Self { + Self { + cycles: 1, + advance: 1, + } + } +} diff --git a/crsn/src/module/mod.rs b/crsn/src/module/mod.rs new file mode 100644 index 0000000..95932b5 --- /dev/null +++ b/crsn/src/module/mod.rs @@ -0,0 +1,36 @@ +use std::fmt::Debug; + +pub use eval_res::EvalRes; + +use crate::asm::error::Error; +use crate::asm::instr::Op; +use crate::asm::parse::arg_parser::ArgParser; +use crate::runtime::fault::Fault; +use crate::runtime::run_thread::state::RunState; +use crate::runtime::run_thread::ThreadInfo; + +mod eval_res; + +/// Result type returned from the op parser. This is the Ok variant of a Result. +pub enum ParseOpRes { + /// Parsing successful. + Parsed(Op), + /// Instruction not recognized, but there was no error. + Unknown(ArgParser), +} + +pub trait OpTrait: Debug + Send + Sync + 'static { + fn execute(&self, ti: &ThreadInfo, state: &mut RunState) -> Result; +} + +pub trait AsmModule: Debug + Send + 'static { + /// Get name of the module + fn name(&self) -> &'static str; + + /// Parse an op. + /// If the keyword matches and the function decides to parse the instruction, it must consume + /// the argument list and either return Ok or Err. + /// + /// If the instruction keyword is not recognized, return Unknown with the unchanged argument list. + fn parse_op(&self, keyword: &str, arg_tokens: ArgParser) -> Result; +} diff --git a/crsn/src/runtime/exec.rs b/crsn/src/runtime/exec.rs index 07b681b..3f83924 100644 --- a/crsn/src/runtime/exec.rs +++ b/crsn/src/runtime/exec.rs @@ -1,14 +1,10 @@ - - -use crate::asm::instr::op::{EvalRes, OpTrait}; +use crate::module::{EvalRes, OpTrait}; use crate::runtime::fault::Fault; - use crate::runtime::run_thread::RunThread; impl RunThread { // TODO unit tests pub fn eval_op(&mut self) -> Result { - let state = &mut self.state; let info = &self.info; diff --git a/crsn/src/runtime/frame.rs b/crsn/src/runtime/frame.rs index c462ada..8cf518f 100644 --- a/crsn/src/runtime/frame.rs +++ b/crsn/src/runtime/frame.rs @@ -1,10 +1,7 @@ use status::StatusFlags; -use crate::asm::data::{DstDisp, Rd, Register, SrcDisp, Wr}; use crate::asm::data::literal::{Addr, Value}; -use super::fault::Fault; - pub(crate) mod status; pub const REG_COUNT: usize = 8; diff --git a/crsn/src/runtime/frame/status.rs b/crsn/src/runtime/frame/status.rs index 61c7e36..3518ff0 100644 --- a/crsn/src/runtime/frame/status.rs +++ b/crsn/src/runtime/frame/status.rs @@ -90,35 +90,36 @@ impl StatusFlags { match cond { Cond::Equal => { self.equal = true; - }, + } Cond::Zero => { self.zero = true; - }, + } Cond::Lower => { self.lower = true; - }, + } Cond::Greater => { self.lower = false; self.equal = false; self.greater = true; - }, + } Cond::Positive => { self.positive = true; - }, + } Cond::Negative => { self.negative = true; - }, + } Cond::Overflow => { self.overflow = true; - }, + } Cond::Invalid => { self.invalid = true; - }, + } Cond::Carry => { self.carry = true; - }, + } other => { - panic!("cannot set cond by {:?}", other); + error!("Cannot set cond by {:?}", other); + // ...and do nothing. Don't panic, we don't want to crash the runtime! } } } diff --git a/crsn/src/runtime/program.rs b/crsn/src/runtime/program.rs index 09e7bb5..a344541 100644 --- a/crsn/src/runtime/program.rs +++ b/crsn/src/runtime/program.rs @@ -3,8 +3,8 @@ use std::sync::Arc; use crate::asm::data::literal::{Addr, Label, RoutineName}; use crate::asm::instr::Op; -use crate::runtime::fault::Fault; use crate::builtin::defs::BuiltinOp; +use crate::runtime::fault::Fault; #[derive(Debug)] pub struct Program { diff --git a/crsn/src/runtime/run_thread.rs b/crsn/src/runtime/run_thread.rs index ba2297e..e435095 100644 --- a/crsn/src/runtime/run_thread.rs +++ b/crsn/src/runtime/run_thread.rs @@ -1,15 +1,12 @@ -use std::any::{Any, TypeId}; -use std::collections::HashMap; use std::sync::Arc; use std::thread::JoinHandle; use std::time::Duration; -use crate::asm::data::literal::{Addr, Value}; -use crate::asm::instr::op::EvalRes; -use crate::runtime::frame::{CallStack, StackFrame, REG_COUNT, status::StatusFlags}; -use crate::runtime::program::Program; +use crate::asm::data::literal::Addr; +use crate::module::EvalRes; use crate::runtime::fault::Fault; -use crate::asm::data::{Rd, SrcDisp, Register, Wr, DstDisp}; +use crate::runtime::frame::StackFrame; +use crate::runtime::program::Program; use crate::runtime::run_thread::state::RunState; #[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)] @@ -47,7 +44,7 @@ impl RunThread { } } - pub fn set_speed(&mut self, cycle_time : Duration) { + pub fn set_speed(&mut self, cycle_time: Duration) { self.info.cycle_time = cycle_time; } diff --git a/crsn/src/runtime/run_thread/state.rs b/crsn/src/runtime/run_thread/state.rs index 61750d7..d3a72fb 100644 --- a/crsn/src/runtime/run_thread/state.rs +++ b/crsn/src/runtime/run_thread/state.rs @@ -3,12 +3,9 @@ use std::collections::HashMap; use crate::asm::data::{DstDisp, Rd, Register, SrcDisp, Wr}; use crate::asm::data::literal::{Addr, Value}; -use crate::asm::instr::op::EvalRes; +use crate::asm::instr::Cond; use crate::runtime::fault::Fault; use crate::runtime::frame::{CallStack, REG_COUNT, StackFrame}; -use crate::runtime::program::Program; -use crate::asm::instr::Cond; -use crate::runtime::frame::status::StatusFlags; pub struct RunState { /// Active stack frame @@ -20,6 +17,8 @@ pub struct RunState { } impl RunState { + /// Get an extension's persistent data object for mutation. + /// It will be created using Default if not present before. pub fn ext_mut(&mut self) -> &mut T { if !self.ext_data.contains_key(&TypeId::of::()) { self.ext_data.insert(TypeId::of::(), Box::new(T::default())); @@ -29,32 +28,40 @@ impl RunState { .downcast_mut().unwrap() } - pub fn set_flag(&mut self, cond : Cond, set : bool) { + /// Set a status flag. Only supports simple, positive conds (i.e. not GreaterOrEqual) + pub fn set_flag(&mut self, cond: Cond, set: bool) { if set { self.frame.status.set(cond); } } - pub fn test_cond(&self, cond : Cond) -> bool { + /// Check status flags for a condition + pub fn test_cond(&self, cond: Cond) -> bool { self.frame.status.test(cond) } + /// Set program counter - address of the next instruction to run pub fn set_pc(&mut self, pc: Addr) { self.frame.pc = pc; } + /// Get program counter - address of the next instruction to run pub fn get_pc(&self) -> Addr { self.frame.pc } + /// Clear status flags pub fn clear_status(&mut self) { self.frame.status.clear(); } + /// Update status flags using a variable. + /// The update is additive - call `clear_status()` first if desired! pub fn update_status(&mut self, val: Value) { self.frame.status.update(val); } + /// Read a `Rd` value pub fn read(&mut self, rd: Rd) -> Result { match rd.d() { SrcDisp::Immediate(v) => Ok(v), @@ -88,6 +95,7 @@ impl RunState { } } + /// Write a value to a `Wr` location pub fn write(&mut self, wr: Wr, val: Value) -> Result<(), Fault> { trace!("WR {:?} := {}", wr, val); diff --git a/crsn_arith/src/exec.rs b/crsn_arith/src/exec.rs index d22c139..30d61f2 100644 --- a/crsn_arith/src/exec.rs +++ b/crsn_arith/src/exec.rs @@ -3,7 +3,7 @@ use std::ops::Rem; use num_traits::PrimInt; use crsn::asm::instr::Cond; -use crsn::asm::instr::op::{EvalRes, OpTrait}; +use crsn::module::{EvalRes, OpTrait}; use crsn::runtime::fault::Fault; use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; diff --git a/crsn_arith/src/parse.rs b/crsn_arith/src/parse.rs index 0381f4a..4c9049c 100644 --- a/crsn_arith/src/parse.rs +++ b/crsn_arith/src/parse.rs @@ -1,8 +1,8 @@ use crsn::asm::data::{Rd, Wr}; use crsn::asm::error::Error; -use crsn::asm::instr::op::{AsmModule, ParseOpResult}; use crsn::asm::instr::Op; use crsn::asm::parse::arg_parser::ArgParser; +use crsn::module::{AsmModule, ParseOpRes}; use crate::defs::ArithOp; @@ -24,8 +24,8 @@ impl AsmModule for ArithOps { "arith" } - fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { - Ok(ParseOpResult::Parsed(Op::Ext(Box::new(match keyword { + fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { + Ok(ParseOpRes::Parsed(Op::Ext(Box::new(match keyword { "cmp" => { ArithOp::Compare { a: args.next_rd()?, @@ -448,7 +448,7 @@ impl AsmModule for ArithOps { } _other => { - return Ok(ParseOpResult::Unknown(args)); + return Ok(ParseOpRes::Unknown(args)); } })))) } diff --git a/crsn_stacks/src/exec.rs b/crsn_stacks/src/exec.rs index f44a2fa..3283a4a 100644 --- a/crsn_stacks/src/exec.rs +++ b/crsn_stacks/src/exec.rs @@ -1,12 +1,12 @@ use std::collections::VecDeque; use crsn::asm::data::literal::Value; -use crsn::asm::instr::op::{EvalRes, OpTrait}; +use crsn::asm::instr::Cond; +use crsn::module::{EvalRes, OpTrait}; use crsn::runtime::fault::Fault; use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; use crate::defs::StackOp; -use crsn::asm::instr::Cond; struct Stacks { stacks: Vec>, diff --git a/crsn_stacks/src/parse.rs b/crsn_stacks/src/parse.rs index c66be11..4ef4a0d 100644 --- a/crsn_stacks/src/parse.rs +++ b/crsn_stacks/src/parse.rs @@ -1,8 +1,7 @@ - use crsn::asm::error::Error; -use crsn::asm::instr::op::{AsmModule, ParseOpResult}; use crsn::asm::instr::Op; use crsn::asm::parse::arg_parser::ArgParser; +use crsn::module::{AsmModule, ParseOpRes}; use crate::defs::StackOp; @@ -24,8 +23,8 @@ impl AsmModule for StackOps { "stacks" } - fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { - Ok(ParseOpResult::Parsed(Op::Ext(Box::new(match keyword { + fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { + Ok(ParseOpRes::Parsed(Op::Ext(Box::new(match keyword { "push" => { StackOp::Push { num: args.next_rd()?, @@ -41,7 +40,7 @@ impl AsmModule for StackOps { } _other => { - return Ok(ParseOpResult::Unknown(args)); + return Ok(ParseOpRes::Unknown(args)); } })))) } diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 3ae587c..d9db713 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -1,14 +1,14 @@ #[macro_use] extern crate log; +use std::time::Duration; + use simple_logger::SimpleLogger; use crsn::asm::data::literal::Addr; - use crsn::runtime::run_thread::{RunThread, ThreadToken}; use crsn_arith::ArithOps; use crsn_stacks::StackOps; -use std::time::Duration; fn main() { SimpleLogger::new().init().unwrap(); @@ -44,28 +44,28 @@ fn main() { ) ";*/ -/* let program = " - ( - (main - (j :lbl) - (ld _ arg0) - (fault) - (:lbl) - (call fac 5) - (ld r0 res0) - ) - (fac - (cmp arg0 2 - (eq? (ret 2))) - (ld r0 arg0) - (dec r0) - (call fac r0) - (ld r0 arg0) - (mul r0 res0) - (ret r0) + /* let program = " + ( + (main + (j :lbl) + (ld _ arg0) + (fault) + (:lbl) + (call fac 5) + (ld r0 res0) + ) + (fac + (cmp arg0 2 + (eq? (ret 2))) + (ld r0 arg0) + (dec r0) + (call fac r0) + (ld r0 arg0) + (mul r0 res0) + (ret r0) + ) ) - ) - ";*/ + ";*/ let program = " ( @@ -97,7 +97,7 @@ fn main() { let mut thread1 = RunThread::new(ThreadToken(0), parsed.clone(), Addr(0), &[]); thread1.set_speed(Duration::from_millis(250)); - let thread2 = RunThread::new(ThreadToken(1), parsed.clone(), Addr(0), &[]); + let _thread2 = RunThread::new(ThreadToken(1), parsed.clone(), Addr(0), &[]); let a = thread1.start(); //let b = thread2.start();