From d8df7e1eac1617db066e5422bc24677f625572ca Mon Sep 17 00:00:00 2001 From: tali Date: Thu, 21 Dec 2023 18:30:41 -0500 Subject: [PATCH] rust fmt ... --- native/src/decode.rs | 4 ++-- native/src/main.rs | 8 +++++--- native/src/opcodes.rs | 7 +++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/native/src/decode.rs b/native/src/decode.rs index 250a12d..f9ef191 100644 --- a/native/src/decode.rs +++ b/native/src/decode.rs @@ -16,8 +16,8 @@ pub enum DecodeError { /// (in bytes). pub fn decode_one(bs: &[u8]) -> Result<(Ins, usize), DecodeError> { let byte0 = byte(bs, 0)?; - let len = (byte0 & 1) as usize * 2 + 2; + let ins = match byte0 & 0x3f { 0x00 => { let a = arg(byte(bs, 1)?, byte0); @@ -50,7 +50,7 @@ pub fn decode_one(bs: &[u8]) -> Result<(Ins, usize), DecodeError> { let opr = opr(byte(bs, 1)?)?; let a = Reg(byte(bs, 2)?); let b = arg(byte(bs, 3)?, byte0); - Ins::MOV(InsMov::Op(opr, a, b)) + Ins::Op(InsOp(opr, a, b)) } 0x17 => { let cnd = cnd(byte(bs, 1)?)?; diff --git a/native/src/main.rs b/native/src/main.rs index 29e5d38..421ccc2 100644 --- a/native/src/main.rs +++ b/native/src/main.rs @@ -16,9 +16,11 @@ fn entry() -> Result<(), Error> { BufReader::new(std::io::stdin()).read_to_end(&mut bytes)?; let mut decode = decode(&bytes); - while let Some(ins) = decode.next() { - let ins = ins.map_err(|err| Error::Decode(err, decode.position()))?; - println!("{ins}"); + loop { + let pos = decode.position(); + let Some(ins) = decode.next() else { break }; + let ins = ins.map_err(|err| Error::Decode(err, pos))?; + println!("{pos:>3} {ins}"); } Ok(()) diff --git a/native/src/opcodes.rs b/native/src/opcodes.rs index d27ef68..cffe704 100644 --- a/native/src/opcodes.rs +++ b/native/src/opcodes.rs @@ -56,6 +56,7 @@ pub enum Ins { LOC(InsLoc), CAL(InsCal), JMP(InsJmp), + Op(InsOp), Br(InsBr), RET(Arg), } @@ -67,7 +68,6 @@ impl fmt::Display for Ins { use InsMov::*; match *self { - MOV(Op(opr, a, b)) => write!(f, "{opr} {a}, {b}"), MOV(RV(a, b)) => write!(f, "mov {a}, {b}"), MOV(RL(a, b)) => write!(f, "mov {a}, {b}"), MOV(LV(a, b)) => write!(f, "mov {a}, {b}"), @@ -75,6 +75,7 @@ impl fmt::Display for Ins { CAL(InsCal(a, b, n)) => write!(f, "cal {a}, {b}({a},...{n})"), JMP(InsJmp(d)) => write!(f, "jmp {d:+}"), RET(v) => write!(f, "ret {v}"), + Op(InsOp(opr, a, b)) => write!(f, "{opr} {a}, {b}"), Br(BTR(v)) => write!(f, "btr {v}"), Br(BFL(v)) => write!(f, "btr {v}"), Br(BCnd(cnd, a, b)) => write!(f, "b{cnd} {a}, {b}"), @@ -84,7 +85,6 @@ impl fmt::Display for Ins { #[derive(Clone, Eq, PartialEq, Debug)] pub enum InsMov { - Op(Opr, Reg, Arg), RV(Reg, Arg), RL(Reg, Loc), LV(Loc, Arg), @@ -97,6 +97,9 @@ pub enum InsBr { BCnd(Cnd, Reg, Arg), } +#[derive(Clone, Eq, PartialEq, Debug)] +pub struct InsOp(pub Opr, pub Reg, pub Arg); + #[derive(Clone, Eq, PartialEq, Debug)] pub struct InsLoc(pub Reg, pub Reg, pub Cst);