pr 03
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use super::ids::ConstId;
|
||||
|
||||
/// Represents a constant value that can be stored in the constant pool.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum ConstantValue {
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
String(String),
|
||||
}
|
||||
|
||||
/// A stable constant pool that handles deduplication and provides IDs.
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct ConstPool {
|
||||
pub constants: Vec<ConstantValue>,
|
||||
}
|
||||
|
||||
impl ConstPool {
|
||||
/// Creates a new, empty constant pool.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Inserts a value into the pool if it doesn't already exist.
|
||||
/// Returns the corresponding `ConstId`.
|
||||
pub fn insert(&mut self, value: ConstantValue) -> ConstId {
|
||||
if let Some(pos) = self.constants.iter().position(|c| c == &value) {
|
||||
ConstId(pos as u32)
|
||||
} else {
|
||||
let id = self.constants.len() as u32;
|
||||
self.constants.push(value);
|
||||
ConstId(id)
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves a value from the pool by its `ConstId`.
|
||||
pub fn get(&self, id: ConstId) -> Option<&ConstantValue> {
|
||||
self.constants.get(id.0 as usize)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod ids;
|
||||
pub mod const_pool;
|
||||
pub mod program;
|
||||
pub mod module;
|
||||
pub mod function;
|
||||
@@ -7,6 +8,7 @@ pub mod instr;
|
||||
pub mod terminator;
|
||||
|
||||
pub use ids::*;
|
||||
pub use const_pool::*;
|
||||
pub use program::*;
|
||||
pub use module::*;
|
||||
pub use function::*;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use super::module::Module;
|
||||
use super::const_pool::ConstPool;
|
||||
|
||||
/// A complete PBS program, consisting of multiple modules.
|
||||
/// A complete PBS program, consisting of multiple modules and a shared constant pool.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Program {
|
||||
pub const_pool: ConstPool,
|
||||
pub modules: Vec<Module>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user