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
@@ -4,13 +4,13 @@
//! converting the Intermediate Representation (IR) into the binary Prometeu ByteCode (PBC) format.
//!
//! It performs two main tasks:
//! 1. **Instruction Lowering**: Translates `ir::Instruction` into `prometeu_bytecode::asm::Asm` ops.
//! 1. **Instruction Lowering**: Translates `ir_vm::Instruction` into `prometeu_bytecode::asm::Asm` ops.
//! 2. **Symbol Mapping**: Associates bytecode offsets (Program Counter) with source code locations.
use crate::common::files::FileManager;
use crate::common::symbols::Symbol;
use crate::ir;
use crate::ir::instr::InstrKind;
use crate::ir_vm;
use crate::ir_vm::instr::InstrKind;
use crate::ir_core::ConstantValue;
use anyhow::{anyhow, Result};
use prometeu_bytecode::asm::{assemble, update_pc_by_operand, Asm, Operand};
@@ -26,7 +26,7 @@ pub struct EmitResult {
}
/// Entry point for emitting a bytecode module from the IR.
pub fn emit_module(module: &ir::Module, file_manager: &FileManager) -> Result<EmitResult> {
pub fn emit_module(module: &ir_vm::Module, file_manager: &FileManager) -> Result<EmitResult> {
let mut emitter = BytecodeEmitter::new(file_manager);
emitter.emit(module)
}
@@ -69,7 +69,7 @@ impl<'a> BytecodeEmitter<'a> {
}
/// Transforms an IR module into a binary PBC file.
fn emit(&mut self, module: &ir::Module) -> Result<EmitResult> {
fn emit(&mut self, module: &ir_vm::Module) -> Result<EmitResult> {
let mut asm_instrs = Vec::new();
let mut ir_instr_map = Vec::new(); // Maps Asm index to IR instruction (for symbols)
@@ -232,9 +232,9 @@ impl<'a> BytecodeEmitter<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::module::{Module, Function};
use crate::ir::instr::{Instruction, InstrKind};
use crate::ir::types::Type;
use crate::ir_vm::module::{Module, Function};
use crate::ir_vm::instr::{Instruction, InstrKind};
use crate::ir_vm::types::Type;
use crate::ir_core::ids::FunctionId;
use crate::ir_core::const_pool::ConstantValue;
use crate::common::files::FileManager;
+2 -2
View File
@@ -8,7 +8,7 @@ use crate::common::config::ProjectConfig;
use crate::common::files::FileManager;
use crate::common::symbols::Symbol;
use crate::frontends::Frontend;
use crate::ir;
use crate::ir_vm;
use anyhow::Result;
use std::path::Path;
@@ -87,7 +87,7 @@ pub fn compile(project_dir: &Path) -> Result<CompilationUnit> {
// 3. IR Validation
// Ensures the generated IR is sound and doesn't violate any VM constraints
// before we spend time generating bytecode.
ir::validate::validate_module(&ir_module)
ir_vm::validate::validate_module(&ir_module)
.map_err(|bundle| anyhow::anyhow!("IR Validation failed: {:?}", bundle))?;
// 4. Emit Bytecode
@@ -1,5 +1,5 @@
use crate::common::diagnostics::DiagnosticBundle;
use crate::ir;
use crate::ir_vm;
use std::path::Path;
use crate::common::files::FileManager;
@@ -13,5 +13,5 @@ pub trait Frontend {
&self,
entry: &Path,
file_manager: &mut FileManager,
) -> Result<ir::Module, DiagnosticBundle>;
) -> Result<ir_vm::Module, DiagnosticBundle>;
}
@@ -21,7 +21,7 @@ pub use lowering::Lowerer;
use crate::common::diagnostics::DiagnosticBundle;
use crate::common::files::FileManager;
use crate::frontends::Frontend;
use crate::ir;
use crate::ir_vm;
use crate::lowering::core_to_vm;
use std::path::Path;
@@ -36,7 +36,7 @@ impl Frontend for PbsFrontend {
&self,
entry: &Path,
file_manager: &mut FileManager,
) -> Result<ir::Module, DiagnosticBundle> {
) -> Result<ir_vm::Module, DiagnosticBundle> {
let source = std::fs::read_to_string(entry).map_err(|e| {
DiagnosticBundle::error(format!("Failed to read file: {}", e), None)
})?;
@@ -4,8 +4,8 @@
//! The IR is a higher-level representation of the program than bytecode, but lower
//! than the source code AST. It is organized into Modules, Functions, and Globals.
use crate::ir::instr::Instruction;
use crate::ir::types::Type;
use crate::ir_vm::instr::Instruction;
use crate::ir_vm::types::Type;
use crate::ir_core::const_pool::ConstPool;
use crate::ir_core::ids::FunctionId;
use serde::{Deserialize, Serialize};
@@ -1,5 +1,5 @@
use crate::common::diagnostics::DiagnosticBundle;
use crate::ir::module::Module;
use crate::ir_vm::module::Module;
pub fn validate_module(_module: &Module) -> Result<(), DiagnosticBundle> {
// TODO: Implement common IR validations:
+1 -1
View File
@@ -38,7 +38,7 @@
//! See the [`compiler`] module for the main entry point to trigger a compilation programmatically.
pub mod common;
pub mod ir;
pub mod ir_vm;
pub mod ir_core;
pub mod lowering;
pub mod backend;
@@ -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() {