2018-11-30 11:08:08 -06:00
|
|
|
#![feature(rustc_private, never_type, decl_macro)]
|
2018-06-30 09:27:11 -05:00
|
|
|
#![allow(intra_doc_link_resolution_failure)]
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2019-03-27 13:24:42 -05:00
|
|
|
extern crate flate2;
|
2018-06-17 11:05:11 -05:00
|
|
|
extern crate rustc;
|
2018-11-24 04:23:49 -06:00
|
|
|
extern crate rustc_codegen_ssa;
|
2018-06-17 11:05:11 -05:00
|
|
|
extern crate rustc_codegen_utils;
|
2018-11-24 05:47:53 -06:00
|
|
|
extern crate rustc_data_structures;
|
2019-07-08 02:54:18 -05:00
|
|
|
extern crate rustc_driver;
|
2018-11-24 05:47:53 -06:00
|
|
|
extern crate rustc_fs_util;
|
2018-06-17 11:05:11 -05:00
|
|
|
extern crate rustc_incremental;
|
2018-07-31 05:25:16 -05:00
|
|
|
extern crate rustc_mir;
|
|
|
|
extern crate rustc_target;
|
2018-11-24 05:47:53 -06:00
|
|
|
extern crate syntax;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-07-23 04:17:39 -05:00
|
|
|
use std::any::Any;
|
2019-02-21 08:06:09 -06:00
|
|
|
use std::sync::mpsc;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-11-07 06:29:38 -06:00
|
|
|
use rustc::dep_graph::DepGraph;
|
2019-05-04 07:57:41 -05:00
|
|
|
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader};
|
2019-05-04 09:54:25 -05:00
|
|
|
use rustc::session::config::OutputFilenames;
|
2018-11-07 06:29:38 -06:00
|
|
|
use rustc::ty::query::Providers;
|
2019-03-11 14:02:47 -05:00
|
|
|
use rustc::util::common::ErrorReported;
|
2018-11-07 06:29:38 -06:00
|
|
|
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-07-23 04:17:39 -05:00
|
|
|
use cranelift::codegen::settings;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-11-10 08:12:00 -06:00
|
|
|
use crate::constant::ConstantCx;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2018-07-19 12:33:42 -05:00
|
|
|
mod abi;
|
2018-11-05 11:29:15 -06:00
|
|
|
mod allocator;
|
2018-08-09 03:46:56 -05:00
|
|
|
mod analyze;
|
2018-11-09 11:38:30 -06:00
|
|
|
mod archive;
|
2018-06-17 11:05:11 -05:00
|
|
|
mod base;
|
2019-07-31 02:45:11 -05:00
|
|
|
mod cast;
|
2019-07-07 11:08:38 -05:00
|
|
|
mod codegen_i128;
|
2018-06-22 12:18:53 -05:00
|
|
|
mod common;
|
2018-07-31 05:25:16 -05:00
|
|
|
mod constant;
|
2019-01-17 11:07:27 -06:00
|
|
|
mod debuginfo;
|
2019-05-04 09:54:25 -05:00
|
|
|
mod driver;
|
2018-10-03 11:21:52 -05:00
|
|
|
mod intrinsics;
|
2019-03-11 14:36:29 -05:00
|
|
|
mod linkage;
|
2019-07-28 02:54:57 -05:00
|
|
|
mod llvm_intrinsics;
|
2018-10-20 11:41:26 -05:00
|
|
|
mod main_shim;
|
2018-08-15 05:07:08 -05:00
|
|
|
mod metadata;
|
2018-08-15 07:45:32 -05:00
|
|
|
mod pretty_clif;
|
2019-07-20 08:33:57 -05:00
|
|
|
mod target_features_whitelist;
|
2018-11-16 10:35:47 -06:00
|
|
|
mod trap;
|
2018-11-16 12:53:27 -06:00
|
|
|
mod unimpl;
|
2018-12-29 08:33:34 -06:00
|
|
|
mod unsize;
|
2019-06-11 08:43:22 -05:00
|
|
|
mod value_and_place;
|
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};
|
2019-07-24 10:16:31 -05:00
|
|
|
pub use std::convert::{TryFrom, TryInto};
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-11-10 05:49:25 -06:00
|
|
|
pub use syntax::ast::{FloatTy, IntTy, UintTy};
|
2019-02-21 08:06:09 -06:00
|
|
|
pub use syntax::source_map::{Pos, Span, DUMMY_SP};
|
2018-11-10 05:49:25 -06:00
|
|
|
|
2018-11-17 11:23:52 -06:00
|
|
|
pub use rustc::bug;
|
2018-11-10 05:49:25 -06:00
|
|
|
pub use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
2019-06-06 13:31:09 -05:00
|
|
|
pub use rustc::mir::{self, interpret::AllocId, mono::MonoItem, *};
|
2018-11-10 08:12:00 -06:00
|
|
|
pub use rustc::session::{
|
|
|
|
config::{CrateType, Lto},
|
|
|
|
Session,
|
|
|
|
};
|
2018-11-14 07:31:42 -06:00
|
|
|
pub use rustc::ty::layout::{self, Abi, LayoutOf, Scalar, Size, TyLayout, VariantIdx};
|
2018-11-07 06:29:38 -06:00
|
|
|
pub use rustc::ty::{
|
2019-02-28 03:55:21 -06:00
|
|
|
self, 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-11-07 06:29:38 -06:00
|
|
|
pub use rustc_data_structures::{
|
2018-08-17 06:01:56 -05:00
|
|
|
fx::{FxHashMap, FxHashSet},
|
|
|
|
indexed_vec::Idx,
|
|
|
|
sync::Lrc,
|
|
|
|
};
|
2019-06-06 13:31:09 -05:00
|
|
|
pub use rustc_mir::monomorphize::collector;
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-11-21 09:01:33 -06:00
|
|
|
pub use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
|
2018-12-12 09:01:34 -06:00
|
|
|
pub use rustc_codegen_ssa::traits::*;
|
2019-02-21 08:06:09 -06:00
|
|
|
pub use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleKind};
|
2018-11-21 09:01:33 -06:00
|
|
|
|
2018-07-14 04:59:42 -05:00
|
|
|
pub use cranelift::codegen::ir::{
|
2019-02-21 08:06:09 -06:00
|
|
|
condcodes::IntCC, function::Function, ExternalName, FuncRef, Inst, SourceLoc, StackSlot,
|
2018-06-22 12:18:53 -05:00
|
|
|
};
|
2018-11-07 06:32:02 -06:00
|
|
|
pub use cranelift::codegen::isa::CallConv;
|
2018-07-14 04:59:42 -05:00
|
|
|
pub use cranelift::codegen::Context;
|
|
|
|
pub use cranelift::prelude::*;
|
2019-01-17 11:07:27 -06:00
|
|
|
pub use cranelift_module::{
|
2019-02-21 08:06:09 -06:00
|
|
|
self, Backend, DataContext, DataId, FuncId, FuncOrDataId, Linkage, Module,
|
2019-01-17 11:07:27 -06:00
|
|
|
};
|
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};
|
2019-07-31 02:45:11 -05:00
|
|
|
pub use crate::cast::*;
|
2018-07-30 09:57:40 -05:00
|
|
|
pub use crate::common::*;
|
2019-01-17 11:07:27 -06:00
|
|
|
pub use crate::debuginfo::{DebugContext, FunctionDebugContext};
|
2018-11-16 10:35:47 -06:00
|
|
|
pub use crate::trap::*;
|
2018-11-16 12:53:27 -06:00
|
|
|
pub use crate::unimpl::{unimpl, with_unimpl_span};
|
2019-06-11 08:43:22 -05:00
|
|
|
pub use crate::value_and_place::{CValue, CPlace};
|
2018-12-18 11:28:02 -06:00
|
|
|
pub use crate::{Caches, CodegenCx};
|
2019-05-04 09:54:25 -05: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 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,
|
2019-01-06 08:27:20 -06:00
|
|
|
pub vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
|
2018-06-30 09:37:02 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
impl Default for Caches<'_> {
|
2018-12-18 11:28:02 -06:00
|
|
|
fn default() -> Self {
|
2018-08-26 09:58:52 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-13 13:44:40 -05:00
|
|
|
pub struct CodegenCx<'clif, 'tcx, B: Backend + 'static> {
|
2019-06-16 04:13:49 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-12-18 11:28:02 -06:00
|
|
|
module: &'clif mut Module<B>,
|
|
|
|
ccx: ConstantCx,
|
|
|
|
caches: Caches<'tcx>,
|
2019-01-17 11:07:27 -06:00
|
|
|
debug_context: Option<&'clif mut DebugContext<'tcx>>,
|
2018-12-18 11:28:02 -06:00
|
|
|
}
|
|
|
|
|
2019-06-13 13:44:40 -05:00
|
|
|
impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> {
|
2019-01-17 11:07:27 -06:00
|
|
|
fn new(
|
2019-06-16 04:13:49 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-01-17 11:07:27 -06:00
|
|
|
module: &'clif mut Module<B>,
|
|
|
|
debug_context: Option<&'clif mut DebugContext<'tcx>>,
|
|
|
|
) -> Self {
|
2018-12-18 11:28:02 -06:00
|
|
|
CodegenCx {
|
|
|
|
tcx,
|
|
|
|
module,
|
|
|
|
ccx: ConstantCx::default(),
|
|
|
|
caches: Caches::default(),
|
2019-01-17 11:07:27 -06:00
|
|
|
debug_context,
|
2018-12-18 11:28:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finalize(self) {
|
|
|
|
self.ccx.finalize(self.tcx, self.module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:17:39 -05:00
|
|
|
struct CraneliftCodegenBackend;
|
|
|
|
|
2018-07-14 04:59:42 -05:00
|
|
|
impl CodegenBackend for CraneliftCodegenBackend {
|
2019-07-03 09:49:42 -05:00
|
|
|
fn init(&self, _sess: &Session) {}
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2018-11-17 11:23:52 -06:00
|
|
|
fn metadata_loader(&self) -> Box<dyn 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-11-24 04:23:49 -06:00
|
|
|
rustc_codegen_ssa::back::symbol_export::provide(providers);
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2019-07-20 08:33:57 -05: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
|
|
|
|
tcx.arena.alloc(target_features_whitelist::all_known_features()
|
|
|
|
.map(|(a, b)| (a.to_string(), b))
|
|
|
|
.collect())
|
|
|
|
} else {
|
|
|
|
tcx.arena.alloc(target_features_whitelist::target_feature_whitelist(tcx.sess)
|
|
|
|
.iter()
|
|
|
|
.map(|&(a, b)| (a.to_string(), b))
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
};
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
fn provide_extern(&self, providers: &mut Providers) {
|
2018-11-24 04:23:49 -06:00
|
|
|
rustc_codegen_ssa::back::symbol_export::provide_extern(providers);
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn codegen_crate<'tcx>(
|
2018-06-17 11:05:11 -05:00
|
|
|
&self,
|
2019-06-16 04:13:49 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-04 07:57:41 -05:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-04 09:54:25 -05:00
|
|
|
need_metadata_module: bool,
|
2018-11-17 11:23:52 -06:00
|
|
|
_rx: mpsc::Receiver<Box<dyn Any + Send>>,
|
|
|
|
) -> Box<dyn Any> {
|
2019-08-04 06:36:43 -05:00
|
|
|
let res = driver::codegen_crate(tcx, metadata, need_metadata_module);
|
|
|
|
|
|
|
|
rustc_incremental::assert_module_sources::assert_module_sources(tcx);
|
|
|
|
|
|
|
|
res
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn join_codegen_and_link(
|
|
|
|
&self,
|
2018-11-17 11:23:52 -06:00
|
|
|
res: Box<dyn Any>,
|
2018-06-17 11:05:11 -05:00
|
|
|
sess: &Session,
|
|
|
|
_dep_graph: &DepGraph,
|
|
|
|
outputs: &OutputFilenames,
|
2019-03-11 14:02:47 -05:00
|
|
|
) -> Result<(), ErrorReported> {
|
2019-04-01 12:34:26 -05:00
|
|
|
use rustc_codegen_ssa::back::link::link_binary;
|
|
|
|
|
|
|
|
let codegen_results = *res
|
2018-11-10 05:49:25 -06:00
|
|
|
.downcast::<CodegenResults>()
|
2018-11-07 07:04:44 -06:00
|
|
|
.expect("Expected CraneliftCodegenBackend's CodegenResult, found Box<Any>");
|
2018-07-24 07:10:53 -05:00
|
|
|
|
2019-04-01 12:34:26 -05:00
|
|
|
let target_cpu = ::target_lexicon::HOST.to_string();
|
|
|
|
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
|
|
|
|
sess,
|
|
|
|
&codegen_results,
|
|
|
|
outputs,
|
|
|
|
&codegen_results.crate_name.as_str(),
|
|
|
|
&target_cpu,
|
|
|
|
);
|
|
|
|
|
2018-06-17 11:05:11 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 05:30:21 -05:00
|
|
|
fn target_triple(sess: &Session) -> target_lexicon::Triple {
|
|
|
|
let mut target = &*sess.target.target.llvm_target;
|
|
|
|
|
|
|
|
// FIXME add support for x86_64-apple-macosx10.7.0 to target-lexicon
|
|
|
|
if target.starts_with("x86_64-apple-macosx") {
|
|
|
|
target = "x86_64-apple-darwin";
|
|
|
|
}
|
|
|
|
|
|
|
|
target.parse().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-08-11 10:06:18 -05:00
|
|
|
fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'static> {
|
2018-12-12 08:11:15 -06:00
|
|
|
let mut flags_builder = settings::builder();
|
2019-08-11 10:06:18 -05:00
|
|
|
if enable_pic {
|
|
|
|
flags_builder.enable("is_pic").unwrap();
|
|
|
|
} else {
|
|
|
|
flags_builder.set("is_pic", "false").unwrap();
|
|
|
|
}
|
|
|
|
flags_builder.set("probestack_enabled", "false").unwrap(); // __cranelift_probestack is not provided
|
2019-02-21 08:06:09 -06:00
|
|
|
flags_builder.set("enable_verifier", if cfg!(debug_assertions) {
|
2018-12-12 08:11:15 -06:00
|
|
|
"true"
|
|
|
|
} else {
|
|
|
|
"false"
|
|
|
|
}).unwrap();
|
|
|
|
|
2019-07-27 09:12:15 -05:00
|
|
|
// FIXME(CraneStation/cranelift#732) fix LICM in presence of jump tables
|
|
|
|
//flags_builder.set("opt_level", "best").unwrap();
|
2019-07-25 08:49:38 -05:00
|
|
|
|
2019-02-11 12:11:55 -06:00
|
|
|
// FIXME enable again when https://github.com/CraneStation/cranelift/issues/664 is fixed
|
2019-05-04 09:54:25 -05:00
|
|
|
/*
|
|
|
|
use rustc::session::config::OptLevel;
|
|
|
|
match sess.opts.optimize {
|
2018-12-12 08:11:15 -06: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 12:11:55 -06:00
|
|
|
}*/
|
2018-12-12 08:11:15 -06:00
|
|
|
|
2019-05-25 05:30:21 -05:00
|
|
|
let target_triple = target_triple(sess);
|
2018-12-12 08:11:15 -06:00
|
|
|
let flags = settings::Flags::new(flags_builder);
|
2019-05-25 05:30:21 -05:00
|
|
|
cranelift::codegen::isa::lookup(target_triple)
|
2018-12-12 08:11:15 -06:00
|
|
|
.unwrap()
|
|
|
|
.finish(flags)
|
|
|
|
}
|
|
|
|
|
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]
|
2018-11-17 11:23:52 -06:00
|
|
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
2018-07-23 04:17:39 -05:00
|
|
|
Box::new(CraneliftCodegenBackend)
|
2018-06-18 11:39:07 -05:00
|
|
|
}
|