Skip to content

Commit

Permalink
Refactor storage of LandingPads
Browse files Browse the repository at this point in the history
  • Loading branch information
pczarn committed Feb 8, 2016
1 parent 06266eb commit a9ab809
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/librustc_trans/trans/base.rs
Expand Up @@ -988,7 +988,7 @@ pub fn wants_msvc_seh(sess: &Session) -> bool {
}

pub fn avoid_invoke(bcx: Block) -> bool {
bcx.sess().no_landing_pads() || bcx.lpad.borrow().is_some()
bcx.sess().no_landing_pads() || bcx.lpad().is_some()
}

pub fn need_invoke(bcx: Block) -> bool {
Expand Down Expand Up @@ -1616,6 +1616,7 @@ pub fn new_fn_ctxt<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
param_substs: param_substs,
span: sp,
block_arena: block_arena,
lpad_arena: TypedArena::new(),
ccx: ccx,
debug_context: debug_context,
scopes: RefCell::new(Vec::new()),
Expand Down
9 changes: 3 additions & 6 deletions src/librustc_trans/trans/build.rs
Expand Up @@ -150,8 +150,7 @@ pub fn Invoke(cx: Block,
cx.val_to_string(fn_),
args.iter().map(|a| cx.val_to_string(*a)).collect::<Vec<String>>().join(", "));
debug_loc.apply(cx.fcx);
let lpad = cx.lpad.borrow();
let bundle = lpad.as_ref().and_then(|b| b.bundle());
let bundle = cx.lpad().and_then(|b| b.bundle());
B(cx).invoke(fn_, args, then, catch, bundle, attributes)
}

Expand Down Expand Up @@ -916,8 +915,7 @@ pub fn Call(cx: Block,
return _UndefReturn(cx, fn_);
}
debug_loc.apply(cx.fcx);
let lpad = cx.lpad.borrow();
let bundle = lpad.as_ref().and_then(|b| b.bundle());
let bundle = cx.lpad.get().and_then(|b| b.bundle());
B(cx).call(fn_, args, bundle, attributes)
}

Expand All @@ -932,8 +930,7 @@ pub fn CallWithConv(cx: Block,
return _UndefReturn(cx, fn_);
}
debug_loc.apply(cx.fcx);
let lpad = cx.lpad.borrow();
let bundle = lpad.as_ref().and_then(|b| b.bundle());
let bundle = cx.lpad.get().and_then(|b| b.bundle());
B(cx).call_with_conv(fn_, args, conv, bundle, attributes)
}

