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
use crate::lua51::ast::*;
use super::Lifter;
use super::util::negate_expr;
impl<'a> Lifter<'a> {
/// Detect and lift OR/AND short-circuit conditional chains.
///
/// OR pattern (`if a or b then T end`):
/// Block A: ConditionalFalse → T, ConditionalTrue → Block B
/// Block B: ConditionalTrue → T, ConditionalFalse → continuation
/// (Intermediate blocks: ConditionalFalse → T, ConditionalTrue → next)
///
/// AND pattern (`if a and b then body end`):
/// Block A: ConditionalFalse → END, ConditionalTrue → Block B
/// Block B: ConditionalFalse → END, ConditionalTrue → body
pub(super) fn try_lift_or_and_chain(&mut self, start: usize, stmts: &mut Block) -> Option<usize> {
let block = &self.cfg.blocks[start];
if block.successors.len() != 2 { return None; }
if self.block_contains_testset(start) {
return None;
}
let _false0 = block.successors[0]; // ConditionalFalse (JMP target)
let true0 = block.successors[1]; // ConditionalTrue (fallthrough)
// true0 must be a conditional block (next test in chain)
if true0 >= self.cfg.num_blocks() { return None; }
if !self.is_conditional_block(&self.cfg.blocks[true0]) { return None; }
if self.block_contains_testset(true0) {
return None;
}
// Try OR chain detection
if let Some(result) = self.try_or_chain(start, stmts) {
return Some(result);
}
// Try AND chain detection
if let Some(result) = self.try_and_chain(start, stmts) {
return Some(result);
}
None
}
/// Detect and lift an OR chain: `if A or B or ... then T`.
///
/// Pattern: intermediate blocks have ConditionalFalse → T (common body),
/// last block has ConditionalTrue → T.
fn try_or_chain(&mut self, start: usize, stmts: &mut Block) -> Option<usize> {
let block = &self.cfg.blocks[start];
let false0 = block.successors[0]; // ConditionalFalse = JMP target = T (body)
let true0 = block.successors[1]; // ConditionalTrue = next test
let body_target = false0;
let mut chain = vec![start]; // blocks in the chain
let mut current = true0;
// Follow the chain
loop {
if current >= self.cfg.num_blocks() { return None; }
if !self.is_conditional_block(&self.cfg.blocks[current]) { return None; }
if self.block_contains_testset(current) { return None; }
let cur_block = &self.cfg.blocks[current];
let cur_false = cur_block.successors[0]; // ConditionalFalse
let cur_true = cur_block.successors[1]; // ConditionalTrue
if cur_false == body_target {
// Intermediate block: false → T, true → next
chain.push(current);
current = cur_true;
} else if cur_true == body_target {
// Last block: true → T, false → continuation
chain.push(current);
let continuation = cur_false;
// Build the combined OR condition
return Some(self.emit_or_chain(&chain, body_target, continuation, stmts));
} else {
// Doesn't match the OR pattern
return None;
}
}
}
/// Emit an OR chain as a single `if` statement.
fn emit_or_chain(
&mut self,
chain: &[usize],
body_target: usize,
continuation: usize,
stmts: &mut Block,
) -> usize {
let mut parts = Vec::new();
for (i, &block_idx) in chain.iter().enumerate() {
let block = self.cfg.blocks[block_idx].clone();
let test_pc = self.find_test_pc(&block);
// Lift pre-test instructions (updates register state)
if let Some(tp) = test_pc {
if tp > block.start {
self.lift_instructions(block.start, tp - 1, stmts);
}
}
let cond = self.extract_condition(block_idx).unwrap_or(Expr::Bool(true));
self.visited_blocks.insert(block_idx);
let is_last = i == chain.len() - 1;
if is_last {
// Last block: ConditionalTrue → body, condition as-is
parts.push(cond);
} else {
// Intermediate block: ConditionalFalse → body, negate condition
parts.push(negate_expr(cond));
}
}
// Combine with OR
let combined = parts.into_iter().reduce(|a, b| {
Expr::BinOp(BinOp::Or, Box::new(a), Box::new(b))
}).unwrap_or(Expr::Bool(true));
// Lift the body (target T)
// Check if body is a guard clause (return)
if self.is_return_block(body_target) {
let then_block = self.lift_block_range(body_target, body_target + 1);
stmts.push(Stat::If {
cond: combined,
then_block,
elseif_clauses: Vec::new(),
else_block: None,
});
return continuation;
}
// Normal if: body with potential else
let merge = if self.block_flows_to(body_target, continuation) {
Some(continuation)
} else {
self.find_merge_point(
*chain.first().unwrap(),
body_target,
continuation,
)
};
let then_end = merge.unwrap_or(continuation);
let then_block = self.lift_block_range(body_target, then_end);
let else_block = if let Some(m) = merge {
if continuation < m {
let eb = self.lift_block_range(continuation, m);
if eb.is_empty() { None } else { Some(eb) }
} else {
None
}
} else {
None
};
stmts.push(Stat::If {
cond: combined,
then_block,
elseif_clauses: Vec::new(),
else_block,
});
merge.unwrap_or(continuation.max(body_target) + 1)
}
/// Detect and lift an AND chain: `if A and B and ... then body end`.
///
/// Pattern: all blocks have ConditionalFalse → END (common else/end target),
/// ConditionalTrue chains to next test, last true → body.
fn try_and_chain(&mut self, start: usize, stmts: &mut Block) -> Option<usize> {
let block = &self.cfg.blocks[start];
let false0 = block.successors[0]; // ConditionalFalse = JMP target = END
let true0 = block.successors[1]; // ConditionalTrue = next test
let end_target = false0;
let mut chain = vec![start];
let mut current = true0;
// Follow the chain
loop {
if current >= self.cfg.num_blocks() {
// Reached the end of blocks; body is current
break;
}
if !self.is_conditional_block(&self.cfg.blocks[current]) {
// Non-conditional block = body
break;
}
if self.block_contains_testset(current) {
return None;
}
let cur_block = &self.cfg.blocks[current];
let cur_false = cur_block.successors[0];
let cur_true = cur_block.successors[1];
if cur_false == end_target {
// Another AND block: false → END, true → next
chain.push(current);
current = cur_true;
} else {
// Doesn't match AND pattern
return None;
}
}
// Need at least 2 blocks for a chain
if chain.len() < 2 { return None; }
let body_target = current;
// Build and emit the AND chain
let mut parts = Vec::new();
for &block_idx in &chain {
let block = self.cfg.blocks[block_idx].clone();
let test_pc = self.find_test_pc(&block);
if let Some(tp) = test_pc {
if tp > block.start {
self.lift_instructions(block.start, tp - 1, stmts);
}
}
let cond = self.extract_condition(block_idx).unwrap_or(Expr::Bool(true));
self.visited_blocks.insert(block_idx);
parts.push(cond);
}
// Combine with AND
let combined = parts.into_iter().reduce(|a, b| {
Expr::BinOp(BinOp::And, Box::new(a), Box::new(b))
}).unwrap_or(Expr::Bool(true));
// Lift body and else
let merge = if self.block_flows_to(body_target, end_target) {
Some(end_target)
} else {
self.find_merge_point(
*chain.first().unwrap(),
body_target,
end_target,
)
};
let then_end = merge.unwrap_or(end_target);
let then_block = self.lift_block_range(body_target, then_end);
let else_block = if let Some(m) = merge {
if end_target < m {
let eb = self.lift_block_range(end_target, m);
if eb.is_empty() { None } else { Some(eb) }
} else {
None
}
} else {
None
};
stmts.push(Stat::If {
cond: combined,
then_block,
elseif_clauses: Vec::new(),
else_block,
});
Some(merge.unwrap_or(end_target.max(body_target) + 1))
}
}