2019-11-27 09:18:17 -05:00
|
|
|
// Proof-of-concept TAME linker
|
|
|
|
//
|
2020-03-06 11:05:18 -05:00
|
|
|
// Copyright (C) 2014-2020 Ryan Specialty Group, LLC.
|
|
|
|
//
|
|
|
|
// This file is part of TAME.
|
2019-11-27 09:18:17 -05:00
|
|
|
//
|
|
|
|
// 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-04-06 16:13:32 -04:00
|
|
|
use crate::fs::{Filesystem, VisitOnceFile, VisitOnceFilesystem};
|
2020-01-12 22:59:16 -05:00
|
|
|
use crate::global;
|
2020-03-07 10:49:41 -05:00
|
|
|
use crate::ir::asg::{
|
2020-03-25 10:20:25 -04:00
|
|
|
Asg, AsgError, DefaultAsg, IdentKind, IdentObject, IdentObjectData,
|
|
|
|
ObjectRef, Sections, SortableAsg, Source,
|
2020-03-07 10:49:41 -05:00
|
|
|
};
|
2020-03-11 10:23:34 -04:00
|
|
|
use crate::obj::xmle::writer::XmleWriter;
|
2020-01-12 22:59:16 -05:00
|
|
|
use crate::obj::xmlo::reader::{XmloError, XmloEvent, XmloReader};
|
2020-01-14 16:26:36 -05:00
|
|
|
use crate::sym::{DefaultInterner, Interner, Symbol};
|
2020-04-06 16:13:32 -04:00
|
|
|
use fxhash::{FxBuildHasher, FxHashMap, FxHashSet};
|
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-02-13 15:43:25 -05:00
|
|
|
use std::io::BufReader;
|
2019-11-27 09:18:17 -05:00
|
|
|
|
2020-03-16 11:49:41 -04:00
|
|
|
type LinkerAsg<'i> = DefaultAsg<'i, IdentObject<'i>, global::ProgIdentSize>;
|
2020-01-12 22:59:16 -05:00
|
|
|
type LinkerObjectRef = ObjectRef<global::ProgIdentSize>;
|
2019-11-27 09:18:17 -05:00
|
|
|
|
2020-03-06 12:44:22 -05:00
|
|
|
type LoadResult<'i> =
|
|
|
|
Result<Option<(Option<&'i Symbol<'i>>, Option<String>)>, Box<dyn Error>>;
|
|
|
|
|
2020-03-04 15:31:20 -05:00
|
|
|
pub fn main(package_path: &str, output: &str) -> Result<(), Box<dyn Error>> {
|
2020-04-06 16:13:32 -04:00
|
|
|
let mut fs = VisitOnceFilesystem::new();
|
2020-01-15 10:27:35 -05:00
|
|
|
let mut fragments: FxHashMap<&str, String> = Default::default();
|
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
|
|
|
|
2020-03-06 11:29:04 -05:00
|
|
|
let abs_path = fs::canonicalize(package_path)?;
|
2019-11-27 09:18:17 -05:00
|
|
|
|
2020-01-14 16:26:36 -05:00
|
|
|
let (name, relroot) = load_xmlo(
|
2019-11-27 09:18:17 -05:00
|
|
|
&abs_path.to_str().unwrap().to_string(),
|
2020-04-06 16:13:32 -04:00
|
|
|
&mut fs,
|
2019-11-27 09:18:17 -05:00
|
|
|
&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,
|
2020-01-14 16:26:36 -05:00
|
|
|
)?
|
|
|
|
.expect("missing root package information");
|
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)),
|
|
|
|
);
|
|
|
|
|
2020-03-25 10:20:25 -04:00
|
|
|
let mut sorted = match depgraph.sort(&roots) {
|
|
|
|
Ok(sections) => sections,
|
|
|
|
Err(AsgError::Cycles(cycles)) => {
|
|
|
|
let msg: Vec<String> = cycles
|
|
|
|
.into_iter()
|
|
|
|
.map(|cycle| {
|
|
|
|
let mut path: Vec<String> = cycle
|
|
|
|
.into_iter()
|
|
|
|
.map(|obj| {
|
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
depgraph.get(obj).unwrap().name().unwrap()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
path.reverse();
|
|
|
|
path.push(path[0].clone());
|
|
|
|
format!("cycle: {}", path.join(" -> "))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
return Err(msg.join("\n").into());
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
};
|
2019-11-27 09:18:17 -05:00
|
|
|
|
2020-01-13 16:41:06 -05:00
|
|
|
//println!("Sorted ({}): {:?}", sorted.len(), sorted);
|
|
|
|
|
2020-01-14 16:26:36 -05:00
|
|
|
output_xmle(
|
|
|
|
&depgraph,
|
|
|
|
&interner,
|
2020-02-13 15:43:25 -05:00
|
|
|
&mut sorted,
|
2020-01-14 16:26:36 -05:00
|
|
|
name.expect("missing root package name"),
|
|
|
|
relroot.expect("missing root package relroot"),
|
2020-03-04 15:31:20 -05:00
|
|
|
output,
|
2020-01-14 16:26:36 -05:00
|
|
|
)?;
|
2019-11-27 09:18:17 -05:00
|
|
|
|
|
|
|
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,
|
2020-04-06 16:13:32 -04:00
|
|
|
fs: &mut VisitOnceFilesystem<FxBuildHasher>,
|
2020-01-15 10:27:35 -05:00
|
|
|
fragments: &mut FxHashMap<&'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>,
|
2020-03-06 12:44:22 -05:00
|
|
|
) -> LoadResult<'i> {
|
2019-11-27 09:18:17 -05:00
|
|
|
let path = fs::canonicalize(path_str)?;
|
2020-04-06 16:13:32 -04:00
|
|
|
let first = fs.visit_len() == 0;
|
2020-01-09 10:56:24 -05:00
|
|
|
|
2020-01-15 10:27:35 -05:00
|
|
|
let mut found: FxHashSet<&str> = Default::default();
|
2020-01-09 10:56:24 -05:00
|
|
|
|
2020-04-06 16:13:32 -04:00
|
|
|
let file: fs::File = match fs.open(&path)? {
|
|
|
|
VisitOnceFile::FirstVisit(file) => file,
|
|
|
|
VisitOnceFile::Visited => return Ok(None),
|
|
|
|
};
|
|
|
|
|
2020-01-09 10:56:24 -05:00
|
|
|
let reader = BufReader::new(file);
|
2020-04-06 13:39:13 -04:00
|
|
|
let mut xmlo: XmloReader<'_, _, _> = (reader, interner).into();
|
2020-01-12 22:59:16 -05:00
|
|
|
let mut elig = None;
|
2020-01-09 10:56:24 -05:00
|
|
|
|
2020-01-14 16:26:36 -05:00
|
|
|
let mut name: Option<&'i Symbol<'i>> = None;
|
|
|
|
let mut relroot: Option<String> = 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)) => {
|
2020-01-14 16:26:36 -05:00
|
|
|
if first {
|
|
|
|
name = attrs.name;
|
|
|
|
relroot = attrs.relroot;
|
|
|
|
}
|
2020-01-12 22:59:16 -05:00
|
|
|
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
|
|
|
|
|
2020-01-13 16:41:06 -05:00
|
|
|
// Maps should not pull in symbols since we may end up
|
|
|
|
// mapping to params that are never actually used
|
|
|
|
if !sym.starts_with(":map:") {
|
|
|
|
for dep_sym in deps {
|
2020-01-14 16:26:36 -05:00
|
|
|
depgraph.add_dep_lookup(sym, dep_sym);
|
2020-01-13 16:41:06 -05:00
|
|
|
}
|
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);
|
2020-01-14 16:26:36 -05:00
|
|
|
} else {
|
|
|
|
let owned = attrs.src.is_none();
|
2020-03-25 23:49:37 -04:00
|
|
|
let extern_ = attrs.extern_;
|
2020-01-14 16:26:36 -05:00
|
|
|
|
|
|
|
let kind = (&attrs).try_into().map_err(|err| {
|
|
|
|
format!("sym `{}` attrs error: {}", sym, err)
|
|
|
|
});
|
|
|
|
|
2020-03-25 23:49:37 -04:00
|
|
|
let mut src: Source = attrs.into();
|
2020-01-14 16:26:36 -05:00
|
|
|
|
2020-03-25 23:49:37 -04:00
|
|
|
// Existing convention is to omit @src of local package
|
|
|
|
// (in this case, the program being linked)
|
|
|
|
if first {
|
|
|
|
src.pkg_name = None;
|
|
|
|
}
|
2020-01-13 15:15:38 -05:00
|
|
|
|
2020-01-14 16:26:36 -05:00
|
|
|
match kind {
|
|
|
|
Ok(kindval) => {
|
|
|
|
// TODO: inefficient
|
|
|
|
let link_root = owned
|
|
|
|
&& (kindval == IdentKind::Meta
|
|
|
|
|| kindval == IdentKind::Map
|
|
|
|
|| kindval == IdentKind::RetMap);
|
2020-01-12 22:59:16 -05:00
|
|
|
|
2020-03-25 23:49:37 -04:00
|
|
|
if extern_ {
|
|
|
|
depgraph.declare_extern(sym, kindval, src)?;
|
|
|
|
} else {
|
2020-03-26 00:10:47 -04:00
|
|
|
let node =
|
|
|
|
depgraph.declare(sym, kindval, src)?;
|
2020-03-25 23:49:37 -04:00
|
|
|
|
|
|
|
if link_root {
|
|
|
|
roots.push(node);
|
|
|
|
}
|
2020-01-14 16:26:36 -05:00
|
|
|
}
|
2020-01-12 22:59:16 -05:00
|
|
|
}
|
2020-03-06 13:39:44 -05:00
|
|
|
Err(e) => return Err(e.into()),
|
2020-01-14 16:26:36 -05:00
|
|
|
};
|
|
|
|
}
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 22:59:16 -05:00
|
|
|
Ok(XmloEvent::Fragment(sym, text)) => {
|
2020-03-07 10:28:39 -05:00
|
|
|
match depgraph.lookup(sym) {
|
|
|
|
Some(frag) => match depgraph.set_fragment(frag, text) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
return Err(XmloError::MissingFragment(String::from(
|
|
|
|
"missing fragment",
|
|
|
|
))
|
|
|
|
.into());
|
|
|
|
}
|
2020-01-12 22:59:16 -05:00
|
|
|
};
|
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,
|
|
|
|
|
2020-03-06 14:08:55 -05:00
|
|
|
Err(e) => return Err(e.into()),
|
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");
|
|
|
|
|
2020-01-14 16:26:36 -05:00
|
|
|
// println!("Trying {:?}", path_buf);
|
2020-03-06 12:32:42 -05:00
|
|
|
let path_abs = path_buf.canonicalize()?;
|
2019-11-27 09:18:17 -05:00
|
|
|
let path = path_abs.to_str().unwrap();
|
|
|
|
|
2020-04-06 16:13:32 -04:00
|
|
|
load_xmlo(path, fs, fragments, depgraph, interner, roots)?;
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:26:36 -05:00
|
|
|
if first {
|
|
|
|
Ok(Some((name, relroot)))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
2019-11-27 09:18:17 -05:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:19:43 -04:00
|
|
|
fn get_ident<'a, 'i>(
|
2020-02-13 15:43:25 -05:00
|
|
|
depgraph: &'a LinkerAsg<'i>,
|
2020-03-13 01:19:43 -04:00
|
|
|
name: &'i Symbol<'i>,
|
2020-03-16 11:49:41 -04:00
|
|
|
) -> Result<&'a IdentObject<'i>, XmloError> {
|
2020-03-13 01:19:43 -04:00
|
|
|
depgraph
|
|
|
|
.lookup(name)
|
|
|
|
.and_then(|id| depgraph.get(id))
|
|
|
|
.ok_or(XmloError::MissingFragment(String::from(name as &str)))
|
2020-02-13 15:43:25 -05:00
|
|
|
}
|
|
|
|
|
2020-01-13 16:41:06 -05:00
|
|
|
fn output_xmle<'a, 'i, I: Interner<'i>>(
|
|
|
|
depgraph: &'a LinkerAsg<'i>,
|
|
|
|
interner: &'i I,
|
2020-03-16 11:49:41 -04:00
|
|
|
sorted: &mut Sections<'a, IdentObject<'i>>,
|
2020-01-14 16:26:36 -05:00
|
|
|
name: &'i Symbol<'i>,
|
|
|
|
relroot: String,
|
2020-03-04 15:31:20 -05:00
|
|
|
output: &str,
|
2020-01-13 16:41:06 -05:00
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-02-13 15:43:25 -05:00
|
|
|
if !sorted.map.is_empty() {
|
2020-03-13 01:19:43 -04:00
|
|
|
sorted
|
|
|
|
.map
|
|
|
|
.push_head(get_ident(depgraph, interner.intern(":map:___head"))?);
|
|
|
|
sorted
|
|
|
|
.map
|
|
|
|
.push_tail(get_ident(depgraph, interner.intern(":map:___tail"))?);
|
2020-01-13 16:41:06 -05:00
|
|
|
}
|
|
|
|
|
2020-02-13 15:43:25 -05:00
|
|
|
if !sorted.retmap.is_empty() {
|
2020-03-13 01:19:43 -04:00
|
|
|
sorted.retmap.push_head(get_ident(
|
2020-02-13 15:43:25 -05:00
|
|
|
depgraph,
|
2020-03-13 01:19:43 -04:00
|
|
|
interner.intern(":retmap:___head"),
|
2020-03-07 11:18:56 -05:00
|
|
|
)?);
|
2020-03-13 01:19:43 -04:00
|
|
|
sorted.retmap.push_tail(get_ident(
|
2020-02-13 15:43:25 -05:00
|
|
|
depgraph,
|
2020-03-13 01:19:43 -04:00
|
|
|
interner.intern(":retmap:___tail"),
|
2020-03-07 11:18:56 -05:00
|
|
|
)?);
|
2020-01-13 16:41:06 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 15:31:20 -05:00
|
|
|
let file = fs::File::create(output)?;
|
|
|
|
let mut xmle_writer = XmleWriter::new(file);
|
2020-03-07 11:18:56 -05:00
|
|
|
xmle_writer.write(&sorted, name, &relroot)?;
|
2020-01-13 16:41:06 -05:00
|
|
|
|
|
|
|
Ok(())
|
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() {}
|
|
|
|
}
|