From af228487f04acdb311f2d3d056ab406557cf570e Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Sat, 11 Jan 2020 02:42:32 -0500 Subject: [PATCH] Add Reg constructor --- toolchain/src/inst.rs | 47 ++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/toolchain/src/inst.rs b/toolchain/src/inst.rs index fea8ed6..ea3d2f4 100644 --- a/toolchain/src/inst.rs +++ b/toolchain/src/inst.rs @@ -1,9 +1,6 @@ pub mod decode; pub mod encode; -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Reg(u8); - #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Op1 { Zero, @@ -40,29 +37,13 @@ pub enum Special { Code, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Reg(U3); #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct U3(u8); #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct AddImm(i8); -impl U3 { - pub fn new(x: u8) -> Option { - match x { - 0..=7 => Some(U3(x)), - _ => None, - } - } -} - -impl AddImm { - pub fn new(x: i8) -> Option { - match x { - -64..=127 => Some(AddImm(x)), - _ => None, - } - } -} - #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum AluI { And(u8), @@ -97,3 +78,27 @@ pub enum Inst { JOfI(i8), COfI(i8), } + +impl Reg { + pub fn new(x: u8) -> Option { + U3::new(x).map(Reg) + } +} + +impl U3 { + pub fn new(x: u8) -> Option { + match x { + 0..=7 => Some(U3(x)), + _ => None, + } + } +} + +impl AddImm { + pub fn new(x: i8) -> Option { + match x { + -64..=127 => Some(AddImm(x)), + _ => None, + } + } +} -- 2.47.0