Add split `order` attribute for manual reordering

Example in splits.txt:
```
file1.cpp: order:0
  ...

file2.cpp: order:1
  ...

file3.cpp: order:2
  ...
```

This ensures that file2.cpp is always
anchored in between 1 and 3 when resolving
the final link order.
This commit is contained in:
Luke Street 2024-08-11 20:38:11 -06:00
parent da6a514fac
commit b6a29fa910
7 changed files with 55 additions and 8 deletions

2
Cargo.lock generated
View File

@ -367,7 +367,7 @@ checksum = "c2e06f9bce634a3c898eb1e5cb949ff63133cbb218af93cc9b38b31d6f3ea285"
[[package]] [[package]]
name = "decomp-toolkit" name = "decomp-toolkit"
version = "0.9.3" version = "0.9.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"ar", "ar",

View File

@ -3,7 +3,7 @@ name = "decomp-toolkit"
description = "Yet another GameCube/Wii decompilation toolkit." description = "Yet another GameCube/Wii decompilation toolkit."
authors = ["Luke Street <luke@street.dev>"] authors = ["Luke Street <luke@street.dev>"]
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
version = "0.9.3" version = "0.9.4"
edition = "2021" edition = "2021"
publish = false publish = false
repository = "https://github.com/encounter/decomp-toolkit" repository = "https://github.com/encounter/decomp-toolkit"

View File

