2019-12-16 11:33:57 +01:00
|
|
|
#![feature(rustc_private, decl_macro, type_alias_impl_trait, associated_type_bounds, never_type)]
|
2018-06-30 16:27:11 +02:00
|
|
|
#![allow(intra_doc_link_resolution_failure)]
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2019-03-27 19:24:42 +01:00
|
|
|
extern crate flate2;
|
2020-01-18 12:43:31 +01:00
|
|
|
extern crate libc;
|
2019-09-21 14:27:38 +02:00
|
|
|
extern crate tempfile;
|
2018-06-17 18:05:11 +02:00
|
|
|
extern crate rustc;
|
2018-11-24 11:23:49 +01:00
|
|
|
extern crate rustc_codegen_ssa;
|
2018-06-17 18:05:11 +02:00
|
|
|
extern crate rustc_codegen_utils;
|
2018-11-24 12:47:53 +01:00
|
|
|
extern crate rustc_data_structures;
|
2019-07-08 09:54:18 +02:00
|
|
|
extern crate rustc_driver;
|
2018-11-24 12:47:53 +01:00
|
|
|
extern crate rustc_fs_util;
|
2020-01-09 17:43:10 +01:00
|
|
|
extern crate rustc_hir;
|
2018-06-17 18:05:11 +02:00
|
|
|
extern crate rustc_incremental;
|
2019-10-03 17:22:01 +02:00
|
|
|
extern crate rustc_index;
|
2018-07-31 12:25:16 +02:00
|
|
|
extern crate rustc_mir;
|
2019-12-31 16:43:24 +01:00
|
|
|
extern crate rustc_session;
|
2020-01-06 20:11:03 +01:00
|
|
|
extern crate rustc_span;
|
2018-07-31 12:25:16 +02:00
|
|
|
extern crate rustc_target;
|
2018-11-24 12:47:53 +01:00
|
|
|
extern crate syntax;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2018-07-23 11:17:39 +02:00
|
|
|
use std::any::Any;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2018-11-07 13:29:38 +01:00
|
|
|
use rustc::dep_graph::DepGraph;
|
2019-05-04 14:57:41 +02:00
|
|
|
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader};
|
2019-05-04 16:54:25 +02:00
|
|
|
use rustc::session::config::OutputFilenames;
|
2018-11-07 13:29:38 +01:00
|
|
|
use rustc::ty::query::Providers;
|
2019-03-11 20:02:47 +01:00
|
|
|
use rustc::util::common::ErrorReported;
|
2018-11-07 13:29:38 +01:00
|
|
|
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2019-12-24 12:40:18 +01:00
|
|
|
use cranelift_codegen::settings;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2018-11-10 15:12:00 +01:00
|
|
|
use crate::constant::ConstantCx;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2018-07-19 19:33:42 +02:00
|
|
|
mod abi;
|
2018-11-05 18:29:15 +01:00
|
|
|
mod allocator;
|
2018-08-09 10:46:56 +02:00
|
|
|
mod analyze;
|
2018-11-09 18:38:30 +01:00
|
|
|
mod archive;
|
2020-01-18 12:43:31 +01:00
|
|
|
mod atomic_shim;
|
2018-06-17 18:05:11 +02:00
|
|
|
mod base;
|
2019-10-16 20:48:09 +02:00
|
|
|
mod backend;
|
2019-07-31 09:45:11 +02:00
|
|
|
mod cast;
|
2019-07-07 18:08:38 +02:00
|
|
|
mod codegen_i128;
|
2018-06-22 19:18:53 +02:00
|
|
|
mod common;
|
2018-07-31 12:25:16 +02:00
|
|
|
mod constant;
|
2019-01-17 18:07:27 +01:00
|
|
|
mod debuginfo;
|
2019-08-14 12:01:41 +02:00
|
|
|
mod discriminant;
|
2019-05-04 16:54:25 +02:00
|
|
|
mod driver;
|
2018-10-03 18:21:52 +02:00
|
|
|
mod intrinsics;
|
2019-03-11 20:36:29 +01:00
|
|
|
mod linkage;
|
2018-10-20 18:41:26 +02:00
|
|
|
mod main_shim;
|
2018-08-15 12:07:08 +02:00
|
|
|
mod metadata;
|
2019-08-14 11:52:39 +02:00
|
|
|
mod num;
|
2019-12-26 13:37:10 +01:00
|
|
|
mod optimize;
|
2019-12-20 16:02:47 +01:00
|
|
|
mod pointer;
|
2018-08-15 14:45:32 +02:00
|
|
|
mod pretty_clif;
|
2019-07-20 15:33:57 +02:00
|
|
|
mod target_features_whitelist;
|
2018-11-16 17:35:47 +01:00
|
|
|
mod trap;
|
2018-11-16 19:53:27 +01:00
|
|
|
mod unimpl;
|
2018-12-29 15:33:34 +01:00
|
|
|
mod unsize;
|
2019-06-11 15:43:22 +02:00
|
|
|
mod value_and_place;
|
2018-09-08 18:00:06 +02:00
|
|
|
mod vtable;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
|
|
|
mod prelude {
|
2018-06-30 16:37:02 +02:00
|
|
|
pub use std::any::Any;
|
2018-07-16 15:13:37 +02:00
|
|
|
pub use std::collections::{HashMap, HashSet};
|
2019-07-24 17:16:31 +02:00
|
|
|
pub use std::convert::{TryFrom, TryInto};
|
2018-06-22 19:18:53 +02:00
|
|
|
|
2018-11-10 12:49:25 +01:00
|
|
|
pub use syntax::ast::{FloatTy, IntTy, UintTy};
|
2020-01-06 20:11:03 +01:00
|
|
|
pub use rustc_span::{Pos, Span};
|
2018-11-10 12:49:25 +01:00
|
|
|
|
2018-11-17 18:23:52 +01:00
|
|
|
pub use rustc::bug;
|
2020-01-09 17:43:10 +01:00
|
|
|
pub use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
2019-06-06 20:31:09 +02:00
|
|
|
pub use rustc::mir::{self, interpret::AllocId, mono::MonoItem, *};
|
2018-11-10 15:12:00 +01:00
|
|
|
pub use rustc::session::{
|
|
|
|
config::{CrateType, Lto},
|
|
|
|
Session,
|
|
|
|
};
|
2018-11-14 14:31:42 +01:00
|
|
|
pub use rustc::ty::layout::{self, Abi, LayoutOf, Scalar, Size, TyLayout, VariantIdx};
|
2018-11-07 13:29:38 +01:00
|
|
|
pub use rustc::ty::{
|
2019-08-31 22:58:09 +05:30
|
|
|
self, FnSig, Instance, InstanceDef, ParamEnv, PolyFnSig, Ty, TyCtxt, TypeAndMut,
|
|
|
|
TypeFoldable,
|
2018-06-17 18:05:11 +02:00
|
|
|
};
|
2019-10-03 17:22:01 +02:00
|
|
|
|
2018-11-07 13:29:38 +01:00
|
|
|
pub use rustc_data_structures::{
|
2018-08-17 13:01:56 +02:00
|
|
|
fx::{FxHashMap, FxHashSet},
|
|
|
|
sync::Lrc,
|
|
|
|
};
|
2019-10-03 17:22:01 +02:00
|
|
|
|
|
|
|
pub use rustc_index::vec::Idx;
|
|
|
|
|
2019-06-06 20:31:09 +02:00
|
|
|
pub use rustc_mir::monomorphize::collector;
|
2018-06-22 19:18:53 +02:00
|
|
|
|
2018-11-21 16:01:33 +01:00
|
|
|
pub use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
|
2018-12-12 16:01:34 +01:00
|
|
|
pub use rustc_codegen_ssa::traits::*;
|
2019-02-21 15:06:09 +01:00
|
|
|
pub use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleKind};
|
2018-11-21 16:01:33 +01:00
|
|
|
|
2019-12-24 12:40:18 +01:00
|
|
|
pub use cranelift_codegen::Context;
|
2020-01-11 17:57:18 +01:00
|
|
|
pub use cranelift_codegen::entity::EntitySet;
|
2020-02-14 18:23:29 +01:00
|
|
|
pub use cranelift_codegen::ir::{AbiParam, Block, ExternalName, FuncRef, Inst, InstBuilder, MemFlags, Signature, SourceLoc, StackSlot, StackSlotData, StackSlotKind, TrapCode, Type, Value};
|
2019-12-24 12:40:18 +01:00
|
|
|
pub use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
|
|
|
|
pub use cranelift_codegen::ir::function::Function;
|
|
|
|
pub use cranelift_codegen::ir::immediates::{Ieee32, Ieee64};
|
|
|
|
pub use cranelift_codegen::ir::types;
|
|
|
|
pub use cranelift_codegen::isa::{self, CallConv};
|
|
|
|
pub use cranelift_codegen::settings::{self, Configurable};
|
|
|
|
pub use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
|
2019-01-17 18:07:27 +01:00
|
|
|
pub use cranelift_module::{
|
2019-02-21 15:06:09 +01:00
|
|
|
self, Backend, DataContext, DataId, FuncId, FuncOrDataId, Linkage, Module,
|
2019-01-17 18:07:27 +01:00
|
|
|
};
|
2018-06-22 19:18:53 +02:00
|
|
|
|
2018-07-30 16:57:40 +02:00
|
|
|
pub use crate::abi::*;
|
2018-07-31 12:25:16 +02:00
|
|
|
pub use crate::base::{trans_operand, trans_place};
|
2019-07-31 09:45:11 +02:00
|
|
|
pub use crate::cast::*;
|
2018-07-30 16:57:40 +02:00
|
|
|
pub use crate::common::*;
|
2019-01-17 18:07:27 +01:00
|
|
|
pub use crate::debuginfo::{DebugContext, FunctionDebugContext};
|
2019-12-20 16:02:47 +01:00
|
|
|
pub use crate::pointer::Pointer;
|
2018-11-16 17:35:47 +01:00
|
|
|
pub use crate::trap::*;
|
2019-10-06 17:31:46 +02:00
|
|
|
pub use crate::unimpl::unimpl;
|
2019-08-31 22:58:09 +05:30
|
|
|
pub use crate::value_and_place::{CPlace, CPlaceInner, CValue};
|
2020-01-04 17:58:38 +01:00
|
|
|
pub use crate::CodegenCx;
|
2019-05-04 16:54:25 +02:00
|
|
|
|
|
|
|
pub struct PrintOnPanic<F: Fn() -> String>(pub F);
|
|
|
|
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if ::std::thread::panicking() {
|
|
|
|
println!("{}", (self.0)());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 20:44:40 +02:00
|
|
|
pub struct CodegenCx<'clif, 'tcx, B: Backend + 'static> {
|
2019-06-16 11:13:49 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-12-18 18:28:02 +01:00
|
|
|
module: &'clif mut Module<B>,
|
2019-08-18 16:52:07 +02:00
|
|
|
constants_cx: ConstantCx,
|
2020-01-04 17:58:38 +01:00
|
|
|
cached_context: Context,
|
|
|
|
vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
|
2019-01-17 18:07:27 +01:00
|
|
|
debug_context: Option<&'clif mut DebugContext<'tcx>>,
|
2018-12-18 18:28:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 20:44:40 +02:00
|
|
|
impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> {
|
2019-01-17 18:07:27 +01:00
|
|
|
fn new(
|
2019-06-16 11:13:49 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-01-17 18:07:27 +01:00
|
|
|
module: &'clif mut Module<B>,
|
|
|
|
debug_context: Option<&'clif mut DebugContext<'tcx>>,
|
|
|
|
) -> Self {
|
2018-12-18 18:28:02 +01:00
|
|
|
CodegenCx {
|
|
|
|
tcx,
|
|
|
|
module,
|
2019-08-18 16:52:07 +02:00
|
|
|
constants_cx: ConstantCx::default(),
|
2020-01-04 17:58:38 +01:00
|
|
|
cached_context: Context::new(),
|
|
|
|
vtables: HashMap::new(),
|
2019-01-17 18:07:27 +01:00
|
|
|
debug_context,
|
2018-12-18 18:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finalize(self) {
|
2019-08-18 16:52:07 +02:00
|
|
|
self.constants_cx.finalize(self.tcx, self.module);
|
2018-12-18 18:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 11:17:39 +02:00
|
|
|
struct CraneliftCodegenBackend;
|
|
|
|
|
2018-07-14 11:59:42 +02:00
|
|
|
impl CodegenBackend for CraneliftCodegenBackend {
|
2020-01-17 20:33:27 +01: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 18:05:11 +02:00
|
|
|
|
2018-11-17 18:23:52 +01:00
|
|
|
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
|
2018-08-15 12:07:08 +02:00
|
|
|
Box::new(crate::metadata::CraneliftMetadataLoader)
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn provide(&self, providers: &mut Providers) {
|
2019-07-20 15:33:57 +02: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 22:58:09 +05:30
|
|
|
tcx.arena.alloc(
|
|
|
|
target_features_whitelist::all_known_features()
|
|
|
|
.map(|(a, b)| (a.to_string(), b))
|
|
|
|
.collect(),
|
|
|
|
)
|
2019-07-20 15:33:57 +02:00
|
|
|
} else {
|
2019-08-31 22:58:09 +05:30
|
|
|
tcx.arena.alloc(
|
|
|
|
target_features_whitelist::target_feature_whitelist(tcx.sess)
|
|
|
|
.iter()
|
|
|
|
.map(|&(a, b)| (a.to_string(), b))
|
|
|
|
.collect(),
|
|
|
|
)
|
2019-07-20 15:33:57 +02:00
|
|
|
}
|
|
|
|
};
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
2019-10-16 18:39:12 +02:00
|
|
|
fn provide_extern(&self, _providers: &mut Providers) {}
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2019-06-16 11:13:49 +02:00
|
|
|
fn codegen_crate<'tcx>(
|
2018-06-17 18:05:11 +02:00
|
|
|
&self,
|
2019-06-16 11:13:49 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-04 14:57:41 +02:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-04 16:54:25 +02:00
|
|
|
need_metadata_module: bool,
|
2018-11-17 18:23:52 +01:00
|
|
|
) -> Box<dyn Any> {
|
2019-08-01 14:07:08 +02:00
|
|
|
rustc_codegen_utils::check_for_rustc_errors_attr(tcx);
|
|
|
|
|
2019-08-04 13:36:43 +02:00
|
|
|
let res = driver::codegen_crate(tcx, metadata, need_metadata_module);
|
|
|
|
|
|
|
|
rustc_incremental::assert_module_sources::assert_module_sources(tcx);
|
2019-08-04 13:16:13 +02:00
|
|
|
rustc_codegen_utils::symbol_names_test::report_symbol_names(tcx);
|
2019-08-04 13:36:43 +02:00
|
|
|
|
|
|
|
res
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 13:49:48 +01:00
|
|
|
fn join_codegen(
|
2018-06-17 18:05:11 +02:00
|
|
|
&self,
|
2020-02-07 13:49:48 +01:00
|
|
|
ongoing_codegen: Box<dyn Any>,
|
|
|
|
_sess: &Session,
|
2018-06-17 18:05:11 +02:00
|
|
|
_dep_graph: &DepGraph,
|
2020-02-07 13:49:48 +01:00
|
|
|
) -> Result<Box<dyn Any>, ErrorReported> {
|
|
|
|
Ok(ongoing_codegen)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn link(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
|
|
|
res: Box<dyn Any>,
|
2018-06-17 18:05:11 +02:00
|
|
|
outputs: &OutputFilenames,
|
2019-03-11 20:02:47 +01:00
|
|
|
) -> Result<(), ErrorReported> {
|
2019-04-01 19:34:26 +02:00
|
|
|
use rustc_codegen_ssa::back::link::link_binary;
|
|
|
|
|
|
|
|
let codegen_results = *res
|
2018-11-10 12:49:25 +01:00
|
|
|
.downcast::<CodegenResults>()
|
2018-11-07 14:04:44 +01:00
|
|
|
.expect("Expected CraneliftCodegenBackend's CodegenResult, found Box<Any>");
|
2018-07-24 14:10:53 +02:00
|
|
|
|
2019-10-03 17:22:01 +02:00
|
|
|
let _timer = sess.prof.generic_activity("link_crate");
|
|
|
|
|
2020-01-06 20:11:03 +01:00
|
|
|
sess.time("linking", || {
|
2019-09-28 17:00:27 +02:00
|
|
|
let target_cpu = crate::target_triple(sess).to_string();
|
2019-09-22 16:21:00 +02:00
|
|
|
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
|
|
|
|
sess,
|
|
|
|
&codegen_results,
|
|
|
|
outputs,
|
|
|
|
&codegen_results.crate_name.as_str(),
|
|
|
|
&target_cpu,
|
|
|
|
);
|
|
|
|
});
|
2019-04-01 19:34:26 +02:00
|
|
|
|
2018-06-17 18:05:11 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 12:30:21 +02:00
|
|
|
fn target_triple(sess: &Session) -> target_lexicon::Triple {
|
2019-09-22 16:05:22 +02:00
|
|
|
sess.target.target.llvm_target.parse().unwrap()
|
2019-05-25 12:30:21 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 17:06:18 +02:00
|
|
|
fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'static> {
|
2018-12-12 15:11:15 +01:00
|
|
|
let mut flags_builder = settings::builder();
|
2019-08-11 17:06:18 +02:00
|
|
|
if enable_pic {
|
|
|
|
flags_builder.enable("is_pic").unwrap();
|
|
|
|
} else {
|
|
|
|
flags_builder.set("is_pic", "false").unwrap();
|
|
|
|
}
|
2020-01-14 13:55:08 +01:00
|
|
|
flags_builder.set("enable_probestack", "false").unwrap(); // __cranelift_probestack is not provided
|
2019-08-31 22:58:09 +05:30
|
|
|
flags_builder
|
|
|
|
.set(
|
|
|
|
"enable_verifier",
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
"true"
|
|
|
|
} else {
|
|
|
|
"false"
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-12-12 15:11:15 +01:00
|
|
|
|
2019-07-27 16:12:15 +02:00
|
|
|
// FIXME(CraneStation/cranelift#732) fix LICM in presence of jump tables
|
2019-05-04 16:54:25 +02:00
|
|
|
/*
|
|
|
|
use rustc::session::config::OptLevel;
|
|
|
|
match sess.opts.optimize {
|
2018-12-12 15:11:15 +01: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 19:11:55 +01:00
|
|
|
}*/
|
2018-12-12 15:11:15 +01:00
|
|
|
|
2019-09-28 17:00:27 +02:00
|
|
|
let target_triple = crate::target_triple(sess);
|
2018-12-12 15:11:15 +01:00
|
|
|
let flags = settings::Flags::new(flags_builder);
|
2019-12-24 12:40:18 +01:00
|
|
|
cranelift_codegen::isa::lookup(target_triple)
|
2018-12-12 15:11:15 +01:00
|
|
|
.unwrap()
|
|
|
|
.finish(flags)
|
|
|
|
}
|
|
|
|
|
2018-07-14 11:59:42 +02:00
|
|
|
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
2018-06-17 18:05:11 +02:00
|
|
|
#[no_mangle]
|
2018-11-17 18:23:52 +01:00
|
|
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
2018-07-23 11:17:39 +02:00
|
|
|
Box::new(CraneliftCodegenBackend)
|
2018-06-18 18:39:07 +02:00
|
|
|
}
|