-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompound.rs
More file actions
520 lines (484 loc) · 18.9 KB
/
compound.rs
File metadata and controls
520 lines (484 loc) · 18.9 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use crate::function::structure::{ControlFlowGraph, Structure};
use crate::graph::{Node, NodeId, Order};
use classfile_parser::code_attribute::Instruction as JVMInstruction;
use std::fmt;
use std::mem::take;
/// Possible short-circuit conditional types for [`Structure::CompoundConditional`]s.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ConditionalKind {
Disjunction, // || OR
Conjunction, // && AND
}
impl fmt::Display for ConditionalKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConditionalKind::Disjunction => write!(f, "||"),
ConditionalKind::Conjunction => write!(f, "&&"),
}
}
}
/// Helper function for [`ControlFlowGraph::structure_compound_conditionals`], returning `true` if
/// and only if the `node` is a conditional branching node (i.e. it branches based on a value).
fn is_conditional_branch(node: &Node<Structure>) -> bool {
match &node.value {
Structure::Block(instructions) => {
// Check loads or computes something, then jumps
instructions.len() >= 2
&& matches!(
instructions.last().unwrap(),
JVMInstruction::IfAcmpeq(_)
| JVMInstruction::IfAcmpne(_)
| JVMInstruction::IfIcmpeq(_)
| JVMInstruction::IfIcmpne(_)
| JVMInstruction::IfIcmplt(_)
| JVMInstruction::IfIcmpge(_)
| JVMInstruction::IfIcmpgt(_)
| JVMInstruction::IfIcmple(_)
| JVMInstruction::Ifeq(_)
| JVMInstruction::Ifne(_)
| JVMInstruction::Iflt(_)
| JVMInstruction::Ifge(_)
| JVMInstruction::Ifgt(_)
| JVMInstruction::Ifle(_)
| JVMInstruction::Ifnonnull(_)
| JVMInstruction::Ifnull(_)
)
}
// Compound conditional is by definition a conditional instruction
Structure::CompoundConditional { .. } => true,
}
}
impl ControlFlowGraph {
/// Helper function for [`ControlFlowGraph::structure_compound_conditionals`], replacing the
/// node at `left_index` with a [`Structure::CompoundConditional`] using nodes at `left_index`
/// and `right_index` as its `left` and `right` expressions, with `false_index` and `true_index`
/// as the false and true branches respectively. The node at `right_index` will be removed.
///
/// The node at `left_index` is effectively rewritten to:
///
/// ```
/// if (!)left_index &&/|| right_index { true_index } else { false_index }
/// ```
fn rewrite_compound_conditional(
&mut self,
kind: ConditionalKind,
left_negated: bool,
left_index: NodeId,
right_index: NodeId,
false_index: NodeId,
true_index: NodeId,
) {
// Extract left and right values to avoid cloning (we'll be replacing left_index and
// removing right_index, so this is safe)
let left_value = take(&mut self[left_index].value);
let right_value = take(&mut self[right_index].value);
// Replace left node with new compound node in graph
self[left_index].value = Structure::CompoundConditional {
kind,
left_negated,
left: Box::new(left_value),
right: Box::new(right_value),
};
// Remove right node from graph
self.remove_node(right_index);
// Remove all of new node's existing outgoing edges
self.remove_all_successors(left_index);
// Connect new node's false/true branches to false_node/true_node respectively
// (note the ordering of these calls is important)
self.add_edge(left_index, /* 0 */ false_index);
self.add_edge(left_index, /* 1 */ true_index);
}
/// Repeatedly rewrites all short-circuit conditional patterns in this control flow graph to
/// single [`Structure::CompoundConditional`] nodes using the algorithm described in Figure 6.34
/// of "Cristina Cifuentes. Reverse Compilation Techniques. PhD thesis, Queensland University of
/// Technology, 1994".
///
/// This is required because short-circuit constructs produce irreducible flow graphs, which
/// would require code duplication to be represented in a structured language like WebAssembly.
///
/// This should be performed before finding loops and two-way conditionals as these may use
/// compound conditionals in their headers/latchings.
///
/// 
pub fn structure_compound_conditionals(&mut self) {
let mut change = true;
while change {
change = false;
for n in self.depth_first(Order::PostOrder).traversal {
let n_node = &self[n];
if n_node.out_degree() == 2 {
let t = n_node.successors[0]; // false branch
let e = n_node.successors[1]; // true branch
let t_node = &self[t];
let e_node = &self[e];
if t_node.out_degree() == 2
&& is_conditional_branch(t_node)
&& t_node.in_degree() == 1
&& t != n
{
if t_node.successors[0] == e {
change = true;
// !n && t
let other_t_edge = t_node.successors[1];
self.rewrite_compound_conditional(
ConditionalKind::Conjunction,
/* left_negated */ true,
/* left */ n,
/* right */ t,
/* false */ e,
/* true */ other_t_edge,
);
} else if t_node.successors[1] == e {
change = true;
// n || t
let other_t_edge = t_node.successors[0];
self.rewrite_compound_conditional(
ConditionalKind::Disjunction,
/* left_negated */ false,
/* left */ n,
/* right */ t,
/* false */ other_t_edge,
/* true */ e,
);
}
} else if e_node.out_degree() == 2
&& is_conditional_branch(e_node)
&& e_node.in_degree() == 1
&& e != n
{
if e_node.successors[0] == t {
change = true;
// n && e
let other_e_edge = e_node.successors[1];
self.rewrite_compound_conditional(
ConditionalKind::Conjunction,
/* left_negated */ false,
/* left */ n,
/* right */ e,
/* false */ t,
/* true */ other_e_edge,
);
} else if e_node.successors[1] == t {
change = true;
// !n || e
let other_e_edge = e_node.successors[0];
self.rewrite_compound_conditional(
ConditionalKind::Disjunction,
/* left_negated */ true,
/* left */ n,
/* right */ e,
/* false */ other_e_edge,
/* true */ t,
);
}
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use crate::function::structure::{ConditionalKind, ControlFlowGraph, Structure};
use crate::graph::NodeId;
use crate::tests::load_basic_blocks;
use classfile_parser::code_attribute::Instruction as JVMInstruction;
fn compound_conditional_fixture() -> (ControlFlowGraph, (NodeId, NodeId, NodeId, NodeId)) {
let mut g = ControlFlowGraph::new();
let x = g.add_node(Structure::Block(vec![
JVMInstruction::Iload0,
JVMInstruction::Iconst1,
JVMInstruction::IfIcmple(0),
]));
let y = g.add_node(Structure::Block(vec![
JVMInstruction::Iload1,
JVMInstruction::Iconst1,
JVMInstruction::IfIcmple(0),
]));
let f = g.add_node(Structure::Block(vec![
JVMInstruction::Iconst0,
JVMInstruction::Ireturn,
]));
let t = g.add_node(Structure::Block(vec![
JVMInstruction::Iconst1,
JVMInstruction::Ireturn,
]));
(g, (x, y, f, t))
}
#[test]
fn compound_conditional_conjunction() {
// Construct `a && b` graph
let (mut g, (x, y, f, t)) = compound_conditional_fixture();
g.add_edge(x, f);
g.add_edge(x, y);
g.add_edge(y, f);
g.add_edge(y, t);
g.structure_compound_conditionals();
// Check graph rewritten correctly
assert_eq!(g.len(), 3);
let entry = g.entry.unwrap();
assert_eq!(g[entry].successors, [f, t]);
assert!(matches!(
g[entry].value,
Structure::CompoundConditional {
left_negated: false,
kind: ConditionalKind::Conjunction,
..
}
));
}
#[test]
fn compound_conditional_negated_conjunction() {
// Construct `!a && b` graph
let (mut g, (x, y, f, t)) = compound_conditional_fixture();
g.add_edge(x, y);
g.add_edge(x, f);
g.add_edge(y, f);
g.add_edge(y, t);
g.structure_compound_conditionals();
// Check graph rewritten correctly
assert_eq!(g.len(), 3);
let entry = g.entry.unwrap();
assert_eq!(g[entry].successors, [f, t]);
assert!(matches!(
g[entry].value,
Structure::CompoundConditional {
left_negated: true,
kind: ConditionalKind::Conjunction,
..
}
));
}
#[test]
fn compound_conditional_disjunction() {
// Construct `a || b` graph
let (mut g, (x, y, f, t)) = compound_conditional_fixture();
g.add_edge(x, y);
g.add_edge(x, t);
g.add_edge(y, f);
g.add_edge(y, t);
g.structure_compound_conditionals();
// Check graph rewritten correctly
assert_eq!(g.len(), 3);
let entry = g.entry.unwrap();
assert_eq!(g[entry].successors, [f, t]);
assert!(matches!(
g[entry].value,
Structure::CompoundConditional {
left_negated: false,
kind: ConditionalKind::Disjunction,
..
}
));
}
#[test]
fn compound_conditional_negated_disjunction() {
// Construct `!a || b` graph
let (mut g, (x, y, f, t)) = compound_conditional_fixture();
g.add_edge(x, t);
g.add_edge(x, y);
g.add_edge(y, f);
g.add_edge(y, t);
g.structure_compound_conditionals();
// Check graph rewritten correctly
assert_eq!(g.len(), 3);
let entry = g.entry.unwrap();
assert_eq!(g[entry].successors, [f, t]);
assert!(matches!(
g[entry].value,
Structure::CompoundConditional {
left_negated: true,
kind: ConditionalKind::Disjunction,
..
}
));
}
fn compound_conditional_single_code(
expression: &str,
expected_left_negated: bool,
expected_kind: ConditionalKind,
expected_left_conditional_instruction: JVMInstruction,
expected_right_conditional_instruction: JVMInstruction,
) -> anyhow::Result<()> {
// Load graph containing basic blocks for expression
let mut g = load_basic_blocks(&format!(
"boolean a = false, b = false;
if ({}) {{ n = 1; }} else {{ n = 0; }}
return n;",
expression
))?;
g.insert_placeholder_nodes();
g.structure_compound_conditionals();
// Check graph rewritten correctly
assert_eq!(g.len(), 4);
let entry = g.entry.unwrap();
assert_eq!(
g[entry].value,
Structure::CompoundConditional {
left_negated: expected_left_negated,
kind: expected_kind,
left: Box::new(Structure::Block(vec![
JVMInstruction::Iconst0,
JVMInstruction::Istore1,
JVMInstruction::Iconst0,
JVMInstruction::Istore2,
JVMInstruction::Iload1,
expected_left_conditional_instruction,
])),
right: Box::new(Structure::Block(vec![
JVMInstruction::Iload2,
expected_right_conditional_instruction,
]))
}
);
assert_eq!(g[entry].successors.len(), 2);
assert_eq!(
g[g[entry].successors[0]].value,
Structure::Block(vec![
JVMInstruction::Iconst1,
JVMInstruction::Istore0,
JVMInstruction::Goto(5),
])
);
assert_eq!(
g[g[entry].successors[1]].value,
Structure::Block(vec![JVMInstruction::Iconst0, JVMInstruction::Istore0])
);
Ok(())
}
#[test]
fn compound_conditional_conjunction_code() -> anyhow::Result<()> {
compound_conditional_single_code(
"a && b",
false,
ConditionalKind::Disjunction,
JVMInstruction::Ifeq(12),
JVMInstruction::Ifeq(8),
)
}
#[test]
fn compound_conditional_negated_conjunction_code() -> anyhow::Result<()> {
compound_conditional_single_code(
"!a && b",
false,
ConditionalKind::Disjunction,
JVMInstruction::Ifne(12),
JVMInstruction::Ifeq(8),
)
}
#[test]
fn compound_conditional_disjunction_code() -> anyhow::Result<()> {
compound_conditional_single_code(
"a || b",
true,
ConditionalKind::Conjunction,
JVMInstruction::Ifne(7),
JVMInstruction::Ifeq(8),
)
}
#[test]
fn compound_conditional_negated_disjunction_code() -> anyhow::Result<()> {
compound_conditional_single_code(
"!a || b",
true,
ConditionalKind::Conjunction,
JVMInstruction::Ifeq(7),
JVMInstruction::Ifeq(8),
)
}
#[test]
fn compound_conditional_multiple_code() -> anyhow::Result<()> {
// Load graph containing basic blocks for multiple sequential short-circuit expressions
let mut g = load_basic_blocks(
"boolean a = false, b = false;
if (a && b) { n = 1; } else { n = 0; }
if (a || b) { n = 1; } else { n = 0; }
return n;",
)?;
g.insert_placeholder_nodes();
g.structure_compound_conditionals();
// Check graph rewritten correctly
assert_eq!(g.len(), 3 + 3 + 1); // `a && b` + `a || b` + follow2
let entry = g.entry.unwrap();
assert!(matches!(
g[entry].value,
Structure::CompoundConditional {
left_negated: false,
kind: ConditionalKind::Disjunction,
..
}
));
let follow1 = g[g[entry].successors[0]].successors[0];
assert!(matches!(
g[follow1].value,
Structure::CompoundConditional {
left_negated: true,
kind: ConditionalKind::Conjunction,
..
}
));
Ok(())
}
#[test]
fn compound_conditional_nested_code() -> anyhow::Result<()> {
// Load graph containing basic blocks for nested short-circuit expression
let mut g = load_basic_blocks(
"boolean a = false, b = false, c = false, d = false;
if (((a || b) && c) || (a && d)) { n = 1; } else { n = 0; }
return n;",
)?;
g.insert_placeholder_nodes();
g.structure_compound_conditionals();
// Check graph rewritten correctly
assert_eq!(g.len(), 4);
let entry = g.entry.unwrap();
assert_eq!(
g[entry].value,
Structure::CompoundConditional {
left_negated: true,
kind: ConditionalKind::Conjunction,
left: Box::new(Structure::CompoundConditional {
left_negated: true,
kind: ConditionalKind::Conjunction,
left: Box::new(Structure::CompoundConditional {
left_negated: true,
kind: ConditionalKind::Conjunction,
left: Box::new(Structure::Block(vec![
JVMInstruction::Iconst0,
JVMInstruction::Istore1,
JVMInstruction::Iconst0,
JVMInstruction::Istore2,
JVMInstruction::Iconst0,
JVMInstruction::Istore3,
JVMInstruction::Iconst0,
JVMInstruction::Istore(4),
JVMInstruction::Iload1,
JVMInstruction::Ifne(7),
])),
right: Box::new(Structure::Block(vec![
JVMInstruction::Iload2,
JVMInstruction::Ifeq(7),
]))
}),
right: Box::new(Structure::Block(vec![
JVMInstruction::Iload3,
JVMInstruction::Ifne(12),
]))
}),
right: Box::new(Structure::CompoundConditional {
left_negated: false,
kind: ConditionalKind::Disjunction,
left: Box::new(Structure::Block(vec![
JVMInstruction::Iload1,
JVMInstruction::Ifeq(13),
])),
right: Box::new(Structure::Block(vec![
JVMInstruction::Iload(4),
JVMInstruction::Ifeq(8),
]))
}),
}
);
Ok(())
}
}