use crate::instruction::OpCode;
use super::super::super::HighLevelEmitter;
impl HighLevelEmitter {
pub(super) fn find_endtry_target(&self, start: usize, end: usize) -> Option<(usize, usize)> {
if start >= end {
return None;
}
let mut first_forward = None;
let mut last_escaping = None;
let mut try_depth: usize = 0;
for (&offset, &index) in self.index_by_offset.range(start..end) {
let instruction = self.program.get(index)?;
if matches!(instruction.opcode, OpCode::Try | OpCode::TryL) {
try_depth += 1;
continue;
}
if !matches!(instruction.opcode, OpCode::Endtry | OpCode::EndtryL) {
if instruction.opcode == OpCode::Endfinally && try_depth > 0 {
try_depth -= 1;
}
continue;
}
if try_depth > 0 {
try_depth -= 1;
continue;
}
if self.skip_jumps.contains(&offset) {
continue;
}
if let Some(target) = self.forward_jump_target(instruction) {
let effective_target = if target == instruction.offset {
end
} else {
target
};
if effective_target > instruction.offset {
if effective_target >= end {
last_escaping = Some((offset, effective_target));
}
if first_forward.is_none() {
first_forward = Some((offset, effective_target));
}
}
}
}
last_escaping.or(first_forward)
}
pub(super) fn find_endfinally_end(&self, start: usize) -> Option<(usize, usize)> {
for (&offset, &index) in self.index_by_offset.range(start..) {
let instruction = self.program.get(index)?;
if instruction.opcode != OpCode::Endfinally {
continue;
}
let end = self
.program
.get(index + 1)
.map(|next| next.offset)
.unwrap_or(offset + 1);
return Some((offset, end));
}
None
}
}