Skip to content

Commit 441a7c1

Browse files
denismerigouxeddyb
authored andcommitted
Generalized mono_item.rs and base.rs:codegen_instance
1 parent 6a993fe commit 441a7c1

File tree

6 files changed

+136
-90
lines changed

6 files changed

+136
-90
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc::middle::weak_lang_items;
3939
use rustc::mir::mono::{Linkage, Visibility, Stats, CodegenUnitNameBuilder};
4040
use rustc::middle::cstore::{EncodedMetadata};
4141
use rustc::ty::{self, Ty, TyCtxt};
42-
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx};
42+
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
4343
use rustc::ty::query::Providers;
4444
use rustc::middle::cstore::{self, LinkagePreference};
4545
use rustc::middle::exported_symbols;
@@ -76,6 +76,7 @@ use interfaces::*;
7676
use std::any::Any;
7777
use std::cmp;
7878
use std::ffi::CString;
79+
use std::marker;
7980
use std::ops::{Deref, DerefMut};
8081
use std::sync::mpsc;
8182
use std::time::{Instant, Duration};
@@ -90,27 +91,29 @@ use mir::operand::OperandValue;
9091

9192
use rustc_codegen_utils::check_for_rustc_errors_attr;
9293

93-
pub struct StatRecorder<'a, 'll: 'a, 'tcx: 'll> {
94-
cx: &'a CodegenCx<'ll, 'tcx>,
94+
pub struct StatRecorder<'a, 'tcx, Cx: 'a + CodegenMethods<'tcx>> {
95+
cx: &'a Cx,
9596
name: Option<String>,
9697
istart: usize,
98+
_marker: marker::PhantomData<&'tcx ()>,
9799
}
98100

99-
impl StatRecorder<'a, 'll, 'tcx> {
100-
pub fn new(cx: &'a CodegenCx<'ll, 'tcx>, name: String) -> Self {
101-
let istart = cx.stats.borrow().n_llvm_insns;
101+
impl<'a, 'tcx, Cx: CodegenMethods<'tcx>> StatRecorder<'a, 'tcx, Cx> {
102+
pub fn new(cx: &'a Cx, name: String) -> Self {
103+
let istart = cx.stats().borrow().n_llvm_insns;
102104
StatRecorder {
103105
cx,
104106
name: Some(name),
105107
istart,
108+
_marker: marker::PhantomData,
106109
}
107110
}
108111
}
109112