@ -47,6 +47,8 @@ pub struct ObjUnit {
pub autogenerated: bool, pub autogenerated: bool,
/// MW `.comment` section version. /// MW `.comment` section version.
pub comment_version: Option<u8>, pub comment_version: Option<u8>,
/// Influences the order of this unit relative to other ordered units.
pub order: Option<i32>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -34,6 +34,16 @@ pub fn parse_u32(s: &str) -> Result<u32, ParseIntError> {
} }
} }
pub fn parse_i32(s: &str) -> Result<i32, ParseIntError> {
if let Some(s) = s.strip_prefix("-0x").or_else(|| s.strip_prefix("-0X")) {
i32::from_str_radix(s, 16).map(|v| -v)
} else if let Some(s) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
i32::from_str_radix(s, 16)
} else {
s.parse::<i32>()
}
}
pub fn apply_symbols_file<P>(path: P, obj: &mut ObjInfo) -> Result<Option<FileReadInfo>> pub fn apply_symbols_file<P>(path: P, obj: &mut ObjInfo) -> Result<Option<FileReadInfo>>
where P: AsRef<Path> { where P: AsRef<Path> {
Ok(if path.as_ref().is_file() { Ok(if path.as_ref().is_file() {
@ -428,6 +438,9 @@ where W: Write + ?Sized {
if let Some(comment_version) = unit.comment_version { if let Some(comment_version) = unit.comment_version {
write!(w, " comment:{}", comment_version)?; write!(w, " comment:{}", comment_version)?;
} }
if let Some(order) = unit.order {
write!(w, " order:{}", order)?;
}
writeln!(w)?; writeln!(w)?;
let mut split_iter = obj.sections.all_splits().peekable(); let mut split_iter = obj.sections.all_splits().peekable();
while let Some((_section_index, section, addr, split)) = split_iter.next() { while let Some((_section_index, section, addr, split)) = split_iter.next() {
@ -475,6 +488,8 @@ struct SplitUnit {
name: String, name: String,
/// MW `.comment` section version /// MW `.comment` section version
comment_version: Option<u8>, comment_version: Option<u8>,
/// Influences the order of this unit relative to other ordered units.
order: Option<i32>,
} }
pub struct SectionDef { pub struct SectionDef {
@ -515,12 +530,13 @@ fn parse_unit_line(captures: Captures) -> Result<SplitLine> {
if name == "Sections" { if name == "Sections" {
return Ok(SplitLine::SectionsStart); return Ok(SplitLine::SectionsStart);
} }
let mut unit = SplitUnit { name: name.to_string(), comment_version: None }; let mut unit = SplitUnit { name: name.to_string(), comment_version: None, order: None };
for attr in captures["attrs"].split(' ').filter(|&s| !s.is_empty()) { for attr in captures["attrs"].split(' ').filter(|&s| !s.is_empty()) {
if let Some((attr, value)) = attr.split_once(':') { if let Some((attr, value)) = attr.split_once(':') {
match attr { match attr {
"comment" => unit.comment_version = Some(u8::from_str(value)?), "comment" => unit.comment_version = Some(u8::from_str(value)?),
"order" => unit.order = Some(parse_i32(value)?),
_ => bail!("Unknown unit attribute '{}'", attr), _ => bail!("Unknown unit attribute '{}'", attr),
} }
} else { } else {
@ -631,12 +647,13 @@ where R: BufRead + ?Sized {
match (&mut state, split_line) { match (&mut state, split_line) {
( (
SplitState::None | SplitState::Unit(_) | SplitState::Sections(_), SplitState::None | SplitState::Unit(_) | SplitState::Sections(_),
SplitLine::Unit(SplitUnit { name, comment_version }), SplitLine::Unit(SplitUnit { name, comment_version, order }),
) => { ) => {
obj.link_order.push(ObjUnit { obj.link_order.push(ObjUnit {
name: name.clone(), name: name.clone(),
autogenerated: false, autogenerated: false,
comment_version, comment_version,
order,
}); });
state = SplitState::Unit(name); state = SplitState::Unit(name);
} }

View File

@ -316,6 +316,7 @@ where P: AsRef<Path> {
name: file_name.clone(), name: file_name.clone(),
autogenerated: false, autogenerated: false,
comment_version: None, comment_version: None,
order: None,
}); });
} }

View File

@ -796,6 +796,7 @@ pub fn apply_map(result: &MapInfo, obj: &mut ObjInfo) -> Result<()> {
name: unit.clone(), name: unit.clone(),
autogenerated: false, autogenerated: false,
comment_version: Some(0), comment_version: Some(0),
order: None,
}); });
} }

View File

@ -1,6 +1,6 @@
use std::{ use std::{
cmp::{max, min, Ordering}, cmp::{max, min, Ordering},
collections::{BTreeMap, HashMap, HashSet}, collections::{btree_map, BTreeMap, HashMap, HashSet},
}; };
use anyhow::{anyhow, bail, ensure, Context, Result}; use anyhow::{anyhow, bail, ensure, Context, Result};
@ -816,8 +816,8 @@ fn resolve_link_order(obj: &ObjInfo) -> Result<Vec<ObjUnit>> {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
struct SplitEdge { struct SplitEdge {
from: u32, from: i64,
to: u32, to: i64,
} }
let mut graph = Graph::<String, SplitEdge>::new(); let mut graph = Graph::<String, SplitEdge>::new();
@ -852,11 +852,36 @@ fn resolve_link_order(obj: &ObjInfo) -> Result<Vec<ObjUnit>> {
); );
let a_index = *unit_to_index_map.get(&a.unit).unwrap(); let a_index = *unit_to_index_map.get(&a.unit).unwrap();
let b_index = *unit_to_index_map.get(&b.unit).unwrap(); let b_index = *unit_to_index_map.get(&b.unit).unwrap();
graph.add_edge(a_index, b_index, SplitEdge { from: a_addr, to: b_addr }); graph.add_edge(a_index, b_index, SplitEdge {
from: a_addr as i64,
to: b_addr as i64,
});
} }
} }
} }
// Apply link order constraints provided by the user
let mut ordered_units = BTreeMap::<i32, String>::new();
for unit in &obj.link_order {
if let Some(order) = unit.order {
match ordered_units.entry(order) {
btree_map::Entry::Vacant(entry) => {
entry.insert(unit.name.clone());
}
btree_map::Entry::Occupied(entry) => {
bail!("Duplicate order {} for units {} and {}", order, entry.get(), unit.name);
}
}
}
}
let mut iter = ordered_units
.into_iter()
.filter_map(|(order, unit)| unit_to_index_map.get(&unit).map(|&index| (order, index)))
.peekable();
while let (Some((a_order, a_index)), Some((b_order, b_index))) = (iter.next(), iter.peek()) {
graph.add_edge(a_index, *b_index, SplitEdge { from: a_order as i64, to: *b_order as i64 });
}
// use petgraph::{ // use petgraph::{
// dot::{Config, Dot}, // dot::{Config, Dot},
// graph::EdgeReference, // graph::EdgeReference,
@ -886,6 +911,7 @@ fn resolve_link_order(obj: &ObjInfo) -> Result<Vec<ObjUnit>> {
name: name.clone(), name: name.clone(),
autogenerated: obj.is_unit_autogenerated(name), autogenerated: obj.is_unit_autogenerated(name),
comment_version: None, comment_version: None,
order: None,
} }
} }
}) })