diff --git a/src/bin/chirp-disasm/main.rs b/src/bin/chirp-disasm/main.rs index 8327581..617d5e2 100644 --- a/src/bin/chirp-disasm/main.rs +++ b/src/bin/chirp-disasm/main.rs @@ -3,6 +3,8 @@ use gumdrop::*; use owo_colors::OwoColorize; use std::{fs::read, path::PathBuf}; +mod tree; + fn main() -> Result<()> { let options = Arguments::parse_args_default_or_exit(); let contents = &read(&options.file)?; diff --git a/src/bin/chirp-disasm/tree.rs b/src/bin/chirp-disasm/tree.rs new file mode 100644 index 0000000..bdac089 --- /dev/null +++ b/src/bin/chirp-disasm/tree.rs @@ -0,0 +1,49 @@ +#![allow(dead_code)] +#![allow(unused_variables)] +use std::collections::HashSet; + +use chirp::cpu::instruction::Insn; + +type Adr = usize; + +/// Represents the kinds of control flow an instruction can take +pub enum DisNode { + Branch { + addr: Adr, + insn: Insn, + a: Box, + b: Box, + }, + Continue { + addr: Adr, + insn: Insn, + next: Box, + }, + Merge { + addr: Adr, + insn: Insn, + }, + End(Insn), + Invalid, +} + +impl DisNode { + pub fn travel( + mem: &[u8], + visited: &mut HashSet, + current: Adr, + ) -> Result { + use DisNode::*; + + // decode an insn at the current Adr + // classify the insn + // If the instruction is invalid, emit an Invalid token + // If the instruction is already visited, emit a Merge token + // If the instruction is a ret instruction, emit a Merge token + // If the instruction is any other instruction, emit a Continue token + // If the instruction is a branch to current, emit an End token + // If the instruction is a branch instruction, recursively follow each branch + + Ok(End(Insn::cls)) + } +}