pr 32
This commit is contained in:
@@ -7,26 +7,26 @@ pub fn lower_program(program: &ir_core::Program) -> Result<ir_vm::Module> {
|
||||
// For now, we assume a single module program or lower the first one.
|
||||
// In the future, we might want to lower all modules and link them.
|
||||
if let Some(core_module) = program.modules.first() {
|
||||
lower_module(core_module, &program.const_pool)
|
||||
lower_module(core_module, program)
|
||||
} else {
|
||||
anyhow::bail!("No modules in core program")
|
||||
}
|
||||
}
|
||||
|
||||
/// Lowers a single Core IR module into a VM IR module.
|
||||
pub fn lower_module(core_module: &ir_core::Module, const_pool: &ir_core::ConstPool) -> Result<ir_vm::Module> {
|
||||
pub fn lower_module(core_module: &ir_core::Module, program: &ir_core::Program) -> Result<ir_vm::Module> {
|
||||
let mut vm_module = ir_vm::Module::new(core_module.name.clone());
|
||||
vm_module.const_pool = const_pool.clone();
|
||||
vm_module.const_pool = program.const_pool.clone();
|
||||
|
||||
for core_func in &core_module.functions {
|
||||
vm_module.functions.push(lower_function(core_func)?);
|
||||
vm_module.functions.push(lower_function(core_func, program)?);
|
||||
}
|
||||
|
||||
Ok(vm_module)
|
||||
}
|
||||
|
||||
/// Lowers a Core IR function into a VM IR function.
|
||||
pub fn lower_function(core_func: &ir_core::Function) -> Result<ir_vm::Function> {
|
||||
pub fn lower_function(core_func: &ir_core::Function, program: &ir_core::Program) -> Result<ir_vm::Function> {
|
||||
let mut vm_func = ir_vm::Function {
|
||||
id: core_func.id,
|
||||
name: core_func.name.clone(),
|
||||
@@ -46,46 +46,125 @@ pub fn lower_function(core_func: &ir_core::Function) -> Result<ir_vm::Function>
|
||||
));
|
||||
|
||||
for instr in &block.instrs {
|
||||
let kind = match instr {
|
||||
ir_core::Instr::PushConst(id) => ir_vm::InstrKind::PushConst(ir_vm::ConstId(id.0)),
|
||||
ir_core::Instr::Call(func_id, arg_count) => ir_vm::InstrKind::Call {
|
||||
func_id: *func_id,
|
||||
arg_count: *arg_count
|
||||
},
|
||||
ir_core::Instr::HostCall(id) => ir_vm::InstrKind::Syscall(*id),
|
||||
ir_core::Instr::GetLocal(slot) => ir_vm::InstrKind::LocalLoad { slot: *slot },
|
||||
ir_core::Instr::SetLocal(slot) => ir_vm::InstrKind::LocalStore { slot: *slot },
|
||||
ir_core::Instr::Pop => ir_vm::InstrKind::Pop,
|
||||
ir_core::Instr::Dup => ir_vm::InstrKind::Dup,
|
||||
ir_core::Instr::Add => ir_vm::InstrKind::Add,
|
||||
ir_core::Instr::Sub => ir_vm::InstrKind::Sub,
|
||||
ir_core::Instr::Mul => ir_vm::InstrKind::Mul,
|
||||
ir_core::Instr::Div => ir_vm::InstrKind::Div,
|
||||
ir_core::Instr::Neg => ir_vm::InstrKind::Neg,
|
||||
ir_core::Instr::Eq => ir_vm::InstrKind::Eq,
|
||||
ir_core::Instr::Neq => ir_vm::InstrKind::Neq,
|
||||
ir_core::Instr::Lt => ir_vm::InstrKind::Lt,
|
||||
ir_core::Instr::Lte => ir_vm::InstrKind::Lte,
|
||||
ir_core::Instr::Gt => ir_vm::InstrKind::Gt,
|
||||
ir_core::Instr::Gte => ir_vm::InstrKind::Gte,
|
||||
ir_core::Instr::And => ir_vm::InstrKind::And,
|
||||
ir_core::Instr::Or => ir_vm::InstrKind::Or,
|
||||
ir_core::Instr::Not => ir_vm::InstrKind::Not,
|
||||
ir_core::Instr::Alloc { ty, slots } => ir_vm::InstrKind::Alloc {
|
||||
type_id: ir_vm::TypeId(ty.0),
|
||||
slots: *slots
|
||||
},
|
||||
ir_core::Instr::BeginPeek { .. } => ir_vm::InstrKind::GateBeginPeek,
|
||||
ir_core::Instr::BeginBorrow { .. } => ir_vm::InstrKind::GateBeginBorrow,
|
||||
ir_core::Instr::BeginMutate { .. } => ir_vm::InstrKind::GateBeginMutate,
|
||||
ir_core::Instr::EndPeek => ir_vm::InstrKind::GateEndPeek,
|
||||
ir_core::Instr::EndBorrow => ir_vm::InstrKind::GateEndBorrow,
|
||||
ir_core::Instr::EndMutate => ir_vm::InstrKind::GateEndMutate,
|
||||
ir_core::Instr::GateLoadField(field_id) => ir_vm::InstrKind::GateLoad { offset: field_id.0 },
|
||||
ir_core::Instr::GateStoreField(field_id) => ir_vm::InstrKind::GateStore { offset: field_id.0 },
|
||||
match instr {
|
||||
ir_core::Instr::PushConst(id) => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::PushConst(ir_vm::ConstId(id.0)), None));
|
||||
}
|
||||
ir_core::Instr::Call(func_id, arg_count) => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Call {
|
||||
func_id: *func_id,
|
||||
arg_count: *arg_count
|
||||
}, None));
|
||||
}
|
||||
ir_core::Instr::HostCall(id) => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Syscall(*id), None));
|
||||
}
|
||||
ir_core::Instr::GetLocal(slot) => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::LocalLoad { slot: *slot }, None));
|
||||
}
|
||||
ir_core::Instr::SetLocal(slot) => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::LocalStore { slot: *slot }, None));
|
||||
}
|
||||
ir_core::Instr::Pop => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Pop, None));
|
||||
}
|
||||
ir_core::Instr::Dup => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Dup, None));
|
||||
}
|
||||
ir_core::Instr::Add => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Add, None));
|
||||
}
|
||||
ir_core::Instr::Sub => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Sub, None));
|
||||
}
|
||||
ir_core::Instr::Mul => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Mul, None));
|
||||
}
|
||||
ir_core::Instr::Div => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Div, None));
|
||||
}
|
||||
ir_core::Instr::Neg => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Neg, None));
|
||||
}
|
||||
ir_core::Instr::Eq => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Eq, None));
|
||||
}
|
||||
ir_core::Instr::Neq => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Neq, None));
|
||||
}
|
||||
ir_core::Instr::Lt => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Lt, None));
|
||||
}
|
||||
ir_core::Instr::Lte => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Lte, None));
|
||||
}
|
||||
ir_core::Instr::Gt => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Gt, None));
|
||||
}
|
||||
ir_core::Instr::Gte => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Gte, None));
|
||||
}
|
||||
ir_core::Instr::And => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::And, None));
|
||||
}
|
||||
ir_core::Instr::Or => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Or, None));
|
||||
}
|
||||
ir_core::Instr::Not => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Not, None));
|
||||
}
|
||||
ir_core::Instr::Alloc { ty, slots } => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Alloc {
|
||||
type_id: ir_vm::TypeId(ty.0),
|
||||
slots: *slots
|
||||
}, None));
|
||||
}
|
||||
ir_core::Instr::BeginPeek { .. } => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateBeginPeek, None));
|
||||
}
|
||||
ir_core::Instr::BeginBorrow { .. } => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateBeginBorrow, None));
|
||||
}
|
||||
ir_core::Instr::BeginMutate { .. } => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateBeginMutate, None));
|
||||
}
|
||||
ir_core::Instr::EndPeek => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateEndPeek, None));
|
||||
}
|
||||
ir_core::Instr::EndBorrow => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateEndBorrow, None));
|
||||
}
|
||||
ir_core::Instr::EndMutate => {
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateEndMutate, None));
|
||||
}
|
||||
ir_core::Instr::GateLoadField { gate, field } => {
|
||||
let offset = program.field_offsets.get(field)
|
||||
.ok_or_else(|| anyhow::anyhow!("E_LOWER_UNRESOLVED_OFFSET: Field {:?} offset cannot be resolved", field))?;
|
||||
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::LocalLoad { slot: gate.0 }, None));
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateLoad { offset: *offset }, None));
|
||||
}
|
||||
ir_core::Instr::GateStoreField { gate, field, value } => {
|
||||
let offset = program.field_offsets.get(field)
|
||||
.ok_or_else(|| anyhow::anyhow!("E_LOWER_UNRESOLVED_OFFSET: Field {:?} offset cannot be resolved", field))?;
|
||||
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::LocalLoad { slot: gate.0 }, None));
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::LocalLoad { slot: value.0 }, None));
|
||||
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::GateStore { offset: *offset }, None));
|
||||
}
|
||||
ir_core::Instr::GateLoadIndex { .. } => {
|
||||
// For indices, we might need a more complex resolution, but for now we assume index is the offset
|
||||
// or maybe it's dynamic. ir_vm::GateLoad only takes a static offset.
|
||||
// If index is dynamic, we can't lower it to GateLoad with static offset.
|
||||
// However, PR-07 says: "if offset cannot be resolved deterministically at compile time, emit diagnostic and abort lowering."
|
||||
// This implies we don't support dynamic indices yet.
|
||||
anyhow::bail!("E_LOWER_UNSUPPORTED: Dynamic HIP index access not supported in v0 lowering");
|
||||
}
|
||||
ir_core::Instr::GateStoreIndex { .. } => {
|
||||
anyhow::bail!("E_LOWER_UNSUPPORTED: Dynamic HIP index access not supported in v0 lowering");
|
||||
}
|
||||
ir_core::Instr::Free => anyhow::bail!("Instruction 'Free' cannot be represented in ir_vm v0"),
|
||||
};
|
||||
vm_func.body.push(ir_vm::Instruction::new(kind, None));
|
||||
}
|
||||
}
|
||||
|
||||
match &block.terminator {
|
||||
@@ -173,6 +252,7 @@ mod tests {
|
||||
],
|
||||
}],
|
||||
}],
|
||||
field_offsets: std::collections::HashMap::new(),
|
||||
};
|
||||
|
||||
let vm_module = lower_program(&program).expect("Lowering failed");
|
||||
@@ -215,4 +295,102 @@ mod tests {
|
||||
_ => panic!("Expected Ret"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_field_access_lowering_golden() {
|
||||
let const_pool = ConstPool::new();
|
||||
let mut field_offsets = std::collections::HashMap::new();
|
||||
let field_id = ir_core::FieldId(42);
|
||||
field_offsets.insert(field_id, 100);
|
||||
|
||||
let program = Program {
|
||||
const_pool,
|
||||
modules: vec![ir_core::Module {
|
||||
name: "test".to_string(),
|
||||
functions: vec![ir_core::Function {
|
||||
id: FunctionId(1),
|
||||
name: "test_fields".to_string(),
|
||||
params: vec![],
|
||||
return_type: ir_core::Type::Void,
|
||||
blocks: vec![Block {
|
||||
id: 0,
|
||||
instrs: vec![
|
||||
Instr::GateLoadField { gate: ir_core::ValueId(0), field: field_id },
|
||||
Instr::GateStoreField { gate: ir_core::ValueId(0), field: field_id, value: ir_core::ValueId(1) },
|
||||
],
|
||||
terminator: Terminator::Return,
|
||||
}],
|
||||
}],
|
||||
}],
|
||||
field_offsets,
|
||||
};
|
||||
|
||||
let vm_module = lower_program(&program).expect("Lowering failed");
|
||||
let func = &vm_module.functions[0];
|
||||
|
||||
// Expected VM IR:
|
||||
// Label block_0
|
||||
// LocalLoad 0 (gate)
|
||||
// GateLoad 100 (offset)
|
||||
// LocalLoad 0 (gate)
|
||||
// LocalLoad 1 (value)
|
||||
// GateStore 100 (offset)
|
||||
// Ret
|
||||
|
||||
assert_eq!(func.body.len(), 7);
|
||||
match &func.body[1].kind {
|
||||
ir_vm::InstrKind::LocalLoad { slot } => assert_eq!(*slot, 0),
|
||||
_ => panic!("Expected LocalLoad 0"),
|
||||
}
|
||||
match &func.body[2].kind {
|
||||
ir_vm::InstrKind::GateLoad { offset } => assert_eq!(*offset, 100),
|
||||
_ => panic!("Expected GateLoad 100"),
|
||||
}
|
||||
match &func.body[5].kind {
|
||||
ir_vm::InstrKind::GateStore { offset } => assert_eq!(*offset, 100),
|
||||
_ => panic!("Expected GateStore 100"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_field_offset_fails() {
|
||||
let program = Program {
|
||||
const_pool: ConstPool::new(),
|
||||
modules: vec![ir_core::Module {
|
||||
name: "test".to_string(),
|
||||
functions: vec![ir_core::Function {
|
||||
id: FunctionId(1),
|
||||
name: "fail".to_string(),
|
||||
params: vec![],
|
||||
return_type: ir_core::Type::Void,
|
||||
blocks: vec![Block {
|
||||
id: 0,
|
||||
instrs: vec![
|
||||
Instr::GateLoadField { gate: ir_core::ValueId(0), field: ir_core::FieldId(999) },
|
||||
],
|
||||
terminator: Terminator::Return,
|
||||
}],
|
||||
}],
|
||||
}],
|
||||
field_offsets: std::collections::HashMap::new(),
|
||||
};
|
||||
|
||||
let result = lower_program(&program);
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("E_LOWER_UNRESOLVED_OFFSET"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_implicit_offsets_in_vm_ir() {
|
||||
// This test ensures that GateLoad and GateStore in VM IR always have explicit offsets.
|
||||
// Since we are using struct variants with mandatory 'offset' field, this is
|
||||
// enforced by the type system, but we can also check the serialized form.
|
||||
let instructions = vec![
|
||||
ir_vm::InstrKind::GateLoad { offset: 123 },
|
||||
ir_vm::InstrKind::GateStore { offset: 456 },
|
||||
];
|
||||
let json = serde_json::to_string(&instructions).unwrap();
|
||||
assert!(json.contains("\"GateLoad\":{\"offset\":123}"));
|
||||
assert!(json.contains("\"GateStore\":{\"offset\":456}"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user