summaryrefslogtreecommitdiff
path: root/src/locators/bytecode.rs
blob: 500e9f01ced8a8f41b68c2152e6ed0179b073dfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pub struct BytecodeSpan {
    /// The location of this span in the assembled bytecode.
    pub location: BytecodeLocation,
    /// The bytes which this span represents.
    pub bytes: Vec<u8>,
}


impl Default for BytecodeSpan {
    fn default() -> Self {
        Self {
            location: BytecodeLocation {
                address: 0,
                length: 0,
            },
            bytes: Vec::new(),
        }
    }
}


#[derive(Clone, Copy)]
pub struct BytecodeLocation {
    // Address of the first byte.
    pub address: usize,
    // Length as a number of bytes.
    pub length: usize,
}


impl std::fmt::Display for BytecodeLocation {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "0x{:>04x}", self.address)?;
        if self.length > 0 {
            write!(f, "-0x{:>04x}", self.address + self.length)?;
        }
        Ok(())
    }
}