1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use cairo_lang_semantic::ConcreteFunctionWithBodyId;
use cairo_lang_sierra as sierra;
use cairo_lang_sierra::ids::ConcreteTypeId;
use cairo_lang_sierra::program;
use cairo_lang_utils::{define_short_id, write_comma_separated};
use crate::db::SierraGenGroup;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct LabelLongId {
pub parent: ConcreteFunctionWithBodyId,
pub id: usize,
}
define_short_id!(LabelId, LabelLongId, SierraGenGroup, lookup_intern_label_id);
impl std::fmt::Display for LabelId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "label{}", self.0)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Function {
pub id: sierra::ids::FunctionId,
pub prolog_size: usize,
pub body: Vec<Statement>,
pub entry_point: LabelId,
pub parameters: Vec<program::Param>,
pub ret_types: Vec<sierra::ids::ConcreteTypeId>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Statement {
Sierra(program::GenStatement<LabelId>),
Label(Label),
PushValues(Vec<PushValue>),
}
impl std::fmt::Display for Statement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Statement::Sierra(value) => write!(f, "{}", value),
Statement::Label(Label { id }) => write!(f, "{}:", id),
Statement::PushValues(values) => {
write!(f, "PushValues(")?;
write_comma_separated(
f,
values.iter().map(|PushValue { var, ty, .. }| format!("{var}: {ty}")),
)?;
write!(f, ") -> (")?;
write_comma_separated(
f,
values.iter().map(|PushValue { var_on_stack, dup_var, .. }| {
if let Some(dup_var) = dup_var {
format!("{var_on_stack} {dup_var}")
} else {
format!("{var_on_stack}")
}
}),
)?;
write!(f, ")")
}
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PushValue {
pub var: sierra::ids::VarId,
pub var_on_stack: sierra::ids::VarId,
pub ty: ConcreteTypeId,
pub dup_var: Option<sierra::ids::VarId>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Label {
pub id: LabelId,
}