rust fmt ...

This commit is contained in:
tali 2023-12-21 18:30:41 -05:00
parent d66b336435
commit d8df7e1eac
3 changed files with 12 additions and 7 deletions

View File

@ -16,8 +16,8 @@ pub enum DecodeError {
/// (in bytes). /// (in bytes).
pub fn decode_one(bs: &[u8]) -> Result<(Ins, usize), DecodeError> { pub fn decode_one(bs: &[u8]) -> Result<(Ins, usize), DecodeError> {
let byte0 = byte(bs, 0)?; let byte0 = byte(bs, 0)?;
let len = (byte0 & 1) as usize * 2 + 2; let len = (byte0 & 1) as usize * 2 + 2;
let ins = match byte0 & 0x3f { let ins = match byte0 & 0x3f {
0x00 => { 0x00 => {
let a = arg(byte(bs, 1)?, byte0); 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 opr = opr(byte(bs, 1)?)?;
let a = Reg(byte(bs, 2)?); let a = Reg(byte(bs, 2)?);
let b = arg(byte(bs, 3)?, byte0); let b = arg(byte(bs, 3)?, byte0);
Ins::MOV(InsMov::Op(opr, a, b)) Ins::Op(InsOp(opr, a, b))
} }
0x17 => { 0x17 => {
let cnd = cnd(byte(bs, 1)?)?; let cnd = cnd(byte(bs, 1)?)?;

View File

@ -16,9 +16,11 @@ fn entry() -> Result<(), Error> {
BufReader::new(std::io::stdin()).read_to_end(&mut bytes)?; BufReader::new(std::io::stdin()).read_to_end(&mut bytes)?;
let mut decode = decode(&bytes); let mut decode = decode(&bytes);
while let Some(ins) = decode.next() { loop {
let ins = ins.map_err(|err| Error::Decode(err, decode.position()))?; let pos = decode.position();
println!("{ins}"); let Some(ins) = decode.next() else { break };
let ins = ins.map_err(|err| Error::Decode(err, pos))?;
println!("{pos:>3} {ins}");
} }
Ok(()) Ok(())

View File

@ -56,6 +56,7 @@ pub enum Ins {
LOC(InsLoc), LOC(InsLoc),
CAL(InsCal), CAL(InsCal),
JMP(InsJmp), JMP(InsJmp),
Op(InsOp),
Br(InsBr), Br(InsBr),
RET(Arg), RET(Arg),
} }
@ -67,7 +68,6 @@ impl fmt::Display for Ins {
use InsMov::*; use InsMov::*;
match *self { match *self {
MOV(Op(opr, a, b)) => write!(f, "{opr} {a}, {b}"),
MOV(RV(a, b)) => write!(f, "mov {a}, {b}"), MOV(RV(a, b)) => write!(f, "mov {a}, {b}"),
MOV(RL(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}"), 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})"), CAL(InsCal(a, b, n)) => write!(f, "cal {a}, {b}({a},...{n})"),
JMP(InsJmp(d)) => write!(f, "jmp {d:+}"), JMP(InsJmp(d)) => write!(f, "jmp {d:+}"),
RET(v) => write!(f, "ret {v}"), RET(v) => write!(f, "ret {v}"),
Op(InsOp(opr, a, b)) => write!(f, "{opr} {a}, {b}"),
Br(BTR(v)) => write!(f, "btr {v}"), Br(BTR(v)) => write!(f, "btr {v}"),
Br(BFL(v)) => write!(f, "btr {v}"), Br(BFL(v)) => write!(f, "btr {v}"),
Br(BCnd(cnd, a, b)) => write!(f, "b{cnd} {a}, {b}"), 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)] #[derive(Clone, Eq, PartialEq, Debug)]
pub enum InsMov { pub enum InsMov {
Op(Opr, Reg, Arg),
RV(Reg, Arg), RV(Reg, Arg),
RL(Reg, Loc), RL(Reg, Loc),
LV(Loc, Arg), LV(Loc, Arg),
@ -97,6 +97,9 @@ pub enum InsBr {
BCnd(Cnd, Reg, Arg), BCnd(Cnd, Reg, Arg),
} }
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct InsOp(pub Opr, pub Reg, pub Arg);
#[derive(Clone, Eq, PartialEq, Debug)] #[derive(Clone, Eq, PartialEq, Debug)]
pub struct InsLoc(pub Reg, pub Reg, pub Cst); pub struct InsLoc(pub Reg, pub Reg, pub Cst);