This commit is contained in:
Nilton Constantino
2026-01-31 20:39:05 +00:00
parent 83e24920b4
commit ff4dcad5dc
42 changed files with 2941 additions and 603 deletions
@@ -52,6 +52,9 @@ pub fn lower_function(
}).collect(),
return_type: lower_type(&core_func.return_type),
body: vec![],
param_slots: core_func.param_slots,
local_slots: core_func.local_slots,
return_slots: core_func.return_slots,
};
// Type tracking for RC insertion
@@ -320,12 +323,8 @@ pub fn lower_function(
}
}
// If the function is Void, we must push a Null value to satisfy the VM's RET instruction.
// The VM always pops one value from the stack to be the return value.
if vm_func.return_type == ir_vm::Type::Void {
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::PushNull, None));
}
// If the function is Void, we don't need to push anything.
// The VM's Ret opcode handles zero return slots correctly.
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Ret, None));
}
ir_core::Terminator::Jump(target) => {
@@ -424,6 +423,9 @@ mod tests {
},
],
local_types: HashMap::new(),
param_slots: 0,
local_slots: 0,
return_slots: 0,
}],
}],
field_offsets: std::collections::HashMap::new(),
@@ -436,7 +438,7 @@ mod tests {
let func = &vm_module.functions[0];
assert_eq!(func.name, "main");
assert_eq!(func.body.len(), 8);
assert_eq!(func.body.len(), 7);
match &func.body[0].kind {
InstrKind::Label(Label(l)) => assert_eq!(l, "block_0"),
@@ -466,10 +468,6 @@ mod tests {
_ => panic!("Expected HostCall 42"),
}
match &func.body[6].kind {
InstrKind::PushNull => (),
_ => panic!("Expected PushNull"),
}
match &func.body[7].kind {
InstrKind::Ret => (),
_ => panic!("Expected Ret"),
}
@@ -500,6 +498,9 @@ mod tests {
terminator: Terminator::Return,
}],
local_types: HashMap::new(),
param_slots: 0,
local_slots: 0,
return_slots: 0,
}],
}],
field_offsets,
@@ -518,7 +519,7 @@ mod tests {
// GateStore 100 (offset)
// Ret
assert_eq!(func.body.len(), 10);
assert_eq!(func.body.len(), 9);
match &func.body[1].kind {
ir_vm::InstrKind::LocalLoad { slot } => assert_eq!(*slot, 0),
_ => panic!("Expected LocalLoad 0"),
@@ -536,10 +537,6 @@ mod tests {
_ => panic!("Expected GateStore 100"),
}
match &func.body[8].kind {
ir_vm::InstrKind::PushNull => (),
_ => panic!("Expected PushNull"),
}
match &func.body[9].kind {
ir_vm::InstrKind::Ret => (),
_ => panic!("Expected Ret"),
}
@@ -564,6 +561,9 @@ mod tests {
terminator: Terminator::Return,
}],
local_types: HashMap::new(),
param_slots: 0,
local_slots: 0,
return_slots: 0,
}],
}],
field_offsets: std::collections::HashMap::new(),
@@ -609,6 +609,9 @@ mod tests {
terminator: Terminator::Return,
}],
local_types: HashMap::new(),
param_slots: 0,
local_slots: 0,
return_slots: 0,
}],
}],
field_offsets: HashMap::new(),
@@ -635,10 +638,10 @@ mod tests {
assert!(found_overwrite, "Should have emitted release-then-store sequence for overwrite");
// Check Ret cleanup:
// LocalLoad 1, GateRelease, PushNull, Ret
// LocalLoad 1, GateRelease, Ret
let mut found_cleanup = false;
for i in 0..kinds.len() - 3 {
if let (InstrKind::LocalLoad { slot: 1 }, InstrKind::GateRelease, InstrKind::PushNull, InstrKind::Ret) = (kinds[i], kinds[i+1], kinds[i+2], kinds[i+3]) {
for i in 0..kinds.len() - 2 {
if let (InstrKind::LocalLoad { slot: 1 }, InstrKind::GateRelease, InstrKind::Ret) = (kinds[i], kinds[i+1], kinds[i+2]) {
found_cleanup = true;
break;
}
@@ -671,6 +674,9 @@ mod tests {
terminator: Terminator::Return,
}],
local_types: HashMap::new(),
param_slots: 0,
local_slots: 0,
return_slots: 0,
}],
}],
field_offsets: HashMap::new(),