Expand Down
10 changes: 5 additions & 5 deletions src/librustc_trans/trans/cleanup.rs
Expand Up @@ -740,7 +740,7 @@ impl<'blk, 'tcx> CleanupHelperMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx
UnwindExit(val) => {
// Generate a block that will resume unwinding to the
// calling function
let bcx = self.new_block("resume", None);
let bcx = self.new_block("resume", None, None);
match val {
UnwindKind::LandingPad => {
let addr = self.landingpad_alloca.get()
Expand Down Expand Up @@ -830,7 +830,7 @@ impl<'blk, 'tcx> CleanupHelperMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx
let name = scope.block_name("clean");
debug!("generating cleanups for {}", name);

let bcx_in = self.new_block(&name[..], None);
let bcx_in = self.new_block(&name[..], None, None);
let exit_label = label.start(bcx_in);
let mut bcx_out = bcx_in;
let len = scope.cleanups.len();
Expand Down Expand Up @@ -873,7 +873,7 @@ impl<'blk, 'tcx> CleanupHelperMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx
Some(llbb) => return llbb,
None => {
let name = last_scope.block_name("unwind");
pad_bcx = self.new_block(&name[..], None);
pad_bcx = self.new_block(&name[..], None, None);
last_scope.cached_landing_pad = Some(pad_bcx.llbb);
}
}
Expand Down Expand Up @@ -1054,11 +1054,11 @@ impl EarlyExitLabel {
match *self {
UnwindExit(UnwindKind::CleanupPad(..)) => {
let pad = build::CleanupPad(bcx, None, &[]);
*bcx.lpad.borrow_mut() = Some(LandingPad::msvc(pad));
bcx.lpad.set(Some(bcx.fcx.lpad_arena.alloc(LandingPad::msvc(pad))));
UnwindExit(UnwindKind::CleanupPad(pad))
}
UnwindExit(UnwindKind::LandingPad) => {
*bcx.lpad.borrow_mut() = Some(LandingPad::gnu());
bcx.lpad.set(Some(bcx.fcx.lpad_arena.alloc(LandingPad::gnu())));
*self
}
label => label,
Expand Down
28 changes: 22 additions & 6 deletions src/librustc_trans/trans/common.rs
Expand Up @@ -367,6 +367,9 @@ pub struct FunctionContext<'a, 'tcx: 'a> {
// The arena that blocks are allocated from.
pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>,

// The arena that landing pads are allocated from.
pub lpad_arena: TypedArena<LandingPad>,

// This function's enclosing crate context.
pub ccx: &'a CrateContext<'a, 'tcx>,

Expand Down Expand Up @@ -431,28 +434,33 @@ impl<'a, 'tcx> FunctionContext<'a, 'tcx> {

pub fn new_block(&'a self,
name: &str,
opt_node_id: Option<ast::NodeId>)
opt_node_id: Option<ast::NodeId>,
landing_pad: Option<LandingPad>)
-> Block<'a, 'tcx> {
unsafe {
let name = CString::new(name).unwrap();
let llbb = llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(),
self.llfn,
name.as_ptr());
BlockS::new(llbb, opt_node_id, self)
let block = BlockS::new(llbb, opt_node_id, self);
if let Some(landing_pad) = landing_pad {
block.lpad.set(Some(self.lpad_arena.alloc(landing_pad)));
}
block
}
}

pub fn new_id_block(&'a self,
name: &str,
node_id: ast::NodeId)
-> Block<'a, 'tcx> {
self.new_block(name, Some(node_id))
self.new_block(name, Some(node_id), None)
}

pub fn new_temp_block(&'a self,
name: &str)
-> Block<'a, 'tcx> {
self.new_block(name, None)
self.new_block(name, None, None)
}

pub fn join_blocks(&'a self,
Expand Down Expand Up @@ -584,7 +592,7 @@ pub struct BlockS<'blk, 'tcx: 'blk> {

// If this block part of a landing pad, then this is `Some` indicating what
// kind of landing pad its in, otherwise this is none.
pub lpad: RefCell<Option<LandingPad>>,
pub lpad: Cell<Option<&'blk LandingPad>>,

// AST node-id associated with this block, if any. Used for
// debugging purposes only.
Expand All @@ -606,7 +614,7 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
llbb: llbb,
terminated: Cell::new(false),
unreachable: Cell::new(false),
lpad: RefCell::new(None),
lpad: Cell::new(None),
opt_node_id: opt_node_id,
fcx: fcx
})
Expand All @@ -623,6 +631,10 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
}
pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() }

pub fn lpad(&self) -> Option<&'blk LandingPad> {
self.lpad.get()
}

pub fn mir(&self) -> &'blk Mir<'tcx> {
self.fcx.mir()
}
Expand Down Expand Up @@ -747,6 +759,10 @@ impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> {
self.bcx.llbb
}

pub fn lpad(&self) -> Option<&'blk LandingPad> {
self.bcx.lpad()
}

