Call linker from cg_clif
This commit is contained in:
parent
104e9ffe89
commit
640d66c777
79
Cargo.lock
generated
79
Cargo.lock
generated
@ -87,6 +87,14 @@ dependencies = [
|
||||
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cloudabi"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cranelift"
|
||||
version = "0.23.0"
|
||||
@ -258,6 +266,20 @@ dependencies = [
|
||||
"synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon-sys"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "gcc"
|
||||
version = "0.3.55"
|
||||
@ -361,6 +383,31 @@ dependencies = [
|
||||
"proc-macro2 0.4.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.5.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "raw-cpuid"
|
||||
version = "6.0.0"
|
||||
@ -415,6 +462,14 @@ dependencies = [
|
||||
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "remove_dir_all"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.9"
|
||||
@ -427,12 +482,15 @@ dependencies = [
|
||||
"ar 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cranelift 0.23.0 (git+https://github.com/CraneStation/cranelift.git)",
|
||||
"cranelift-faerie 0.23.0 (git+https://github.com/CraneStation/cranelift.git)",
|
||||
"cranelift-module 0.23.0 (git+https://github.com/CraneStation/cranelift.git)",
|
||||
"cranelift-simplejit 0.23.0 (git+https://github.com/CraneStation/cranelift.git)",
|
||||
"faerie 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"target-lexicon 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -559,6 +617,19 @@ dependencies = [
|
||||
"serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.0.4"
|
||||
@ -676,6 +747,7 @@ dependencies = [
|
||||
"checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16"
|
||||
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
|
||||
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
|
||||
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
|
||||
"checksum cranelift 0.23.0 (git+https://github.com/CraneStation/cranelift.git)" = "<none>"
|
||||
"checksum cranelift-bforest 0.23.0 (git+https://github.com/CraneStation/cranelift.git)" = "<none>"
|
||||
"checksum cranelift-codegen 0.23.0 (git+https://github.com/CraneStation/cranelift.git)" = "<none>"
|
||||
@ -692,6 +764,8 @@ dependencies = [
|
||||
"checksum faerie 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7f869e12bcad2b4ff6ac60e01c2354957312fbf0c10dc938f340d58932b3db52"
|
||||
"checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7"
|
||||
"checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596"
|
||||
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
|
||||
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
||||
"checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
|
||||
"checksum goblin 0.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "c65cd533b33e3d04c6e393225fa8919ddfcf5862ca8919c7f9a167c312ef41c2"
|
||||
"checksum heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea04fa3ead4e05e51a7c806fc07271fdbde4e246a6c6d1efd52e72230b771b82"
|
||||
@ -707,12 +781,16 @@ dependencies = [
|
||||
"checksum proc-macro2 0.4.21 (registry+https://github.com/rust-lang/crates.io-index)" = "ab2fc21ba78ac73e4ff6b3818ece00be4e175ffbef4d0a717d978b48b24150c4"
|
||||
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
|
||||
"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
|
||||
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
|
||||
"checksum rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1961a422c4d189dfb50ffa9320bf1f2a9bd54ecb92792fb9477f99a1045f3372"
|
||||
"checksum rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0905b6b7079ec73b314d4c748701f6931eb79fd97c668caa3f1899b22b32c6db"
|
||||
"checksum raw-cpuid 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "41219962ecab392f1e68db9e7ebd972800d4045a128cc23462b384e8c312cde1"
|
||||
"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
|
||||
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
|
||||
"checksum regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ee84f70c8c08744ea9641a731c7fadb475bf2ecc52d7f627feb833e0b3990467"
|
||||
"checksum regex-syntax 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fbc557aac2b708fe84121caf261346cc2eed71978024337e42eb46b8a252ac6e"
|
||||
"checksum region 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cff177f64744f6f2a2054d231279383e59b0857b4d58dc2e8881e60b58aec8e5"
|
||||
"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5"
|
||||
"checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395"
|
||||
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
"checksum ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7"
|
||||
@ -729,6 +807,7 @@ dependencies = [
|
||||
"checksum syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)" = "90c39a061e2f412a9f869540471ab679e85e50c6b05604daf28bc3060f75c430"
|
||||
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
|
||||
"checksum target-lexicon 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4af5e2227f0b887d591d3724b796a96eff04226104d872f5b3883fcd427d64b9"
|
||||
"checksum tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "55c1195ef8513f3273d55ff59fe5da6940287a0d7a98331254397f464833675b"
|
||||
"checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f"
|
||||
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
|
||||
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
|
||||
|
@ -25,6 +25,9 @@ faerie = "0.6.0"
|
||||
ar = "0.6.0"
|
||||
bitflags = "1.0.3"
|
||||
byteorder = "1.2.6"
|
||||
cc = "1.0.25"
|
||||
libc = "0.2.43"
|
||||
tempfile = "3.0.4"
|
||||
|
||||
# Uncomment to use local checkout of cranelift
|
||||
#[patch."https://github.com/CraneStation/cranelift.git"]
|
||||
|
16
build.sh
16
build.sh
@ -14,11 +14,7 @@ fi
|
||||
|
||||
build_example_bin() {
|
||||
$RUSTC $2 --crate-name $1 --crate-type bin
|
||||
|
||||
pushd target/out
|
||||
gcc $1 libmini_core.rlib -o $1_bin
|
||||
sh -c ./$1_bin || true
|
||||
popd
|
||||
sh -c ./target/out/$1 || true
|
||||
}
|
||||
|
||||
if [[ "$1" == "--release" ]]; then
|
||||
@ -54,16 +50,12 @@ rm -r target || true
|
||||
time xargo build
|
||||
popd
|
||||
|
||||
$RUSTC --sysroot ~/.xargo/HOST example/alloc_example.rs --crate-type bin
|
||||
# TODO linux linker doesn't accept duplicate definitions
|
||||
#gcc -Wl,--gc-sections target/out/alloc_example ~/.xargo/HOST/lib/rustlib/*/lib/libcore-*.rlib ~/.xargo/HOST/lib/rustlib/*/lib/liballoc-*.rlib ~/.xargo/HOST/lib/rustlib/*/lib/liballoc_system-*.rlib -o target/out/alloc_example_exe
|
||||
#hyperfine ./target/out/alloc_example_exe
|
||||
$RUSTC --sysroot ~/.xargo/HOST example/alloc_example.rs --crate-type bin
|
||||
./target/out/alloc_example
|
||||
|
||||
$RUSTC --sysroot ~/.xargo/HOST example/mod_bench.rs --crate-type bin
|
||||
gcc -Wl,--gc-sections target/out/mod_bench -lc -o target/out/mod_bench_exe
|
||||
|
||||
$RUSTC --sysroot ~/.xargo/HOST example/mod_bench.rs --crate-type bin -Zmir-opt-level=3 --crate-name mod_bench_inline
|
||||
gcc -Wl,--gc-sections target/out/mod_bench_inline -lc -o target/out/mod_bench_inline_exe
|
||||
|
||||
rustc example/mod_bench.rs --crate-type bin -Copt-level=0 -o target/out/mod_bench_llvm_0 -Cpanic=abort
|
||||
rustc example/mod_bench.rs --crate-type bin -Copt-level=1 -o target/out/mod_bench_llvm_1 -Cpanic=abort
|
||||
@ -71,6 +63,6 @@ rustc example/mod_bench.rs --crate-type bin -Copt-level=2 -o target/out/mod_benc
|
||||
rustc example/mod_bench.rs --crate-type bin -Copt-level=3 -o target/out/mod_bench_llvm_3 -Cpanic=abort
|
||||
echo
|
||||
echo "[Bench] mod_bench"
|
||||
hyperfine ./target/out/mod_bench{,_inline}_exe ./target/out/mod_bench_llvm_*
|
||||
hyperfine ./target/out/mod_bench{,_inline} ./target/out/mod_bench_llvm_*
|
||||
|
||||
cat target/out/log.txt | sort | uniq -c
|
||||
|
99
src/archive.rs
Normal file
99
src/archive.rs
Normal file
@ -0,0 +1,99 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct ArchiveConfig<'a> {
|
||||
pub sess: &'a Session,
|
||||
pub dst: PathBuf,
|
||||
pub src: Option<PathBuf>,
|
||||
pub lib_search_paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
pub struct ArchiveBuilder<'a> {
|
||||
cfg: ArchiveConfig<'a>,
|
||||
src_archive: Option<ar::Archive<File>>,
|
||||
src_entries: HashMap<String, usize>,
|
||||
builder: ar::Builder<File>,
|
||||
update_symbols: bool,
|
||||
}
|
||||
|
||||
impl<'a> ArchiveBuilder<'a> {
|
||||
pub fn new(cfg: ArchiveConfig<'a>) -> Self {
|
||||
let (src_archive, src_entries) = if let Some(src) = &cfg.src {
|
||||
let mut archive = ar::Archive::new(File::open(src).unwrap());
|
||||
let mut entries = HashMap::new();
|
||||
|
||||
let mut i = 0;
|
||||
while let Some(entry) = archive.next_entry() {
|
||||
let entry = entry.unwrap();
|
||||
entries.insert(
|
||||
String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
|
||||
i,
|
||||
);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
(Some(archive), entries)
|
||||
} else {
|
||||
(None, HashMap::new())
|
||||
};
|
||||
|
||||
let builder = ar::Builder::new(File::create(&cfg.dst).unwrap());
|
||||
|
||||
ArchiveBuilder {
|
||||
cfg,
|
||||
src_archive,
|
||||
src_entries,
|
||||
builder,
|
||||
update_symbols: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn src_files(&self) -> Vec<String> {
|
||||
self.src_entries.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn remove_file(&mut self, name: &str) {
|
||||
assert!(
|
||||
self.src_entries.remove(name).is_some(),
|
||||
"Tried to remove file not existing in src archive",
|
||||
);
|
||||
}
|
||||
|
||||
pub fn update_symbols(&mut self) {
|
||||
self.update_symbols = true;
|
||||
}
|
||||
|
||||
pub fn build(mut self) {
|
||||
// Add files from original archive
|
||||
if let Some(mut src_archive) = self.src_archive {
|
||||
for (_entry_name, entry_idx) in self.src_entries.into_iter() {
|
||||
let entry = src_archive.jump_to_entry(entry_idx).unwrap();
|
||||
let orig_header = entry.header();
|
||||
let mut header =
|
||||
ar::Header::new(orig_header.identifier().to_vec(), orig_header.size());
|
||||
header.set_mtime(orig_header.mtime());
|
||||
header.set_uid(orig_header.uid());
|
||||
header.set_gid(orig_header.gid());
|
||||
header.set_mode(orig_header.mode());
|
||||
self.builder.append(&header, entry).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize archive
|
||||
std::mem::drop(self.builder);
|
||||
|
||||
// Run ranlib to be able to link the archive
|
||||
let status = std::process::Command::new("ranlib")
|
||||
.arg(self.cfg.dst)
|
||||
.status()
|
||||
.expect("Couldn't run ranlib");
|
||||
assert!(
|
||||
status.success(),
|
||||
"Ranlib exited with code {:?}",
|
||||
status.code()
|
||||
);
|
||||
}
|
||||
}
|
175
src/lib.rs
175
src/lib.rs
@ -17,6 +17,9 @@ extern crate rustc_mir;
|
||||
extern crate rustc_target;
|
||||
#[macro_use]
|
||||
extern crate rustc_data_structures;
|
||||
extern crate rustc_fs_util;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
extern crate ar;
|
||||
#[macro_use]
|
||||
@ -37,11 +40,19 @@ use std::sync::mpsc;
|
||||
use syntax::symbol::Symbol;
|
||||
|
||||
use rustc::dep_graph::DepGraph;
|
||||
use rustc::middle::cstore::MetadataLoader;
|
||||
use rustc::session::{config::OutputFilenames, CompileIncomplete};
|
||||
use rustc::middle::cstore::{
|
||||
self, CrateSource, LibSource, LinkagePreference, MetadataLoader, NativeLibrary,
|
||||
};
|
||||
use rustc::middle::lang_items::LangItem;
|
||||
use rustc::middle::weak_lang_items;
|
||||
use rustc::session::{
|
||||
config::{self, OutputFilenames, OutputType},
|
||||
CompileIncomplete,
|
||||
};
|
||||
use rustc::ty::query::Providers;
|
||||
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
||||
use rustc_codegen_utils::link::out_filename;
|
||||
use rustc_codegen_utils::linker::LinkerInfo;
|
||||
|
||||
use cranelift::codegen::settings;
|
||||
use cranelift_faerie::*;
|
||||
@ -60,6 +71,7 @@ macro_rules! unimpl {
|
||||
mod abi;
|
||||
mod allocator;
|
||||
mod analyze;
|
||||
mod archive;
|
||||
mod base;
|
||||
mod common;
|
||||
mod constant;
|
||||
@ -88,7 +100,7 @@ mod prelude {
|
||||
self, subst::Substs, FnSig, Instance, InstanceDef, ParamEnv, PolyFnSig, Ty, TyCtxt,
|
||||
TypeAndMut, TypeFoldable,
|
||||
};
|
||||
pub use rustc_codegen_utils::CompiledModule;
|
||||
pub use rustc_codegen_utils::{CompiledModule, ModuleKind};
|
||||
pub use rustc_data_structures::{
|
||||
fx::{FxHashMap, FxHashSet},
|
||||
indexed_vec::Idx,
|
||||
@ -108,7 +120,7 @@ mod prelude {
|
||||
pub use crate::abi::*;
|
||||
pub use crate::base::{trans_operand, trans_place};
|
||||
pub use crate::common::*;
|
||||
pub use crate::{Caches, CodegenResults};
|
||||
pub use crate::{Caches, CodegenResults, CrateInfo};
|
||||
}
|
||||
|
||||
pub struct Caches<'tcx> {
|
||||
@ -127,10 +139,132 @@ impl<'tcx> Caches<'tcx> {
|
||||
|
||||
struct CraneliftCodegenBackend;
|
||||
|
||||
pub struct CrateInfo {
|
||||
panic_runtime: Option<CrateNum>,
|
||||
compiler_builtins: Option<CrateNum>,
|
||||
profiler_runtime: Option<CrateNum>,
|
||||
sanitizer_runtime: Option<CrateNum>,
|
||||
is_no_builtins: FxHashSet<CrateNum>,
|
||||
native_libraries: FxHashMap<CrateNum, Lrc<Vec<NativeLibrary>>>,
|
||||
crate_name: FxHashMap<CrateNum, String>,
|
||||
used_libraries: Lrc<Vec<NativeLibrary>>,
|
||||
link_args: Lrc<Vec<String>>,
|
||||
used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
|
||||
used_crates_static: Vec<(CrateNum, LibSource)>,
|
||||
used_crates_dynamic: Vec<(CrateNum, LibSource)>,
|
||||
wasm_imports: FxHashMap<String, String>,
|
||||
lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
|
||||
missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
|
||||
}
|
||||
|
||||
impl CrateInfo {
|
||||
pub fn new(tcx: TyCtxt) -> CrateInfo {
|
||||
let mut info = CrateInfo {
|
||||
panic_runtime: None,
|
||||
compiler_builtins: None,
|
||||
profiler_runtime: None,
|
||||
sanitizer_runtime: None,
|
||||
is_no_builtins: Default::default(),
|
||||
native_libraries: Default::default(),
|
||||
used_libraries: tcx.native_libraries(LOCAL_CRATE),
|
||||
link_args: tcx.link_args(LOCAL_CRATE),
|
||||
crate_name: Default::default(),
|
||||
used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
|
||||
used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
|
||||
used_crate_source: Default::default(),
|
||||
wasm_imports: Default::default(),
|
||||
lang_item_to_crate: Default::default(),
|
||||
missing_lang_items: Default::default(),
|
||||
};
|
||||
let lang_items = tcx.lang_items();
|
||||
|
||||
let load_wasm_items = tcx
|
||||
.sess
|
||||
.crate_types
|
||||
.borrow()
|
||||
.iter()
|
||||
.any(|c| *c != config::CrateType::Rlib)
|
||||
&& tcx.sess.opts.target_triple.triple() == "wasm32-unknown-unknown";
|
||||
|
||||
if load_wasm_items {
|
||||
info.load_wasm_imports(tcx, LOCAL_CRATE);
|
||||
}
|
||||
|
||||
let crates = tcx.crates();
|
||||
|
||||
let n_crates = crates.len();
|
||||
info.native_libraries.reserve(n_crates);
|
||||
info.crate_name.reserve(n_crates);
|
||||
info.used_crate_source.reserve(n_crates);
|
||||
info.missing_lang_items.reserve(n_crates);
|
||||
|
||||
for &cnum in crates.iter() {
|
||||
info.native_libraries
|
||||
.insert(cnum, tcx.native_libraries(cnum));
|
||||
info.crate_name
|
||||
.insert(cnum, tcx.crate_name(cnum).to_string());
|
||||
info.used_crate_source
|
||||
.insert(cnum, tcx.used_crate_source(cnum));
|
||||
if tcx.is_panic_runtime(cnum) {
|
||||
info.panic_runtime = Some(cnum);
|
||||
}
|
||||
if tcx.is_compiler_builtins(cnum) {
|
||||
info.compiler_builtins = Some(cnum);
|
||||
}
|
||||
if tcx.is_profiler_runtime(cnum) {
|
||||
info.profiler_runtime = Some(cnum);
|
||||
}
|
||||
if tcx.is_sanitizer_runtime(cnum) {
|
||||
info.sanitizer_runtime = Some(cnum);
|
||||
}
|
||||
if tcx.is_no_builtins(cnum) {
|
||||
info.is_no_builtins.insert(cnum);
|
||||
}
|
||||
if load_wasm_items {
|
||||
info.load_wasm_imports(tcx, cnum);
|
||||
}
|
||||
let missing = tcx.missing_lang_items(cnum);
|
||||
for &item in missing.iter() {
|
||||
if let Ok(id) = lang_items.require(item) {
|
||||
info.lang_item_to_crate.insert(item, id.krate);
|
||||
}
|
||||
}
|
||||
|
||||
// No need to look for lang items that are whitelisted and don't
|
||||
// actually need to exist.
|
||||
let missing = missing
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|&l| !weak_lang_items::whitelisted(tcx, l))
|
||||
.collect();
|
||||
info.missing_lang_items.insert(cnum, missing);
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
fn load_wasm_imports(&mut self, tcx: TyCtxt, cnum: CrateNum) {
|
||||
self.wasm_imports.extend(
|
||||
tcx.wasm_import_module_map(cnum)
|
||||
.iter()
|
||||
.map(|(&id, module)| {
|
||||
let instance = Instance::mono(tcx, id);
|
||||
let import_name = tcx.symbol_name(instance);
|
||||
|
||||
(import_name.to_string(), module.clone())
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CodegenResults {
|
||||
artifact: faerie::Artifact,
|
||||
modules: Vec<CompiledModule>,
|
||||
allocator_module: Option<CompiledModule>,
|
||||
metadata: Vec<u8>,
|
||||
crate_name: Symbol,
|
||||
crate_info: CrateInfo,
|
||||
linker_info: LinkerInfo,
|
||||
}
|
||||
|
||||
impl CodegenBackend for CraneliftCodegenBackend {
|
||||
@ -261,10 +395,39 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
|
||||
tcx.sess.abort_if_errors();
|
||||
|
||||
let artifact = faerie_module.finish().artifact;
|
||||
|
||||
let tmp_file = tcx
|
||||
.output_filenames(LOCAL_CRATE)
|
||||
.temp_path(OutputType::Object, None);
|
||||
let obj = artifact.emit().unwrap();
|
||||
std::fs::write(&tmp_file, obj).unwrap();
|
||||
|
||||
/*use rustc_mir::monomorphize::partitioning::CodegenUnitExt;
|
||||
|
||||
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
|
||||
let ((stats, module), _) = tcx.dep_graph.with_task(
|
||||
dep_node,
|
||||
tcx,
|
||||
cgu_name,
|
||||
module_codegen,
|
||||
);*/
|
||||
|
||||
return Box::new(CodegenResults {
|
||||
artifact: faerie_module.finish().artifact,
|
||||
artifact,
|
||||
metadata: metadata.raw_data,
|
||||
crate_name: tcx.crate_name(LOCAL_CRATE),
|
||||
crate_info: CrateInfo::new(tcx),
|
||||
linker_info: LinkerInfo::new(tcx),
|
||||
modules: vec![CompiledModule {
|
||||
name: "dummy".to_string(),
|
||||
kind: ModuleKind::Regular,
|
||||
object: Some(tmp_file),
|
||||
bytecode: None,
|
||||
bytecode_compressed: None,
|
||||
}],
|
||||
//modules: vec![],
|
||||
allocator_module: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -284,7 +447,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||
let output_name = out_filename(sess, crate_type, &outputs, &res.crate_name.as_str());
|
||||
match crate_type {
|
||||
CrateType::Rlib => link::link_rlib(sess, &res, output_name),
|
||||
CrateType::Executable => link::link_bin(sess, &res, output_name),
|
||||
CrateType::Executable => link::link_bin(sess, &res, &output_name),
|
||||
_ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)),
|
||||
}
|
||||
}
|
||||
|
1141
src/link.rs
1141
src/link.rs
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user