Skip to content

Commit

Permalink
Cleanup based on review by @nagisa
Browse files Browse the repository at this point in the history
* We don't have SEH-based unwinding yet.
  For this reason we don't need operand bundles in MIR trans.
* Refactored some uses of fcx.
* Refactored some calls to `with_block`.
  • Loading branch information
pczarn committed Feb 8, 2016
1 parent a9ab809 commit 38fa06b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 67 deletions.
6 changes: 3 additions & 3 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, None);
let bcx = self.new_block("resume", 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, None);
let bcx_in = self.new_block(&name[..], 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, None);
pad_bcx = self.new_block(&name[..], None);
last_scope.cached_landing_pad = Some(pad_bcx.llbb);
}
}
Expand Down
17 changes: 4 additions & 13 deletions src/librustc_trans/trans/common.rs
Expand Up @@ -434,33 +434,28 @@ impl<'a, 'tcx> FunctionContext<'a, 'tcx> {

pub fn new_block(&'a self,
name: &str,
opt_node_id: Option<ast::NodeId>,
landing_pad: Option<LandingPad>)
opt_node_id: Option<ast::NodeId>)
-> Block<'a, 'tcx> {
unsafe {
let name = CString::new(name).unwrap();
let llbb = llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(),
self.llfn,
name.as_ptr());
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
BlockS::new(llbb, opt_node_id, self)
}
}

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

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

pub fn join_blocks(&'a self,
Expand Down Expand Up @@ -759,10 +754,6 @@ 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
47 changes: 18 additions & 29 deletions src/librustc_trans/trans/mir/block.rs
Expand Up @@ -16,7 +16,7 @@ use trans::adt;
use trans::attributes;
use trans::base;
use trans::build;
use trans::common::{self, Block, BlockAndBuilder, LandingPad};
use trans::common::{self, Block, BlockAndBuilder};
use trans::debuginfo::DebugLoc;
use trans::Disr;
use trans::foreign;
Expand Down Expand Up @@ -119,16 +119,14 @@ 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,
None);
} else {
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.call(drop_fn, &[llvalue], bundle, None);
bcx.call(drop_fn, &[llvalue], None, None);
bcx.br(self.llblock(target));
}
}
Expand Down Expand Up @@ -190,28 +188,26 @@ 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,
None,
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, None).build(), Some(self.bcx(success)))
(bcx.fcx().new_block("", 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,
None,
Some(attrs));
if let Some(postinvoketarget) = postinvoke {
// We translate the copy into a temporary block. The temporary block is
Expand Down Expand Up @@ -247,15 +243,13 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
}
},
(false, _, _, &None) => {
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.call(callee.immediate(), &llargs[..], bundle, Some(attrs));
bcx.call(callee.immediate(), &llargs[..], None, Some(attrs));
bcx.unreachable();
}
(false, _, _, &Some((_, target))) => {
let bundle = bcx.lpad().and_then(|b| b.bundle());
let llret = bcx.call(callee.immediate(),
&llargs[..],
bundle,
None,
Some(attrs));
if must_copy_dest {
let (ret_dest, ret_ty) = ret_dest_ty
Expand Down Expand Up @@ -294,40 +288,35 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
slot
} else {
let llretty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false);
let slot = bcx.with_block(|bcx| {
base::alloca(bcx, llretty, "personalityslot")
});
self.llpersonalityslot = Some(slot);
bcx.with_block(|bcx| {
let slot = base::alloca(bcx, llretty, "personalityslot");
self.llpersonalityslot = Some(slot);
base::call_lifetime_start(bcx, slot);
});
slot
slot
})
}
}

fn make_landing_pad(&mut self,
cleanup: BlockAndBuilder<'bcx, 'tcx>)
-> BlockAndBuilder<'bcx, 'tcx>
{
let cleanup_llbb = cleanup.llbb();
let bcx = cleanup.map_block(|cleanup| {
// 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
let bcx = self.fcx.new_block("cleanup", None).build();
let ccx = bcx.ccx();
let llpersonality = bcx.fcx().eh_personality();
let llpersonality = self.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);
let llretval = bcx.landing_pad(llretty, llpersonality, 1, self.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, None);
let bl = self.fcx.new_block("unreachable", None);
bl.build().unreachable();
self.unreachable_block = Some(bl);
bl
Expand Down
27 changes: 9 additions & 18 deletions src/librustc_trans/trans/mir/mod.rs
Expand Up @@ -13,7 +13,7 @@ use llvm::{self, ValueRef};
use rustc::mir::repr as mir;
use rustc::mir::tcx::LvalueTy;
use trans::base;
use trans::common::{self, Block, BlockAndBuilder, LandingPad};
use trans::common::{self, Block, BlockAndBuilder};
use trans::expr;
use trans::type_of;

Expand Down Expand Up @@ -115,12 +115,7 @@ pub fn trans_mir<'bcx, 'tcx>(bcx: BlockAndBuilder<'bcx, 'tcx>) {
mir_blocks.iter()
.map(|&bb|{
// FIXME(#30941) this doesn't handle msvc-style exceptions
let lpad = if mir.basic_block_data(bb).is_cleanup {
Some(LandingPad::gnu())
} else {
None
};
fcx.new_block(&format!("{:?}", bb), None, lpad)
fcx.new_block(&format!("{:?}", bb), None)
})
.collect();

Expand Down Expand Up @@ -175,11 +170,9 @@ fn arg_value_refs<'bcx, 'tcx>(bcx: &BlockAndBuilder<'bcx, 'tcx>,
let lldata = llvm::get_param(fcx.llfn, idx);
let llextra = llvm::get_param(fcx.llfn, idx + 1);
idx += 2;
let lltemp = bcx.with_block(|bcx| {
base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index))
});
let (dataptr, meta) = bcx.with_block(|bcx| {
(expr::get_dataptr(bcx, lltemp), expr::get_meta(bcx, lltemp))
let (lltemp, dataptr, meta) = bcx.with_block(|bcx| {
let lltemp = base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index));
(lltemp, expr::get_dataptr(bcx, lltemp), expr::get_meta(bcx, lltemp))
});
bcx.store(lldata, dataptr);
bcx.store(llextra, meta);
Expand All @@ -189,13 +182,11 @@ fn arg_value_refs<'bcx, 'tcx>(bcx: &BlockAndBuilder<'bcx, 'tcx>,
// temporary and store it there
let llarg = llvm::get_param(fcx.llfn, idx);
idx += 1;
let lltemp = bcx.with_block(|bcx| {
base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index))
});
bcx.with_block(|bcx| {
base::store_ty(bcx, llarg, lltemp, arg_ty)
});
lltemp
let lltemp = base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index));
base::store_ty(bcx, llarg, lltemp, arg_ty);
lltemp
})
};
LvalueRef::new_sized(llval, LvalueTy::from_ty(arg_ty))
})
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_trans/trans/mir/rvalue.rs
Expand Up @@ -497,12 +497,10 @@ 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 bundle = bcx.lpad().and_then(|b| b.bundle());
let llres = bcx.call(llfn, &[lllhs, llrhs], bundle, None);
let llres = bcx.call(llfn, &[lllhs, llrhs], None, None);
bcx.fptrunc(llres, Type::f32(bcx.ccx()))
} else {
let bundle = bcx.lpad().and_then(|b| b.bundle());
bcx.call(llfn, &[lhs, rhs], bundle, None)
bcx.call(llfn, &[lhs, rhs], None, None)
}
} else {
bcx.frem(lhs, rhs)
Expand Down

0 comments on commit 38fa06b

Please sign in to comment.