1
//! End-reachability (`is_live`) edge rules of `FunctionEmitter` (plan 0014 P4).
2

            
3
use wasm_encoder::{BlockType, Catch};
4

            
5
use super::FunctionEmitter;
6

            
7
const EMPTY: BlockType = BlockType::Empty;
8

            
9
28
fn closed(emit: &mut FunctionEmitter) -> bool {
10
28
    emit.block_end().expect("matching end");
11
28
    emit.frame.is_live()
12
28
}
13

            
14
#[test]
15
1
fn br_marks_its_target_and_kills_the_rest() {
16
1
    let mut emit = FunctionEmitter::new();
17
1
    emit.block_start();
18
1
    emit.block_start();
19
1
    emit.br(1);
20
1
    assert!(!emit.frame.is_live());
21
1
    assert!(!closed(&mut emit), "inner end is not targeted");
22
1
    assert!(closed(&mut emit), "outer end is the br target");
23
1
}
24

            
25
#[test]
26
1
fn br_if_marks_its_target_and_stays_live() {
27
1
    let mut emit = FunctionEmitter::new();
28
1
    emit.block_start();
29
1
    emit.br_if(0);
30
1
    assert!(emit.frame.is_live());
31
1
    emit.unreachable();
32
1
    assert!(closed(&mut emit));
33
1
}
34

            
35
#[test]
36
1
fn br_table_marks_every_target_and_the_default() {
37
1
    let mut emit = FunctionEmitter::new();
38
1
    emit.block_start();
39
1
    emit.block_start();
40
1
    emit.block_start();
41
1
    emit.block_start();
42
1
    emit.br_table(&[1, 2], 3);
43
1
    assert!(!emit.frame.is_live());
44
1
    assert!(!closed(&mut emit), "depth 0 is not a target");
45
1
    assert!(closed(&mut emit), "depth 1 is the first listed target");
46
1
    emit.unreachable();
47
1
    assert!(closed(&mut emit), "depth 2 is the second listed target");
48
1
    emit.unreachable();
49
1
    assert!(closed(&mut emit), "depth 3 is the default");
50
1
}
51

            
52
#[test]
53
1
fn throw_kills_live_and_marks_nothing() {
54
1
    let mut emit = FunctionEmitter::new();
55
1
    emit.block_start();
56
1
    emit.throw(0);
57
1
    assert!(!emit.frame.is_live());
58
1
    assert!(!closed(&mut emit));
59
1
}
60

            
61
#[test]
62
1
fn unreachable_kills_live_and_marks_nothing() {
63
1
    let mut emit = FunctionEmitter::new();
64
1
    emit.block_start();
65
1
    emit.unreachable();
66
1
    assert!(!emit.frame.is_live());
67
1
    assert!(!closed(&mut emit));
68
1
}
69

            
70
#[test]
71
1
fn an_edge_emitted_while_dead_marks_nothing() {
72
1
    let mut emit = FunctionEmitter::new();
73
1
    emit.block_start();
74
1
    emit.block_start();
75
1
    emit.try_table(EMPTY, &[]);
76
1
    emit.throw(0);
77
1
    assert!(!closed(&mut emit), "sealed try_table is not end-reachable");
78
1
    emit.br(1);
79
1
    emit.br_if(1);
80
1
    emit.br_table(&[1], 1);
81
1
    assert!(!closed(&mut emit), "dead branches target nothing");
82
1
    assert!(!closed(&mut emit), "the outer end stays unreachable");
83
1
}
84

            
85
#[test]
86
1
fn block_and_loop_inherit_live_when_opened() {
87
1
    let mut emit = FunctionEmitter::new();
88
1
    emit.block_start();
89
1
    assert!(emit.frame.is_live());
90
1
    emit.loop_start();
91
1
    assert!(emit.frame.is_live());
92
1
    emit.unreachable();
93
1
    emit.block_start();
94
1
    assert!(!emit.frame.is_live());
95
1
    emit.loop_start();
96
1
    assert!(!emit.frame.is_live());
97
1
}
98

            
99
#[test]
100
1
fn block_is_end_reachable_by_fallthrough() {
101
1
    let mut emit = FunctionEmitter::new();
102
1
    emit.block_start();
103
1
    emit.i32_const(1);
104
1
    assert!(closed(&mut emit));
105
1
}
106

            
107
#[test]
108
1
fn block_is_end_reachable_by_a_branch_alone() {
109
1
    let mut emit = FunctionEmitter::new();
110
1
    emit.block_start_typed(EMPTY);
111
1
    emit.br(0);
112
1
    assert!(closed(&mut emit));
113
1
}
114

            
115
#[test]
116
1
fn a_branch_to_a_loop_does_not_make_its_end_reachable() {
117
1
    let mut emit = FunctionEmitter::new();
118
1
    emit.loop_start();
119
1
    emit.br(0);
120
1
    assert!(!closed(&mut emit));
121
1
}
122

            
123
#[test]
124
1
fn if_with_else_is_reachable_when_the_then_arm_falls_through() {
125
1
    let mut emit = FunctionEmitter::new();
126
1
    emit.if_block(EMPTY);
127
1
    emit.i32_const(1);
128
1
    emit.else_block();
129
1
    assert!(emit.frame.is_live(), "else restarts at the entry live");
130
1
    emit.unreachable();
131
1
    assert!(closed(&mut emit));
132
1
}
133

            
134
#[test]
135
1
fn else_restarts_live_after_a_diverging_then_arm() {
136
1
    let mut emit = FunctionEmitter::new();
137
1
    emit.if_block(EMPTY);
138
1
    emit.unreachable();
139
1
    emit.else_block();
140
1
    assert!(emit.frame.is_live(), "else restarts at the entry live");
141
1
    assert!(closed(&mut emit), "the else arm falls through");
142
1
}
143

            
144
#[test]
145
1
fn if_with_else_is_dead_when_both_arms_diverge() {
146
1
    let mut emit = FunctionEmitter::new();
147
1
    emit.if_block(EMPTY);
148
1
    emit.unreachable();
149
1
    emit.else_block();
150
1
    emit.unreachable();
151
1
    assert!(!closed(&mut emit));
152
1
}
153

            
154
#[test]
155
1
fn if_without_else_is_reachable_through_the_implicit_false_path() {
156
1
    let mut emit = FunctionEmitter::new();
157
1
    emit.if_block(EMPTY);
158
1
    emit.unreachable();
159
1
    assert!(closed(&mut emit));
160
1
}
161

            
162
#[test]
163
1
fn an_if_opened_dead_stays_dead() {
164
1
    let mut emit = FunctionEmitter::new();
165
1
    emit.unreachable();
166
1
    emit.if_block(EMPTY);
167
1
    emit.else_block();
168
1
    assert!(!emit.frame.is_live());
169
1
    assert!(!closed(&mut emit));
170
1
}
171

            
172
#[test]
173
1
fn try_table_marks_its_catch_labels_outside_itself() {
174
1
    let mut live = FunctionEmitter::new();
175
1
    live.block_start();
176
1
    live.try_table(EMPTY, &[Catch::All { label: 0 }]);
177
1
    live.throw(0);
178
1
    assert!(!closed(&mut live));
179
1
    assert!(closed(&mut live), "the catch label names the block's end");
180

            
181
1
    let mut dead = FunctionEmitter::new();
182
1
    dead.block_start();
183
1
    dead.unreachable();
184
1
    dead.try_table(EMPTY, &[Catch::All { label: 0 }]);
185
1
    dead.block_end().expect("end try_table");
186
1
    assert!(!closed(&mut dead));
187
1
}
188

            
189
#[test]
190
1
fn try_table_marks_every_catch_label() {
191
1
    let mut emit = FunctionEmitter::new();
192
1
    emit.block_start();
193
1
    emit.block_start();
194
1
    emit.block_start();
195
1
    emit.try_table(
196
1
        EMPTY,
197
1
        &[Catch::One { tag: 0, label: 1 }, Catch::All { label: 2 }],
198
1
    );
199
1
    emit.throw(0);
200
1
    assert!(!closed(&mut emit), "the try_table itself");
201
1
    assert!(!closed(&mut emit), "label 0 is not a catch target");
202
1
    assert!(closed(&mut emit), "label 1 is the first catch");
203
1
    emit.unreachable();
204
1
    assert!(closed(&mut emit), "label 2 is the second catch");
205
1
}
206

            
207
#[test]
208
1
fn try_table_whose_body_throws_is_not_end_reachable() {
209
1
    let mut emit = FunctionEmitter::new();
210
1
    emit.try_table(EMPTY, &[]);
211
1
    emit.throw(0);
212
1
    assert!(!closed(&mut emit));
213
1
}
214

            
215
#[test]
216
1
fn live_after_end_is_the_closed_frames_answer_not_reset() {
217
1
    let mut emit = FunctionEmitter::new();
218
1
    emit.block_start();
219
1
    emit.unreachable();
220
1
    emit.block_end().expect("matching end");
221
1
    assert!(!emit.is_live(), "semantic state is the closed frame's");
222
1
    assert!(!emit.is_reachable(), "the dead fallthrough is sealed");
223
1
}
224

            
225
#[test]
226
1
fn splicing_a_dead_scratch_makes_the_parent_dead() {
227
1
    let mut emit = FunctionEmitter::new();
228
1
    let mut scratch = FunctionEmitter::new();
229
1
    scratch.block_start();
230
1
    scratch.unreachable();
231
1
    scratch.block_end().expect("matching end");
232
1
    assert!(!scratch.frame.is_live());
233
1
    emit.splice(&scratch).expect("balanced scratch");
234
1
    assert!(!emit.frame.is_live());
235
1
    assert!(!emit.is_reachable(), "the scratch's seal carries over");
236
1
}
237

            
238
#[test]
239
1
fn splicing_a_live_scratch_keeps_a_dead_parent_dead() {
240
1
    let mut emit = FunctionEmitter::new();
241
1
    emit.block_start();
242
1
    emit.unreachable();
243
1
    emit.block_end().expect("matching end");
244
1
    let scratch = FunctionEmitter::new();
245
1
    assert!(scratch.frame.is_live());
246
1
    emit.splice(&scratch).expect("balanced scratch");
247
1
    assert!(!emit.frame.is_live());
248
1
}
249

            
250
#[test]
251
1
fn live_never_outlives_reachability() {
252
1
    let mut emit = FunctionEmitter::new();
253
8
    let check = |emit: &FunctionEmitter| {
254
8
        assert!(emit.is_reachable() || !emit.frame.is_live());
255
8
    };
256
1
    emit.block_start();
257
1
    check(&emit);
258
1
    emit.if_block(EMPTY);
259
1
    emit.br(1);
260
1
    check(&emit);
261
1
    emit.else_block();
262
1
    check(&emit);
263
1
    emit.throw(0);
264
1
    check(&emit);
265
1
    emit.block_end().expect("end if");
266
1
    check(&emit);
267
1
    emit.loop_start();
268
1
    emit.unreachable();
269
1
    check(&emit);
270
1
    emit.block_end().expect("end loop");
271
1
    check(&emit);
272
1
    emit.block_end().expect("end block");
273
1
    check(&emit);
274
1
}