This commit is contained in:
Nilton Constantino
2026-01-30 15:15:38 +00:00
parent 25482e865f
commit a534b226fb
9 changed files with 392 additions and 93 deletions
@@ -41,8 +41,12 @@ pub enum Instr {
EndBorrow,
EndMutate,
/// Reads from heap at gate + field. Pops gate, pushes value.
GateLoadField(FieldId),
GateLoadField { gate: ValueId, field: FieldId },
/// Writes to heap at gate + field. Pops gate and value.
GateStoreField(FieldId),
GateStoreField { gate: ValueId, field: FieldId, value: ValueId },
/// Reads from heap at gate + index.
GateLoadIndex { gate: ValueId, index: ValueId },
/// Writes to heap at gate + index.
GateStoreIndex { gate: ValueId, index: ValueId, value: ValueId },
Free,
}
+3 -1
View File
@@ -49,6 +49,7 @@ mod tests {
}],
}],
}],
field_offsets: std::collections::HashMap::new(),
};
let json = serde_json::to_string_pretty(&program).unwrap();
@@ -90,7 +91,8 @@ mod tests {
}
]
}
]
],
"field_offsets": {}
}"#;
assert_eq!(json, expected);
}
@@ -1,10 +1,13 @@
use serde::{Deserialize, Serialize};
use super::module::Module;
use super::const_pool::ConstPool;
use super::ids::FieldId;
use std::collections::HashMap;
/// 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>,
#[serde(default)]
pub field_offsets: HashMap<FieldId, u32>,
}
@@ -88,15 +88,15 @@ fn validate_function(func: &super::function::Function) -> Result<(), String> {
None => return Err("EndMutate without matching BeginMutate".to_string()),
}
}
Instr::GateLoadField(_) => {
Instr::GateLoadField { .. } | Instr::GateLoadIndex { .. } => {
if current_stack.is_empty() {
return Err("GateLoadField outside of HIP operation".to_string());
return Err("GateLoad outside of HIP operation".to_string());
}
}
Instr::GateStoreField(_) => {
Instr::GateStoreField { .. } | Instr::GateStoreIndex { .. } => {
match current_stack.last() {
Some(op) if op.kind == HipOpKind::Mutate => {},
_ => return Err("GateStoreField outside of BeginMutate".to_string()),
_ => return Err("GateStore outside of BeginMutate".to_string()),
}
}
Instr::Call(id, _) => {
@@ -171,6 +171,7 @@ mod tests {
name: "test".to_string(),
functions: vec![func],
}],
field_offsets: HashMap::new(),
}
}
@@ -180,9 +181,9 @@ mod tests {
id: 0,
instrs: vec![
Instr::BeginPeek { gate: ValueId(0) },
Instr::GateLoadField(FieldId(0)),
Instr::GateLoadField { gate: ValueId(0), field: FieldId(0) },
Instr::BeginMutate { gate: ValueId(1) },
Instr::GateStoreField(FieldId(0)),
Instr::GateStoreField { gate: ValueId(1), field: FieldId(0), value: ValueId(2) },
Instr::EndMutate,
Instr::EndPeek,
],
@@ -229,7 +230,7 @@ mod tests {
id: 0,
instrs: vec![
Instr::BeginBorrow { gate: ValueId(0) },
Instr::GateStoreField(FieldId(0)),
Instr::GateStoreField { gate: ValueId(0), field: FieldId(0), value: ValueId(1) },
Instr::EndBorrow,
],
terminator: Terminator::Return,
@@ -237,7 +238,7 @@ mod tests {
let prog = create_dummy_program(create_dummy_function(vec![block]));
let res = validate_program(&prog);
assert!(res.is_err());
assert!(res.unwrap_err().contains("GateStoreField outside of BeginMutate"));
assert!(res.unwrap_err().contains("GateStore outside of BeginMutate"));
}
#[test]
@@ -246,7 +247,7 @@ mod tests {
id: 0,
instrs: vec![
Instr::BeginMutate { gate: ValueId(0) },
Instr::GateStoreField(FieldId(0)),
Instr::GateStoreField { gate: ValueId(0), field: FieldId(0), value: ValueId(1) },
Instr::EndMutate,
],
terminator: Terminator::Return,
@@ -260,14 +261,14 @@ mod tests {
let block = Block {
id: 0,
instrs: vec![
Instr::GateLoadField(FieldId(0)),
Instr::GateLoadField { gate: ValueId(0), field: FieldId(0) },
],
terminator: Terminator::Return,
};
let prog = create_dummy_program(create_dummy_function(vec![block]));
let res = validate_program(&prog);
assert!(res.is_err());
assert!(res.unwrap_err().contains("GateLoadField outside of HIP operation"));
assert!(res.unwrap_err().contains("GateLoad outside of HIP operation"));
}
#[test]
@@ -282,7 +283,7 @@ mod tests {
let block1 = Block {
id: 1,
instrs: vec![
Instr::GateLoadField(FieldId(0)),
Instr::GateLoadField { gate: ValueId(0), field: FieldId(0) },
Instr::EndPeek,
],
terminator: Terminator::Return,