From c710e1b893f16b684d2ec8774267f7250c1c039c Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Mon, 13 Jan 2020 10:41:47 -0500 Subject: [PATCH] Add disassembler, update spec --- isa.txt | 3 +-- toolchain/src/bin/disassembler.rs | 14 ++++++++++++++ toolchain/src/lib.rs | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 toolchain/src/bin/disassembler.rs diff --git a/isa.txt b/isa.txt index 041ecb7..ac9e4ca 100644 --- a/isa.txt +++ b/isa.txt @@ -42,8 +42,7 @@ Special registers: pc, it (accumulator), data1 (segment), data2 (segment), code 1111_00oo iiiiiiii ALUI it <- it {andi, iori, xori, (see below)} i 0011 0iiiiiii ALUI it <- it + i (ADDI) - 0011 100ooiii ALUI it <- it {roli, lsli, lsri, asri} i - 0011 101xxxxx (probably permanently unused, but you could cram in more small ALU ops?) + 0011 10oooiii ALUI it <- it {roli, lsli, lsri, asri, clri, seti, togi, exti} i 0011 11iiiiii ALUI it <- it + (whole field, thus allowing many negative numbers) (ADDI) 0100 iiiiiiii BEZI branch ±i if it == 0 0101 iiiiiiii (reserved branch; SUB2, BLTI is no faster than ISLT, BEZI) diff --git a/toolchain/src/bin/disassembler.rs b/toolchain/src/bin/disassembler.rs new file mode 100644 index 0000000..b7d729e --- /dev/null +++ b/toolchain/src/bin/disassembler.rs @@ -0,0 +1,14 @@ +use std::io; +use toolchain::{inst::decode::Decode, Inst}; + +fn main() { + let stdin = io::stdin(); + let mut input = stdin.lock(); + loop { + match Inst::decode(&mut input) { + Ok(inst) => println!("{}", inst), + Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => break, + Err(err) => println!("Error: {}", err), + } + } +} diff --git a/toolchain/src/lib.rs b/toolchain/src/lib.rs index d4cc11e..67c875f 100644 --- a/toolchain/src/lib.rs +++ b/toolchain/src/lib.rs @@ -1 +1,16 @@ pub mod inst; + +pub use inst::{decode::Decode, Inst}; +use std::io; + +pub fn disassemble(reader: &mut impl io::Read) -> io::Result> { + let mut results = Vec::new(); + loop { + match Inst::decode(reader) { + Ok(inst) => results.push(inst), + Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => break, + Err(err) => return Err(err), + } + } + Ok(results) +} -- 2.47.0