2015-10-21 16:42:25 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-03-06 05:23:20 -06:00
|
|
|
use llvm::{self, BasicBlockRef, ValueRef, OperandBundleDef};
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty;
|
2015-11-19 09:37:34 -06:00
|
|
|
use rustc::mir::repr as mir;
|
2016-04-04 02:21:27 -05:00
|
|
|
use abi::{Abi, FnType, ArgType};
|
2016-03-22 12:23:36 -05:00
|
|
|
use adt;
|
|
|
|
use base;
|
|
|
|
use build;
|
|
|
|
use callee::{Callee, CalleeData, Fn, Intrinsic, NamedTupleConstructor, Virtual};
|
2016-04-07 22:37:56 -05:00
|
|
|
use common::{self, type_is_fat_ptr, Block, BlockAndBuilder, C_undef};
|
2016-03-22 12:23:36 -05:00
|
|
|
use debuginfo::DebugLoc;
|
|
|
|
use Disr;
|
|
|
|
use machine::{llalign_of_min, llbitsize_of_real};
|
|
|
|
use meth;
|
|
|
|
use type_of;
|
|
|
|
use glue;
|
|
|
|
use type_::Type;
|
2015-10-21 16:42:25 -05:00
|
|
|
|
2016-04-04 02:21:27 -05:00
|
|
|
use super::{MirContext, TempRef, drop};
|
2016-03-09 06:20:22 -06:00
|
|
|
use super::lvalue::{LvalueRef, load_fat_ptr};
|
2016-03-08 06:40:37 -06:00
|
|
|
use super::operand::OperandRef;
|
2016-03-06 05:23:20 -06:00
|
|
|
use super::operand::OperandValue::{self, FatPtr, Immediate, Ref};
|
2015-10-21 16:42:25 -05:00
|
|
|
|
|
|
|
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
|
|
|
|
pub fn trans_block(&mut self, bb: mir::BasicBlock) {
|
|
|
|
debug!("trans_block({:?})", bb);
|
|
|
|
|
|
|
|
let mut bcx = self.bcx(bb);
|
2016-03-08 06:38:13 -06:00
|
|
|
let mir = self.mir.clone();
|
|
|
|
let data = mir.basic_block_data(bb);
|
2015-10-21 16:42:25 -05:00
|
|
|
|
2016-02-11 14:57:09 -06:00
|
|
|
// MSVC SEH bits
|
|
|
|
let (cleanup_pad, cleanup_bundle) = if let Some((cp, cb)) = self.make_cleanup_pad(bb) {
|
|
|
|
(Some(cp), Some(cb))
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
|
|
|
let funclet_br = |bcx: BlockAndBuilder, llbb: BasicBlockRef| if let Some(cp) = cleanup_pad {
|
|
|
|
bcx.cleanup_ret(cp, Some(llbb));
|
|
|
|
} else {
|
|
|
|
bcx.br(llbb);
|
|
|
|
};
|
|
|
|
|
2015-10-21 16:42:25 -05:00
|
|
|
for statement in &data.statements {
|
|
|
|
bcx = self.trans_statement(bcx, statement);
|
|
|
|
}
|
|
|
|
|
2015-12-18 16:44:32 -06:00
|
|
|
debug!("trans_block: terminator: {:?}", data.terminator());
|
2015-10-21 16:42:25 -05:00
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
match data.terminator().kind {
|
|
|
|
mir::TerminatorKind::Resume => {
|
2016-02-11 14:57:09 -06:00
|
|
|
if let Some(cleanup_pad) = cleanup_pad {
|
|
|
|
bcx.cleanup_ret(cleanup_pad, None);
|
|
|
|
} else {
|
|
|
|
let ps = self.get_personality_slot(&bcx);
|
|
|
|
let lp = bcx.load(ps);
|
|
|
|
bcx.with_block(|bcx| {
|
|
|
|
base::call_lifetime_end(bcx, ps);
|
|
|
|
base::trans_unwind_resume(bcx, lp);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
mir::TerminatorKind::Goto { target } => {
|
2016-02-11 14:57:09 -06:00
|
|
|
funclet_br(bcx, self.llblock(target));
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
mir::TerminatorKind::If { ref cond, targets: (true_bb, false_bb) } => {
|
2016-02-01 04:04:46 -06:00
|
|
|
let cond = self.trans_operand(&bcx, cond);
|
2015-10-21 16:42:25 -05:00
|
|
|
let lltrue = self.llblock(true_bb);
|
|
|
|
let llfalse = self.llblock(false_bb);
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx.cond_br(cond.immediate(), lltrue, llfalse);
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
mir::TerminatorKind::Switch { ref discr, ref adt_def, ref targets } => {
|
2016-02-01 04:04:46 -06:00
|
|
|
let discr_lvalue = self.trans_lvalue(&bcx, discr);
|
2016-01-13 19:22:02 -06:00
|
|
|
let ty = discr_lvalue.ty.to_ty(bcx.tcx());
|
|
|
|
let repr = adt::represent_type(bcx.ccx(), ty);
|
2016-02-01 04:04:46 -06:00
|
|
|
let discr = bcx.with_block(|bcx|
|
|
|
|
adt::trans_get_discr(bcx, &repr, discr_lvalue.llval, None, true)
|
|
|
|
);
|
2015-12-11 12:19:19 -06:00
|
|
|
|
|
|
|
// The else branch of the Switch can't be hit, so branch to an unreachable
|
|
|
|
// instruction so LLVM knows that
|
2015-12-19 08:47:52 -06:00
|
|
|
let unreachable_blk = self.unreachable_block();
|
2016-02-01 04:04:46 -06:00
|
|
|
let switch = bcx.switch(discr, unreachable_blk.llbb, targets.len());
|
2015-12-11 12:19:19 -06:00
|
|
|
assert_eq!(adt_def.variants.len(), targets.len());
|
|
|
|
for (adt_variant, target) in adt_def.variants.iter().zip(targets) {
|
2016-02-01 04:04:46 -06:00
|
|
|
let llval = bcx.with_block(|bcx|
|
2016-02-09 14:24:11 -06:00
|
|
|
adt::trans_case(bcx, &repr, Disr::from(adt_variant.disr_val))
|
2016-02-01 04:04:46 -06:00
|
|
|
);
|
2015-12-11 12:19:19 -06:00
|
|
|
let llbb = self.llblock(*target);
|
|
|
|
build::AddCase(switch, llval, llbb)
|
|
|
|
}
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
|
2015-11-08 12:11:11 -06:00
|
|
|
let (otherwise, targets) = targets.split_last().unwrap();
|
2016-02-01 04:04:46 -06:00
|
|
|
let discr = bcx.load(self.trans_lvalue(&bcx, discr).llval);
|
2016-03-08 06:31:23 -06:00
|
|
|
let discr = bcx.with_block(|bcx| base::to_immediate(bcx, discr, switch_ty));
|
2016-02-01 04:04:46 -06:00
|
|
|
let switch = bcx.switch(discr, self.llblock(*otherwise), values.len());
|
2015-11-08 12:11:11 -06:00
|
|
|
for (value, target) in values.iter().zip(targets) {
|
2016-02-01 04:04:46 -06:00
|
|
|
let llval = self.trans_constval(&bcx, value, switch_ty).immediate();
|
2015-11-08 12:11:11 -06:00
|
|
|
let llbb = self.llblock(*target);
|
|
|
|
build::AddCase(switch, llval, llbb)
|
|
|
|
}
|
2015-10-26 13:35:18 -05:00
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
mir::TerminatorKind::Return => {
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx.with_block(|bcx| {
|
2016-03-06 08:30:21 -06:00
|
|
|
self.fcx.build_return_block(bcx, DebugLoc::None);
|
2016-02-01 04:04:46 -06:00
|
|
|
})
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
mir::TerminatorKind::Drop { ref value, target, unwind } => {
|
2016-02-01 04:04:46 -06:00
|
|
|
let lvalue = self.trans_lvalue(&bcx, value);
|
2016-01-30 11:32:50 -06:00
|
|
|
let ty = lvalue.ty.to_ty(bcx.tcx());
|
|
|
|
// Double check for necessity to drop
|
|
|
|
if !glue::type_needs_drop(bcx.tcx(), ty) {
|
2016-02-11 14:57:09 -06:00
|
|
|
funclet_br(bcx, self.llblock(target));
|
2016-01-30 11:32:50 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let drop_fn = glue::get_drop_glue(bcx.ccx(), ty);
|
|
|
|
let drop_ty = glue::get_drop_glue_type(bcx.ccx(), ty);
|
|
|
|
let llvalue = if drop_ty != ty {
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx.pointercast(lvalue.llval, type_of::type_of(bcx.ccx(), drop_ty).ptr_to())
|
2016-01-30 11:32:50 -06:00
|
|
|
} else {
|
|
|
|
lvalue.llval
|
|
|
|
};
|
|
|
|
if let Some(unwind) = unwind {
|
|
|
|
let uwbcx = self.bcx(unwind);
|
|
|
|
let unwind = self.make_landing_pad(uwbcx);
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx.invoke(drop_fn,
|
|
|
|
&[llvalue],
|
|
|
|
self.llblock(target),
|
|
|
|
unwind.llbb(),
|
2016-02-25 17:10:40 -06:00
|
|
|
cleanup_bundle.as_ref());
|
2016-02-04 11:40:28 -06:00
|
|
|
self.bcx(target).at_start(|bcx| drop::drop_fill(bcx, lvalue.llval, ty));
|
2016-01-30 11:32:50 -06:00
|
|
|
} else {
|
2016-02-25 17:10:40 -06:00
|
|
|
bcx.call(drop_fn, &[llvalue], cleanup_bundle.as_ref());
|
2016-02-04 11:40:28 -06:00
|
|
|
drop::drop_fill(&bcx, lvalue.llval, ty);
|
2016-02-11 14:57:09 -06:00
|
|
|
funclet_br(bcx, self.llblock(target));
|
2016-01-30 11:32:50 -06:00
|
|
|
}
|
2016-01-29 16:18:47 -06:00
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
mir::TerminatorKind::Call { ref func, ref args, ref destination, ref cleanup } => {
|
2016-03-06 09:32:47 -06:00
|
|
|
// Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar.
|
2016-02-01 04:04:46 -06:00
|
|
|
let callee = self.trans_operand(&bcx, func);
|
2016-01-12 07:20:18 -06:00
|
|
|
|
2016-03-06 05:23:20 -06:00
|
|
|
let (mut callee, abi, sig) = match callee.ty.sty {
|
2016-03-06 09:32:47 -06:00
|
|
|
ty::TyFnDef(def_id, substs, f) => {
|
2016-03-06 05:23:20 -06:00
|
|
|
(Callee::def(bcx.ccx(), def_id, substs), f.abi, &f.sig)
|
2016-03-06 09:32:47 -06:00
|
|
|
}
|
|
|
|
ty::TyFnPtr(f) => {
|
|
|
|
(Callee {
|
|
|
|
data: Fn(callee.immediate()),
|
|
|
|
ty: callee.ty
|
2016-03-06 05:23:20 -06:00
|
|
|
}, f.abi, &f.sig)
|
2016-03-06 09:32:47 -06:00
|
|
|
}
|
2016-03-28 18:46:02 -05:00
|
|
|
_ => bug!("{} is not callable", callee.ty)
|
2016-03-06 09:32:47 -06:00
|
|
|
};
|
|
|
|
|
2016-04-07 22:37:56 -05:00
|
|
|
let sig = bcx.tcx().erase_late_bound_regions(sig);
|
|
|
|
|
2016-03-08 06:40:37 -06:00
|
|
|
// Handle intrinsics old trans wants Expr's for, ourselves.
|
|
|
|
let intrinsic = match (&callee.ty.sty, &callee.data) {
|
|
|
|
(&ty::TyFnDef(def_id, _, _), &Intrinsic) => {
|
|
|
|
Some(bcx.tcx().item_name(def_id).as_str())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
let intrinsic = intrinsic.as_ref().map(|s| &s[..]);
|
|
|
|
|
|
|
|
if intrinsic == Some("move_val_init") {
|
|
|
|
let &(_, target) = destination.as_ref().unwrap();
|
|
|
|
// The first argument is a thin destination pointer.
|
|
|
|
let llptr = self.trans_operand(&bcx, &args[0]).immediate();
|
|
|
|
let val = self.trans_operand(&bcx, &args[1]);
|
|
|
|
self.store_operand(&bcx, llptr, val);
|
|
|
|
self.set_operand_dropped(&bcx, &args[1]);
|
|
|
|
funclet_br(bcx, self.llblock(target));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if intrinsic == Some("transmute") {
|
|
|
|
let &(ref dest, target) = destination.as_ref().unwrap();
|
2016-04-04 02:21:27 -05:00
|
|
|
self.with_lvalue_ref(&bcx, dest, |this, dest| {
|
|
|
|
this.trans_transmute(&bcx, &args[0], dest);
|
|
|
|
});
|
2016-03-08 06:40:37 -06:00
|
|
|
|
|
|
|
self.set_operand_dropped(&bcx, &args[0]);
|
|
|
|
funclet_br(bcx, self.llblock(target));
|
|
|
|
return;
|
|
|
|
}
|
2016-03-06 05:23:20 -06:00
|
|
|
|
2016-04-07 22:37:56 -05:00
|
|
|
let extra_args = &args[sig.inputs.len()..];
|
2016-03-06 05:23:20 -06:00
|
|
|
let extra_args = extra_args.iter().map(|op_arg| {
|
|
|
|
self.mir.operand_ty(bcx.tcx(), op_arg)
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
let fn_ty = callee.direct_fn_type(bcx.ccx(), &extra_args);
|
|
|
|
|
|
|
|
// The arguments we'll be passing. Plus one to account for outptr, if used.
|
|
|
|
let arg_count = fn_ty.args.len() + fn_ty.ret.is_indirect() as usize;
|
|
|
|
let mut llargs = Vec::with_capacity(arg_count);
|
2015-12-21 17:46:56 -06:00
|
|
|
|
|
|
|
// Prepare the return value destination
|
2016-04-06 00:57:42 -05:00
|
|
|
let ret_dest = if let Some((ref dest, _)) = *destination {
|
|
|
|
let is_intrinsic = if let Intrinsic = callee.data {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
self.make_return_dest(&bcx, dest, &fn_ty.ret, &mut llargs, is_intrinsic)
|
2015-12-13 07:48:43 -06:00
|
|
|
} else {
|
2016-04-04 02:21:27 -05:00
|
|
|
ReturnDest::Nothing
|
2015-12-13 07:48:43 -06:00
|
|
|
};
|
|
|
|
|
2016-03-06 09:32:47 -06:00
|
|
|
// Split the rust-call tupled arguments off.
|
2016-03-08 06:31:48 -06:00
|
|
|
let (first_args, untuple) = if abi == Abi::RustCall && !args.is_empty() {
|
2016-03-06 09:32:47 -06:00
|
|
|
let (tup, args) = args.split_last().unwrap();
|
2016-03-06 05:23:20 -06:00
|
|
|
(args, Some(tup))
|
2016-03-06 09:32:47 -06:00
|
|
|
} else {
|
2016-03-06 05:23:20 -06:00
|
|
|
(&args[..], None)
|
2016-03-06 09:32:47 -06:00
|
|
|
};
|
|
|
|
|
2016-03-06 05:23:20 -06:00
|
|
|
let mut idx = 0;
|
2016-03-08 06:31:48 -06:00
|
|
|
for arg in first_args {
|
2016-03-06 05:23:20 -06:00
|
|
|
let val = self.trans_operand(&bcx, arg).val;
|
|
|
|
self.trans_argument(&bcx, val, &mut llargs, &fn_ty,
|
|
|
|
&mut idx, &mut callee.data);
|
|
|
|
}
|
|
|
|
if let Some(tup) = untuple {
|
|
|
|
self.trans_arguments_untupled(&bcx, tup, &mut llargs, &fn_ty,
|
|
|
|
&mut idx, &mut callee.data)
|
|
|
|
}
|
2016-02-25 17:10:40 -06:00
|
|
|
|
2016-03-08 06:40:37 -06:00
|
|
|
let fn_ptr = match callee.data {
|
|
|
|
NamedTupleConstructor(_) => {
|
|
|
|
// FIXME translate this like mir::Rvalue::Aggregate.
|
|
|
|
callee.reify(bcx.ccx()).val
|
|
|
|
}
|
|
|
|
Intrinsic => {
|
2016-03-22 12:23:36 -05:00
|
|
|
use callee::ArgVals;
|
|
|
|
use expr::{Ignore, SaveIn};
|
|
|
|
use intrinsic::trans_intrinsic_call;
|
2016-03-08 06:40:37 -06:00
|
|
|
|
2016-04-04 02:21:27 -05:00
|
|
|
let (dest, llargs) = match ret_dest {
|
|
|
|
_ if fn_ty.ret.is_indirect() => {
|
|
|
|
(SaveIn(llargs[0]), &llargs[1..])
|
|
|
|
}
|
|
|
|
ReturnDest::Nothing => (Ignore, &llargs[..]),
|
|
|
|
ReturnDest::IndirectOperand(dst, _) |
|
|
|
|
ReturnDest::Store(dst) => (SaveIn(dst), &llargs[..]),
|
|
|
|
ReturnDest::DirectOperand(_) =>
|
|
|
|
bug!("Cannot use direct operand with an intrinsic call")
|
2016-03-08 06:40:37 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
bcx.with_block(|bcx| {
|
2016-04-07 22:37:56 -05:00
|
|
|
trans_intrinsic_call(bcx, callee.ty, &fn_ty,
|
2016-03-08 06:40:37 -06:00
|
|
|
ArgVals(llargs), dest,
|
|
|
|
DebugLoc::None);
|
|
|
|
});
|
2016-04-04 02:21:27 -05:00
|
|
|
|
|
|
|
if let ReturnDest::IndirectOperand(dst, _) = ret_dest {
|
|
|
|
// Make a fake operand for store_return
|
|
|
|
let op = OperandRef {
|
|
|
|
val: OperandValue::Ref(dst),
|
2016-04-07 22:37:56 -05:00
|
|
|
ty: sig.output.unwrap()
|
2016-04-04 02:21:27 -05:00
|
|
|
};
|
|
|
|
self.store_return(&bcx, ret_dest, fn_ty.ret, op);
|
|
|
|
}
|
|
|
|
|
2016-04-07 22:37:56 -05:00
|
|
|
if let Some((_, target)) = *destination {
|
|
|
|
for op in args {
|
|
|
|
self.set_operand_dropped(&bcx, op);
|
|
|
|
}
|
|
|
|
funclet_br(bcx, self.llblock(target));
|
|
|
|
} else {
|
|
|
|
// trans_intrinsic_call already used Unreachable.
|
|
|
|
// bcx.unreachable();
|
|
|
|
}
|
|
|
|
|
2016-03-08 06:40:37 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Fn(f) => f,
|
2016-03-28 18:46:02 -05:00
|
|
|
Virtual(_) => bug!("Virtual fn ptr not extracted")
|
2016-03-08 06:40:37 -06:00
|
|
|
};
|
2015-12-13 07:48:43 -06:00
|
|
|
|
2015-12-21 17:46:56 -06:00
|
|
|
// Many different ways to call a function handled here
|
2016-03-08 06:40:04 -06:00
|
|
|
if let Some(cleanup) = cleanup.map(|bb| self.bcx(bb)) {
|
2016-03-10 23:00:52 -06:00
|
|
|
let ret_bcx = if let Some((_, target)) = *destination {
|
|
|
|
self.blocks[target.index()]
|
2016-03-08 06:40:04 -06:00
|
|
|
} else {
|
|
|
|
self.unreachable_block()
|
|
|
|
};
|
|
|
|
let landingpad = self.make_landing_pad(cleanup);
|
|
|
|
|
|
|
|
let invokeret = bcx.invoke(fn_ptr,
|
|
|
|
&llargs,
|
|
|
|
ret_bcx.llbb,
|
|
|
|
landingpad.llbb(),
|
|
|
|
cleanup_bundle.as_ref());
|
|
|
|
fn_ty.apply_attrs_callsite(invokeret);
|
|
|
|
|
|
|
|
landingpad.at_start(|bcx| for op in args {
|
|
|
|
self.set_operand_dropped(bcx, op);
|
|
|
|
});
|
|
|
|
|
2016-03-10 23:00:52 -06:00
|
|
|
if destination.is_some() {
|
2016-03-08 06:40:04 -06:00
|
|
|
let ret_bcx = ret_bcx.build();
|
2016-03-10 23:00:52 -06:00
|
|
|
ret_bcx.at_start(|ret_bcx| {
|
2016-04-04 02:21:27 -05:00
|
|
|
let op = OperandRef {
|
|
|
|
val: OperandValue::Immediate(invokeret),
|
2016-04-07 22:37:56 -05:00
|
|
|
ty: sig.output.unwrap()
|
2016-04-04 02:21:27 -05:00
|
|
|
};
|
|
|
|
self.store_return(&ret_bcx, ret_dest, fn_ty.ret, op);
|
2016-03-10 23:00:52 -06:00
|
|
|
for op in args {
|
|
|
|
self.set_operand_dropped(&ret_bcx, op);
|
|
|
|
}
|
|
|
|
});
|
2015-12-21 17:46:56 -06:00
|
|
|
}
|
2016-03-08 06:40:04 -06:00
|
|
|
} else {
|
|
|
|
let llret = bcx.call(fn_ptr, &llargs, cleanup_bundle.as_ref());
|
|
|
|
fn_ty.apply_attrs_callsite(llret);
|
|
|
|
if let Some((_, target)) = *destination {
|
2016-04-04 02:21:27 -05:00
|
|
|
let op = OperandRef {
|
|
|
|
val: OperandValue::Immediate(llret),
|
2016-04-07 22:37:56 -05:00
|
|
|
ty: sig.output.unwrap()
|
2016-04-04 02:21:27 -05:00
|
|
|
};
|
|
|
|
self.store_return(&bcx, ret_dest, fn_ty.ret, op);
|
2016-02-04 11:40:28 -06:00
|
|
|
for op in args {
|
|
|
|
self.set_operand_dropped(&bcx, op);
|
|
|
|
}
|
2016-02-11 14:57:09 -06:00
|
|
|
funclet_br(bcx, self.llblock(target));
|
2016-03-08 06:40:04 -06:00
|
|
|
} else {
|
|
|
|
// no need to drop args, because the call never returns
|
|
|
|
bcx.unreachable();
|
2015-12-13 07:48:43 -06:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 05:23:20 -06:00
|
|
|
fn trans_argument(&mut self,
|
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
|
|
|
val: OperandValue,
|
|
|
|
llargs: &mut Vec<ValueRef>,
|
|
|
|
fn_ty: &FnType,
|
|
|
|
next_idx: &mut usize,
|
|
|
|
callee: &mut CalleeData) {
|
|
|
|
// Treat the values in a fat pointer separately.
|
|
|
|
if let FatPtr(ptr, meta) = val {
|
|
|
|
if *next_idx == 0 {
|
|
|
|
if let Virtual(idx) = *callee {
|
|
|
|
let llfn = bcx.with_block(|bcx| {
|
|
|
|
meth::get_virtual_method(bcx, meta, idx)
|
|
|
|
});
|
|
|
|
let llty = fn_ty.llvm_type(bcx.ccx()).ptr_to();
|
|
|
|
*callee = Fn(bcx.pointercast(llfn, llty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.trans_argument(bcx, Immediate(ptr), llargs, fn_ty, next_idx, callee);
|
|
|
|
self.trans_argument(bcx, Immediate(meta), llargs, fn_ty, next_idx, callee);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg = &fn_ty.args[*next_idx];
|
|
|
|
*next_idx += 1;
|
|
|
|
|
|
|
|
// Fill padding with undef value, where applicable.
|
|
|
|
if let Some(ty) = arg.pad {
|
|
|
|
llargs.push(C_undef(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
if arg.is_ignore() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force by-ref if we have to load through a cast pointer.
|
|
|
|
let (mut llval, by_ref) = match val {
|
2016-03-10 13:30:52 -06:00
|
|
|
Immediate(llval) if arg.is_indirect() || arg.cast.is_some() => {
|
2016-03-06 05:23:20 -06:00
|
|
|
let llscratch = build::AllocaFcx(bcx.fcx(), arg.original_ty, "arg");
|
|
|
|
bcx.store(llval, llscratch);
|
|
|
|
(llscratch, true)
|
|
|
|
}
|
|
|
|
Immediate(llval) => (llval, false),
|
|
|
|
Ref(llval) => (llval, true),
|
2016-03-28 18:46:02 -05:00
|
|
|
FatPtr(_, _) => bug!("fat pointers handled above")
|
2016-03-06 05:23:20 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if by_ref && !arg.is_indirect() {
|
|
|
|
// Have to load the argument, maybe while casting it.
|
|
|
|
if arg.original_ty == Type::i1(bcx.ccx()) {
|
|
|
|
// We store bools as i8 so we need to truncate to i1.
|
|
|
|
llval = bcx.load_range_assert(llval, 0, 2, llvm::False);
|
|
|
|
llval = bcx.trunc(llval, arg.original_ty);
|
|
|
|
} else if let Some(ty) = arg.cast {
|
|
|
|
llval = bcx.load(bcx.pointercast(llval, ty.ptr_to()));
|
|
|
|
let llalign = llalign_of_min(bcx.ccx(), arg.ty);
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetAlignment(llval, llalign);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
llval = bcx.load(llval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llargs.push(llval);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_arguments_untupled(&mut self,
|
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
|
|
|
operand: &mir::Operand<'tcx>,
|
|
|
|
llargs: &mut Vec<ValueRef>,
|
|
|
|
fn_ty: &FnType,
|
|
|
|
next_idx: &mut usize,
|
|
|
|
callee: &mut CalleeData) {
|
|
|
|
// FIXME: consider having some optimization to avoid tupling/untupling
|
|
|
|
// (and storing/loading in the case of immediates)
|
|
|
|
|
|
|
|
// avoid trans_operand for pointless copying
|
|
|
|
let lv = match *operand {
|
|
|
|
mir::Operand::Consume(ref lvalue) => self.trans_lvalue(bcx, lvalue),
|
|
|
|
mir::Operand::Constant(ref constant) => {
|
|
|
|
// FIXME: consider being less pessimized
|
|
|
|
if constant.ty.is_nil() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty = bcx.monomorphize(&constant.ty);
|
|
|
|
let lv = LvalueRef::alloca(bcx, ty, "__untuple_alloca");
|
|
|
|
let constant = self.trans_constant(bcx, constant);
|
|
|
|
self.store_operand(bcx, lv.llval, constant);
|
|
|
|
lv
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let lv_ty = lv.ty.to_ty(bcx.tcx());
|
|
|
|
let result_types = match lv_ty.sty {
|
|
|
|
ty::TyTuple(ref tys) => tys,
|
2016-03-28 18:46:02 -05:00
|
|
|
_ => span_bug!(
|
2016-03-06 05:23:20 -06:00
|
|
|
self.mir.span,
|
2016-03-28 18:46:02 -05:00
|
|
|
"bad final argument to \"rust-call\" fn {:?}", lv_ty)
|
2016-03-06 05:23:20 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let base_repr = adt::represent_type(bcx.ccx(), lv_ty);
|
|
|
|
let base = adt::MaybeSizedValue::sized(lv.llval);
|
|
|
|
for (n, &ty) in result_types.iter().enumerate() {
|
2016-03-09 06:20:22 -06:00
|
|
|
let ptr = adt::trans_field_ptr_builder(bcx, &base_repr, base, Disr(0), n);
|
2016-03-06 05:23:20 -06:00
|
|
|
let val = if common::type_is_fat_ptr(bcx.tcx(), ty) {
|
2016-03-09 06:20:22 -06:00
|
|
|
let (lldata, llextra) = load_fat_ptr(bcx, ptr);
|
2016-03-06 05:23:20 -06:00
|
|
|
FatPtr(lldata, llextra)
|
|
|
|
} else {
|
|
|
|
// Don't bother loading the value, trans_argument will.
|
|
|
|
Ref(ptr)
|
|
|
|
};
|
|
|
|
self.trans_argument(bcx, val, llargs, fn_ty, next_idx, callee);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 04:04:46 -06:00
|
|
|
fn get_personality_slot(&mut self, bcx: &BlockAndBuilder<'bcx, 'tcx>) -> ValueRef {
|
2016-01-01 16:45:21 -06:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
if let Some(slot) = self.llpersonalityslot {
|
|
|
|
slot
|
|
|
|
} else {
|
|
|
|
let llretty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false);
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx.with_block(|bcx| {
|
2016-02-08 16:08:47 -06:00
|
|
|
let slot = base::alloca(bcx, llretty, "personalityslot");
|
|
|
|
self.llpersonalityslot = Some(slot);
|
2016-02-01 04:04:46 -06:00
|
|
|
base::call_lifetime_start(bcx, slot);
|
2016-02-08 16:08:47 -06:00
|
|
|
slot
|
|
|
|
})
|
2016-01-01 16:45:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:57:09 -06:00
|
|
|
/// Create a landingpad wrapper around the given Block.
|
|
|
|
///
|
|
|
|
/// No-op in MSVC SEH scheme.
|
2016-02-01 04:04:46 -06:00
|
|
|
fn make_landing_pad(&mut self,
|
|
|
|
cleanup: BlockAndBuilder<'bcx, 'tcx>)
|
|
|
|
-> BlockAndBuilder<'bcx, 'tcx>
|
|
|
|
{
|
2016-02-11 14:57:09 -06:00
|
|
|
if base::wants_msvc_seh(cleanup.sess()) {
|
|
|
|
return cleanup;
|
|
|
|
}
|
2016-02-08 16:08:47 -06:00
|
|
|
let bcx = self.fcx.new_block("cleanup", None).build();
|
2015-12-19 08:48:49 -06:00
|
|
|
let ccx = bcx.ccx();
|
2016-02-08 16:08:47 -06:00
|
|
|
let llpersonality = self.fcx.eh_personality();
|
2015-12-19 08:48:49 -06:00
|
|
|
let llretty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false);
|
2016-02-08 16:08:47 -06:00
|
|
|
let llretval = bcx.landing_pad(llretty, llpersonality, 1, self.fcx.llfn);
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx.set_cleanup(llretval);
|
|
|
|
let slot = self.get_personality_slot(&bcx);
|
|
|
|
bcx.store(llretval, slot);
|
2016-02-08 16:08:47 -06:00
|
|
|
bcx.br(cleanup.llbb());
|
2015-12-19 08:48:49 -06:00
|
|
|
bcx
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:57:09 -06:00
|
|
|
/// Create prologue cleanuppad instruction under MSVC SEH handling scheme.
|
|
|
|
///
|
|
|
|
/// Also handles setting some state for the original trans and creating an operand bundle for
|
|
|
|
/// function calls.
|
|
|
|
fn make_cleanup_pad(&mut self, bb: mir::BasicBlock) -> Option<(ValueRef, OperandBundleDef)> {
|
|
|
|
let bcx = self.bcx(bb);
|
|
|
|
let data = self.mir.basic_block_data(bb);
|
|
|
|
let use_funclets = base::wants_msvc_seh(bcx.sess()) && data.is_cleanup;
|
|
|
|
let cleanup_pad = if use_funclets {
|
|
|
|
bcx.set_personality_fn(self.fcx.eh_personality());
|
2016-02-04 11:40:28 -06:00
|
|
|
bcx.at_start(|bcx| Some(bcx.cleanup_pad(None, &[])))
|
2016-02-11 14:57:09 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
// Set the landingpad global-state for old translator, so it knows about the SEH used.
|
|
|
|
bcx.set_lpad(if let Some(cleanup_pad) = cleanup_pad {
|
|
|
|
Some(common::LandingPad::msvc(cleanup_pad))
|
|
|
|
} else if data.is_cleanup {
|
|
|
|
Some(common::LandingPad::gnu())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
});
|
|
|
|
cleanup_pad.map(|f| (f, OperandBundleDef::new("funclet", &[f])))
|
|
|
|
}
|
|
|
|
|
2015-12-19 08:47:52 -06:00
|
|
|
fn unreachable_block(&mut self) -> Block<'bcx, 'tcx> {
|
2016-02-01 04:04:46 -06:00
|
|
|
self.unreachable_block.unwrap_or_else(|| {
|
2016-02-08 16:08:47 -06:00
|
|
|
let bl = self.fcx.new_block("unreachable", None);
|
2016-02-01 04:04:46 -06:00
|
|
|
bl.build().unreachable();
|
|
|
|
self.unreachable_block = Some(bl);
|
|
|
|
bl
|
|
|
|
})
|
2015-12-19 08:47:52 -06:00
|
|
|
}
|
|
|
|
|
2016-02-01 04:04:46 -06:00
|
|
|
fn bcx(&self, bb: mir::BasicBlock) -> BlockAndBuilder<'bcx, 'tcx> {
|
|
|
|
self.blocks[bb.index()].build()
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
|
2016-02-04 11:40:28 -06:00
|
|
|
pub fn llblock(&self, bb: mir::BasicBlock) -> BasicBlockRef {
|
2015-10-21 16:42:25 -05:00
|
|
|
self.blocks[bb.index()].llbb
|
|
|
|
}
|
2016-04-04 02:21:27 -05:00
|
|
|
|
2016-04-06 00:57:42 -05:00
|
|
|
fn make_return_dest(&mut self, bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
|
|
|
dest: &mir::Lvalue<'tcx>, fn_ret_ty: &ArgType,
|
|
|
|
llargs: &mut Vec<ValueRef>, is_intrinsic: bool) -> ReturnDest {
|
|
|
|
// If the return is ignored, we can just return a do-nothing ReturnDest
|
|
|
|
if fn_ret_ty.is_ignore() {
|
|
|
|
return ReturnDest::Nothing;
|
|
|
|
}
|
|
|
|
let dest = match *dest {
|
|
|
|
mir::Lvalue::Temp(idx) => {
|
|
|
|
let lvalue_ty = self.mir.lvalue_ty(bcx.tcx(), dest);
|
2016-04-07 22:37:56 -05:00
|
|
|
let lvalue_ty = bcx.monomorphize(&lvalue_ty);
|
2016-04-06 00:57:42 -05:00
|
|
|
let ret_ty = lvalue_ty.to_ty(bcx.tcx());
|
|
|
|
match self.temps[idx as usize] {
|
|
|
|
TempRef::Lvalue(dest) => dest,
|
|
|
|
TempRef::Operand(None) => {
|
|
|
|
// Handle temporary lvalues, specifically Operand ones, as
|
|
|
|
// they don't have allocas
|
|
|
|
return if fn_ret_ty.is_indirect() {
|
|
|
|
// Odd, but possible, case, we have an operand temporary,
|
|
|
|
// but the calling convention has an indirect return.
|
|
|
|
let tmp = bcx.with_block(|bcx| {
|
|
|
|
base::alloc_ty(bcx, ret_ty, "tmp_ret")
|
|
|
|
});
|
|
|
|
llargs.push(tmp);
|
|
|
|
ReturnDest::IndirectOperand(tmp, idx)
|
|
|
|
} else if is_intrinsic {
|
|
|
|
// Currently, intrinsics always need a location to store
|
|
|
|
// the result. so we create a temporary alloca for the
|
|
|
|
// result
|
|
|
|
let tmp = bcx.with_block(|bcx| {
|
|
|
|
base::alloc_ty(bcx, ret_ty, "tmp_ret")
|
|
|
|
});
|
|
|
|
ReturnDest::IndirectOperand(tmp, idx)
|
|
|
|
} else {
|
|
|
|
ReturnDest::DirectOperand(idx)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
TempRef::Operand(Some(_)) => {
|
|
|
|
bug!("lvalue temp already assigned to");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.trans_lvalue(bcx, dest)
|
|
|
|
};
|
|
|
|
if fn_ret_ty.is_indirect() {
|
|
|
|
llargs.push(dest.llval);
|
|
|
|
ReturnDest::Nothing
|
|
|
|
} else {
|
|
|
|
ReturnDest::Store(dest.llval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 02:21:27 -05:00
|
|
|
fn trans_transmute(&mut self, bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
|
|
|
src: &mir::Operand<'tcx>, dst: LvalueRef<'tcx>) {
|
|
|
|
let mut val = self.trans_operand(bcx, src);
|
|
|
|
if let ty::TyFnDef(def_id, substs, _) = val.ty.sty {
|
|
|
|
let llouttype = type_of::type_of(bcx.ccx(), dst.ty.to_ty(bcx.tcx()));
|
|
|
|
let out_type_size = llbitsize_of_real(bcx.ccx(), llouttype);
|
|
|
|
if out_type_size != 0 {
|
|
|
|
// FIXME #19925 Remove this hack after a release cycle.
|
|
|
|
let f = Callee::def(bcx.ccx(), def_id, substs);
|
|
|
|
let datum = f.reify(bcx.ccx());
|
|
|
|
val = OperandRef {
|
|
|
|
val: OperandValue::Immediate(datum.val),
|
|
|
|
ty: datum.ty
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let llty = type_of::type_of(bcx.ccx(), val.ty);
|
|
|
|
let cast_ptr = bcx.pointercast(dst.llval, llty.ptr_to());
|
|
|
|
self.store_operand(bcx, cast_ptr, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stores the return value of a function call into it's final location.
|
|
|
|
fn store_return(&mut self,
|
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
|
|
|
dest: ReturnDest,
|
|
|
|
ret_ty: ArgType,
|
|
|
|
op: OperandRef<'tcx>) {
|
|
|
|
use self::ReturnDest::*;
|
|
|
|
|
|
|
|
match dest {
|
|
|
|
Nothing => (),
|
|
|
|
Store(dst) => ret_ty.store(bcx, op.immediate(), dst),
|
|
|
|
IndirectOperand(tmp, idx) => {
|
|
|
|
let op = self.trans_load(bcx, tmp, op.ty);
|
|
|
|
self.temps[idx as usize] = TempRef::Operand(Some(op));
|
|
|
|
}
|
|
|
|
DirectOperand(idx) => {
|
2016-04-07 22:37:56 -05:00
|
|
|
let op = if type_is_fat_ptr(bcx.tcx(), op.ty) {
|
|
|
|
let llval = op.immediate();
|
|
|
|
let ptr = bcx.extract_value(llval, 0);
|
|
|
|
let meta = bcx.extract_value(llval, 1);
|
|
|
|
|
|
|
|
OperandRef {
|
|
|
|
val: OperandValue::FatPtr(ptr, meta),
|
|
|
|
ty: op.ty
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
op
|
|
|
|
};
|
2016-04-04 02:21:27 -05:00
|
|
|
self.temps[idx as usize] = TempRef::Operand(Some(op));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ReturnDest {
|
|
|
|
// Do nothing, the return value is indirect or ignored
|
|
|
|
Nothing,
|
|
|
|
// Store the return value to the pointer
|
|
|
|
Store(ValueRef),
|
|
|
|
// Stores an indirect return value to an operand temporary lvalue
|
|
|
|
IndirectOperand(ValueRef, u32),
|
|
|
|
// Stores a direct return value to an operand temporary lvalue
|
|
|
|
DirectOperand(u32)
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|