comments, refactor

pull/21/head
Ondřej Hruška 4 years ago
parent 8f076e195a
commit 547beed847
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 94
      Cargo.lock
  2. 49
      asm/src/instr/cond.rs
  3. 2
      asm/src/instr/mod.rs
  4. 27
      asm/src/parse/parse_cond.rs
  5. 2
      asm/src/parse/parse_op.rs

94
Cargo.lock generated

@ -10,9 +10,9 @@ checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b"
name = "asm" name = "asm"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"sexp", "sexp",
"thiserror", "thiserror",
] ]
[[package]] [[package]]
@ -21,9 +21,9 @@ version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi",
"libc", "libc",
"winapi", "winapi",
] ]
[[package]] [[package]]
@ -44,9 +44,9 @@ version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b" checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
dependencies = [ dependencies = [
"num-integer", "num-integer",
"num-traits", "num-traits",
"time", "time",
] ]
[[package]] [[package]]
@ -55,21 +55,21 @@ version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59" checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59"
dependencies = [ dependencies = [
"atty", "atty",
"lazy_static", "lazy_static",
"winapi", "winapi",
] ]
[[package]] [[package]]
name = "crsn" name = "crsn"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"asm", "asm",
"log", "log",
"runtime", "runtime",
"simple_logger", "simple_logger",
"thiserror", "thiserror",
] ]
[[package]] [[package]]
@ -78,7 +78,7 @@ version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c30f6d0bc6b00693347368a67d41b58f2fb851215ff1da49e90fe2c5c667151" checksum = "4c30f6d0bc6b00693347368a67d41b58f2fb851215ff1da49e90fe2c5c667151"
dependencies = [ dependencies = [
"libc", "libc",
] ]
[[package]] [[package]]
@ -99,7 +99,7 @@ version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]] [[package]]
@ -108,8 +108,8 @@ version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"num-traits", "num-traits",
] ]
[[package]] [[package]]
@ -118,7 +118,7 @@ version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [ dependencies = [
"autocfg", "autocfg",
] ]
[[package]] [[package]]
@ -127,7 +127,7 @@ version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c" checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]] [[package]]
@ -136,18 +136,18 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]] [[package]]
name = "runtime" name = "runtime"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"asm", "asm",
"log", "log",
"num-traits", "num-traits",
"thiserror", "thiserror",
] ]
[[package]] [[package]]
@ -162,11 +162,11 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13a53ed2efd04911c8280f2da7bf9abd350c931b86bc7f9f2386fbafbf525ff9" checksum = "13a53ed2efd04911c8280f2da7bf9abd350c931b86bc7f9f2386fbafbf525ff9"
dependencies = [ dependencies = [
"atty", "atty",
"chrono", "chrono",
"colored", "colored",
"log", "log",
"winapi", "winapi",
] ]
[[package]] [[package]]
@ -175,9 +175,9 @@ version = "1.0.41"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b" checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"unicode-xid", "unicode-xid",
] ]
[[package]] [[package]]
@ -186,7 +186,7 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
@ -195,9 +195,9 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
] ]
[[package]] [[package]]
@ -206,9 +206,9 @@ version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [ dependencies = [
"libc", "libc",
"wasi", "wasi",
"winapi", "winapi",
] ]
[[package]] [[package]]
@ -229,8 +229,8 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [ dependencies = [
"winapi-i686-pc-windows-gnu", "winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu",
] ]
[[package]] [[package]]