110-
impl Drop for StatRecorder<'a, 'll, 'tcx> {
113+
impl<'a, 'tcx, Cx: CodegenMethods<'tcx>> Drop for StatRecorder<'a, 'tcx, Cx> {
111114
fn drop(&mut self) {
112115
if self.cx.sess().codegen_stats() {
113-
let mut stats = self.cx.stats.borrow_mut();
116+
let mut stats = self.cx.stats().borrow_mut();
114117
let iend = stats.n_llvm_insns;
115118
stats.fn_stats.push((self.name.take().unwrap(), iend - self.istart));
116119
stats.n_fns += 1;
@@ -449,10 +452,13 @@ pub fn memcpy_ty<'a, 'tcx: 'a, Builder: BuilderMethods<'a, 'tcx>>(
449452
bx.memcpy(dst, dst_align, src, src_align, bx.cx().const_usize(size), flags);
450453
}
451454

452-
pub fn codegen_instance(cx: &CodegenCx<'_, 'tcx>, instance: Instance<'tcx>) {
455+
pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
456+
cx: &'a Bx::CodegenCx,
457+
instance: Instance<'tcx>,
458+
) {
453459
let _s = if cx.sess().codegen_stats() {
454460
let mut instance_name = String::new();
455-
DefPathBasedNames::new(cx.tcx, true, true)
461+
DefPathBasedNames::new(cx.tcx(), true, true)
456462
.push_def_path(instance.def_id(), &mut instance_name);
457463
Some(StatRecorder::new(cx, instance_name))
458464
} else {
@@ -464,16 +470,16 @@ pub fn codegen_instance(cx: &CodegenCx<'_, 'tcx>, instance: Instance<'tcx>) {
464470
// release builds.
465471
info!("codegen_instance({})", instance);
466472

467-
let sig = instance.fn_sig(cx.tcx);
468-
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
473+
let sig = instance.fn_sig(cx.tcx());
474+
let sig = cx.tcx().normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
469475

470-
let lldecl = cx.instances.borrow().get(&instance).cloned().unwrap_or_else(||
476+
let lldecl = cx.instances().borrow().get(&instance).cloned().unwrap_or_else(||
471477
bug!("Instance `{:?}` not already declared", instance));
472478

473-
cx.stats.borrow_mut().n_closures += 1;
479+
cx.stats().borrow_mut().n_closures += 1;
474480

475-
let mir = cx.tcx.instance_mir(instance.def);
476-
mir::codegen_mir::<Builder>(cx, lldecl, &mir, instance, sig);
481+
let mir = cx.tcx().instance_mir(instance.def);
482+
mir::codegen_mir::<Bx>(cx, lldecl, &mir, instance, sig);
477483
}
478484

479485
pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {

src/librustc_codegen_llvm/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
417417
fn check_overflow(&self) -> bool {
418418
self.check_overflow
419419
}
420+
421+
fn stats(&self) -> &RefCell<Stats> {
422+
&self.stats
423+
}
424+
425+
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
426+
&self.codegen_unit
427+
}
420428
}
421429

422430
impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {

src/librustc_codegen_llvm/interfaces/declare.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// except according to those terms.
1010

1111
use super::backend::Backend;
12+
use monomorphize::Instance;
13+
use rustc::hir::def_id::DefId;
14+
use rustc::mir::mono::{Linkage, Visibility};
1215
use rustc::ty;
1316

1417
pub trait DeclareMethods<'tcx>: Backend<'tcx> {
@@ -22,3 +25,20 @@ pub trait DeclareMethods<'tcx>: Backend<'tcx> {
2225
fn get_declared_value(&self, name: &str) -> Option<Self::Value>;
2326
fn get_defined_value(&self, name: &str) -> Option<Self::Value>;
2427
}
28+
29+
pub trait PreDefineMethods<'tcx>: Backend<'tcx> {
30+
fn predefine_static(
31+
&self,
32+
def_id: DefId,
33+
linkage: Linkage,
34+
visibility: Visibility,
35+
symbol_name: &str,
36+
);
37+
fn predefine_fn(
38+
&self,
39+
instance: Instance<'tcx>,
40+
linkage: Linkage,
41+
visibility: Visibility,
42+
symbol_name: &str,
43+
);
44+
}

src/librustc_codegen_llvm/interfaces/misc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
use super::backend::Backend;
1212
use libc::c_uint;
13+
use monomorphize::partitioning::CodegenUnit;
14+
use rustc::mir::mono::Stats;
1315
use rustc::session::Session;
1416
use rustc::ty::{self, Instance, Ty};
1517
use rustc::util::nodemap::FxHashMap;
1618
use std::cell::RefCell;
19+
use std::sync::Arc;
1720

1821
pub trait MiscMethods<'tcx>: Backend<'tcx> {
1922
fn vtables(
@@ -26,4 +29,6 @@ pub trait MiscMethods<'tcx>: Backend<'tcx> {
2629
fn eh_personality(&self) -> Self::Value;
2730
fn eh_unwind_resume(&self) -> Self::Value;
2831
fn sess(&self) -> &Session;
32+
fn stats(&self) -> &RefCell<Stats>;
33+
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>>;
2934
}

src/librustc_codegen_llvm/interfaces/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use self::backend::{Backend, BackendTypes};
2626
pub use self::builder::BuilderMethods;
2727
pub use self::consts::ConstMethods;
2828
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};
29-
pub use self::declare::DeclareMethods;
29+
pub use self::declare::{DeclareMethods, PreDefineMethods};
3030
pub use self::intrinsic::{IntrinsicCallMethods, IntrinsicDeclarationMethods};
3131
pub use self::misc::MiscMethods;
3232
pub use self::statics::StaticMethods;
@@ -47,6 +47,7 @@ pub trait CodegenMethods<'tcx>:
4747
+ IntrinsicDeclarationMethods<'tcx>
4848
+ DeclareMethods<'tcx>
4949
+ AsmMethods<'tcx>
50+
+ PreDefineMethods<'tcx>
5051
{
5152
}
5253

@@ -61,6 +62,7 @@ impl<'tcx, T> CodegenMethods<'tcx> for T where
6162
+ IntrinsicDeclarationMethods<'tcx>
6263
+ DeclareMethods<'tcx>
6364
+ AsmMethods<'tcx>
65+
+ PreDefineMethods<'tcx>
6466
{}
6567

6668
pub trait HasCodegen<'tcx>: Backend<'tcx> {

0 commit comments

Comments
 (0)