pub fn mir(&self) -> &'blk Mir<'tcx> {
self.bcx.mir()
}
Expand Down
25 changes: 16 additions & 9 deletions src/librustc_trans/trans/mir/block.rs
Expand Up @@ -119,13 +119,16 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
if let Some(unwind) = unwind {
let uwbcx = self.bcx(unwind);
let unwind = self.make_landing_pad(uwbcx);
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.invoke(drop_fn,
&[llvalue],
self.llblock(target),
unwind.llbb(),
bundle,
None);
} else {
bcx.call(drop_fn, &[llvalue], None);
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.call(drop_fn, &[llvalue], bundle, None);
bcx.br(self.llblock(target));
}
}
Expand Down Expand Up @@ -187,24 +190,28 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
let cleanup = self.bcx(cleanup);
let landingpad = self.make_landing_pad(cleanup);
let unreachable_blk = self.unreachable_block();
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.invoke(callee.immediate(),
&llargs[..],
unreachable_blk.llbb,
landingpad.llbb(),
bundle,
Some(attrs));
},
(false, false, &Some(cleanup), &Some((_, success))) => {
let cleanup = self.bcx(cleanup);
let landingpad = self.make_landing_pad(cleanup);
let (target, postinvoke) = if must_copy_dest {
(bcx.fcx().new_block("", None), Some(self.bcx(success)))
(bcx.fcx().new_block("", None, None).build(), Some(self.bcx(success)))
} else {
(self.bcx(success), None)
};
let bundle = bcx.lpad().and_then(|b| b.bundle());
let invokeret = bcx.invoke(callee.immediate(),
&llargs[..],
target.llbb(),
landingpad.llbb(),
bundle,
Some(attrs));
if let Some(postinvoketarget) = postinvoke {
// We translate the copy into a temporary block. The temporary block is
Expand Down Expand Up @@ -240,7 +247,8 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
}
},
(false, _, _, &None) => {
bcx.call(callee.immediate(), &llargs[..], Some(attrs));
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.call(callee.immediate(), &llargs[..], bundle, Some(attrs));
bcx.unreachable();
}
(false, _, _, &Some((_, target))) => {
Expand Down Expand Up @@ -301,26 +309,25 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
cleanup: BlockAndBuilder<'bcx, 'tcx>)
-> BlockAndBuilder<'bcx, 'tcx>
{
let cleanup_llbb = cleanup.llbb();
let bcx = cleanup.map_block(|cleanup| {
cleanup.fcx.new_block("cleanup", None)
// FIXME(#30941) this doesn't handle msvc-style exceptions
cleanup.fcx.new_block("cleanup", None, Some(LandingPad::gnu()))
});
// FIXME(#30941) this doesn't handle msvc-style exceptions
*bcx.lpad.borrow_mut() = Some(LandingPad::gnu());
let bcx = bcx.build();
let ccx = bcx.ccx();
let llpersonality = bcx.fcx().eh_personality();
let llretty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false);
let llretval = bcx.landing_pad(llretty, llpersonality, 1, bcx.fcx().llfn);
bcx.set_cleanup(llretval);
let slot = self.get_personality_slot(&bcx);
bcx.store(llretval, slot);
bcx.br(cleanup.llbb());
bcx.br(cleanup_llbb);
bcx
}

fn unreachable_block(&mut self) -> Block<'bcx, 'tcx> {
self.unreachable_block.unwrap_or_else(|| {
let bl = self.fcx.new_block("unreachable", None);
let bl = self.fcx.new_block("unreachable", None, None);
bl.build().unreachable();
self.unreachable_block = Some(bl);
bl
Expand Down
11 changes: 6 additions & 5 deletions src/librustc_trans/trans/mir/mod.rs
Expand Up @@ -114,12 +114,13 @@ pub fn trans_mir<'bcx, 'tcx>(bcx: BlockAndBuilder<'bcx, 'tcx>) {
let block_bcxs: Vec<Block<'bcx,'tcx>> =
mir_blocks.iter()
.map(|&bb|{
let bcx = fcx.new_block(&format!("{:?}", bb), None);
// FIXME(#30941) this doesn't handle msvc-style exceptions
if mir.basic_block_data(bb).is_cleanup {
*bcx.lpad.borrow_mut() = Some(LandingPad::gnu())
}
bcx
let lpad = if mir.basic_block_data(bb).is_cleanup {
Some(LandingPad::gnu())
} else {
None
};
fcx.new_block(&format!("{:?}", bb), None, lpad)
})
.collect();

Expand Down
6 changes: 4 additions & 2 deletions src/librustc_trans/trans/mir/rvalue.rs
Expand Up @@ -497,10 +497,12 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
if input_ty == tcx.types.f32 {
let lllhs = bcx.fpext(lhs, f64t);
let llrhs = bcx.fpext(rhs, f64t);
let llres = bcx.call(llfn, &[lllhs, llrhs], None);
let bundle = bcx.lpad().and_then(|b| b.bundle());
let llres = bcx.call(llfn, &[lllhs, llrhs], bundle, None);
bcx.fptrunc(llres, Type::f32(bcx.ccx()))
} else {
bcx.call(llfn, &[lhs, rhs], None)
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.call(llfn, &[lhs, rhs], bundle, None)
}
} else {
bcx.frem(lhs, rhs)
Expand Down

0 comments on commit a9ab809

Please sign in to comment.