Skip to content

Commit 1afebde

Browse files
committed
compiler: reorg intermediate representation
1 parent a1690c4 commit 1afebde

File tree

11 files changed

+278
-207
lines changed

11 files changed

+278
-207
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
Counter, F,
3-
intermediate_bytecode::HighLevelOperation,
3+
ir::HighLevelOperation,
44
lang::{
55
Boolean, ConstExpression, ConstMallocLabel, Expression, Function, Line, Program,
66
SimpleExpr, Var,

crates/lean_compiler/src/b_compile_intermediate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{F, a_simplify_lang::*, intermediate_bytecode::*, lang::*, precompiles::*};
1+
use crate::{F, a_simplify_lang::*, ir::*, lang::*, precompiles::*};
22
use lean_vm::*;
33
use p3_field::Field;
44
use std::{

crates/lean_compiler/src/c_compile_final.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{F, PUBLIC_INPUT_START, ZERO_VEC_PTR, intermediate_bytecode::*, lang::*};
1+
use crate::{F, PUBLIC_INPUT_START, ZERO_VEC_PTR, ir::*, lang::*};
22
use lean_vm::*;
33
use p3_field::{PrimeCharacteristicRing, PrimeField32};
44
use std::collections::BTreeMap;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)