change ir to ir_vm and make it feature frozen

This commit is contained in:
Nilton Constantino
2026-01-29 16:45:02 +00:00
parent 4174d01a43
commit c34166435f
12 changed files with 197 additions and 331 deletions
@@ -1,9 +1,9 @@
use crate::ir;
use crate::ir_vm;
use crate::ir_core;
use anyhow::Result;
/// Lowers a Core IR program into a VM IR module.
pub fn lower_program(program: &ir_core::Program) -> Result<ir::Module> {
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() {
@@ -14,8 +14,8 @@ pub fn lower_program(program: &ir_core::Program) -> Result<ir::Module> {
}
/// 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::Module> {
let mut vm_module = ir::Module::new(core_module.name.clone());
pub fn lower_module(core_module: &ir_core::Module, const_pool: &ir_core::ConstPool) -> Result<ir_vm::Module> {
let mut vm_module = ir_vm::Module::new(core_module.name.clone());
vm_module.const_pool = const_pool.clone();
for core_func in &core_module.functions {
@@ -26,11 +26,11 @@ pub fn lower_module(core_module: &ir_core::Module, const_pool: &ir_core::ConstPo
}
/// Lowers a Core IR function into a VM IR function.
pub fn lower_function(core_func: &ir_core::Function) -> Result<ir::Function> {
let mut vm_func = ir::Function {
pub fn lower_function(core_func: &ir_core::Function) -> Result<ir_vm::Function> {
let mut vm_func = ir_vm::Function {
id: core_func.id,
name: core_func.name.clone(),
params: core_func.params.iter().map(|p| ir::Param {
params: core_func.params.iter().map(|p| ir_vm::Param {
name: p.name.clone(),
r#type: lower_type(&p.ty),
}).collect(),
@@ -40,62 +40,62 @@ pub fn lower_function(core_func: &ir_core::Function) -> Result<ir::Function> {
for block in &core_func.blocks {
// Core blocks map to labels in the flat VM IR instruction list.
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::Label(ir::Label(format!("block_{}", block.id))),
vm_func.body.push(ir_vm::Instruction::new(
ir_vm::InstrKind::Label(ir_vm::Label(format!("block_{}", block.id))),
None,
));
for instr in &block.instrs {
let kind = match instr {
ir_core::Instr::PushConst(id) => ir::InstrKind::PushConst(*id),
ir_core::Instr::Call(func_id, arg_count) => ir::InstrKind::Call {
ir_core::Instr::PushConst(id) => ir_vm::InstrKind::PushConst(*id),
ir_core::Instr::Call(func_id, arg_count) => ir_vm::InstrKind::Call {
func_id: *func_id,
arg_count: *arg_count
},
ir_core::Instr::Syscall(id) => ir::InstrKind::Syscall(*id),
ir_core::Instr::GetLocal(slot) => ir::InstrKind::GetLocal(*slot),
ir_core::Instr::SetLocal(slot) => ir::InstrKind::SetLocal(*slot),
ir_core::Instr::Pop => ir::InstrKind::Pop,
ir_core::Instr::Dup => ir::InstrKind::Dup,
ir_core::Instr::Add => ir::InstrKind::Add,
ir_core::Instr::Sub => ir::InstrKind::Sub,
ir_core::Instr::Mul => ir::InstrKind::Mul,
ir_core::Instr::Div => ir::InstrKind::Div,
ir_core::Instr::Neg => ir::InstrKind::Neg,
ir_core::Instr::Eq => ir::InstrKind::Eq,
ir_core::Instr::Neq => ir::InstrKind::Neq,
ir_core::Instr::Lt => ir::InstrKind::Lt,
ir_core::Instr::Lte => ir::InstrKind::Lte,
ir_core::Instr::Gt => ir::InstrKind::Gt,
ir_core::Instr::Gte => ir::InstrKind::Gte,
ir_core::Instr::And => ir::InstrKind::And,
ir_core::Instr::Or => ir::InstrKind::Or,
ir_core::Instr::Not => ir::InstrKind::Not,
ir_core::Instr::Alloc => ir::InstrKind::Alloc,
ir_core::Instr::ReadGate => ir::InstrKind::LoadRef(0),
ir_core::Instr::WriteGate => ir::InstrKind::StoreRef(0),
ir_core::Instr::Free => ir::InstrKind::Nop,
ir_core::Instr::Syscall(id) => ir_vm::InstrKind::Syscall(*id),
ir_core::Instr::GetLocal(slot) => ir_vm::InstrKind::GetLocal(*slot),
ir_core::Instr::SetLocal(slot) => ir_vm::InstrKind::SetLocal(*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 => ir_vm::InstrKind::Alloc,
ir_core::Instr::ReadGate => ir_vm::InstrKind::LoadRef(0),
ir_core::Instr::WriteGate => ir_vm::InstrKind::StoreRef(0),
ir_core::Instr::Free => ir_vm::InstrKind::Nop,
};
vm_func.body.push(ir::Instruction::new(kind, None));
vm_func.body.push(ir_vm::Instruction::new(kind, None));
}
match &block.terminator {
ir_core::Terminator::Return => {
vm_func.body.push(ir::Instruction::new(ir::InstrKind::Ret, None));
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Ret, None));
}
ir_core::Terminator::Jump(target) => {
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::Jmp(ir::Label(format!("block_{}", target))),
vm_func.body.push(ir_vm::Instruction::new(
ir_vm::InstrKind::Jmp(ir_vm::Label(format!("block_{}", target))),
None,
));
}
ir_core::Terminator::JumpIfFalse { target, else_target } => {
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::JmpIfFalse(ir::Label(format!("block_{}", target))),
vm_func.body.push(ir_vm::Instruction::new(
ir_vm::InstrKind::JmpIfFalse(ir_vm::Label(format!("block_{}", target))),
None,
));
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::Jmp(ir::Label(format!("block_{}", else_target))),
vm_func.body.push(ir_vm::Instruction::new(
ir_vm::InstrKind::Jmp(ir_vm::Label(format!("block_{}", else_target))),
None,
));
}
@@ -105,20 +105,20 @@ pub fn lower_function(core_func: &ir_core::Function) -> Result<ir::Function> {
Ok(vm_func)
}
fn lower_type(ty: &ir_core::Type) -> ir::Type {
fn lower_type(ty: &ir_core::Type) -> ir_vm::Type {
match ty {
ir_core::Type::Void => ir::Type::Void,
ir_core::Type::Int => ir::Type::Int,
ir_core::Type::Float => ir::Type::Float,
ir_core::Type::Bool => ir::Type::Bool,
ir_core::Type::String => ir::Type::String,
ir_core::Type::Optional(inner) => ir::Type::Array(Box::new(lower_type(inner))), // Approximation
ir_core::Type::Void => ir_vm::Type::Void,
ir_core::Type::Int => ir_vm::Type::Int,
ir_core::Type::Float => ir_vm::Type::Float,
ir_core::Type::Bool => ir_vm::Type::Bool,
ir_core::Type::String => ir_vm::Type::String,
ir_core::Type::Optional(inner) => ir_vm::Type::Array(Box::new(lower_type(inner))), // Approximation
ir_core::Type::Result(ok, _) => lower_type(ok), // Approximation
ir_core::Type::Struct(_) => ir::Type::Object,
ir_core::Type::Service(_) => ir::Type::Object,
ir_core::Type::Contract(_) => ir::Type::Object,
ir_core::Type::ErrorType(_) => ir::Type::Object,
ir_core::Type::Function { .. } => ir::Type::Function,
ir_core::Type::Struct(_) => ir_vm::Type::Object,
ir_core::Type::Service(_) => ir_vm::Type::Object,
ir_core::Type::Contract(_) => ir_vm::Type::Object,
ir_core::Type::ErrorType(_) => ir_vm::Type::Object,
ir_core::Type::Function { .. } => ir_vm::Type::Function,
}
}
@@ -127,7 +127,7 @@ mod tests {
use super::*;
use crate::ir_core;
use crate::ir_core::*;
use crate::ir::*;
use crate::ir_vm::*;
#[test]
fn test_full_lowering() {