rtlola_streamir/ir/
memory.rs

1//! Contains the internal representation of the memory in the StreamIR.
2
3use std::ops::Add;
4
5use super::Type;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8/// All memory information of a stream
9pub struct Memory {
10    /// The buffer of the stream
11    pub buffer: StreamMemory,
12    /// The value type of the stream
13    pub ty: Type,
14    /// The name of the stream
15    pub name: String,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19/// The kind of a stream's memory
20pub enum StreamMemory {
21    /// No memory is needed for this stream
22    NoMemory,
23    /// Memory is needed, but only for a single instance living for the whole runtime
24    Static(StreamBuffer),
25    /// Memory is needed for a single instance that dynamically spawns and closes
26    Dynamic {
27        /// The buffer of the memory
28        buffer: StreamBuffer,
29        /// Whether this stream is dynamically spawned
30        has_spawn: bool,
31        /// Whether this stream is dynamically closed
32        has_close: bool,
33    },
34    /// Memory is required for a stream spawning multiple instances
35    Instances {
36        /// The buffer of each stream's instance
37        buffer: StreamBuffer,
38        /// The parameter of the stream
39        parameter: Vec<Parameter>,
40    },
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
44/// The kind of buffer for a stream's instance
45pub enum StreamBuffer {
46    /// Only the newest value needs to be stored
47    SingleValue,
48    /// Only a bounded number of values needs to be stored
49    Bounded(usize),
50    /// All values need to be stored
51    UnBounded,
52}
53
54impl StreamBuffer {
55    /// Returns the Memorybound of the Streambuffer. Returns None if the Buffer is unbounded.
56    pub fn bound(&self) -> Option<usize> {
57        match self {
58            StreamBuffer::SingleValue => Some(1),
59            StreamBuffer::Bounded(b) => Some(*b),
60            StreamBuffer::UnBounded => None,
61        }
62    }
63}
64
65#[derive(Debug, Clone, PartialEq, Eq, Hash)]
66/// A single parameter of a parameterized stream
67pub struct Parameter {
68    /// The name of the parameter
69    pub name: String,
70    /// The type of the parameter
71    pub ty: Type,
72}
73
74impl Memory {
75    /// Returns the parameters of a stream if it is parameterized (or None otherwise)
76    pub fn parameters(&self) -> Option<&[Parameter]> {
77        self.buffer.parameters()
78    }
79
80    /// Returns the number of parameters of a parameterized streams or 0 for unparameterized streams
81    pub fn num_parameters(&self) -> usize {
82        self.buffer.num_parameters()
83    }
84}
85
86impl StreamMemory {
87    /// Returns the parameters of a stream if it is parameterized (or None otherwise)
88    pub fn parameters(&self) -> Option<&[Parameter]> {
89        match self {
90            StreamMemory::Instances {
91                buffer: _,
92                parameter,
93            } => Some(parameter),
94            _ => None,
95        }
96    }
97
98    /// Returns the number of parameters of a parameterized streams or 0 for unparameterized streams
99    pub fn num_parameters(&self) -> usize {
100        match self {
101            StreamMemory::Instances {
102                buffer: _,
103                parameter,
104            } => parameter.len(),
105            _ => 0,
106        }
107    }
108}
109
110impl StreamMemory {
111    /// Returns the [Streambuffer] of the memory. Returns [None] if no memory is required
112    pub fn buffer(&self) -> Option<&StreamBuffer> {
113        match self {
114            StreamMemory::NoMemory => None,
115            StreamMemory::Static(buffer)
116            | StreamMemory::Dynamic { buffer, .. }
117            | StreamMemory::Instances { buffer, .. } => Some(buffer),
118        }
119    }
120}
121
122impl Add for StreamMemory {
123    type Output = StreamMemory;
124
125    fn add(self, rhs: Self) -> Self::Output {
126        assert!(self == rhs);
127        self
128    }
129}