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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use super::labels::LabelRef;
use crate::module::BlockType;
/// A Wasm `block` control flow frame.
#[derive(Debug, Copy, Clone)]
pub struct BlockControlFrame {
/// The type of the [`BlockControlFrame`].
block_type: BlockType,
/// The value stack height upon entering the [`BlockControlFrame`].
stack_height: u32,
/// Label representing the end of the [`BlockControlFrame`].
end_label: LabelRef,
}
impl BlockControlFrame {
/// Creates a new [`BlockControlFrame`].
pub fn new(block_type: BlockType, end_label: LabelRef, stack_height: u32) -> Self {
Self {
block_type,
stack_height,
end_label,
}
}
/// Returns the label for the branch destination of the [`BlockControlFrame`].
///
/// # Note
///
/// Branches to [`BlockControlFrame`] jump to the end of the frame.
pub fn branch_destination(&self) -> LabelRef {
self.end_label
}
/// Returns the label to the end of the [`BlockControlFrame`].
pub fn end_label(&self) -> LabelRef {
self.end_label
}
/// Returns the value stack height upon entering the [`BlockControlFrame`].
pub fn stack_height(&self) -> u32 {
self.stack_height
}
/// Returns the [`BlockType`] of the [`BlockControlFrame`].
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// A Wasm `loop` control flow frame.
#[derive(Debug, Copy, Clone)]
pub struct LoopControlFrame {
/// The type of the [`LoopControlFrame`].
block_type: BlockType,
/// The value stack height upon entering the [`LoopControlFrame`].
stack_height: u32,
/// Label representing the head of the [`LoopControlFrame`].
head_label: LabelRef,
}
impl LoopControlFrame {
/// Creates a new [`LoopControlFrame`].
pub fn new(block_type: BlockType, head_label: LabelRef, stack_height: u32) -> Self {
Self {
block_type,
stack_height,
head_label,
}
}
/// Returns the label for the branch destination of the [`LoopControlFrame`].
///
/// # Note
///
/// Branches to [`LoopControlFrame`] jump to the head of the loop.
pub fn branch_destination(&self) -> LabelRef {
self.head_label
}
/// Returns the value stack height upon entering the [`LoopControlFrame`].
pub fn stack_height(&self) -> u32 {
self.stack_height
}
/// Returns the [`BlockType`] of the [`LoopControlFrame`].
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// A Wasm `if` and `else` control flow frames.
#[derive(Debug, Copy, Clone)]
pub struct IfControlFrame {
/// The type of the [`IfControlFrame`].
block_type: BlockType,
/// The value stack height upon entering the [`IfControlFrame`].
stack_height: u32,
/// Label representing the end of the [`IfControlFrame`].
end_label: LabelRef,
/// Label representing the optional `else` branch of the [`IfControlFrame`].
else_label: LabelRef,
/// End of `then` branch is reachable.
///
/// # Note
///
/// - This is `None` upon entering the `if` control flow frame.
/// Once the optional `else` case or the `end` of the `if` control
/// flow frame is reached this field will be computed.
/// - This information is important to know how to continue after a
/// diverging `if` control flow frame.
/// - An `end_of_else_is_reachable` field is not needed since it will
/// be easily computed once the translation reaches the end of the `if`.
end_of_then_is_reachable: Option<bool>,
}
impl IfControlFrame {
/// Creates a new [`IfControlFrame`].
pub fn new(
block_type: BlockType,
end_label: LabelRef,
else_label: LabelRef,
stack_height: u32,
) -> Self {
assert_ne!(
end_label, else_label,
"end and else labels must be different"
);
Self {
block_type,
stack_height,
end_label,
else_label,
end_of_then_is_reachable: None,
}
}
/// Returns the label for the branch destination of the [`IfControlFrame`].
///
/// # Note
///
/// Branches to [`IfControlFrame`] jump to the end of the if and else frame.
pub fn branch_destination(&self) -> LabelRef {
self.end_label
}
/// Returns the label to the end of the [`IfControlFrame`].
pub fn end_label(&self) -> LabelRef {
self.end_label
}
/// Returns the label to the optional `else` of the [`IfControlFrame`].
pub fn else_label(&self) -> LabelRef {
self.else_label
}
/// Returns the value stack height upon entering the [`IfControlFrame`].
pub fn stack_height(&self) -> u32 {
self.stack_height
}
/// Returns the [`BlockType`] of the [`IfControlFrame`].
pub fn block_type(&self) -> BlockType {
self.block_type
}
/// Updates the reachability of the end of the `then` branch.
///
/// # Panics
///
/// If this information has already been provided prior.
pub fn update_end_of_then_reachability(&mut self, reachable: bool) {
assert!(self.end_of_then_is_reachable.is_none());
self.end_of_then_is_reachable = Some(reachable);
}
}
/// An unreachable control flow frame of any kind.
#[derive(Debug, Copy, Clone)]
pub struct UnreachableControlFrame {
/// The non-SSA input and output types of the unreachable control frame.
pub block_type: BlockType,
/// The kind of the unreachable control flow frame.
pub kind: ControlFrameKind,
}
/// The kind of a control flow frame.
#[derive(Debug, Copy, Clone)]
pub enum ControlFrameKind {
/// A basic `block` control flow frame.
Block,
/// A `loop` control flow frame.
Loop,
/// An `if` and `else` block control flow frame.
If,
}
impl UnreachableControlFrame {
/// Creates a new [`UnreachableControlFrame`] with the given type and kind.
pub fn new(kind: ControlFrameKind, block_type: BlockType) -> Self {
Self { block_type, kind }
}
/// Returns the [`ControlFrameKind`] of the [`UnreachableControlFrame`].
pub fn kind(&self) -> ControlFrameKind {
self.kind
}
/// Returns the [`BlockType`] of the [`IfControlFrame`].
pub fn block_type(&self) -> BlockType {
self.block_type
}
}
/// A control flow frame.
#[derive(Debug, Copy, Clone)]
pub enum ControlFrame {
/// Basic block control frame.
Block(BlockControlFrame),
/// Loop control frame.
Loop(LoopControlFrame),
/// If and else control frame.
If(IfControlFrame),
/// An unreachable control frame.
Unreachable(UnreachableControlFrame),
}
impl From<BlockControlFrame> for ControlFrame {
fn from(frame: BlockControlFrame) -> Self {
Self::Block(frame)
}
}
impl From<LoopControlFrame> for ControlFrame {
fn from(frame: LoopControlFrame) -> Self {
Self::Loop(frame)
}
}
impl From<IfControlFrame> for ControlFrame {
fn from(frame: IfControlFrame) -> Self {
Self::If(frame)
}
}
impl From<UnreachableControlFrame> for ControlFrame {
fn from(frame: UnreachableControlFrame) -> Self {
Self::Unreachable(frame)
}
}
impl ControlFrame {
/// Returns the [`ControlFrameKind`] of the [`ControlFrame`].
pub fn kind(&self) -> ControlFrameKind {
match self {
ControlFrame::Block(_) => ControlFrameKind::Block,
ControlFrame::Loop(_) => ControlFrameKind::Loop,
ControlFrame::If(_) => ControlFrameKind::If,
ControlFrame::Unreachable(frame) => frame.kind(),
}
}
/// Returns the label for the branch destination of the [`ControlFrame`].
pub fn branch_destination(&self) -> LabelRef {
match self {
Self::Block(frame) => frame.branch_destination(),
Self::Loop(frame) => frame.branch_destination(),
Self::If(frame) => frame.branch_destination(),
Self::Unreachable(frame) => panic!(
"tried to get `branch_destination` for an unreachable control frame: {frame:?}"
),
}
}
/// Returns a label which should be resolved at the `End` Wasm opcode.
///
/// All [`ControlFrame`] kinds have it except [`ControlFrame::Loop`].
/// In order to a [`ControlFrame::Loop`] to branch outside it is required
/// to be wrapped in another control frame such as [`ControlFrame::Block`].
pub fn end_label(&self) -> LabelRef {
match self {
Self::Block(frame) => frame.end_label(),
Self::If(frame) => frame.end_label(),
Self::Loop(frame) => {
panic!("tried to get `end_label` for a loop control frame: {frame:?}")
}
Self::Unreachable(frame) => {
panic!("tried to get `end_label` for an unreachable control frame: {frame:?}")
}
}
}
/// Returns the value stack height upon entering the control flow frame.
pub fn stack_height(&self) -> Option<u32> {
match self {
Self::Block(frame) => Some(frame.stack_height()),
Self::Loop(frame) => Some(frame.stack_height()),
Self::If(frame) => Some(frame.stack_height()),
Self::Unreachable(_frame) => None,
}
}
/// Returns the [`BlockType`] of the control flow frame.
pub fn block_type(&self) -> BlockType {
match self {
Self::Block(frame) => frame.block_type(),
Self::Loop(frame) => frame.block_type(),
Self::If(frame) => frame.block_type(),
Self::Unreachable(frame) => frame.block_type(),
}
}
/// Returns `true` if the control flow frame is reachable.
pub fn is_reachable(&self) -> bool {
!matches!(self, ControlFrame::Unreachable(_))
}
}