pr 27
This commit is contained in:
@@ -5,7 +5,8 @@
|
||||
//! easy to lower into VM-specific bytecode.
|
||||
|
||||
use crate::common::spans::Span;
|
||||
use crate::ir_core::ids::{ConstId, FunctionId};
|
||||
use crate::ir_vm::types::{ConstId, TypeId};
|
||||
use crate::ir_core::ids::FunctionId;
|
||||
|
||||
/// An `Instruction` combines an instruction's behavior (`kind`) with its
|
||||
/// source code location (`span`) for debugging and error reporting.
|
||||
@@ -139,10 +140,186 @@ pub enum InstrKind {
|
||||
|
||||
// --- HIP / Memory ---
|
||||
|
||||
/// Allocates memory on the heap. Pops size from stack.
|
||||
Alloc,
|
||||
/// Reads from heap at reference + offset. Pops reference, pushes value.
|
||||
LoadRef(u32),
|
||||
/// Writes to heap at reference + offset. Pops reference and value.
|
||||
StoreRef(u32),
|
||||
/// Allocates memory on the heap.
|
||||
Alloc { type_id: TypeId, slots: u32 },
|
||||
/// Reads from heap at gate + offset. Pops gate, pushes value.
|
||||
GateLoad { offset: u32 },
|
||||
/// Writes to heap at gate + offset. Pops gate and value.
|
||||
GateStore { offset: u32 },
|
||||
|
||||
// --- Scope Markers ---
|
||||
GateBeginPeek,
|
||||
GateEndPeek,
|
||||
GateBeginBorrow,
|
||||
GateEndBorrow,
|
||||
GateBeginMutate,
|
||||
GateEndMutate,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ir_vm::types::{ConstId, TypeId};
|
||||
|
||||
#[test]
|
||||
fn test_instr_kind_is_cloneable() {
|
||||
let instr = InstrKind::Alloc { type_id: TypeId(1), slots: 2 };
|
||||
let cloned = instr.clone();
|
||||
match cloned {
|
||||
InstrKind::Alloc { type_id, slots } => {
|
||||
assert_eq!(type_id, TypeId(1));
|
||||
assert_eq!(slots, 2);
|
||||
}
|
||||
_ => panic!("Clone failed"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_isa_surface_snapshot() {
|
||||
// This test ensures that the instruction set surface remains stable.
|
||||
// If you add/remove/change instructions, this test will fail,
|
||||
// prompting an explicit review of the ISA change.
|
||||
let instructions = vec![
|
||||
InstrKind::Nop,
|
||||
InstrKind::Halt,
|
||||
InstrKind::PushConst(ConstId(0)),
|
||||
InstrKind::PushBool(true),
|
||||
InstrKind::PushNull,
|
||||
InstrKind::Pop,
|
||||
InstrKind::Dup,
|
||||
InstrKind::Swap,
|
||||
InstrKind::Add,
|
||||
InstrKind::Sub,
|
||||
InstrKind::Mul,
|
||||
InstrKind::Div,
|
||||
InstrKind::Neg,
|
||||
InstrKind::Eq,
|
||||
InstrKind::Neq,
|
||||
InstrKind::Lt,
|
||||
InstrKind::Gt,
|
||||
InstrKind::Lte,
|
||||
InstrKind::Gte,
|
||||
InstrKind::And,
|
||||
InstrKind::Or,
|
||||
InstrKind::Not,
|
||||
InstrKind::BitAnd,
|
||||
InstrKind::BitOr,
|
||||
InstrKind::BitXor,
|
||||
InstrKind::Shl,
|
||||
InstrKind::Shr,
|
||||
InstrKind::GetLocal(0),
|
||||
InstrKind::SetLocal(0),
|
||||
InstrKind::GetGlobal(0),
|
||||
InstrKind::SetGlobal(0),
|
||||
InstrKind::Jmp(Label("target".to_string())),
|
||||
InstrKind::JmpIfFalse(Label("target".to_string())),
|
||||
InstrKind::Label(Label("target".to_string())),
|
||||
InstrKind::Call { func_id: FunctionId(0), arg_count: 0 },
|
||||
InstrKind::Ret,
|
||||
InstrKind::Syscall(0),
|
||||
InstrKind::FrameSync,
|
||||
InstrKind::Alloc { type_id: TypeId(0), slots: 0 },
|
||||
InstrKind::GateLoad { offset: 0 },
|
||||
InstrKind::GateStore { offset: 0 },
|
||||
InstrKind::GateBeginPeek,
|
||||
InstrKind::GateEndPeek,
|
||||
InstrKind::GateBeginBorrow,
|
||||
InstrKind::GateEndBorrow,
|
||||
InstrKind::GateBeginMutate,
|
||||
InstrKind::GateEndMutate,
|
||||
];
|
||||
|
||||
let serialized = serde_json::to_string_pretty(&instructions).unwrap();
|
||||
|
||||
// This is a "lock" on the ISA surface.
|
||||
// If the structure of InstrKind changes, the serialization will change.
|
||||
let expected_json = r#"[
|
||||
"Nop",
|
||||
"Halt",
|
||||
{
|
||||
"PushConst": 0
|
||||
},
|
||||
{
|
||||
"PushBool": true
|
||||
},
|
||||
"PushNull",
|
||||
"Pop",
|
||||
"Dup",
|
||||
"Swap",
|
||||
"Add",
|
||||
"Sub",
|
||||
"Mul",
|
||||
"Div",
|
||||
"Neg",
|
||||
"Eq",
|
||||
"Neq",
|
||||
"Lt",
|
||||
"Gt",
|
||||
"Lte",
|
||||
"Gte",
|
||||
"And",
|
||||
"Or",
|
||||
"Not",
|
||||
"BitAnd",
|
||||
"BitOr",
|
||||
"BitXor",
|
||||
"Shl",
|
||||
"Shr",
|
||||
{
|
||||
"GetLocal": 0
|
||||
},
|
||||
{
|
||||
"SetLocal": 0
|
||||
},
|
||||
{
|
||||
"GetGlobal": 0
|
||||
},
|
||||
{
|
||||
"SetGlobal": 0
|
||||
},
|
||||
{
|
||||
"Jmp": "target"
|
||||
},
|
||||
{
|
||||
"JmpIfFalse": "target"
|
||||
},
|
||||
{
|
||||
"Label": "target"
|
||||
},
|
||||
{
|
||||
"Call": {
|
||||
"func_id": 0,
|
||||
"arg_count": 0
|
||||
}
|
||||
},
|
||||
"Ret",
|
||||
{
|
||||
"Syscall": 0
|
||||
},
|
||||
"FrameSync",
|
||||
{
|
||||
"Alloc": {
|
||||
"type_id": 0,
|
||||
"slots": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"GateLoad": {
|
||||
"offset": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"GateStore": {
|
||||
"offset": 0
|
||||
}
|
||||
},
|
||||
"GateBeginPeek",
|
||||
"GateEndPeek",
|
||||
"GateBeginBorrow",
|
||||
"GateEndBorrow",
|
||||
"GateBeginMutate",
|
||||
"GateEndMutate"
|
||||
]"#;
|
||||
assert_eq!(serialized, expected_json);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,7 @@ pub mod validate;
|
||||
|
||||
pub use instr::{Instruction, InstrKind, Label};
|
||||
pub use module::{Module, Function, Global, Param};
|
||||
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.
|
||||
pub use types::{Type, Value, GateId, ConstId, TypeId};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@@ -40,7 +38,7 @@ mod tests {
|
||||
params: vec![],
|
||||
return_type: Type::Null,
|
||||
body: vec![
|
||||
Instruction::new(InstrKind::PushConst(ConstId(0)), None),
|
||||
Instruction::new(InstrKind::PushConst(crate::ir_vm::types::ConstId(0)), None),
|
||||
Instruction::new(InstrKind::Call { func_id: FunctionId(2), arg_count: 1 }, None),
|
||||
Instruction::new(InstrKind::Ret, None),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user