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
use super::super::HighLevelEmitter;
impl HighLevelEmitter {
/// Removes `label_0xXXXX:` lines that have no matching `goto`, `leave`,
/// or inline `if ... { goto label_X; }` reference. These artifacts arise
/// when control-flow lifting falls through to straight-line emission and
/// emits a label whose only intended target was inlined away.
pub(crate) fn remove_orphaned_labels(statements: &mut [String]) {
let mut referenced: std::collections::HashSet<String> = std::collections::HashSet::new();
for stmt in statements.iter() {
let trimmed = stmt.trim();
if let Some(name) = trimmed
.strip_prefix("goto ")
.and_then(|s| s.strip_suffix(';'))
.map(str::trim)
{
if name.starts_with("label_0x") {
referenced.insert(name.to_string());
}
}
if let Some(name) = trimmed
.strip_prefix("leave ")
.and_then(|s| s.strip_suffix(';'))
.map(str::trim)
{
if name.starts_with("label_0x") {
referenced.insert(name.to_string());
}
}
if trimmed.starts_with("if ") && trimmed.ends_with('}') {
if let Some(brace_idx) = trimmed.find('{') {
let body = trimmed[brace_idx + 1..trimmed.len() - 1].trim();
if let Some(name) = body
.strip_prefix("goto ")
.and_then(|s| s.strip_suffix(';'))
.map(str::trim)
{
if name.starts_with("label_0x") {
referenced.insert(name.to_string());
}
}
}
}
}
for stmt in statements.iter_mut() {
let trimmed = stmt.trim();
if let Some(name) = trimmed.strip_suffix(':') {
if name.starts_with("label_0x") && !referenced.contains(name) {
stmt.clear();
}
}
}
}
}