Skip to content

Commit 7e50931

Browse files
committed
intermediate bytecode: mv intermediate value
1 parent 6aa8ee5 commit 7e50931

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

crates/leanVm/src/bytecode/operation.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt;
22

33
use p3_field::Field;
44

5-
use crate::constant::F;
5+
use crate::{constant::F, intermediate_bytecode::HighLevelOperation};
66

77
/// The basic arithmetic operations supported by the VM's `Computation` instruction.
88
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -58,6 +58,18 @@ impl Operation {
5858
}
5959
}
6060

61+
impl TryFrom<HighLevelOperation> for Operation {
62+
type Error = String;
63+
64+
fn try_from(value: HighLevelOperation) -> Result<Self, Self::Error> {
65+
match value {
66+
HighLevelOperation::Add => Ok(Self::Add),
67+
HighLevelOperation::Mul => Ok(Self::Mul),
68+
_ => Err(format!("Cannot convert {value:?} to +/x")),
69+
}
70+
}
71+
}
72+
6173
impl fmt::Display for Operation {
6274
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6375
match self {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::fmt;
2+
3+
use crate::lang::{ConstExpression, Label};
4+
5+
#[derive(Debug, Clone, PartialEq, Eq)]
6+
pub enum IntermediateValue {
7+
Constant(ConstExpression),
8+
Fp,
9+
MemoryAfterFp { offset: ConstExpression }, // m[fp + offset]
10+
}
11+
12+
impl IntermediateValue {
13+
#[must_use]
14+
pub const fn label(label: Label) -> Self {
15+
Self::Constant(ConstExpression::label(label))
16+
}
17+
18+
#[must_use]
19+
pub const fn is_constant(&self) -> bool {
20+
matches!(self, Self::Constant(_))
21+
}
22+
}
23+
24+
impl From<ConstExpression> for IntermediateValue {
25+
fn from(value: ConstExpression) -> Self {
26+
Self::Constant(value)
27+
}
28+
}
29+
30+
impl fmt::Display for IntermediateValue {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
match self {
33+
Self::Constant(value) => write!(f, "{value}"),
34+
Self::Fp => write!(f, "fp"),
35+
Self::MemoryAfterFp { offset } => write!(f, "m[fp + {offset}]"),
36+
}
37+
}
38+
}

crates/leanVm/src/intermediate_bytecode.rs renamed to crates/leanVm/src/intermediate_bytecode/mod.rs

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,22 @@ use crate::{
88
lang::{Label, const_expr::ConstExpression},
99
};
1010

11+
pub mod intermediate_value;
12+
pub use intermediate_value::*;
13+
1114
#[derive(Debug, Clone)]
1215
pub struct IntermediateBytecode {
1316
pub bytecode: BTreeMap<Label, Vec<IntermediateInstruction>>,
1417
pub memory_size_per_function: BTreeMap<String, usize>,
1518
}
1619

17-
#[derive(Debug, Clone, PartialEq, Eq)]
18-
pub enum IntermediateValue {
19-
Constant(ConstExpression),
20-
Fp,
21-
MemoryAfterFp { offset: ConstExpression }, // m[fp + offset]
22-
}
23-
24-
impl From<ConstExpression> for IntermediateValue {
25-
fn from(value: ConstExpression) -> Self {
26-
Self::Constant(value)
27-
}
28-
}
29-
impl TryFrom<HighLevelOperation> for Operation {
30-
type Error = String;
31-
32-
fn try_from(value: HighLevelOperation) -> Result<Self, Self::Error> {
33-
match value {
34-
HighLevelOperation::Add => Ok(Self::Add),
35-
HighLevelOperation::Mul => Ok(Self::Mul),
36-
_ => Err(format!("Cannot convert {value:?} to +/x")),
37-
}
38-
}
39-
}
40-
4120
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4221
pub enum IntermediaryMemOrFpOrConstant {
4322
MemoryAfterFp { offset: ConstExpression }, // m[fp + offset]
4423
Fp,
4524
Constant(ConstExpression),
4625
}
4726

48-
impl IntermediateValue {
49-
#[must_use]
50-
pub const fn label(label: Label) -> Self {
51-
Self::Constant(ConstExpression::label(label))
52-
}
53-
54-
#[must_use]
55-
pub const fn is_constant(&self) -> bool {
56-
matches!(self, Self::Constant(_))
57-
}
58-
}
59-
6027
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6128
pub enum HighLevelOperation {
6229
Add,
@@ -193,16 +160,6 @@ impl IntermediateInstruction {
193160
}
194161
}
195162

196-
impl fmt::Display for IntermediateValue {
197-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198-
match self {
199-
Self::Constant(value) => write!(f, "{value}"),
200-
Self::Fp => write!(f, "fp"),
201-
Self::MemoryAfterFp { offset } => write!(f, "m[fp + {offset}]"),
202-
}
203-
}
204-
}
205-
206163
impl fmt::Display for IntermediaryMemOrFpOrConstant {
207164
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208165
match self {

0 commit comments

Comments
 (0)