This commit is contained in:
Nilton Constantino
2026-01-28 18:58:31 +00:00
parent 5bd26a2991
commit ced7ff0607
21 changed files with 934 additions and 37 deletions
@@ -30,8 +30,11 @@ pub fn lower_function(core_func: &ir_core::Function) -> Result<ir::Function> {
let mut vm_func = ir::Function {
id: core_func.id,
name: core_func.name.clone(),
params: vec![], // Params are not yet represented in Core IR Function
return_type: ir::Type::Null, // Return type is not yet represented in Core IR Function
params: core_func.params.iter().map(|p| ir::Param {
name: p.name.clone(),
r#type: lower_type(&p.ty),
}).collect(),
return_type: lower_type(&core_func.return_type),
body: vec![],
};
@@ -43,29 +46,38 @@ pub fn lower_function(core_func: &ir_core::Function) -> Result<ir::Function> {
));
for instr in &block.instrs {
match instr {
ir_core::Instr::PushConst(id) => {
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::PushConst(*id),
None,
));
let kind = match instr {
ir_core::Instr::PushConst(id) => ir::InstrKind::PushConst(*id),
ir_core::Instr::Call(func_id, arg_count) => ir::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_core::Instr::Free | ir_core::Instr::ReadGate | ir_core::Instr::WriteGate => {
// HIP effects are not yet supported in VM IR, so we emit Nop or similar.
// For now, let's use Nop.
ir::InstrKind::Nop
}
ir_core::Instr::Call(func_id, arg_count) => {
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::Call {
func_id: *func_id,
arg_count: *arg_count
},
None,
));
}
ir_core::Instr::Syscall(id) => {
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::Syscall(*id),
None,
));
}
}
};
vm_func.body.push(ir::Instruction::new(kind, None));
}
match &block.terminator {
@@ -78,8 +90,35 @@ pub fn lower_function(core_func: &ir_core::Function) -> Result<ir::Function> {
None,
));
}
ir_core::Terminator::JumpIfFalse { target, else_target } => {
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::JmpIfFalse(ir::Label(format!("block_{}", target))),
None,
));
vm_func.body.push(ir::Instruction::new(
ir::InstrKind::Jmp(ir::Label(format!("block_{}", else_target))),
None,
));
}
}
}
Ok(vm_func)
}
fn lower_type(ty: &ir_core::Type) -> ir::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::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,
}
}