Skip to main content

i8051_disassembler/commands/
auto_disassemble.rs

1use crate::address::SpaceAddressValue;
2use crate::db::{Db, Error, ErrorKind};
3
4use super::{Apply, Command, Environment, boxed};
5
6#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
7pub struct AutoDisassemble {
8    pub address: SpaceAddressValue,
9}
10
11register!(AutoDisassemble(
12    /// Recursively disassemble from `address`, following control flow. Recorded
13    /// as a root, so the saved DB regenerates the code from this one command.
14    address: SpaceAddressValue,
15));
16
17impl Apply for AutoDisassemble {
18    fn apply(
19        self,
20        db: &mut Db,
21        _env: Option<&dyn Environment>,
22    ) -> Result<Vec<Box<dyn Command>>, Error> {
23        if db.platform().is_none() {
24            return Err(ErrorKind::NoCpu.into());
25        }
26        let Self { address } = self;
27        let SpaceAddressValue {
28            space,
29            offset: start,
30        } = address;
31        let region = db.region_mut(space);
32        let was_root = region.is_auto_root(start);
33        region.add_auto_root(start);
34
35        // Only undo the root if this call introduced it (idempotent re-runs).
36        let mut undo: Vec<Box<dyn Command>> = Vec::new();
37        if !was_root {
38            undo.push(boxed(ClearAutoDisassembleRoot { address }));
39        }
40        Ok(undo)
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
45pub struct ClearAutoDisassembleRoot {
46    pub address: SpaceAddressValue,
47}
48
49register!(ClearAutoDisassembleRoot(
50    /// Remove an auto-disassemble root. Code reachable only through it
51    /// re-derives away (code still reached by another root stays). The inverse
52    /// of `auto_disassemble`.
53    address: SpaceAddressValue,
54));
55
56impl Apply for ClearAutoDisassembleRoot {
57    fn apply(
58        self,
59        db: &mut Db,
60        _env: Option<&dyn Environment>,
61    ) -> Result<Vec<Box<dyn Command>>, Error> {
62        let Self { address } = self;
63        let SpaceAddressValue { space, offset } = address;
64        let region = db.region_mut(space);
65        if region.remove_auto_root(offset) {
66            Ok(vec![boxed(AutoDisassemble { address })])
67        } else {
68            Ok(vec![])
69        }
70    }
71}