2019-11-27 09:18:17 -05:00
|
|
|
// Proof-of-concept TAME linker
|
|
|
|
//
|
|
|
|
// Copyright (C) 2014-2019 Ryan Specialty Group, LLC.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! **This is a poorly-written proof of concept; do not use!** It has been
|
|
|
|
//! banished to its own file to try to make that more clear.
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
use crate::global;
|
|
|
|
use crate::ir::asg::IdentKind;
|
|
|
|
use crate::ir::asg::{Asg, DefaultAsg, Object, ObjectRef};
|
|
|
|
use crate::obj::xmlo::reader::{XmloError, XmloEvent, XmloReader};
|
2020-01-09 10:56:24 -05:00
|
|
|
use crate::sym::{DefaultInterner, Interner};
|
2020-01-12 22:59:16 -05:00
|
|
|
use petgraph::visit::DfsPostOrder;
|
2019-11-27 09:18:17 -05:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2020-01-12 22:59:16 -05:00
|
|
|
use std::convert::TryInto;
|
2019-11-27 09:18:17 -05:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fs;
|
2020-01-09 10:56:24 -05:00
|
|
|
use std::io::BufReader;
|
2019-11-27 09:18:17 -05:00
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
type LinkerAsg<'i> = DefaultAsg<'i, global::ProgIdentSize>;
|
|
|
|
type LinkerObjectRef = ObjectRef<global::ProgIdentSize>;
|
2019-11-27 09:18:17 -05:00
|
|
|
|
|
|
|
pub fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
let mut pkgs_seen = HashSet::<String>::new();
|
2020-01-09 10:56:24 -05:00
|
|
|
let mut fragments = HashMap::<&str, String>::new();
|
2020-01-12 22:59:16 -05:00
|
|
|
let mut depgraph = LinkerAsg::with_capacity(65536, 65536);
|
|
|
|
let mut roots = Vec::new();
|
2020-01-09 10:56:24 -05:00
|
|
|
let interner = DefaultInterner::new();
|
2019-11-27 09:18:17 -05:00
|
|
|
|
|
|
|
let package_path = std::env::args().nth(1).expect("Missing argument");
|
|
|
|
let abs_path = fs::canonicalize(package_path).unwrap();
|
|
|
|
|
|
|
|
println!("WARNING: This is proof-of-concept; do not use!");
|
|
|
|
|
|
|
|
load_xmlo(
|
|
|
|
&abs_path.to_str().unwrap().to_string(),
|
|
|
|
&mut pkgs_seen,
|
|
|
|
&mut fragments,
|
2019-12-01 01:17:37 -05:00
|
|
|
&mut depgraph,
|
2020-01-09 10:56:24 -05:00
|
|
|
&interner,
|
2020-01-12 22:59:16 -05:00
|
|
|
&mut roots,
|
2019-11-27 09:18:17 -05:00
|
|
|
)?;
|
|
|
|
|
2019-12-01 01:17:37 -05:00
|
|
|
// println!(
|
|
|
|
// "Graph {:?}",
|
|
|
|
// depgraph
|
|
|
|
// .graph
|
|
|
|
// .raw_nodes()
|
|
|
|
// .iter()
|
|
|
|
// .map(|node| &node.weight)
|
|
|
|
// .collect::<Vec<_>>()
|
|
|
|
// );
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
roots.extend(
|
|
|
|
vec!["___yield", "___worksheet"]
|
|
|
|
.iter()
|
|
|
|
.map(|name| interner.intern(name))
|
|
|
|
.filter_map(|sym| depgraph.lookup(sym)),
|
|
|
|
);
|
|
|
|
|
|
|
|
let sorted = sort_deps(&depgraph, &roots);
|
2019-11-27 09:18:17 -05:00
|
|
|
|
|
|
|
println!("Sorted ({}): {:?}", sorted.len(), sorted);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-09 10:56:24 -05:00
|
|
|
fn load_xmlo<'a, 'i, I: Interner<'i>>(
|
2019-11-27 09:18:17 -05:00
|
|
|
path_str: &'a str,
|
|
|
|
pkgs_seen: &mut HashSet<String>,
|
2020-01-09 10:56:24 -05:00
|
|
|
fragments: &mut HashMap<&'i str, String>,
|
2020-01-12 22:59:16 -05:00
|
|
|
depgraph: &mut LinkerAsg<'i>,
|
2020-01-09 10:56:24 -05:00
|
|
|
interner: &'i I,
|
2020-01-12 22:59:16 -05:00
|
|
|
roots: &mut Vec<LinkerObjectRef>,
|
2019-11-27 09:18:17 -05:00
|
|
|
) -> Result<(), Box<dyn Error>> {
|
|
|
|
let path = fs::canonicalize(path_str)?;
|
|
|
|
let path_str = path.to_str().unwrap();
|
|
|
|
|
|
|
|
if !pkgs_seen.insert(path_str.to_string()) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-01-09 10:56:24 -05:00
|
|
|
//println!("processing {}", path_str);
|
|
|
|
|
|
|
|
let mut found = HashSet::<&str>::new();
|
|
|
|
|
|
|
|
let file = fs::File::open(&path)?;
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
let mut xmlo = XmloReader::new(reader, interner);
|
2020-01-12 22:59:16 -05:00
|
|
|
let mut elig = None;
|
2020-01-09 10:56:24 -05:00
|
|
|
|
|
|
|
loop {
|
2020-01-12 22:59:16 -05:00
|
|
|
match xmlo.read_event() {
|
|
|
|
Ok(XmloEvent::Package(attrs)) => {
|
|
|
|
elig = attrs.elig;
|
|
|
|
}
|
2020-01-11 23:29:53 -05:00
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
Ok(XmloEvent::SymDeps(sym, deps)) => {
|
2020-01-09 10:56:24 -05:00
|
|
|
// TODO: API needs to expose whether a symbol is already
|
|
|
|
// known so that we can warn on them
|
|
|
|
//
|
|
|
|
// note: using from_utf8_unchecked here did _not_ improve
|
|
|
|
// performance
|
2020-01-12 22:59:16 -05:00
|
|
|
let sym_node = depgraph
|
|
|
|
.lookup(sym)
|
|
|
|
.expect(&format!("missing sym for deps: `{}`", sym));
|
2020-01-09 10:56:24 -05:00
|
|
|
|
|
|
|
for dep_sym in deps {
|
2020-01-12 22:59:16 -05:00
|
|
|
let dep_node = depgraph.lookup(dep_sym).expect(&format!(
|
|
|
|
"missing dep sym for deps: `{}` -> `{}`",
|
|
|
|
sym, dep_sym
|
|
|
|
));
|
|
|
|
|
|
|
|
depgraph.add_dep(sym_node, dep_node);
|
2020-01-02 23:29:49 -05:00
|
|
|
}
|
2020-01-09 10:56:24 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
Ok(XmloEvent::SymDecl(sym, attrs)) => {
|
2020-01-09 10:56:24 -05:00
|
|
|
if let Some(sym_src) = attrs.src {
|
|
|
|
found.insert(sym_src);
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
2020-01-12 22:59:16 -05:00
|
|
|
|
|
|
|
let owned = attrs.src.is_none();
|
|
|
|
|
|
|
|
let kind = attrs.try_into().map_err(|err| {
|
|
|
|
format!("sym `{}` attrs error: {}", sym, err)
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: should probably track these down in the XSLT linker...
|
|
|
|
match kind {
|
|
|
|
Ok(kindval) => {
|
|
|
|
// TODO: inefficient
|
|
|
|
let link_root = owned
|
|
|
|
&& (kindval == IdentKind::Meta
|
|
|
|
|| sym.starts_with(":map:")
|
|
|
|
|| sym.starts_with(":retmap:"));
|
|
|
|
|
|
|
|
let node = depgraph.declare(sym, kindval)?;
|
|
|
|
|
|
|
|
if link_root {
|
|
|
|
roots.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => println!("{:?}; skipping...", e),
|
|
|
|
};
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
Ok(XmloEvent::Fragment(sym, text)) => {
|
|
|
|
let result = depgraph.set_fragment(
|
|
|
|
depgraph.lookup(sym).expect(&format!(
|
|
|
|
"missing symbol for fragment: {}",
|
|
|
|
sym
|
|
|
|
)),
|
|
|
|
text,
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => println!("{:?}; skipping...", e),
|
|
|
|
};
|
2020-01-09 10:56:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to read any further than the end of the
|
|
|
|
// header (symtable, sym-deps, fragments)
|
2020-01-12 22:59:16 -05:00
|
|
|
Ok(XmloEvent::Eoh) => break,
|
|
|
|
|
|
|
|
Err(err @ XmloError::UnassociatedFragment) => {
|
|
|
|
println!("{:?}; skipping...", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
err @ Err(_) => err.map(|_| ())?,
|
2020-01-09 10:56:24 -05:00
|
|
|
}
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
if let Some(elig_sym) = elig {
|
|
|
|
roots.push(depgraph.lookup(elig_sym).expect(
|
|
|
|
"internal error: package elig references nonexistant symbol",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:18:17 -05:00
|
|
|
let mut dir = path.clone();
|
|
|
|
dir.pop();
|
|
|
|
|
|
|
|
for relpath in found.iter() {
|
|
|
|
let mut path_buf = dir.clone();
|
|
|
|
path_buf.push(relpath);
|
|
|
|
path_buf.set_extension("xmlo");
|
|
|
|
|
|
|
|
//println!("Trying {:?}", path_buf);
|
|
|
|
let path_abs = path_buf.canonicalize().unwrap();
|
|
|
|
let path = path_abs.to_str().unwrap();
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
load_xmlo(path, pkgs_seen, fragments, depgraph, interner, roots)?;
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
fn sort_deps<'a, 'i>(
|
|
|
|
depgraph: &'a LinkerAsg<'i>,
|
|
|
|
roots: &Vec<LinkerObjectRef>,
|
|
|
|
) -> Vec<&'a Object<'i>> {
|
2019-11-27 09:18:17 -05:00
|
|
|
// @type=meta, @preproc:elig-class-yields
|
|
|
|
// @type={ret}map{,:head,:tail}
|
|
|
|
|
2019-12-01 01:17:37 -05:00
|
|
|
// This is technically a topological sort, but functions have
|
|
|
|
// cycles. Once we have more symbol metadata, we can filter them out
|
|
|
|
// and actually invoke toposort.
|
|
|
|
let mut dfs = DfsPostOrder::empty(&depgraph);
|
|
|
|
let mut sorted = Vec::new();
|
2019-11-27 09:18:17 -05:00
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
//println!("discovered roots: {:?}", roots);
|
|
|
|
|
2019-12-01 01:17:37 -05:00
|
|
|
// TODO: we'll be processing various roots separately
|
|
|
|
for index in roots {
|
2020-01-12 22:59:16 -05:00
|
|
|
dfs.stack.push((*index).into());
|
2019-12-01 01:17:37 -05:00
|
|
|
}
|
2019-11-27 09:18:17 -05:00
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
// TODO: can we encapsulate NodeIndex?
|
2019-12-01 01:17:37 -05:00
|
|
|
while let Some(index) = dfs.next(&depgraph) {
|
2020-01-12 22:59:16 -05:00
|
|
|
sorted.push(depgraph.get(index).unwrap());
|
2019-12-01 01:17:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sorted
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-12-06 15:03:29 -05:00
|
|
|
mod test {
|
2019-11-27 09:18:17 -05:00
|
|
|
#[test]
|
|
|
|
fn placeholder() {}
|
|
|
|
}
|