Skip to content

Commit 937e8da

Browse files
Purge FunctionContext
1 parent 1be170b commit 937e8da

File tree

12 files changed

+82
-145
lines changed

12 files changed

+82
-145
lines changed

src/librustc_trans/base.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use callee::{Callee};
5454
use common::{C_bool, C_bytes_in_context, C_i32, C_uint};
5555
use collector::{self, TransItemCollectionMode};
5656
use common::{C_struct_in_context, C_u64, C_undef};
57-
use common::{CrateContext, FunctionContext};
57+
use common::CrateContext;
5858
use common::{fulfill_obligation};
5959
use common::{type_is_zero_size, val_ty};
6060
use common;
@@ -590,32 +590,30 @@ pub fn trans_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, instance: Instance
590590

591591
let fn_ty = FnType::new(ccx, abi, &sig, &[]);
592592

593-
let fcx = FunctionContext::new(ccx, lldecl);
594593
let mir = ccx.tcx().item_mir(instance.def);
595-
mir::trans_mir(&fcx, fn_ty, &mir, instance, &sig, abi);
594+
mir::trans_mir(ccx, lldecl, fn_ty, &mir, instance, &sig, abi);
596595
}
597596

598597
pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
599598
def_id: DefId,
600599
substs: &'tcx Substs<'tcx>,
601600
disr: Disr,
602-
llfndecl: ValueRef) {
603-
attributes::inline(llfndecl, attributes::InlineAttr::Hint);
604-
attributes::set_frame_pointer_elimination(ccx, llfndecl);
601+
llfn: ValueRef) {
602+
attributes::inline(llfn, attributes::InlineAttr::Hint);
603+
attributes::set_frame_pointer_elimination(ccx, llfn);
605604

606605
let ctor_ty = ccx.tcx().item_type(def_id);
607606
let ctor_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &ctor_ty);
608607

609608
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&ctor_ty.fn_sig());
610609
let fn_ty = FnType::new(ccx, Abi::Rust, &sig, &[]);
611610

