From f493cc102a65cec455598c3a5503aebba4f18896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 25 Sep 2020 22:52:02 +0200 Subject: [PATCH] add "_" pattern for explicitly specifying the "discard" destination (may be used for side effects) --- asm/src/data/mod.rs | 9 ++++++++- asm/src/error.rs | 2 ++ asm/src/parse/parse_data.rs | 4 ++++ crsn/src/main.rs | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/asm/src/data/mod.rs b/asm/src/data/mod.rs index 15b2d6c..84df646 100644 --- a/asm/src/data/mod.rs +++ b/asm/src/data/mod.rs @@ -26,6 +26,8 @@ pub enum DataDisp { Register(Register), /// Pointer into memory, stored in a numbered register RegisterPtr(Register), + /// Discard the written value + Discard, } impl Display for DataDisp { @@ -43,6 +45,9 @@ impl Display for DataDisp { DataDisp::RegisterPtr(r) => { write!(f, "@{}", r) } + DataDisp::Discard => { + write!(f, "_") + } } } } @@ -64,7 +69,7 @@ impl From for DataDisp { DstDisp::ImmediatePtr(x) => DataDisp::ImmediatePtr(x), DstDisp::Register(x) => DataDisp::Register(x), DstDisp::RegisterPtr(x) => DataDisp::RegisterPtr(x), - DstDisp::Discard => DataDisp::Immediate(0), + DstDisp::Discard => DataDisp::Discard, } } } @@ -91,6 +96,7 @@ impl TryFrom for SrcDisp { DataDisp::ImmediatePtr(x) => Ok(SrcDisp::ImmediatePtr(x)), DataDisp::Register(x) => Ok(SrcDisp::Register(x)), DataDisp::RegisterPtr(x) => Ok(SrcDisp::RegisterPtr(x)), + DataDisp::Discard => Err(AsmError::DiscardAsValue), } } } @@ -128,6 +134,7 @@ impl TryFrom for DstDisp { DataDisp::ImmediatePtr(x) => Ok(DstDisp::ImmediatePtr(x)), DataDisp::Register(x) => Ok(DstDisp::Register(x)), DataDisp::RegisterPtr(x) => Ok(DstDisp::RegisterPtr(x)), + DataDisp::Discard => Ok(DstDisp::Discard), } } } diff --git a/asm/src/error.rs b/asm/src/error.rs index bee9bc4..9f66cd6 100644 --- a/asm/src/error.rs +++ b/asm/src/error.rs @@ -33,6 +33,8 @@ pub enum AsmError { UnevenOperandSize(Mask, Mask), #[error("Value provided as output argument")] ValueAsOutput, + #[error("Discard (_) provided as input argument")] + DiscardAsValue, #[error("Conditional branch already defined for \"{0}\"")] ConditionalAlreadyUsed(Cond), #[error("Label \"{0:?}\" not defined")] diff --git a/asm/src/parse/parse_data.rs b/asm/src/parse/parse_data.rs index 6c443cc..1673cd7 100644 --- a/asm/src/parse/parse_data.rs +++ b/asm/src/parse/parse_data.rs @@ -26,6 +26,10 @@ pub fn parse_data_disp(tok: Option) -> Result { Ok(DataDisp::Immediate(unsafe { std::mem::transmute(*val) })) }, Sexp::Atom(Atom::S(s)) => { + if s == "_" { + return Ok(DataDisp::Discard); + } + if let Some(reference) = s.strip_prefix('@') { if reference.starts_with(|c : char| c.is_ascii_digit()) { let val : u64 = parse_u64(reference)?; diff --git a/crsn/src/main.rs b/crsn/src/main.rs index 4293ee5..2ad8d9b 100644 --- a/crsn/src/main.rs +++ b/crsn/src/main.rs @@ -43,6 +43,7 @@ fn main() { let program = " ( (main + (ld _ arg0) (call fibo 5) (ld r0 res0) )