2018-08-26 09:28:00 -05:00
|
|
|
#![feature(rustc_private, macro_at_most_once_rep)]
|
2018-06-30 09:27:11 -05:00
|
|
|
#![allow(intra_doc_link_resolution_failure)]
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-09-08 11:00:06 -05:00
|
|
|
extern crate byteorder;
|
2018-06-17 11:05:11 -05:00
|
|
|
extern crate syntax;
|
2018-06-18 11:39:07 -05:00
|
|
|
#[macro_use]
|
2018-06-17 11:05:11 -05:00
|
|
|
extern crate rustc;
|
|
|
|
extern crate rustc_codegen_utils;
|
|
|
|
extern crate rustc_incremental;
|
2018-07-31 05:25:16 -05:00
|
|
|
extern crate rustc_mir;
|
|
|
|
extern crate rustc_target;
|
2018-07-24 07:10:53 -05:00
|
|
|
#[macro_use]
|
2018-06-17 11:05:11 -05:00
|
|
|
extern crate rustc_data_structures;
|
|
|
|
|
2018-07-24 07:10:53 -05:00
|
|
|
extern crate ar;
|
2018-08-09 03:46:56 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
2018-07-24 07:10:53 -05:00
|
|
|
extern crate faerie;
|
|
|
|
//extern crate goblin;
|
2018-07-14 04:59:42 -05:00
|
|
|
extern crate cranelift;
|
2018-07-31 05:25:16 -05:00
|
|
|
extern crate cranelift_faerie;
|
2018-07-14 04:59:42 -05:00
|
|
|
extern crate cranelift_module;
|
|
|
|
extern crate cranelift_simplejit;
|
2018-07-31 05:25:16 -05:00
|
|
|
extern crate target_lexicon;
|
2018-07-23 04:17:39 -05:00
|
|
|
|
|
|
|
use std::any::Any;
|
|
|
|
use std::fs::File;
|
2018-07-31 05:25:16 -05:00
|
|
|
use std::sync::{mpsc, Arc};
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-09-17 11:49:10 -05:00
|
|
|
use crate::rustc::dep_graph::DepGraph;
|
|
|
|
use crate::rustc::middle::cstore::MetadataLoader;
|
|
|
|
use crate::rustc::session::{config::OutputFilenames, CompileIncomplete};
|
|
|
|
use crate::rustc::ty::query::Providers;
|
|
|
|
use crate::rustc_codegen_utils::codegen_backend::CodegenBackend;
|
|
|
|
use crate::rustc_codegen_utils::link::out_filename;
|
|
|
|
use crate::rustc_data_structures::svh::Svh;
|
|
|
|
use crate::syntax::symbol::Symbol;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-07-23 04:17:39 -05:00
|
|
|
use cranelift::codegen::settings;
|
|
|
|
use cranelift_faerie::*;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-08-08 08:38:03 -05:00
|
|
|
struct NonFatal(pub String);
|
|
|
|
|
|
|
|
macro_rules! unimpl {
|
|
|
|
($($tt:tt)*) => {
|
2018-08-15 04:34:35 -05:00
|
|
|
panic!(crate::NonFatal(format!($($tt)*)));
|
2018-08-08 08:38:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:33:42 -05:00
|
|
|
mod abi;
|
2018-08-09 03:46:56 -05:00
|
|
|
mod analyze;
|
2018-06-17 11:05:11 -05:00
|
|
|
mod base;
|
2018-06-22 12:18:53 -05:00
|
|
|
mod common;
|
2018-07-31 05:25:16 -05:00
|
|
|
mod constant;
|
2018-10-03 11:21:52 -05:00
|
|
|
mod intrinsics;
|
2018-08-15 05:07:08 -05:00
|
|
|
mod metadata;
|
2018-08-15 07:45:32 -05:00
|
|
|
mod pretty_clif;
|
2018-09-08 11:00:06 -05:00
|
|
|
mod vtable;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
|
|
|
mod prelude {
|
2018-06-30 09:37:02 -05:00
|
|
|
pub use std::any::Any;
|
2018-07-16 08:13:37 -05:00
|
|
|
pub use std::collections::{HashMap, HashSet};
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-09-17 11:49:10 -05:00
|
|
|
pub use crate::rustc::hir::def_id::{DefId, LOCAL_CRATE};
|
|
|
|
pub use crate::rustc::mir;
|
|
|
|
pub use crate::rustc::mir::interpret::AllocId;
|
|
|
|
pub use crate::rustc::mir::*;
|
|
|
|
pub use crate::rustc::session::{config::CrateType, Session};
|
|
|
|
pub use crate::rustc::ty::layout::{self, Abi, LayoutOf, Scalar, Size, TyLayout};
|
|
|
|
pub use crate::rustc::ty::{
|
2018-06-23 11:54:15 -05:00
|
|
|
self, subst::Substs, FnSig, Instance, InstanceDef, ParamEnv, PolyFnSig, Ty, TyCtxt,
|
2018-08-24 07:51:02 -05:00
|
|
|
TypeAndMut, TypeFoldable,
|
2018-06-17 11:05:11 -05:00
|
|
|
};
|
2018-09-17 11:49:10 -05:00
|
|
|
pub use crate::rustc_data_structures::{
|
2018-08-17 06:01:56 -05:00
|
|
|
fx::{FxHashMap, FxHashSet},
|
|
|
|
indexed_vec::Idx,
|
|
|
|
sync::Lrc,
|
|
|
|
};
|
2018-09-17 11:49:10 -05:00
|
|
|
pub use crate::rustc_mir::monomorphize::{collector, MonoItem};
|
|
|
|
pub use crate::syntax::ast::{FloatTy, IntTy, UintTy};
|
|
|
|
pub use crate::syntax::source_map::DUMMY_SP;
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-07-14 04:59:42 -05:00
|
|
|
pub use cranelift::codegen::ir::{
|
2018-07-31 05:25:16 -05:00
|
|
|
condcodes::IntCC, function::Function, ExternalName, FuncRef, Inst, StackSlot,
|
2018-06-22 12:18:53 -05:00
|
|
|
};
|
2018-07-14 04:59:42 -05:00
|
|
|
pub use cranelift::codegen::Context;
|
|
|
|
pub use cranelift::prelude::*;
|
2018-09-08 10:24:52 -05:00
|
|
|
pub use cranelift_module::{Backend, DataContext, DataId, FuncId, Linkage, Module};
|
2018-07-31 05:25:16 -05:00
|
|
|
pub use cranelift_simplejit::{SimpleJITBackend, SimpleJITBuilder};
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-07-30 09:57:40 -05:00
|
|
|
pub use crate::abi::*;
|
2018-07-31 05:25:16 -05:00
|
|
|
pub use crate::base::{trans_operand, trans_place};
|
2018-07-30 09:57:40 -05:00
|
|
|
pub use crate::common::*;
|
2018-08-26 09:58:52 -05:00
|
|
|
pub use crate::Caches;
|
2018-08-14 05:13:07 -05:00
|
|
|
|
2018-08-14 15:01:18 -05:00
|
|
|
pub fn should_codegen(sess: &Session) -> bool {
|
2018-08-15 07:49:36 -05:00
|
|
|
//return true;
|
2018-08-14 05:13:07 -05:00
|
|
|
::std::env::var("SHOULD_CODEGEN").is_ok()
|
2018-08-14 15:01:18 -05:00
|
|
|
|| sess.crate_types.get().contains(&CrateType::Executable)
|
2018-08-14 05:13:07 -05:00
|
|
|
}
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
|
2018-08-14 13:58:24 -05:00
|
|
|
use crate::constant::ConstantCx;
|
2018-07-30 09:57:40 -05:00
|
|
|
use crate::prelude::*;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-09-08 11:00:06 -05:00
|
|
|
pub struct Caches<'tcx> {
|
2018-08-14 11:52:43 -05:00
|
|
|
pub context: Context,
|
2018-09-08 11:00:06 -05:00
|
|
|
pub vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
|
2018-06-30 09:37:02 -05:00
|
|
|
}
|
|
|
|
|
2018-09-08 11:00:06 -05:00
|
|
|
impl<'tcx> Caches<'tcx> {
|
2018-08-26 09:58:52 -05:00
|
|
|
fn new() -> Self {
|
|
|
|
Caches {
|
|
|
|
context: Context::new(),
|
2018-09-08 11:00:06 -05:00
|
|
|
vtables: HashMap::new(),
|
2018-08-26 09:58:52 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-14 13:58:24 -05:00
|
|
|
}
|
|
|
|
|
2018-07-23 04:17:39 -05:00
|
|
|
struct CraneliftCodegenBackend;
|
|
|
|
|
|
|
|
struct OngoingCodegen {
|
2018-07-24 07:10:53 -05:00
|
|
|
product: cranelift_faerie::FaerieProduct,
|
|
|
|
metadata: Vec<u8>,
|
2018-07-23 04:17:39 -05:00
|
|
|
crate_name: Symbol,
|
2018-08-14 15:01:18 -05:00
|
|
|
crate_hash: Svh,
|
2018-07-23 04:17:39 -05:00
|
|
|
}
|
|
|
|
|
2018-07-14 04:59:42 -05:00
|
|
|
impl CodegenBackend for CraneliftCodegenBackend {
|
2018-06-17 11:05:11 -05:00
|
|
|
fn init(&self, sess: &Session) {
|
|
|
|
for cty in sess.opts.crate_types.iter() {
|
|
|
|
match *cty {
|
2018-08-07 09:43:09 -05:00
|
|
|
CrateType::Rlib | CrateType::Dylib | CrateType::Executable => {}
|
2018-06-17 11:05:11 -05:00
|
|
|
_ => {
|
2018-08-14 15:01:18 -05:00
|
|
|
sess.err(&format!(
|
|
|
|
"Rustc codegen cranelift doesn't support output type {}",
|
2018-07-31 05:25:16 -05:00
|
|
|
cty
|
|
|
|
));
|
|
|
|
}
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn metadata_loader(&self) -> Box<MetadataLoader + Sync> {
|
2018-08-15 05:07:08 -05:00
|
|
|
Box::new(crate::metadata::CraneliftMetadataLoader)
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn provide(&self, providers: &mut Providers) {
|
|
|
|
rustc_codegen_utils::symbol_names::provide(providers);
|
|
|
|
|
2018-07-31 05:25:16 -05:00
|
|
|
providers.target_features_whitelist = |_tcx, _cnum| Lrc::new(Default::default());
|
2018-06-17 11:05:11 -05:00
|
|
|
providers.is_reachable_non_generic = |_tcx, _defid| true;
|
|
|
|
providers.exported_symbols = |_tcx, _crate| Arc::new(Vec::new());
|
2018-08-12 13:49:43 -05:00
|
|
|
providers.upstream_monomorphizations = |_tcx, _cnum| Lrc::new(FxHashMap());
|
|
|
|
providers.upstream_monomorphizations_for = |tcx, def_id| {
|
|
|
|
debug_assert!(!def_id.is_local());
|
|
|
|
tcx.upstream_monomorphizations(LOCAL_CRATE)
|
|
|
|
.get(&def_id)
|
|
|
|
.cloned()
|
|
|
|
};
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
fn provide_extern(&self, providers: &mut Providers) {
|
|
|
|
providers.is_reachable_non_generic = |_tcx, _defid| true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn codegen_crate<'a, 'tcx>(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-07-31 05:25:16 -05:00
|
|
|
_rx: mpsc::Receiver<Box<Any + Send>>,
|
2018-06-17 11:05:11 -05:00
|
|
|
) -> Box<Any> {
|
2018-08-14 15:01:18 -05:00
|
|
|
if !tcx.sess.crate_types.get().contains(&CrateType::Executable)
|
|
|
|
&& std::env::var("SHOULD_RUN").is_ok()
|
|
|
|
{
|
|
|
|
tcx.sess
|
|
|
|
.err("Can't JIT run non executable (SHOULD_RUN env var is set)");
|
|
|
|
}
|
|
|
|
|
2018-06-17 11:05:11 -05:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
2018-08-24 07:51:02 -05:00
|
|
|
let metadata = tcx.encode_metadata();
|
2018-06-30 09:37:02 -05:00
|
|
|
|
2018-09-26 08:42:07 -05:00
|
|
|
fn build_isa(tcx: TyCtxt) -> Box<isa::TargetIsa> {
|
|
|
|
let mut flags_builder = settings::builder();
|
|
|
|
flags_builder.enable("is_pic").unwrap();
|
|
|
|
let flags = settings::Flags::new(flags_builder);
|
2018-08-22 05:31:45 -05:00
|
|
|
cranelift::codegen::isa::lookup(tcx.sess.target.target.llvm_target.parse().unwrap())
|
|
|
|
.unwrap()
|
2018-09-26 08:42:07 -05:00
|
|
|
.finish(flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
let isa = build_isa(tcx);
|
2018-06-30 09:37:02 -05:00
|
|
|
|
2018-08-17 05:57:41 -05:00
|
|
|
let mono_items =
|
2018-10-10 12:04:20 -05:00
|
|
|
collector::collect_crate_mono_items(tcx, collector::MonoItemCollectionMode::Lazy).0;
|
2018-08-17 06:01:56 -05:00
|
|
|
|
2018-08-17 05:57:41 -05:00
|
|
|
// TODO: move to the end of this function when compiling libcore doesn't have unimplemented stuff anymore
|
|
|
|
save_incremental(tcx);
|
|
|
|
tcx.sess.warn("Saved incremental data");
|
2018-08-11 06:59:34 -05:00
|
|
|
|
2018-08-17 05:57:41 -05:00
|
|
|
if std::env::var("SHOULD_RUN").is_ok() {
|
|
|
|
let mut jit_module: Module<SimpleJITBackend> = Module::new(SimpleJITBuilder::new());
|
2018-08-18 10:10:02 -05:00
|
|
|
assert_eq!(pointer_ty(tcx), jit_module.pointer_type());
|
2018-06-30 09:37:02 -05:00
|
|
|
|
2018-09-26 08:42:07 -05:00
|
|
|
codegen_mono_items(tcx, &*isa, &mut jit_module, &mono_items);
|
2018-08-14 13:58:24 -05:00
|
|
|
|
2018-08-17 05:57:41 -05:00
|
|
|
tcx.sess.abort_if_errors();
|
2018-09-09 07:45:23 -05:00
|
|
|
println!("Compiled everything");
|
|
|
|
println!("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set");
|
2018-08-11 05:04:54 -05:00
|
|
|
|
2018-08-17 06:21:03 -05:00
|
|
|
let sig = Signature {
|
|
|
|
params: vec![
|
2018-08-18 10:10:02 -05:00
|
|
|
AbiParam::new(jit_module.pointer_type()),
|
|
|
|
AbiParam::new(jit_module.pointer_type()),
|
2018-08-17 06:21:03 -05:00
|
|
|
],
|
2018-08-18 10:10:02 -05:00
|
|
|
returns: vec![AbiParam::new(jit_module.pointer_type() /*isize*/)],
|
2018-08-17 06:21:03 -05:00
|
|
|
call_conv: CallConv::SystemV,
|
|
|
|
};
|
|
|
|
let main_func_id = jit_module
|
|
|
|
.declare_function("main", Linkage::Import, &sig)
|
2018-08-11 05:04:54 -05:00
|
|
|
.unwrap();
|
|
|
|
|
2018-09-22 06:55:23 -05:00
|
|
|
jit_module.finalize_definitions();
|
2018-09-09 07:45:23 -05:00
|
|
|
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
|
|
|
|
println!("🎉 Finalized everything");
|
2018-08-11 05:04:54 -05:00
|
|
|
|
2018-08-17 06:21:03 -05:00
|
|
|
let f: extern "C" fn(isize, *const *const u8) -> isize =
|
|
|
|
unsafe { ::std::mem::transmute(finalized_main) };
|
|
|
|
let res = f(0, 0 as *const _);
|
2018-09-09 07:45:23 -05:00
|
|
|
tcx.sess.warn(&format!("🚀 main returned {}", res));
|
2018-06-30 09:37:02 -05:00
|
|
|
|
2018-08-14 11:52:43 -05:00
|
|
|
jit_module.finish();
|
2018-08-14 15:01:18 -05:00
|
|
|
::std::process::exit(0);
|
2018-08-17 05:57:41 -05:00
|
|
|
} else {
|
|
|
|
let mut faerie_module: Module<FaerieBackend> = Module::new(
|
2018-08-17 06:01:56 -05:00
|
|
|
FaerieBuilder::new(
|
|
|
|
isa,
|
|
|
|
"some_file.o".to_string(),
|
|
|
|
FaerieTrapCollection::Disabled,
|
|
|
|
FaerieBuilder::default_libcall_names(),
|
2018-10-10 12:07:13 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2018-08-17 06:01:56 -05:00
|
|
|
);
|
2018-08-18 10:10:02 -05:00
|
|
|
assert_eq!(pointer_ty(tcx), faerie_module.pointer_type());
|
2018-08-17 06:01:56 -05:00
|
|
|
|
2018-09-26 08:42:07 -05:00
|
|
|
codegen_mono_items(tcx, &*build_isa(tcx), &mut faerie_module, &mono_items);
|
2018-08-14 05:13:07 -05:00
|
|
|
|
2018-08-17 05:57:41 -05:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
|
|
|
if should_codegen(tcx.sess) {
|
2018-09-22 06:55:23 -05:00
|
|
|
faerie_module.finalize_definitions();
|
2018-08-17 05:57:41 -05:00
|
|
|
}
|
2018-06-30 09:37:02 -05:00
|
|
|
|
2018-08-17 05:57:41 -05:00
|
|
|
return Box::new(OngoingCodegen {
|
|
|
|
product: faerie_module.finish(),
|
|
|
|
metadata: metadata.raw_data,
|
|
|
|
crate_name: tcx.crate_name(LOCAL_CRATE),
|
|
|
|
crate_hash: tcx.crate_hash(LOCAL_CRATE),
|
|
|
|
});
|
|
|
|
}
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn join_codegen_and_link(
|
|
|
|
&self,
|
|
|
|
ongoing_codegen: Box<Any>,
|
|
|
|
sess: &Session,
|
|
|
|
_dep_graph: &DepGraph,
|
|
|
|
outputs: &OutputFilenames,
|
|
|
|
) -> Result<(), CompileIncomplete> {
|
2018-07-31 05:25:16 -05:00
|
|
|
let ongoing_codegen = *ongoing_codegen
|
|
|
|
.downcast::<OngoingCodegen>()
|
2018-07-23 04:17:39 -05:00
|
|
|
.expect("Expected CraneliftCodegenBackend's OngoingCodegen, found Box<Any>");
|
2018-07-24 07:10:53 -05:00
|
|
|
|
|
|
|
let mut artifact = ongoing_codegen.product.artifact;
|
|
|
|
let metadata = ongoing_codegen.metadata;
|
|
|
|
|
2018-08-15 07:45:32 -05:00
|
|
|
let metadata_name =
|
|
|
|
".rustc.clif_metadata".to_string() + &ongoing_codegen.crate_hash.to_string();
|
2018-07-31 05:25:16 -05:00
|
|
|
artifact
|
|
|
|
.declare_with(
|
2018-08-14 15:01:18 -05:00
|
|
|
&metadata_name,
|
2018-07-31 05:25:16 -05:00
|
|
|
faerie::artifact::Decl::Data {
|
|
|
|
global: true,
|
2018-09-05 12:43:42 -05:00
|
|
|
writable: false,
|
2018-07-31 05:25:16 -05:00
|
|
|
},
|
|
|
|
metadata.clone(),
|
2018-10-10 12:07:13 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-07-24 07:10:53 -05:00
|
|
|
|
2018-06-17 11:05:11 -05:00
|
|
|
for &crate_type in sess.opts.crate_types.iter() {
|
2018-08-11 08:05:57 -05:00
|
|
|
match crate_type {
|
2018-08-14 15:01:18 -05:00
|
|
|
// TODO: link executable
|
|
|
|
CrateType::Executable | CrateType::Rlib => {
|
2018-08-11 08:05:57 -05:00
|
|
|
let output_name = out_filename(
|
|
|
|
sess,
|
|
|
|
crate_type,
|
|
|
|
&outputs,
|
|
|
|
&ongoing_codegen.crate_name.as_str(),
|
|
|
|
);
|
|
|
|
let file = File::create(&output_name).unwrap();
|
|
|
|
let mut builder = ar::Builder::new(file);
|
|
|
|
builder
|
|
|
|
.append(
|
2018-08-15 07:45:32 -05:00
|
|
|
&ar::Header::new(
|
|
|
|
metadata_name.as_bytes().to_vec(),
|
|
|
|
metadata.len() as u64,
|
|
|
|
),
|
2018-08-11 08:05:57 -05:00
|
|
|
::std::io::Cursor::new(metadata.clone()),
|
2018-10-10 12:07:13 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-14 15:01:18 -05:00
|
|
|
if should_codegen(sess) {
|
|
|
|
let obj = artifact.emit().unwrap();
|
|
|
|
builder
|
|
|
|
.append(
|
|
|
|
&ar::Header::new(b"data.o".to_vec(), obj.len() as u64),
|
|
|
|
::std::io::Cursor::new(obj),
|
2018-10-10 12:07:13 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-14 15:01:18 -05:00
|
|
|
}
|
2018-08-11 08:05:57 -05:00
|
|
|
}
|
|
|
|
_ => sess.fatal(&format!("Unsupported crate type: {:?}", crate_type)),
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 06:01:56 -05:00
|
|
|
fn codegen_mono_items<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-09-26 08:42:07 -05:00
|
|
|
isa: &isa::TargetIsa,
|
2018-08-17 06:01:56 -05:00
|
|
|
module: &mut Module<impl Backend + 'static>,
|
|
|
|
mono_items: &FxHashSet<MonoItem<'tcx>>,
|
|
|
|
) {
|
2018-08-17 05:57:41 -05:00
|
|
|
use std::io::Write;
|
|
|
|
|
2018-08-26 09:58:52 -05:00
|
|
|
let mut caches = Caches::new();
|
|
|
|
let mut ccx = ConstantCx::default();
|
2018-08-17 05:57:41 -05:00
|
|
|
|
2018-10-06 07:48:34 -05:00
|
|
|
let mut log = if cfg!(debug_assertions) {
|
2018-10-10 12:07:13 -05:00
|
|
|
Some(
|
|
|
|
::std::fs::File::create(concat!(env!("CARGO_MANIFEST_DIR"), "/target/out/log.txt"))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
2018-10-06 07:48:34 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-08-17 05:57:41 -05:00
|
|
|
|
|
|
|
let before = ::std::time::Instant::now();
|
2018-08-31 12:50:26 -05:00
|
|
|
println!("[codegen mono items] start");
|
2018-08-17 05:57:41 -05:00
|
|
|
|
|
|
|
for mono_item in mono_items {
|
2018-08-26 09:58:52 -05:00
|
|
|
let res = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
|
2018-09-26 08:42:07 -05:00
|
|
|
base::trans_mono_item(tcx, isa, module, &mut caches, &mut ccx, *mono_item);
|
2018-08-17 05:57:41 -05:00
|
|
|
}));
|
2018-08-26 09:58:52 -05:00
|
|
|
|
2018-08-17 05:57:41 -05:00
|
|
|
if let Err(err) = res {
|
|
|
|
match err.downcast::<NonFatal>() {
|
|
|
|
Ok(non_fatal) => {
|
2018-10-06 07:48:34 -05:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
writeln!(log.as_mut().unwrap(), "{}", &non_fatal.0);
|
|
|
|
}
|
2018-08-17 05:57:41 -05:00
|
|
|
tcx.sess.err(&non_fatal.0)
|
|
|
|
}
|
|
|
|
Err(err) => ::std::panic::resume_unwind(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 09:58:52 -05:00
|
|
|
maybe_create_entry_wrapper(tcx, module);
|
2018-08-17 06:21:03 -05:00
|
|
|
|
2018-08-26 09:58:52 -05:00
|
|
|
ccx.finalize(tcx, module);
|
2018-08-17 05:57:41 -05:00
|
|
|
|
|
|
|
let after = ::std::time::Instant::now();
|
2018-08-31 12:50:26 -05:00
|
|
|
println!("[codegen mono items] end time: {:?}", after - before);
|
2018-08-17 05:57:41 -05:00
|
|
|
}
|
|
|
|
|
2018-08-12 13:49:43 -05:00
|
|
|
fn save_incremental<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
|
|
|
rustc_incremental::assert_dep_graph(tcx);
|
|
|
|
rustc_incremental::save_dep_graph(tcx);
|
|
|
|
rustc_incremental::finalize_session_directory(tcx.sess, tcx.crate_hash(LOCAL_CRATE));
|
|
|
|
}
|
|
|
|
|
2018-07-14 04:59:42 -05:00
|
|
|
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
2018-06-17 11:05:11 -05:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn __rustc_codegen_backend() -> Box<CodegenBackend> {
|
2018-07-23 04:17:39 -05:00
|
|
|
Box::new(CraneliftCodegenBackend)
|
2018-06-18 11:39:07 -05:00
|
|
|
}
|
2018-08-17 06:21:03 -05:00
|
|
|
|
|
|
|
/// Create the `main` function which will initialize the rust runtime and call
|
|
|
|
/// users main function.
|
2018-08-26 09:58:52 -05:00
|
|
|
fn maybe_create_entry_wrapper<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
module: &mut Module<impl Backend + 'static>,
|
|
|
|
) {
|
2018-09-17 11:49:10 -05:00
|
|
|
use crate::rustc::middle::lang_items::StartFnLangItem;
|
|
|
|
use crate::rustc::session::config::EntryFnType;
|
2018-08-17 06:21:03 -05:00
|
|
|
|
|
|
|
let (main_def_id, use_start_lang_item) = match *tcx.sess.entry_fn.borrow() {
|
|
|
|
Some((id, _, entry_ty)) => (
|
|
|
|
tcx.hir.local_def_id(id),
|
|
|
|
match entry_ty {
|
|
|
|
EntryFnType::Main => true,
|
|
|
|
EntryFnType::Start => false,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2018-08-26 09:58:52 -05:00
|
|
|
create_entry_fn(tcx, module, main_def_id, use_start_lang_item);;
|
2018-08-17 06:21:03 -05:00
|
|
|
|
|
|
|
fn create_entry_fn<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
m: &mut Module<impl Backend + 'static>,
|
|
|
|
rust_main_def_id: DefId,
|
|
|
|
use_start_lang_item: bool,
|
|
|
|
) {
|
|
|
|
let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
|
|
|
|
// Given that `main()` has no arguments,
|
|
|
|
// then its return type cannot have
|
|
|
|
// late-bound regions, since late-bound
|
|
|
|
// regions must appear in the argument
|
|
|
|
// listing.
|
|
|
|
let main_ret_ty = tcx.erase_regions(&main_ret_ty.no_late_bound_regions().unwrap());
|
|
|
|
|
|
|
|
let cmain_sig = Signature {
|
|
|
|
params: vec![
|
2018-08-18 10:10:02 -05:00
|
|
|
AbiParam::new(m.pointer_type()),
|
|
|
|
AbiParam::new(m.pointer_type()),
|
2018-08-17 06:21:03 -05:00
|
|
|
],
|
2018-08-18 10:10:02 -05:00
|
|
|
returns: vec![AbiParam::new(m.pointer_type() /*isize*/)],
|
2018-08-17 06:21:03 -05:00
|
|
|
call_conv: CallConv::SystemV,
|
|
|
|
};
|
|
|
|
|
|
|
|
let cmain_func_id = m
|
|
|
|
.declare_function("main", Linkage::Export, &cmain_sig)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let instance = Instance::mono(tcx, rust_main_def_id);
|
|
|
|
|
|
|
|
let (main_name, main_sig) = get_function_name_and_sig(tcx, instance);
|
|
|
|
|
|
|
|
let main_func_id = m
|
|
|
|
.declare_function(&main_name, Linkage::Import, &main_sig)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig.clone());
|
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
|
|
let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
|
|
|
|
|
|
|
let ebb = bcx.create_ebb();
|
|
|
|
bcx.switch_to_block(ebb);
|
2018-08-18 10:10:02 -05:00
|
|
|
let arg_argc = bcx.append_ebb_param(ebb, m.pointer_type());
|
|
|
|
let arg_argv = bcx.append_ebb_param(ebb, m.pointer_type());
|
2018-08-17 06:21:03 -05:00
|
|
|
|
|
|
|
let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
|
|
|
|
|
|
|
|
let call_inst = if use_start_lang_item {
|
|
|
|
let start_def_id = tcx.require_lang_item(StartFnLangItem);
|
|
|
|
let start_instance = Instance::resolve(
|
|
|
|
tcx,
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
start_def_id,
|
|
|
|
tcx.intern_substs(&[main_ret_ty.into()]),
|
2018-10-10 12:07:13 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-17 06:21:03 -05:00
|
|
|
|
|
|
|
let (start_name, start_sig) = get_function_name_and_sig(tcx, start_instance);
|
|
|
|
let start_func_id = m
|
|
|
|
.declare_function(&start_name, Linkage::Import, &start_sig)
|
|
|
|
.unwrap();
|
|
|
|
|
2018-08-18 10:10:02 -05:00
|
|
|
let main_val = bcx.ins().func_addr(m.pointer_type(), main_func_ref);
|
2018-08-17 06:21:03 -05:00
|
|
|
|
|
|
|
let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
|
|
|
|
bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
|
|
|
|
} else {
|
|
|
|
// using user-defined start fn
|
|
|
|
bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = bcx.inst_results(call_inst)[0];
|
|
|
|
bcx.ins().return_(&[result]);
|
|
|
|
bcx.seal_all_blocks();
|
|
|
|
bcx.finalize();
|
|
|
|
}
|
|
|
|
m.define_function(cmain_func_id, &mut ctx).unwrap();
|
|
|
|
}
|
|
|
|
}
|