@ -1,28 +1,75 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use std::ops::Not; use std::ops::Not;
use crate::error::Error;
/// Condition flag
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Cond { pub enum Cond {
/// Equality
Equal, Equal,
/// Inequality
NotEqual, NotEqual,
/// Equal to zero
Zero, Zero,
/// Not equal zero
NotZero, NotZero,
/// A < B
Lower, Lower,
/// A <= B
LowerOrEqual, LowerOrEqual,
/// A > B
Greater, Greater,
/// A >= B
GreaterOrEqual, GreaterOrEqual,
/// A > 0
Positive, Positive,
/// A <= 0
NonPositive, NonPositive,
/// A < 0
Negative, Negative,
/// A >= 0
NonNegative, NonNegative,
/// Arithmetic operation caused an integer operand to overflow
Overflow, Overflow,
/// No overflow
NotOverflow, NotOverflow,
/// Arithmetic (or other) operation was invalid.
/// Example: division by zero
Invalid, Invalid,
/// Operation was valid
Valid, Valid,
/// Arithmetic carry
Carry, Carry,
/// Carry not set
NotCarry, NotCarry,
} }
pub fn parse_cond(text: &str) -> Result<Cond, Error> {
Ok(match text.trim_end_matches('?') {
"eq" | "=" | "==" => Cond::Equal,
"ne" | "<>" | "!=" | "≠" => Cond::NotEqual,
"z" | "0" => Cond::Zero,
"nz" | "<>0" | "!0" => Cond::NotZero,
"lt" | "<" => Cond::Lower,
"le" | "<=" | "≤" => Cond::LowerOrEqual,
"gt" | ">" => Cond::Greater,
"ge" | ">=" | "≥" => Cond::GreaterOrEqual,
"pos" | "+" | ">0" => Cond::Positive,
"neg" | "-" | "<0" => Cond::Negative,
"npos" | "!+" | "0-" | "<=0" | "≥0" => Cond::NonPositive,
"nneg" | "!-" | "0+" | ">=0" | "≤0" => Cond::NonNegative,
"c" => Cond::Carry,
"nc" | "!c" => Cond::NotCarry,
"valid" => Cond::Valid,
"inval" => Cond::Invalid,
"ov" | "^" => Cond::Overflow,
"nov" | "!ov" | "!^" => Cond::NotOverflow,
_ => {
return Err(Error::Parse(format!("Unknown cond: {}", text).into()));
}
})
}
impl Display for Cond { impl Display for Cond {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self { f.write_str(match self {
@ -43,7 +90,7 @@ impl Display for Cond {
Cond::Carry => "c", Cond::Carry => "c",
Cond::NotCarry => "nc", Cond::NotCarry => "nc",
Cond::Invalid => "inval", Cond::Invalid => "inval",
Cond::Valid => "ok", Cond::Valid => "valid",
}) })
} }
} }

@ -7,7 +7,7 @@ pub use op::Op;
use crate::data::literal::RoutineName; use crate::data::literal::RoutineName;
mod op; mod op;
mod cond; pub mod cond;
mod flatten; mod flatten;
/// A higher-level instruction /// A higher-level instruction

@ -1,7 +1,7 @@
use sexp::Sexp; use sexp::Sexp;
use crate::error::Error; use crate::error::Error;
use crate::instr::{Cond, Instr}; use crate::instr::{Cond, Instr, cond};
use crate::parse::parse_instr::parse_instructions; use crate::parse::parse_instr::parse_instructions;
use crate::parse::sexp_expect::{expect_list, expect_string_atom}; use crate::parse::sexp_expect::{expect_list, expect_string_atom};
use crate::patches::TryRemove; use crate::patches::TryRemove;
@ -14,29 +14,6 @@ pub fn parse_cond_branch(tok: Sexp) -> Result<(Cond, Vec<Instr>), Error> {
return Err(Error::Parse(format!("Condition must end with '?': {}", kw).into())); return Err(Error::Parse(format!("Condition must end with '?': {}", kw).into()));
} }
Ok((parse_cond(&kw)?, parse_instructions(list)?)) Ok((cond::parse_cond(&kw)?, parse_instructions(list)?))
} }
pub fn parse_cond(text: &str) -> Result<Cond, Error> {
Ok(match text.trim_end_matches('?') {
"eq" | "=" | "==" => Cond::Equal,
"ne" | "<>" | "!=" | "≠" => Cond::NotEqual,
"z" | "0" => Cond::Zero,
"nz" | "<>0" | "!0" => Cond::NotZero,
"lt" | "<" => Cond::Lower,
"le" | "<=" | "≤" => Cond::LowerOrEqual,
"gt" | ">" => Cond::Greater,
"ge" | ">=" | "≥" => Cond::GreaterOrEqual,
"pos" | "+" | ">0" => Cond::Positive,
"neg" | "-" | "<0" => Cond::Negative,
"npos" | "!+" | "0-" | "<=0" | "≥0" => Cond::NonPositive,
"nneg" | "!-" | "0+" | ">=0" | "≤0" => Cond::NonNegative,
"ov" | "^" => Cond::Overflow,
"c" => Cond::Carry,
"nc" | "!c" => Cond::NotCarry,
"nov" | "!ov" | "!^" => Cond::NotOverflow,
_ => {
return Err(Error::Parse(format!("Unknown cond: {}", text).into()));
}
})
}

@ -4,7 +4,7 @@ use crate::data::{Rd, Wr};
use crate::data::literal::{Label, RoutineName}; use crate::data::literal::{Label, RoutineName};
use crate::error::Error; use crate::error::Error;
use crate::instr::{HLOp, Op}; use crate::instr::{HLOp, Op};
use crate::parse::parse_cond::parse_cond; use crate::instr::cond::parse_cond;
use crate::parse::parse_data::{parse_label, parse_rd, parse_wr}; use crate::parse::parse_data::{parse_label, parse_rd, parse_wr};
use crate::parse::sexp_expect::expect_string_atom; use crate::parse::sexp_expect::expect_string_atom;

Loading…
Cancel
Save