|
| 1 | +use std::collections::BTreeMap; |
| 2 | +use std::fmt::{Display, Formatter, Result as FmtResult}; |
| 3 | +use super::instruction::IntermediateInstruction; |
| 4 | +use lean_vm::Label; |
| 5 | + |
| 6 | +/// Container for the complete intermediate representation of a program. |
| 7 | +/// |
| 8 | +/// This structure holds all the compiled intermediate bytecode along with |
| 9 | +/// metadata needed for execution and analysis. |
| 10 | +#[derive(Debug, Clone)] |
| 11 | +pub struct IntermediateBytecode { |
| 12 | + /// Main bytecode organized by function labels. |
| 13 | + /// |
| 14 | + /// Each label corresponds to a function entry point. |
| 15 | + pub bytecode: BTreeMap<Label, Vec<IntermediateInstruction>>, |
| 16 | + |
| 17 | + /// Match statement bytecode blocks. |
| 18 | + /// |
| 19 | + /// Each match statement produces multiple case blocks. |
| 20 | + pub match_blocks: Vec<Vec<Vec<IntermediateInstruction>>>, |
| 21 | + |
| 22 | + /// Memory requirements for each function. |
| 23 | + /// |
| 24 | + /// Maps function names to their stack frame size. |
| 25 | + pub memory_size_per_function: BTreeMap<String, usize>, |
| 26 | +} |
| 27 | + |
| 28 | +impl Display for IntermediateBytecode { |
| 29 | + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { |
| 30 | + for (label, instructions) in &self.bytecode { |
| 31 | + writeln!(f, "\n{label}:")?; |
| 32 | + for instruction in instructions { |
| 33 | + writeln!(f, " {instruction}")?; |
| 34 | + } |
| 35 | + } |
| 36 | + for (i, match_blocks) in self.match_blocks.iter().enumerate() { |
| 37 | + writeln!(f, "\nMatch {i}:")?; |
| 38 | + for (j, case) in match_blocks.iter().enumerate() { |
| 39 | + writeln!(f, " Case {j}:")?; |
| 40 | + for instruction in case { |
| 41 | + writeln!(f, " {instruction}")?; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + writeln!(f, "\nMemory size per function:")?; |
| 46 | + for (function_name, size) in &self.memory_size_per_function { |
| 47 | + writeln!(f, "{function_name}: {size}")?; |
| 48 | + } |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
0 commit comments