2019-12-16 10:33:57 +00:00
|
|
|
#![feature(rustc_private, decl_macro, type_alias_impl_trait, associated_type_bounds, never_type)]
|
2018-06-30 14:27:11 +00:00
|
|
|
#![allow(intra_doc_link_resolution_failure)]
|
2018-06-17 16:05:11 +00:00
|
|
|
|
2019-03-27 18:24:42 +00:00
|
|
|
extern crate flate2;
|
2020-01-18 11:43:31 +00:00
|
|
|
extern crate libc;
|
2019-09-21 12:27:38 +00:00
|
|
|
extern crate tempfile;
|
2018-06-17 16:05:11 +00:00
|
|
|
extern crate rustc;
|
2018-11-24 10:23:49 +00:00
|
|
|
extern crate rustc_codegen_ssa;
|
2018-11-24 11:47:53 +00:00
|
|
|
extern crate rustc_data_structures;
|
2019-07-08 07:54:18 +00:00
|
|
|
extern crate rustc_driver;
|
2018-11-24 11:47:53 +00:00
|
|
|
extern crate rustc_fs_util;
|
2020-01-09 16:43:10 +00:00
|
|
|
extern crate rustc_hir;
|
2018-06-17 16:05:11 +00:00
|
|
|
extern crate rustc_incremental;
|
2019-10-03 15:22:01 +00:00
|
|
|
extern crate rustc_index;
|
2018-07-31 10:25:16 +00:00
|
|
|
extern crate rustc_mir;
|
2019-12-31 15:43:24 +00:00
|
|
|
extern crate rustc_session;
|
2020-01-06 19:11:03 +00:00
|
|
|
extern crate rustc_span;
|
2020-03-24 12:09:44 +00:00
|
|
|
extern crate rustc_symbol_mangling;
|
2018-07-31 10:25:16 +00:00
|
|
|
extern crate rustc_target;
|
2020-03-04 14:04:28 +00:00
|
|
|
extern crate rustc_ast;
|
2018-06-17 16:05:11 +00:00
|
|
|
|
2018-07-23 09:17:39 +00:00
|
|
|
use std::any::Any;
|
2018-06-17 16:05:11 +00:00
|
|
|
|
2020-03-11 14:18:01 +00:00
|
|
|
use rustc::dep_graph::{DepGraph, WorkProduct, WorkProductId};
|
2019-05-04 12:57:41 +00:00
|
|
|
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader};
|
2020-03-24 12:09:44 +00:00
|
|
|
use rustc_session::config::OutputFilenames;
|
2018-11-07 12:29:38 +00:00
|
|
|
use rustc::ty::query::Providers;
|
2019-03-11 19:02:47 +00:00
|
|
|
use rustc::util::common::ErrorReported;
|
2020-03-24 12:09:44 +00:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
2018-06-17 16:05:11 +00:00
|
|
|
|
2019-12-24 11:40:18 +00:00
|
|
|
use cranelift_codegen::settings;
|
2018-06-17 16:05:11 +00:00
|
|
|
|
2018-11-10 14:12:00 +00:00
|
|
|
use crate::constant::ConstantCx;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2018-07-19 17:33:42 +00:00
|
|
|
mod abi;
|
2018-11-05 17:29:15 +00:00
|
|
|
mod allocator;
|
2018-08-09 08:46:56 +00:00
|
|
|
mod analyze;
|
2018-11-09 17:38:30 +00:00
|
|
|
mod archive;
|
2020-01-18 11:43:31 +00:00
|
|
|
mod atomic_shim;
|
2018-06-17 16:05:11 +00:00
|
|
|
mod base;
|
2019-10-16 18:48:09 +00:00
|
|
|
mod backend;
|
2019-07-31 07:45:11 +00:00
|
|
|
mod cast;
|
2019-07-07 16:08:38 +00:00
|
|
|
mod codegen_i128;
|
2018-06-22 17:18:53 +00:00
|
|
|
mod common;
|
2018-07-31 10:25:16 +00:00
|
|
|
mod constant;
|
2019-01-17 17:07:27 +00:00
|
|
|
mod debuginfo;
|
2019-08-14 10:01:41 +00:00
|
|
|
mod discriminant;
|
2019-05-04 14:54:25 +00:00
|
|
|
mod driver;
|
2018-10-03 16:21:52 +00:00
|
|
|
mod intrinsics;
|
2019-03-11 19:36:29 +00:00
|
|
|
mod linkage;
|
2018-10-20 16:41:26 +00:00
|
|
|
mod main_shim;
|
2018-08-15 10:07:08 +00:00
|
|
|
mod metadata;
|
2019-08-14 09:52:39 +00:00
|
|
|
mod num;
|
2019-12-26 12:37:10 +00:00
|
|
|
mod optimize;
|
2019-12-20 15:02:47 +00:00
|
|
|
mod pointer;
|
2018-08-15 12:45:32 +00:00
|
|
|
mod pretty_clif;
|
2019-07-20 13:33:57 +00:00
|
|
|
mod target_features_whitelist;
|
2018-11-16 16:35:47 +00:00
|
|
|
mod trap;
|
2018-12-29 14:33:34 +00:00
|
|
|
mod unsize;
|
2019-06-11 13:43:22 +00:00
|
|
|
mod value_and_place;
|
2018-09-08 16:00:06 +00:00
|
|
|
mod vtable;
|
2018-06-17 16:05:11 +00:00
|
|
|
|
|
|
|
mod prelude {
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) use std::collections::HashMap;
|
|
|
|
pub(crate) use std::convert::{TryFrom, TryInto};
|
|
|
|
|
|
|
|
pub(crate) use rustc_ast::ast::{FloatTy, IntTy, UintTy};
|
|
|
|
pub(crate) use rustc_span::Span;
|
|
|
|
|
|
|
|
pub(crate) use rustc::bug;
|
|
|
|
pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
|
|
|
pub(crate) use rustc::mir::{self, *};
|
|
|
|
pub(crate) use rustc_session::Session;
|
2020-03-30 17:00:24 +00:00
|
|
|
pub(crate) use rustc::ty::layout::{self, Abi, LayoutOf, Scalar, Size, TyAndLayout, VariantIdx};
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) use rustc::ty::{
|
|
|
|
self, FnSig, Instance, InstanceDef, ParamEnv, Ty, TyCtxt, TypeAndMut, TypeFoldable,
|
2018-06-17 16:05:11 +00:00
|
|
|
};
|
2019-10-03 15:22:01 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) use rustc_data_structures::fx::FxHashMap;
|
|
|
|
|
|
|
|
pub(crate) use rustc_index::vec::Idx;
|
|
|
|
|
|
|
|
pub(crate) use rustc_codegen_ssa::traits::*;
|
|
|
|
pub(crate) use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleKind};
|
2019-10-03 15:22:01 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) use cranelift_codegen::Context;
|
|
|
|
pub(crate) use cranelift_codegen::entity::EntitySet;
|
|
|
|
pub(crate) use cranelift_codegen::ir::{AbiParam, Block, ExternalName, FuncRef, Inst, InstBuilder, MemFlags, Signature, SourceLoc, StackSlot, StackSlotData, StackSlotKind, TrapCode, Type, Value};
|
|
|
|
pub(crate) use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
|
|
|
|
pub(crate) use cranelift_codegen::ir::function::Function;
|
|
|
|
pub(crate) use cranelift_codegen::ir::types;
|
|
|
|
pub(crate) use cranelift_codegen::isa::{self, CallConv};
|
|
|
|
pub(crate) use cranelift_codegen::settings::{self, Configurable};
|
|
|
|
pub(crate) use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
|
|
|
|
pub(crate) use cranelift_module::{
|
|
|
|
self, Backend, DataContext, DataId, FuncId, Linkage, Module,
|
2019-01-17 17:07:27 +00:00
|
|
|
};
|
2018-06-22 17:18:53 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) use crate::abi::*;
|
|
|
|
pub(crate) use crate::base::{trans_operand, trans_place};
|
|
|
|
pub(crate) use crate::cast::*;
|
|
|
|
pub(crate) use crate::common::*;
|
|
|
|
pub(crate) use crate::debuginfo::{DebugContext, FunctionDebugContext};
|
|
|
|
pub(crate) use crate::pointer::Pointer;
|
|
|
|
pub(crate) use crate::trap::*;
|
|
|
|
pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue};
|
|
|
|
pub(crate) use crate::CodegenCx;
|
|
|
|
|
|
|
|
pub(crate) struct PrintOnPanic<F: Fn() -> String>(pub F);
|
2019-05-04 14:54:25 +00:00
|
|
|
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if ::std::thread::panicking() {
|
|
|
|
println!("{}", (self.0)());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 15:26:56 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) macro unimpl_fatal($tcx:expr, $span:expr, $($tt:tt)*) {
|
2020-03-17 15:26:56 +00:00
|
|
|
$tcx.sess.span_fatal($span, &format!($($tt)*));
|
|
|
|
}
|
2018-06-17 16:05:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) struct CodegenCx<'clif, 'tcx, B: Backend + 'static> {
|
2019-06-16 09:13:49 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-12-18 17:28:02 +00:00
|
|
|
module: &'clif mut Module<B>,
|
2019-08-18 14:52:07 +00:00
|
|
|
constants_cx: ConstantCx,
|
2020-01-04 16:58:38 +00:00
|
|
|
cached_context: Context,
|
|
|
|
vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
|
2019-01-17 17:07:27 +00:00
|
|
|
debug_context: Option<&'clif mut DebugContext<'tcx>>,
|
2018-12-18 17:28:02 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 18:44:40 +00:00
|
|
|
impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> {
|
2019-01-17 17:07:27 +00:00
|
|
|
fn new(
|
2019-06-16 09:13:49 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-01-17 17:07:27 +00:00
|
|
|
module: &'clif mut Module<B>,
|
|
|
|
debug_context: Option<&'clif mut DebugContext<'tcx>>,
|
|
|
|
) -> Self {
|
2018-12-18 17:28:02 +00:00
|
|
|
CodegenCx {
|
|
|
|
tcx,
|
|
|
|
module,
|
2019-08-18 14:52:07 +00:00
|
|
|
constants_cx: ConstantCx::default(),
|
2020-01-04 16:58:38 +00:00
|
|
|
cached_context: Context::new(),
|
|
|
|
vtables: HashMap::new(),
|
2019-01-17 17:07:27 +00:00
|
|
|
debug_context,
|
2018-12-18 17:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finalize(self) {
|
2019-08-18 14:52:07 +00:00
|
|
|
self.constants_cx.finalize(self.tcx, self.module);
|
2018-12-18 17:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 09:17:39 +00:00
|
|
|
struct CraneliftCodegenBackend;
|
|
|
|
|
2018-07-14 09:59:42 +00:00
|
|
|
impl CodegenBackend for CraneliftCodegenBackend {
|
2020-01-17 19:33:27 +00:00
|
|
|
fn init(&self, sess: &Session) {
|
|
|
|
if sess.lto() != rustc_session::config::Lto::No {
|
|
|
|
sess.warn("LTO is not supported. You may get a linker error.");
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 16:05:11 +00:00
|
|
|
|
2018-11-17 17:23:52 +00:00
|
|
|
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
|
2018-08-15 10:07:08 +00:00
|
|
|
Box::new(crate::metadata::CraneliftMetadataLoader)
|
2018-06-17 16:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn provide(&self, providers: &mut Providers) {
|
2019-07-20 13:33:57 +00:00
|
|
|
providers.target_features_whitelist = |tcx, cnum| {
|
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
if tcx.sess.opts.actually_rustdoc {
|
|
|
|
// rustdoc needs to be able to document functions that use all the features, so
|
|
|
|
// whitelist them all
|
2019-08-31 17:28:09 +00:00
|
|
|
tcx.arena.alloc(
|
|
|
|
target_features_whitelist::all_known_features()
|
|
|
|
.map(|(a, b)| (a.to_string(), b))
|
|
|
|
.collect(),
|
|
|
|
)
|
2019-07-20 13:33:57 +00:00
|
|
|
} else {
|
2019-08-31 17:28:09 +00:00
|
|
|
tcx.arena.alloc(
|
|
|
|
target_features_whitelist::target_feature_whitelist(tcx.sess)
|
|
|
|
.iter()
|
|
|
|
.map(|&(a, b)| (a.to_string(), b))
|
|
|
|
.collect(),
|
|
|
|
)
|
2019-07-20 13:33:57 +00:00
|
|
|
}
|
|
|
|
};
|
2018-06-17 16:05:11 +00:00
|
|
|
}
|
2019-10-16 16:39:12 +00:00
|
|
|
fn provide_extern(&self, _providers: &mut Providers) {}
|
2018-06-17 16:05:11 +00:00
|
|
|
|
2019-06-16 09:13:49 +00:00
|
|
|
fn codegen_crate<'tcx>(
|
2018-06-17 16:05:11 +00:00
|
|
|
&self,
|
2019-06-16 09:13:49 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-04 12:57:41 +00:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-04 14:54:25 +00:00
|
|
|
need_metadata_module: bool,
|
2018-11-17 17:23:52 +00:00
|
|
|
) -> Box<dyn Any> {
|
2019-08-04 11:36:43 +00:00
|
|
|
let res = driver::codegen_crate(tcx, metadata, need_metadata_module);
|
|
|
|
|
|
|
|
rustc_incremental::assert_module_sources::assert_module_sources(tcx);
|
2020-03-24 12:09:44 +00:00
|
|
|
rustc_symbol_mangling::test::report_symbol_names(tcx);
|
2019-08-04 11:36:43 +00:00
|
|
|
|
|
|
|
res
|
2018-06-17 16:05:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 12:49:48 +00:00
|
|
|
fn join_codegen(
|
2018-06-17 16:05:11 +00:00
|
|
|
&self,
|
2020-02-07 12:49:48 +00:00
|
|
|
ongoing_codegen: Box<dyn Any>,
|
2020-03-11 14:18:01 +00:00
|
|
|
sess: &Session,
|
|
|
|
dep_graph: &DepGraph,
|
2020-02-07 12:49:48 +00:00
|
|
|
) -> Result<Box<dyn Any>, ErrorReported> {
|
2020-03-11 14:18:01 +00:00
|
|
|
let (codegen_results, work_products) = *ongoing_codegen.downcast::<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)>().unwrap();
|
|
|
|
|
|
|
|
sess.time("serialize_work_products", move || {
|
|
|
|
rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(Box::new(codegen_results))
|
2020-02-07 12:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn link(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
|
|
|
res: Box<dyn Any>,
|
2018-06-17 16:05:11 +00:00
|
|
|
outputs: &OutputFilenames,
|
2019-03-11 19:02:47 +00:00
|
|
|
) -> Result<(), ErrorReported> {
|
2019-04-01 17:34:26 +00:00
|
|
|
use rustc_codegen_ssa::back::link::link_binary;
|
|
|
|
|
|
|
|
let codegen_results = *res
|
2018-11-10 11:49:25 +00:00
|
|
|
.downcast::<CodegenResults>()
|
2018-11-07 13:04:44 +00:00
|
|
|
.expect("Expected CraneliftCodegenBackend's CodegenResult, found Box<Any>");
|
2018-07-24 12:10:53 +00:00
|
|
|
|
2019-10-03 15:22:01 +00:00
|
|
|
let _timer = sess.prof.generic_activity("link_crate");
|
|
|
|
|
2020-01-06 19:11:03 +00:00
|
|
|
sess.time("linking", || {
|
2019-09-28 15:00:27 +00:00
|
|
|
let target_cpu = crate::target_triple(sess).to_string();
|
2019-09-22 14:21:00 +00:00
|
|
|
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
|
|
|
|
sess,
|
|
|
|
&codegen_results,
|
|
|
|
outputs,
|
|
|
|
&codegen_results.crate_name.as_str(),
|
|
|
|
&target_cpu,
|
|
|
|
);
|
|
|
|
});
|
2019-04-01 17:34:26 +00:00
|
|
|
|
2020-03-11 14:18:01 +00:00
|
|
|
rustc_incremental::finalize_session_directory(sess, codegen_results.crate_hash);
|
|
|
|
|
2018-06-17 16:05:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 10:30:21 +00:00
|
|
|
fn target_triple(sess: &Session) -> target_lexicon::Triple {
|
2019-09-22 14:05:22 +00:00
|
|
|
sess.target.target.llvm_target.parse().unwrap()
|
2019-05-25 10:30:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 15:06:18 +00:00
|
|
|
fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'static> {
|
2019-10-27 15:55:35 +00:00
|
|
|
use target_lexicon::BinaryFormat;
|
|
|
|
|
|
|
|
let target_triple = crate::target_triple(sess);
|
|
|
|
|
2018-12-12 14:11:15 +00:00
|
|
|
let mut flags_builder = settings::builder();
|
2019-08-11 15:06:18 +00:00
|
|
|
if enable_pic {
|
|
|
|
flags_builder.enable("is_pic").unwrap();
|
|
|
|
} else {
|
|
|
|
flags_builder.set("is_pic", "false").unwrap();
|
|
|
|
}
|
2020-01-14 12:55:08 +00:00
|
|
|
flags_builder.set("enable_probestack", "false").unwrap(); // __cranelift_probestack is not provided
|
2019-08-31 17:28:09 +00:00
|
|
|
flags_builder
|
|
|
|
.set(
|
|
|
|
"enable_verifier",
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
"true"
|
|
|
|
} else {
|
|
|
|
"false"
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-12-12 14:11:15 +00:00
|
|
|
|
2019-10-27 15:55:35 +00:00
|
|
|
let tls_model = match target_triple.binary_format {
|
|
|
|
BinaryFormat::Elf => "elf_gd",
|
|
|
|
BinaryFormat::Macho => "macho",
|
|
|
|
BinaryFormat::Coff => "coff",
|
|
|
|
_ => "none",
|
|
|
|
};
|
|
|
|
flags_builder.set("tls_model", tls_model).unwrap();
|
|
|
|
|
2019-07-27 14:12:15 +00:00
|
|
|
// FIXME(CraneStation/cranelift#732) fix LICM in presence of jump tables
|
2019-05-04 14:54:25 +00:00
|
|
|
/*
|
2020-03-24 12:09:44 +00:00
|
|
|
use rustc_session::config::OptLevel;
|
2019-05-04 14:54:25 +00:00
|
|
|
match sess.opts.optimize {
|
2018-12-12 14:11:15 +00:00
|
|
|
OptLevel::No => {
|
|
|
|
flags_builder.set("opt_level", "fastest").unwrap();
|
|
|
|
}
|
|
|
|
OptLevel::Less | OptLevel::Default => {}
|
|
|
|
OptLevel::Aggressive => {
|
|
|
|
flags_builder.set("opt_level", "best").unwrap();
|
|
|
|
}
|
|
|
|
OptLevel::Size | OptLevel::SizeMin => {
|
|
|
|
sess.warn("Optimizing for size is not supported. Just ignoring the request");
|
|
|
|
}
|
2019-02-11 18:11:55 +00:00
|
|
|
}*/
|
2018-12-12 14:11:15 +00:00
|
|
|
|
|
|
|
let flags = settings::Flags::new(flags_builder);
|
2019-12-24 11:40:18 +00:00
|
|
|
cranelift_codegen::isa::lookup(target_triple)
|
2018-12-12 14:11:15 +00:00
|
|
|
.unwrap()
|
|
|
|
.finish(flags)
|
|
|
|
}
|
|
|
|
|
2018-07-14 09:59:42 +00:00
|
|
|
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
2018-06-17 16:05:11 +00:00
|
|
|
#[no_mangle]
|
2018-11-17 17:23:52 +00:00
|
|
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
2018-07-23 09:17:39 +00:00
|
|
|
Box::new(CraneliftCodegenBackend)
|
2018-06-18 16:39:07 +00:00
|
|
|
}
|