osom_asm_x86_64/assembler/
emission_data.rs

1use std::{collections::HashMap, mem::forget};
2
3use crate::models::Label;
4
5pub struct DeconstructedEmissionData {
6    pub emitted_bytes: usize,
7    pub labels_to_position_map: HashMap<Label, usize>,
8}
9
10#[derive(Debug)]
11pub struct EmissionData {
12    emitted_bytes: usize,
13    labels_to_position_map: HashMap<Label, usize>,
14}
15
16impl EmissionData {
17    #[inline(always)]
18    pub(crate) fn new(emitted_bytes: usize, labels_to_position_map: HashMap<Label, usize>) -> Self {
19        Self {
20            emitted_bytes,
21            labels_to_position_map,
22        }
23    }
24
25    /// Releases the internal data of the [`EmissionData`] into a free struct.
26    #[inline(always)]
27    pub const fn deconstruct(self) -> DeconstructedEmissionData {
28        let emitted_bytes = self.emitted_bytes;
29        let labels_to_position_map = unsafe { std::ptr::read(&self.labels_to_position_map) };
30        forget(self);
31        DeconstructedEmissionData {
32            emitted_bytes,
33            labels_to_position_map,
34        }
35    }
36
37    /// Returns a map of public labels to their positions in the emitted code,
38    /// relative to the beginning of the code, not to the passed stream.
39    #[inline(always)]
40    pub const fn labels_to_position_map(&self) -> &HashMap<Label, usize> {
41        &self.labels_to_position_map
42    }
43
44    /// Returns the number of bytes emitted.
45    #[inline(always)]
46    pub const fn emitted_bytes(&self) -> usize {
47        self.emitted_bytes
48    }
49}