use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::fmt::Write as _;
use std::ops::Range;
use std::sync::atomic::{AtomicBool, Ordering};
use binbun_ast::ir::{Expr, ExprId, Function as Ast, Logical, Node, Origin, Stmt, StmtId, VarId};
use binbun_ast::text::print as print_l2;
use binbun_decompile::names;
use binbun_decompile::pass::{Feeds, L2Env};
use binbun_decompile::structure::Rotated;
use binbun_print::c::function as print_c;
use binbun_print::layout::{Span, render_spans};
use binbun_print::style::{IntSpelling, Naming, Placement, Style};
use binbun_semantics::core::pass::{PassStats, Refusal};
use binbun_semantics::ir::Declarations;
use binbun_ssa::ir::{Definition, Function, Value};
use binbun_ssa::text::print as print_l1;
use crate::Database;
use crate::cfg::FunctionId;
use crate::decompile::{
Assumed, Passed, View, exact_l1, l2_passes_recorded, lift_l1, lower_function, reading_comment,
reading_l1_recorded,
};
use crate::lift::disassemble;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Options {
pub view: View,
pub disabled: Vec<String>,
pub style: String,
pub types: IntSpelling,
pub declarations: Placement,
pub names: Naming,
}
impl Default for Options {
fn default() -> Self {
Self {
view: View::Exact,
disabled: Vec::new(),
style: "kr".to_owned(),
types: IntSpelling::Fixed,
declarations: Placement::Scoped,
names: Naming::Numbered,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Level {
Machine,
L1,
L2,
C,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Key {
Header,
Instruction(u64),
Block(u32),
Inst(u32),
Terminator(u32),
Type(u32),
Var(u32),
Facts(u32),
Open,
Close,
Stmt {
id: u32,
part: Part,
ordinal: u32,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Part {
First,
Close,
Else,
While,
Case,
Default,
Fallthrough,
Handler,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Item {
Value(Value),
Node(Node),
Var(VarId),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Change {
Added,
Changed,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Line {
pub range: Range<usize>,
pub key: Option<Key>,
pub addresses: Vec<u64>,
pub defines: Vec<Item>,
pub change: Option<Change>,
pub was: Option<usize>,
pub reason: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Removed {
pub before: usize,
pub text: String,
pub key: Option<Key>,
pub addresses: Vec<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Stage {
pub name: &'static str,
pub label: String,
pub level: Level,
pub text: String,
pub lines: Vec<Line>,
pub removed: Vec<Removed>,
pub compared: bool,
}
impl Stage {
pub fn line_text(&self, index: usize) -> &str {
self.lines
.get(index)
.and_then(|line| self.text.get(line.range.clone()))
.unwrap_or_default()
}
pub fn counts(&self) -> (usize, usize, usize) {
let count = |change| {
self.lines
.iter()
.filter(|line| line.change == Some(change))
.count()
};
(
count(Change::Added),
count(Change::Changed),
self.removed.len(),
)
}
pub fn line_at(&self, address: u64) -> Option<usize> {
let names = |line: &Line| line.addresses.binary_search(&address).is_ok();
if let Some(found) = self.lines.iter().position(|line| code(line) && names(line)) {
return Some(found);
}
if let Some(found) = self.lines.iter().position(names) {
return Some(found);
}
self.lines
.iter()
.enumerate()
.filter(|(_, line)| code(line))
.filter_map(|(index, line)| {
let below = line.addresses.iter().rev().find(|&&a| a < address)?;
Some((*below, index))
})
.max_by_key(|&(below, index)| (below, core::cmp::Reverse(index)))
.map(|(_, index)| index)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PassCount {
pub name: &'static str,
pub level: Level,
pub stats: PassStats,
pub refusals: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Stages {
pub entry: u64,
pub view: View,
pub stages: Vec<Stage>,
pub passes: Vec<PassCount>,
}
impl Stages {
pub fn find(&self, wanted: &str) -> Option<usize> {
self.stages
.iter()
.position(|stage| stage.label == wanted)
.or_else(|| self.stages.iter().position(|stage| stage.name == wanted))
}
pub fn pass(&self, index: usize) -> Option<&PassCount> {
let stage = self.stages.get(index)?;
self.passes.iter().find(|pass| pass.name == stage.name)
}
pub fn follow(&self, from: usize, line: usize, to: usize) -> Option<usize> {
let source = self.stages.get(from)?;
let target = self.stages.get(to)?;
let at = source.lines.get(line)?;
if source.level == target.level
&& let Some(key) = at.key
&& let Some(found) = target.lines.iter().position(|l| l.key == Some(key))
{
return Some(found);
}
if let Some(found) = at.defines.iter().find_map(|item| {
target
.lines
.iter()
.position(|l| l.defines.contains(item) && l.key.is_none_or(first_part))
}) {
return Some(found);
}
if let Some(found) = at.addresses.iter().find_map(|address| {
target
.lines
.iter()
.position(|l| l.addresses.binary_search(address).is_ok())
}) {
return Some(found);
}
at.addresses.first().and_then(|&a| target.line_at(a))
}
}
fn first_part(key: Key) -> bool {
!matches!(key, Key::Stmt { part, .. } if part != Part::First)
}
fn code(line: &Line) -> bool {
!matches!(
line.key,
Some(Key::Header | Key::Type(_) | Key::Var(_) | Key::Facts(_))
)
}
pub fn stages(
database: &Database<'_>,
id: FunctionId,
options: &Options,
) -> Result<Stages, String> {
stages_cancellable(database, id, options, &AtomicBool::new(false))
.unwrap_or_else(|| Err("cancelled".to_owned()))
}
pub fn stages_cancellable(
database: &Database<'_>,
id: FunctionId,
options: &Options,
cancel: &AtomicBool,
) -> Option<Result<Stages, String>> {
Maker {
database,
cancel,
options,
}
.make(id)
.transpose()
}
struct Maker<'d, 'a> {
database: &'d Database<'a>,
cancel: &'d AtomicBool,
options: &'d Options,
}
struct Raw {
name: &'static str,
level: Level,
text: String,
lines: Vec<Line>,
}
impl Maker<'_, '_> {
fn cancelled(&self) -> bool {
self.cancel.load(Ordering::Relaxed)
}
#[allow(
clippy::too_many_lines,
reason = "the pipeline's steps in order, each a few lines"
)]
fn make(&self, id: FunctionId) -> Result<Option<Stages>, String> {
let database = self.database;
let image = database.image();
let disabled = &self.options.disabled;
let declarations = database
.declarations()
.ok_or_else(|| "the image's code is not lifted".to_owned())?;
let style = Style::preset(&self.options.style)
.ok_or_else(|| format!("no style preset {:?}", self.options.style))?
.with_int_spelling(self.options.types)
.with_declarations(self.options.declarations)
.with_naming(self.options.names);
let function = database
.function(id)
.ok_or_else(|| "not a function of the program".to_owned())?;
let entry = function.entry;
let mut raw = vec![self.machine(id)];
let (_, interfaces) = database.prepare_in(id, self.options.view, disabled);
if self.cancelled() {
return Ok(None);
}
let lifted = lift_l1(
image,
database.program(),
id,
database.summaries(),
disabled,
)?;
raw.push(l1_stage("lifting", &lifted, declarations));
let (mut prepared, recorded) = exact_l1(image, lifted, disabled, true)?;
let mut pass_counts = counts(Level::L1, &prepared.passes, &recorded.refusals);
raw.extend(
recorded
.stages
.iter()
.map(|(name, l1)| l1_stage(name, l1, declarations)),
);
let mut assumed = Assumed::default();
if self.options.view == View::Reading {
let (reading, folded, recorded) =
reading_l1_recorded(image, &prepared, disabled, true)?;
let exact = prepared.passes.len();
pass_counts.extend(counts(
Level::L1,
reading.passes.get(exact..).unwrap_or_default(),
&recorded.refusals,
));
raw.extend(
recorded
.stages
.iter()
.map(|(name, l1)| l1_stage(name, l1, declarations)),
);
prepared = reading;
assumed = folded;
}
if self.cancelled() {
return Ok(None);
}
let prototypes = database.prototypes();
let (mut l2, _, _) =
lower_function(image, &prepared, &interfaces, &prototypes, database.data())?;
l2.name = database.data_name(entry);
let l1 = &prepared.l1;
let lowered = l2.clone();
if self.cancelled() {
return Ok(None);
}
let feeds = Feeds::new(l1);
let env = L2Env {
declarations,
exceptions: database.exceptions(),
feeds: Some(&feeds),
};
let mut passed = l2_passes_recorded(&mut l2, &env, disabled, true)?;
let mut lowering = l2_stage("lowering", &lowered, l1, declarations);
floors_l2(&mut lowering, &passed);
raw.push(lowering);
drop(lowered);
let recorded: Vec<(&'static str, Ast)> = passed.manager.recorded();
let refusals = passed.manager.refusals().to_vec();
pass_counts.extend(counts(
Level::L2,
&passed.manager.stats().collect::<Vec<_>>(),
&refusals,
));
let mut l2_stages: Vec<(&'static str, &Ast)> =
recorded.iter().map(|(name, ast)| (*name, ast)).collect();
let before = l2.clone();
names::name(&mut l2, &prototypes);
if l2 != before {
l2_stages.push(("naming", &l2));
}
for &(name, ast) in &l2_stages {
if self.cancelled() {
return Ok(None);
}
let mut stage = l2_stage(name, ast, l1, declarations);
reasons_l2(&mut stage, ast, &passed);
floors_l2(&mut stage, &passed);
raw.push(stage);
}
let printed = print_c(
&l2,
declarations,
&style,
database.data(),
self.options.view.opaque(),
)
.map_err(|error| format!("cannot print: {error}"))?;
let (text, spans) = render_spans(&printed.document, &style);
let prefix = if self.options.view == View::Reading && !assumed.is_empty() {
reading_comment(&assumed)
} else {
String::new()
};
raw.push(c_stage(&prefix, &text, &spans, &l2, l1));
Ok(Some(Stages {
entry,
view: self.options.view,
stages: compare(raw),
passes: pass_counts,
}))
}
fn machine(&self, id: FunctionId) -> Raw {
let database = self.database;
let program = database.program();
let mut blocks: Vec<(u64, u64)> = database
.function(id)
.map(|function| {
function
.blocks
.iter()
.filter_map(|&block| program.blocks.get(block))
.map(|block| (block.start, block.end))
.collect()
})
.unwrap_or_default();
blocks.sort_unstable();
blocks.dedup();
let mut text = String::new();
let mut lines = Vec::new();
for (start, end) in blocks {
let mut at = start;
while at < end {
let begin = text.len();
let next = match disassemble(database.image(), at) {
Ok((instruction, next)) => {
let _ = write!(text, "{at:#x} {instruction}");
next
}
Err(error) => {
let _ = write!(text, "{at:#x} ?? {error}");
end
}
};
lines.push(Line {
range: begin..text.len(),
key: Some(Key::Instruction(at)),
addresses: vec![at],
defines: Vec::new(),
change: None,
was: None,
reason: None,
});
text.push('\n');
at = next.max(at.saturating_add(1));
}
}
Raw {
name: "machine",
level: Level::Machine,
text,
lines,
}
}
}
fn counts(
level: Level,
stats: &[(&'static str, PassStats)],
refusals: &[(&'static str, Refusal)],
) -> Vec<PassCount> {
stats
.iter()
.map(|&(name, stats)| PassCount {
name,
level,
stats,
refusals: refusals
.iter()
.filter(|(pass, _)| *pass == name)
.map(|(_, refusal)| refusal.to_string())
.collect(),
})
.collect()
}
fn bare(range: Range<usize>, key: Option<Key>) -> Line {
Line {
range,
key,
addresses: Vec::new(),
defines: Vec::new(),
change: None,
was: None,
reason: None,
}
}
fn ranges(text: &str) -> Vec<Range<usize>> {
let mut out = Vec::new();
let mut start = 0;
for piece in text.split_inclusive('\n') {
let end = start + piece.len();
out.push(start..end - usize::from(piece.ends_with('\n')));
start = end;
}
out
}
fn value_address(l1: &Function, value: Value) -> Option<u64> {
match l1.value(value)?.definition {
Definition::Result { inst, .. } => l1.inst(inst).map(|inst| inst.address),
Definition::Parameter { block, .. } => l1.block(block).map(|block| block.address),
}
}
fn origin_addresses(l1: &Function, origins: &[Origin], out: &mut Vec<u64>) {
for origin in origins {
match *origin {
Origin::Value(value) => out.extend(value_address(l1, value)),
Origin::Instruction(address) => out.push(address),
Origin::Register(_)
| Origin::Held(_)
| Origin::Slot(_)
| Origin::Block(_)
| Origin::Absorbed(_)
| Origin::Proved => {}
}
}
}
fn expr_addresses(ast: &Ast, l1: &Function, roots: Vec<ExprId>, out: &mut Vec<u64>) {
let mut work = roots;
let mut seen = BTreeSet::new();
while let Some(id) = work.pop() {
if !seen.insert(id) {
continue;
}
origin_addresses(l1, ast.provenance.expr(id), out);
if let Some(expr) = ast.expr(id) {
work.extend(expr.operands(ast));
}
}
}
fn l1_stage(name: &'static str, function: &Function, declarations: &Declarations) -> Raw {
let text = print_l1(function, declarations);
let ranges = ranges(&text);
let mut lines = Vec::with_capacity(ranges.len());
let mut ranges = ranges.into_iter();
let mut next = |key, addresses: Vec<u64>, defines: Vec<Item>| {
if let Some(range) = ranges.next() {
lines.push(Line {
addresses,
defines,
..bare(range, Some(key))
});
}
};
next(Key::Header, Vec::new(), Vec::new());
for (index, block) in (0u32..).zip(&function.blocks) {
next(
Key::Block(index),
vec![block.address],
block.parameters.iter().copied().map(Item::Value).collect(),
);
let mut last = block.address;
for &inst in &block.insts {
let (addresses, defines) = function.inst(inst).map_or_else(Default::default, |i| {
last = i.address;
(
vec![i.address],
i.results.iter().copied().map(Item::Value).collect(),
)
});
next(Key::Inst(inst.0), addresses, defines);
}
next(Key::Terminator(index), vec![last], Vec::new());
}
Raw {
name,
level: Level::L1,
text,
lines,
}
}
fn compound(stmt: &Stmt) -> bool {
matches!(
stmt,
Stmt::If { .. }
| Stmt::While { .. }
| Stmt::DoWhile { .. }
| Stmt::For { .. }
| Stmt::Switch { .. }
| Stmt::Try { .. }
)
}
fn statement_number(text: &str) -> Option<u32> {
let rest = text.strip_prefix('s')?;
let digits = rest.find(|c: char| !c.is_ascii_digit())?;
rest.get(digits..)?.starts_with(": ").then_some(())?;
rest.get(..digits)?.parse().ok()
}
fn numbered(text: &str, prefix: &str) -> Option<u32> {
let rest = text.strip_prefix(prefix)?;
let digits = rest
.find(|c: char| !c.is_ascii_digit())
.unwrap_or(rest.len());
rest.get(..digits)?.parse().ok()
}
fn part(trimmed: &str) -> Part {
if trimmed == "}" {
Part::Close
} else if trimmed == "} else {" {
Part::Else
} else if trimmed.starts_with("} while ") {
Part::While
} else if trimmed.starts_with("case ") {
Part::Case
} else if trimmed.starts_with("default") {
Part::Default
} else if trimmed == "fallthrough" {
Part::Fallthrough
} else {
Part::Handler
}
}
fn l2_stage(name: &'static str, ast: &Ast, l1: &Function, declarations: &Declarations) -> Raw {
let text = print_l2(ast, declarations);
let mut lines = Vec::new();
let mut body = false;
let mut facts = 0;
let mut open: Vec<u32> = Vec::new();
let mut ordinals: BTreeMap<(u32, Part), u32> = BTreeMap::new();
for range in ranges(&text) {
let line = text.get(range.clone()).unwrap_or_default();
let trimmed = line.trim_start();
let mut addresses = Vec::new();
let mut defines = Vec::new();
let key = if !body {
if line.starts_with("function ") {
Key::Header
} else if let Some(n) = numbered(line, " type t") {
Key::Type(n)
} else if let Some(n) = numbered(line, " var v") {
let var = VarId(n);
if let Some(variable) = ast.var(var) {
origin_addresses(l1, &variable.origins, &mut addresses);
}
defines.push(Item::Var(var));
Key::Var(n)
} else if line == "{" {
body = true;
Key::Open
} else {
facts += 1;
Key::Facts(facts - 1)
}
} else if let Some(n) = statement_number(trimmed) {
let id = StmtId(n);
if let Some(stmt) = ast.stmt(id) {
statement_line(ast, l1, id, stmt, &mut addresses, &mut defines);
if compound(stmt) {
open.push(n);
}
}
Key::Stmt {
id: n,
part: Part::First,
ordinal: 0,
}
} else if let Some(&id) = open.last() {
let part = part(trimmed);
if matches!(part, Part::Close | Part::While) {
open.pop();
}
if part == Part::While
&& let Some(&Stmt::DoWhile { condition, .. }) = ast.stmt(StmtId(id))
{
expr_addresses(ast, l1, vec![condition], &mut addresses);
}
let ordinal = ordinals.entry((id, part)).or_default();
*ordinal += 1;
Key::Stmt {
id,
part,
ordinal: *ordinal - 1,
}
} else {
Key::Close
};
addresses.sort_unstable();
addresses.dedup();
lines.push(Line {
addresses,
defines,
..bare(range, Some(key))
});
}
Raw {
name,
level: Level::L2,
text,
lines,
}
}
fn statement_line(
ast: &Ast,
l1: &Function,
id: StmtId,
stmt: &Stmt,
addresses: &mut Vec<u64>,
defines: &mut Vec<Item>,
) {
let mut ids = vec![id];
if let Stmt::For { init, step, .. } = stmt {
ids.extend(init.iter().chain(step));
}
for id in ids {
origin_addresses(l1, ast.provenance.stmt(id), addresses);
if let Some(stmt) = ast.stmt(id)
&& !matches!(stmt, Stmt::DoWhile { .. })
{
expr_addresses(ast, l1, stmt.roots(), addresses);
}
defines.push(Item::Node(Node::Stmt(id)));
}
}
fn c_stage(prefix: &str, text: &str, spans: &[Span], ast: &Ast, l1: &Function) -> Raw {
let full = format!("{prefix}{text}");
let ranges = ranges(&full);
let mut lines: Vec<Line> = ranges
.iter()
.map(|range| bare(range.clone(), None))
.collect();
let starts: Vec<usize> = ranges.iter().map(|range| range.start).collect();
for span in spans {
let Some(node) = span.node else {
continue;
};
let at = prefix.len() + span.range.start;
let index = starts
.partition_point(|&start| start <= at)
.saturating_sub(1);
let Some(line) = lines.get_mut(index) else {
continue;
};
match node {
Node::Stmt(id) => {
origin_addresses(l1, ast.provenance.stmt(id), &mut line.addresses);
if !line.defines.contains(&Item::Node(node)) {
line.defines.push(Item::Node(node));
}
}
Node::Expr(id) => origin_addresses(l1, ast.provenance.expr(id), &mut line.addresses),
Node::Var(var) => {
if let Some(variable) = ast.var(var) {
origin_addresses(l1, &variable.origins, &mut line.addresses);
}
if !line.defines.contains(&Item::Var(var)) {
line.defines.push(Item::Var(var));
}
}
Node::Type(_) => {}
}
}
for line in &mut lines {
line.addresses.sort_unstable();
line.addresses.dedup();
}
Raw {
name: "c",
level: Level::C,
text: full,
lines,
}
}
fn reasons_l2(stage: &mut Raw, ast: &Ast, passed: &Passed<'_>) {
for line in &mut stage.lines {
let Some(Key::Stmt {
id,
part: Part::First,
..
}) = line.key
else {
continue;
};
let id = StmtId(id);
let Some(stmt) = ast.stmt(id) else {
continue;
};
line.reason = match stage.name {
"loops" => loop_reason(passed, id),
"switches" => {
if let Stmt::Switch { arms, .. } = stmt {
Some(format!(
"switches: a chain of {} comparisons of one value",
arms.iter().map(|arm| arm.values.len()).sum::<usize>()
))
} else {
None
}
}
"short-circuit" => {
if let Stmt::If { condition, .. } | Stmt::While { condition, .. } = stmt {
match ast.expr(*condition) {
Some(Expr::Logical(Logical::And, ..)) => {
Some("short-circuit: an inner decision merged with &&".to_owned())
}
Some(Expr::Logical(Logical::Or, ..)) => {
Some("short-circuit: an inner decision merged with ||".to_owned())
}
_ => None,
}
} else {
None
}
}
_ => None,
};
if line.reason.is_none() && ast.provenance.stmt(id).contains(&Origin::Proved) {
line.reason = Some(
"its first test always holds on entry (range analysis): entering it decides nothing"
.to_owned(),
);
}
}
}
fn loop_reason(passed: &Passed<'_>, id: StmtId) -> Option<String> {
let rule = |rule: u8| -> String {
let what = match rule {
1 => "if and do-while on one condition as while".to_owned(),
2 => "for formation".to_owned(),
3 => "exit condition as the counted loop's test".to_owned(),
4 => match passed.outcomes.get(&id) {
Some(Rotated::Guarded) => "counted loop, its guard as the first test".to_owned(),
_ => "counted loop, the first test proved".to_owned(),
},
_ => "the first test implied by the code before".to_owned(),
};
format!("loop rule {rule}: {what}")
};
match passed.rules.get(&id) {
Some(rules) => Some(
rules
.iter()
.map(|&number| rule(number))
.collect::<Vec<_>>()
.join("; "),
),
None => match passed.outcomes.get(&id) {
Some(Rotated::Left(refused)) => Some(format!("loop rules: still do-while ({refused})")),
_ => None,
},
}
}
fn floors_l2(stage: &mut Raw, passed: &Passed<'_>) {
for line in &mut stage.lines {
if !matches!(
line.key,
Some(Key::Stmt {
part: Part::Handler,
..
})
) {
continue;
}
let text = stage.text.get(line.range.clone()).unwrap_or_default();
let pad = text
.trim_start()
.strip_prefix("} landing 0x")
.and_then(|rest| rest.split_whitespace().next())
.and_then(|hex| u64::from_str_radix(hex, 16).ok());
if let Some(floor) = pad.and_then(|pad| passed.floors.get(&pad)) {
line.reason = Some(format!("catch dispatch left it a landing pad: {floor}"));
}
}
}
fn l1_reason(pass: &str) -> Option<&'static str> {
match pass {
"assume-canonical" => Some(
"under the ABI's assumption that the stack and return address are canonical, the check cannot fail",
),
"assume-dereferenced-canonical" => Some(
"the reading view's assumption: the address dereferenced is canonical, the check's trap is gone",
),
"assume-aligned-stack" => Some(
"the reading view's assumption: the stack pointer is aligned at entry as the ABI says, the check's trap is gone",
),
"assume-authenticated-returns" => Some(
"the reading view's assumption: the signed return address authenticates, the check's trap is gone",
),
"assume-default-float-controls" => Some(
"the reading view's assumption: the floating-point controls are what a process starts with",
),
"assume-float-results" => Some(
"the reading view's assumption: a NaN is not told from another, a float converted to an integer is in its range",
),
"assume-empty-x87-stack" => {
Some("the reading view's assumption: the x87 stack is empty at entry, TOP 0")
}
"fold-branches" => Some("the condition is constant: the branch goes one way"),
"fold-implied-branches" => {
Some("an earlier test of the same value decides it: the branch goes one way")
}
_ => None,
}
}
fn compare(raw: Vec<Raw>) -> Vec<Stage> {
let mut seen: BTreeMap<&str, usize> = BTreeMap::new();
for stage in &raw {
*seen.entry(stage.name).or_default() += 1;
}
let mut numbered: BTreeMap<&str, usize> = BTreeMap::new();
let mut out: Vec<Stage> = Vec::with_capacity(raw.len());
for stage in raw {
let label = if seen.get(stage.name).copied().unwrap_or(0) > 1 {
let n = numbered.entry(stage.name).or_default();
*n += 1;
format!("{}#{n}", stage.name)
} else {
stage.name.to_owned()
};
let previous = out.last().filter(|before| {
before.level == stage.level && matches!(stage.level, Level::L1 | Level::L2)
});
let Raw {
name,
level,
text,
mut lines,
} = stage;
let mut removed = Vec::new();
let compared = previous.is_some();
if let Some(previous) = previous {
let old: Vec<(Option<Key>, &str)> = (0..previous.lines.len())
.map(|index| {
(
previous.lines.get(index).and_then(|line| line.key),
previous.line_text(index),
)
})
.collect();
let new: Vec<(Option<Key>, &str)> = lines
.iter()
.map(|line| (line.key, text.get(line.range.clone()).unwrap_or_default()))
.collect();
let found = diff(&old, &new);
for ((line, change), was) in lines.iter_mut().zip(found.changes).zip(found.paired) {
line.change = change;
line.was = was;
if level == Level::L1
&& change == Some(Change::Changed)
&& matches!(line.key, Some(Key::Terminator(_)))
{
line.reason = l1_reason(name).map(str::to_owned);
}
}
removed = found
.removed
.into_iter()
.map(|(before, index)| {
let line = previous.lines.get(index);
Removed {
before,
text: previous.line_text(index).to_owned(),
key: line.and_then(|line| line.key),
addresses: line.map(|line| line.addresses.clone()).unwrap_or_default(),
}
})
.collect();
}
out.push(Stage {
name,
label,
level,
text,
lines,
removed,
compared,
});
}
out
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Diff {
pub changes: Vec<Option<Change>>,
pub paired: Vec<Option<usize>>,
pub removed: Vec<(usize, usize)>,
}
pub fn diff(old: &[(Option<Key>, &str)], new: &[(Option<Key>, &str)]) -> Diff {
let by_key: BTreeMap<Key, usize> = old
.iter()
.enumerate()
.rev()
.filter_map(|(index, &(key, _))| Some((key?, index)))
.collect();
let mut paired: Vec<Option<usize>> = vec![None; new.len()];
let mut used = vec![false; old.len()];
for (slot, &(key, _)) in paired.iter_mut().zip(new) {
if let Some(&index) = key.and_then(|key| by_key.get(&key))
&& let Some(taken) = used.get_mut(index)
&& !*taken
{
*taken = true;
*slot = Some(index);
}
}
let mut by_text: BTreeMap<&str, VecDeque<usize>> = BTreeMap::new();
for (index, &(_, text)) in old.iter().enumerate() {
if !used.get(index).copied().unwrap_or(true) {
by_text.entry(text).or_default().push_back(index);
}
}
for (slot, &(_, text)) in paired.iter_mut().zip(new) {
if slot.is_none()
&& let Some(index) = by_text.get_mut(text).and_then(VecDeque::pop_front)
{
if let Some(taken) = used.get_mut(index) {
*taken = true;
}
*slot = Some(index);
}
}
let changes = paired
.iter()
.zip(new)
.map(|(slot, &(_, text))| match slot {
None => Some(Change::Added),
Some(index) if old.get(*index).is_some_and(|&(_, was)| was != text) => {
Some(Change::Changed)
}
Some(_) => None,
})
.collect();
let mut at_old: Vec<Option<usize>> = vec![None; old.len()];
for (position, slot) in paired.iter().enumerate() {
if let Some(cell) = slot.and_then(|index| at_old.get_mut(index)) {
*cell = Some(position);
}
}
let mut removed = Vec::new();
let mut next = new.len();
for (index, at) in at_old.iter().enumerate().rev() {
match at {
Some(position) => next = *position,
None => removed.push((next, index)),
}
}
removed.reverse();
Diff {
changes,
paired,
removed,
}
}