rust/src/driver/mod.rs

129 lines
4.5 KiB
Rust
Raw Normal View History

2019-05-04 09:54:25 -05:00
use std::any::Any;
use rustc::middle::cstore::EncodedMetadata;
use rustc::mir::mono::{Linkage as RLinkage, MonoItem, Visibility};
2019-05-04 09:54:25 -05:00
use crate::prelude::*;
2020-03-12 05:48:17 -05:00
mod aot;
2020-03-12 05:44:27 -05:00
#[cfg(not(target_arch = "wasm32"))]
mod jit;
pub(crate) fn codegen_crate(
tcx: TyCtxt<'_>,
2019-05-04 09:54:25 -05:00
metadata: EncodedMetadata,
need_metadata_module: bool,
2019-05-04 09:54:25 -05:00
) -> Box<dyn Any> {
tcx.sess.abort_if_errors();
2020-03-12 05:17:19 -05:00
if std::env::var("CG_CLIF_JIT").is_ok()
&& tcx.sess.crate_types.get().contains(&rustc_session::config::CrateType::Executable)
2019-08-11 10:33:52 -05:00
{
#[cfg(not(target_arch = "wasm32"))]
2020-03-12 05:44:27 -05:00
let _: ! = jit::run_jit(tcx);
#[cfg(target_arch = "wasm32")]
panic!("jit not supported on wasm");
}
2020-03-12 05:48:17 -05:00
aot::run_aot(tcx, metadata, need_metadata_module)
2019-05-04 09:54:25 -05:00
}
fn codegen_mono_items<'tcx>(
tcx: TyCtxt<'tcx>,
2019-05-04 09:54:25 -05:00
module: &mut Module<impl Backend + 'static>,
debug_context: Option<&mut DebugContext<'tcx>>,
2020-03-09 05:21:40 -05:00
mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>,
2019-05-04 09:54:25 -05:00
) {
let mut cx = CodegenCx::new(tcx, module, debug_context);
tcx.sess.time("predefine functions", || {
2020-03-09 05:21:40 -05:00
for &(mono_item, (linkage, visibility)) in &mono_items {
match mono_item {
MonoItem::Fn(instance) => {
let (name, sig) =
get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false);
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
cx.module.declare_function(&name, linkage, &sig).unwrap();
}
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
}
2019-05-04 09:54:25 -05:00
}
});
for (mono_item, (linkage, visibility)) in mono_items {
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
trans_mono_item(&mut cx, mono_item, linkage);
}
tcx.sess.time("finalize CodegenCx", || cx.finalize());
2019-05-04 09:54:25 -05:00
}
fn trans_mono_item<'clif, 'tcx, B: Backend + 'static>(
cx: &mut crate::CodegenCx<'clif, 'tcx, B>,
2019-05-04 09:54:25 -05:00
mono_item: MonoItem<'tcx>,
linkage: Linkage,
) {
let tcx = cx.tcx;
match mono_item {
MonoItem::Fn(inst) => {
let _inst_guard =
PrintOnPanic(|| format!("{:?} {}", inst, tcx.symbol_name(inst).name.as_str()));
2019-05-04 09:54:25 -05:00
debug_assert!(!inst.substs.needs_infer());
let _mir_guard = PrintOnPanic(|| {
match inst.def {
InstanceDef::Item(_)
| InstanceDef::DropGlue(_, _)
| InstanceDef::Virtual(_, _) => {
2019-05-04 09:54:25 -05:00
let mut mir = ::std::io::Cursor::new(Vec::new());
crate::rustc_mir::util::write_mir_pretty(
tcx,
Some(inst.def_id()),
&mut mir,
)
.unwrap();
String::from_utf8(mir.into_inner()).unwrap()
}
_ => {
// FIXME fix write_mir_pretty for these instances
format!("{:#?}", tcx.instance_mir(inst.def))
}
}
});
2020-01-10 07:15:14 -06:00
cx.tcx.sess.time("codegen fn", || crate::base::trans_fn(cx, inst, linkage));
2019-05-04 09:54:25 -05:00
}
MonoItem::Static(def_id) => {
2019-08-18 09:52:07 -05:00
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
2019-05-04 09:54:25 -05:00
}
2020-01-25 10:24:45 -06:00
MonoItem::GlobalAsm(hir_id) => {
let item = tcx.hir().expect_item(hir_id);
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
// FIXME implement global asm using an external assembler
if asm.as_str().contains("__rust_probestack") {
return;
} else {
tcx
.sess
.fatal(&format!("Unimplemented global asm mono item \"{}\"", asm));
}
} else {
bug!("Expected GlobalAsm found {:?}", item);
}
}
2019-05-04 09:54:25 -05:00
}
}
fn time<R>(tcx: TyCtxt<'_>, name: &'static str, f: impl FnOnce() -> R) -> R {
2020-03-12 05:17:19 -05:00
if std::env::var("CG_CLIF_DISPLAY_CG_TIME").is_ok() {
println!("[{:<30}: {}] start", tcx.crate_name(LOCAL_CRATE), name);
2020-03-12 05:17:19 -05:00
let before = std::time::Instant::now();
let res = tcx.sess.time(name, f);
2020-03-12 05:17:19 -05:00
let after = std::time::Instant::now();
println!("[{:<30}: {}] end time: {:?}", tcx.crate_name(LOCAL_CRATE), name, after - before);
2020-03-12 05:17:19 -05:00
res
} else {
tcx.sess.time(name, f)
2020-03-12 05:17:19 -05:00
}
2019-05-04 09:54:25 -05:00
}