This commit is contained in:
Nilton Constantino
2026-01-30 11:46:17 +00:00
parent 81453cde84
commit 91af0d6f98
4 changed files with 267 additions and 33 deletions
+13 -1
View File
@@ -1,3 +1,13 @@
//! # VM Intermediate Representation (ir_vm)
//!
//! This module defines the Intermediate Representation for the Prometeu VM.
//!
//! ## Memory Model
//!
//! * Heap is never directly addressable.
//! * All HIP (Heap) access is mediated via Gate Pool resolution.
//! * `Gate(GateId)` is the only HIP pointer form in `ir_vm`.
pub mod types;
pub mod module;
pub mod instr;
@@ -5,7 +15,9 @@ pub mod validate;
pub use instr::{Instruction, InstrKind, Label};
pub use module::{Module, Function, Global, Param};
pub use types::Type;
pub use types::{Type, Value, GateId};
// Note: ConstId and TypeId are not exported here to avoid conflict with ir_core::ids
// until the crates are fully decoupled.
#[cfg(test)]
mod tests {
@@ -1,5 +1,28 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct GateId(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ConstId(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct TypeId(pub u32);
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
Int(i64),
Float(f64),
Bounded(u32),
Bool(bool),
Unit,
Const(ConstId),
Gate(GateId),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Type {
Any,
@@ -14,3 +37,51 @@ pub enum Type {
Function,
Void,
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
#[test]
fn test_ids_implement_required_traits() {
fn assert_copy<T: Copy>() {}
fn assert_eq_hash<T: Eq + std::hash::Hash>() {}
assert_copy::<GateId>();
assert_eq_hash::<GateId>();
assert_copy::<ConstId>();
assert_eq_hash::<ConstId>();
assert_copy::<TypeId>();
assert_eq_hash::<TypeId>();
}
#[test]
fn test_gate_id_usage() {
let id1 = GateId(1);
let id2 = GateId(1);
let id3 = GateId(2);
assert_eq!(id1, id2);
assert_ne!(id1, id3);
let mut set = HashSet::new();
set.insert(id1);
assert!(set.contains(&id2));
}
#[test]
fn test_value_gate_exists_and_is_clonable() {
let gate_id = GateId(42);
let val = Value::Gate(gate_id);
let cloned_val = val.clone();
if let Value::Gate(id) = cloned_val {
assert_eq!(id, gate_id);
} else {
panic!("Expected Value::Gate");
}
}
}