Co-authored-by: Nilton Constantino <nilton.constantino@visma.com> Reviewed-on: #8
80 lines
2.6 KiB
Rust
80 lines
2.6 KiB
Rust
//! # IR Module Structure
|
|
//!
|
|
//! This module defines the structure of the Intermediate Representation (IR).
|
|
//! 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_core::const_pool::ConstPool;
|
|
use crate::ir_core::ids::FunctionId;
|
|
use crate::ir_vm::instr::Instruction;
|
|
use crate::ir_vm::types::Type;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// A `Module` is the top-level container for a compiled program or library.
|
|
/// It contains a collection of global variables, functions, and a constant pool.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Module {
|
|
/// The name of the module (usually derived from the project name).
|
|
pub name: String,
|
|
/// Shared constant pool for this module.
|
|
pub const_pool: ConstPool,
|
|
/// List of all functions defined in this module.
|
|
pub functions: Vec<Function>,
|
|
/// List of all global variables available in this module.
|
|
pub globals: Vec<Global>,
|
|
}
|
|
|
|
/// Represents a function in the IR.
|
|
///
|
|
/// Functions consist of a signature (name, parameters, return type) and a body
|
|
/// which is a flat list of IR instructions.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Function {
|
|
/// The unique identifier of the function.
|
|
pub id: FunctionId,
|
|
/// The unique name of the function.
|
|
pub name: String,
|
|
/// The list of input parameters.
|
|
pub params: Vec<Param>,
|
|
/// The type of value this function returns.
|
|
pub return_type: Type,
|
|
/// The sequence of instructions that make up the function's logic.
|
|
pub body: Vec<Instruction>,
|
|
|
|
pub param_slots: u16,
|
|
pub local_slots: u16,
|
|
pub return_slots: u16,
|
|
}
|
|
|
|
/// A parameter passed to a function.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Param {
|
|
/// The name of the parameter (useful for debugging and symbols).
|
|
pub name: String,
|
|
/// The data type of the parameter.
|
|
pub r#type: Type,
|
|
}
|
|
|
|
/// A global variable accessible by any function in the module.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Global {
|
|
/// The name of the global variable.
|
|
pub name: String,
|
|
/// The data type of the variable.
|
|
pub r#type: Type,
|
|
/// The unique memory slot index assigned to this global variable.
|
|
pub slot: u32,
|
|
}
|
|
|
|
impl Module {
|
|
/// Creates a new, empty module with the given name.
|
|
pub fn new(name: String) -> Self {
|
|
Self {
|
|
name,
|
|
const_pool: ConstPool::new(),
|
|
functions: Vec::new(),
|
|
globals: Vec::new(),
|
|
}
|
|
}
|
|
}
|