612-
let fcx = FunctionContext::new(ccx, llfndecl);
613-
let bcx = fcx.get_entry_block();
611+
let bcx = Builder::entry_block(ccx, llfn);
614612
if !fn_ty.ret.is_ignore() {
615613
// But if there are no nested returns, we skip the indirection
616614
// and have a single retslot
617615
let dest = if fn_ty.ret.is_indirect() {
618-
get_param(fcx.llfn, 0)
616+
get_param(llfn, 0)
619617
} else {
620618
// We create an alloca to hold a pointer of type `ret.original_ty`
621619
// which will hold the pointer to the right alloca which has the

src/librustc_trans/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ fn noname() -> *const c_char {
5050
}
5151

5252
impl<'a, 'tcx> Builder<'a, 'tcx> {
53+
pub fn entry_block(ccx: &'a CrateContext<'a, 'tcx>, llfn: ValueRef) -> Self {
54+
Builder::new_block(ccx, llfn, "entry-block")
55+
}
56+
5357
pub fn new_block<'b>(ccx: &'a CrateContext<'a, 'tcx>, llfn: ValueRef, name: &'b str) -> Self {
5458
let builder = Builder::with_ccx(ccx);
5559
let llbb = unsafe {

src/librustc_trans/callee.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use rustc::traits;
2323
use abi::{Abi, FnType};
2424
use attributes;
2525
use base;
26-
use common::{
27-
self, CrateContext, FunctionContext, SharedCrateContext
28-
};
26+
use builder::Builder;
27+
use common::{self, CrateContext, SharedCrateContext};
28+
use cleanup::CleanupScope;
2929
use adt::MaybeSizedValue;
3030
use consts;
3131
use declare;
@@ -329,8 +329,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
329329
attributes::set_frame_pointer_elimination(ccx, lloncefn);
330330

331331
let orig_fn_ty = fn_ty;
332-
let fcx = FunctionContext::new(ccx, lloncefn);
333-
let mut bcx = fcx.get_entry_block();
332+
let mut bcx = Builder::entry_block(ccx, lloncefn);
334333

335334
let callee = Callee {
336335
data: Fn(llreffn),
@@ -339,7 +338,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
339338

340339
// the first argument (`self`) will be the (by value) closure env.
341340

342-
let mut llargs = get_params(fcx.llfn);
341+
let mut llargs = get_params(lloncefn);
343342
let fn_ret = callee.ty.fn_ret();
344343
let fn_ty = callee.direct_fn_type(bcx.ccx, &[]);
345344
let self_idx = fn_ty.ret.is_indirect() as usize;
@@ -364,7 +363,9 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
364363

365364
// Call the by-ref closure body with `self` in a cleanup scope,
366365
// to drop `self` when the body returns, or in case it unwinds.
367-
let self_scope = fcx.schedule_drop_mem(&bcx, MaybeSizedValue::sized(llenv), closure_ty);
366+
let self_scope = CleanupScope::schedule_drop_mem(
367+
&bcx, MaybeSizedValue::sized(llenv), closure_ty
368+
);
368369

369370
let llfn = callee.reify(bcx.ccx);
370371
let llret;
@@ -488,10 +489,9 @@ fn trans_fn_pointer_shim<'a, 'tcx>(
488489
let llfn = declare::define_internal_fn(ccx, &function_name, tuple_fn_ty);
489490
attributes::set_frame_pointer_elimination(ccx, llfn);
490491
//
491-
let fcx = FunctionContext::new(ccx, llfn);
492-
let bcx = fcx.get_entry_block();
492+
let bcx = Builder::entry_block(ccx, llfn);
493493

494-
let mut llargs = get_params(fcx.llfn);
494+
let mut llargs = get_params(llfn);
495495

496496
let self_arg = llargs.remove(fn_ty.ret.is_indirect() as usize);
497497
let llfnpointer = llfnpointer.unwrap_or_else(|| {

src/librustc_trans/cleanup.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use llvm::BasicBlockRef;
2222
use base;
2323
use adt::MaybeSizedValue;
2424
use builder::Builder;
25-
use common::{FunctionContext, Funclet};
25+
use common::Funclet;
2626
use glue;
2727
use type_::Type;
2828
use rustc::ty::Ty;
@@ -93,12 +93,12 @@ impl<'tcx> DropValue<'tcx> {
9393
}
9494
}
9595

96-
impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
96+
impl<'a, 'tcx> CleanupScope<'tcx> {
9797
/// Schedules a (deep) drop of `val`, which is a pointer to an instance of `ty`
9898
pub fn schedule_drop_mem(
99-
&self, bcx: &Builder<'a, 'tcx>, val: MaybeSizedValue, ty: Ty<'tcx>
99+
bcx: &Builder<'a, 'tcx>, val: MaybeSizedValue, ty: Ty<'tcx>
100100
) -> CleanupScope<'tcx> {
101-
if !self.ccx.shared().type_needs_drop(ty) { return CleanupScope::noop(); }
101+
if !bcx.ccx.shared().type_needs_drop(ty) { return CleanupScope::noop(); }
102102
let drop = DropValue {
103103
val: val,
104104
ty: ty,
@@ -114,11 +114,11 @@ impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
114114
/// and dropping the contents associated with that variant
115115
/// *without* executing any associated drop implementation.
116116
pub fn schedule_drop_adt_contents(
117-
&self, bcx: &Builder<'a, 'tcx>, val: MaybeSizedValue, ty: Ty<'tcx>
117+
bcx: &Builder<'a, 'tcx>, val: MaybeSizedValue, ty: Ty<'tcx>
118118
) -> CleanupScope<'tcx> {
119119
// `if` below could be "!contents_needs_drop"; skipping drop
120120
// is just an optimization, so sound to be conservative.
121-
if !self.ccx.shared().type_needs_drop(ty) { return CleanupScope::noop(); }
121+
if !bcx.ccx.shared().type_needs_drop(ty) { return CleanupScope::noop(); }
122122

123123
let drop = DropValue {
124124
val: val,
@@ -128,10 +128,8 @@ impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
128128

129129
CleanupScope::new(bcx, drop)
130130
}
131-
}
132131

133-
impl<'tcx> CleanupScope<'tcx> {
134-
fn new<'a>(bcx: &Builder<'a, 'tcx>, drop_val: DropValue<'tcx>) -> CleanupScope<'tcx> {
132+
fn new(bcx: &Builder<'a, 'tcx>, drop_val: DropValue<'tcx>) -> CleanupScope<'tcx> {
135133
CleanupScope {
136134
cleanup: Some(drop_val),
137135
landing_pad: if !bcx.ccx.sess().no_landing_pads() {
@@ -149,7 +147,7 @@ impl<'tcx> CleanupScope<'tcx> {
149147
}
150148
}
151149

152-
pub fn trans<'a>(self, bcx: &'a Builder<'a, 'tcx>) {
150+
pub fn trans(self, bcx: &'a Builder<'a, 'tcx>) {
153151
if let Some(cleanup) = self.cleanup {
154152
cleanup.trans(None, &bcx);
155153
}

src/librustc_trans/common.rs

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! Code that is useful in various trans modules.
1414
1515
use llvm;
16-
use llvm::{ValueRef, BasicBlockRef, ContextRef, TypeKind};
16+
use llvm::{ValueRef, ContextRef, TypeKind};
1717
use llvm::{True, False, Bool, OperandBundleDef};
1818
use rustc::hir::def::Def;
1919
use rustc::hir::def_id::DefId;
@@ -36,7 +36,6 @@ use rustc::hir;
3636
use libc::{c_uint, c_char};
3737
use std::borrow::Cow;
3838
use std::iter;
39-
use std::ffi::CString;
4039

4140
use syntax::ast;
4241
use syntax::symbol::{Symbol, InternedString};
@@ -219,71 +218,6 @@ impl<'a, 'tcx> VariantInfo<'tcx> {
219218
}
220219
}
221220

222-
// Function context. Every LLVM function we create will have one of these.
223-
pub struct FunctionContext<'a, 'tcx: 'a> {
224-
// The ValueRef returned from a call to llvm::LLVMAddFunction; the
225-
// address of the first instruction in the sequence of
226-
// instructions for this function that will go in the .text
227-
// section of the executable we're generating.
228-
pub llfn: ValueRef,
229-
230-
// A marker for the place where we want to insert the function's static
231-
// allocas, so that LLVM will coalesce them into a single alloca call.
232-
alloca_insert_pt: Option<ValueRef>,
233-
234-
// This function's enclosing crate context.
235-
pub ccx: &'a CrateContext<'a, 'tcx>,
236-
}
237-
238-
impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
239-
/// Create a function context for the given function.
240-
/// Call FunctionContext::get_entry_block for the first entry block.
241-
pub fn new(ccx: &'a CrateContext<'a, 'tcx>, llfndecl: ValueRef) -> FunctionContext<'a, 'tcx> {
242-
let mut fcx = FunctionContext {
243-
llfn: llfndecl,
244-
alloca_insert_pt: None,
245-
ccx: ccx,
246-
};
247-
248-
let entry_bcx = Builder::new_block(fcx.ccx, fcx.llfn, "entry-block");
249-
entry_bcx.position_at_start(entry_bcx.llbb());
250-
// Use a dummy instruction as the insertion point for all allocas.
251-
// This is later removed in the drop of FunctionContext.
252-
fcx.alloca_insert_pt = Some(entry_bcx.load(C_null(Type::i8p(ccx))));
253-
254-
fcx
255-
}
256-
257-
pub fn new_block(&self, name: &str) -> BasicBlockRef {
258-
unsafe {
259-
let name = CString::new(name).unwrap();
260-
llvm::LLVMAppendBasicBlockInContext(
261-
self.ccx.llcx(),
262-
self.llfn,
263-
name.as_ptr()
264-
)
265-
}
266-
}
267-
268-
pub fn build_new_block(&self, name: &str) -> Builder<'a, 'tcx> {
269-
Builder::new_block(self.ccx, self.llfn, name)
270-
}
271-
272-
pub fn get_entry_block(&'a self) -> Builder<'a, 'tcx> {
273-
let builder = Builder::with_ccx(self.ccx);
274-
builder.position_at_end(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn) });
275-
builder
276-
}
277-
}
278-
279-
impl<'a, 'tcx> Drop for FunctionContext<'a, 'tcx> {
280-
fn drop(&mut self) {
281-
unsafe {
282-
llvm::LLVMInstructionEraseFromParent(self.alloca_insert_pt.unwrap());
283-
}
284-
}
285-
}
286-
287221
/// A structure representing an active landing pad for the duration of a basic
288222
/// block.
289223
///

src/librustc_trans/debuginfo/create_scope_map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::utils::{DIB, span_start};
1414

1515
use llvm;
1616
use llvm::debuginfo::{DIScope, DISubprogram};
17-
use common::{CrateContext, FunctionContext};
17+
use common::CrateContext;
1818
use rustc::mir::{Mir, VisibilityScope};
1919

2020
use libc::c_uint;
@@ -44,7 +44,7 @@ impl MirDebugScope {
4444

4545
/// Produce DIScope DIEs for each MIR Scope which has variables defined in it.
4646
/// If debuginfo is disabled, the returned vector is empty.
47-
pub fn create_mir_scopes(fcx: &FunctionContext, mir: &Mir, debug_context: &FunctionDebugContext)
47+
pub fn create_mir_scopes(ccx: &CrateContext, mir: &Mir, debug_context: &FunctionDebugContext)
4848
-> IndexVec<VisibilityScope, MirDebugScope> {
4949
let null_scope = MirDebugScope {
5050
scope_metadata: ptr::null_mut(),
@@ -71,7 +71,7 @@ pub fn create_mir_scopes(fcx: &FunctionContext, mir: &Mir, debug_context: &Funct
7171
// Instantiate all scopes.
7272
for idx in 0..mir.visibility_scopes.len() {
7373
let scope = VisibilityScope::new(idx);
74-
make_mir_scope(fcx.ccx, &mir, &has_variables, fn_metadata, scope, &mut scopes);
74+
make_mir_scope(ccx, &mir, &has_variables, fn_metadata, scope, &mut scopes);
7575
}
7676

7777
scopes

src/librustc_trans/debuginfo/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
//!
4646
//! All private state used by the module is stored within either the
4747
//! CrateDebugContext struct (owned by the CrateContext) or the
48-
//! FunctionDebugContext (owned by the FunctionContext).
48+
//! FunctionDebugContext (owned by the MirContext).
4949
//!
5050
//! This file consists of three conceptual sections:
5151
//! 1. The public interface of the module

src/librustc_trans/glue.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc::ty::subst::Kind;
2525
use adt::{self, MaybeSizedValue};
2626
use base::*;
2727
use callee::Callee;
28+
use cleanup::CleanupScope;
2829
use common::*;
2930
use machine::*;
3031
use monomorphize;
@@ -34,7 +35,6 @@ use type_of::{type_of, sizing_type_of, align_of};
3435
use type_::Type;
3536
use value::Value;
3637
use Disr;
37-
use cleanup::CleanupScope;
3838
use builder::Builder;
3939

4040
use syntax_pos::DUMMY_SP;
@@ -174,8 +174,7 @@ pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, g: DropGlueKi
174174
assert_eq!(g.ty(), get_drop_glue_type(ccx.shared(), g.ty()));
175175
let (llfn, _) = ccx.drop_glues().borrow().get(&g).unwrap().clone();
176176

177-
let fcx = FunctionContext::new(ccx, llfn);
178-
let mut bcx = fcx.get_entry_block();
177+
let mut bcx = Builder::entry_block(ccx, llfn);
179178

180179
ccx.stats().n_glues_created.set(ccx.stats().n_glues_created.get() + 1);
181180
// All glue functions take values passed *by alias*; this is a
@@ -246,7 +245,7 @@ pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, g: DropGlueKi
246245
// Issue #23611: schedule cleanup of contents, re-inspecting the
247246
// discriminant (if any) in case of variant swap in drop code.
248247
let contents_scope = if !shallow_drop {
249-
fcx.schedule_drop_adt_contents(&bcx, ptr, t)
248+
CleanupScope::schedule_drop_adt_contents(&bcx, ptr, t)
250249
} else {
251250
CleanupScope::noop()
252251
};

0 commit comments

Comments
 (0)