2015-10-21 17:42:25 -04: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-12-11 22:19:39 -07:00
|
|
|
use llvm::{self, ValueRef, BasicBlockRef};
|
2016-08-16 17:41:38 +03:00
|
|
|
use rustc_const_eval::{ErrKind, ConstEvalErr, note_const_eval_err};
|
2016-05-25 08:39:32 +03:00
|
|
|
use rustc::middle::lang_items;
|
2017-02-02 06:44:30 +02:00
|
|
|
use rustc::middle::const_val::ConstInt;
|
2017-02-06 17:37:58 +01:00
|
|
|
use rustc::ty::{self, layout, TypeFoldable};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2016-04-04 19:21:27 +12:00
|
|
|
use abi::{Abi, FnType, ArgType};
|
2016-12-11 15:03:52 -07:00
|
|
|
use base::{self, Lifetime};
|
2017-03-08 18:33:21 +02:00
|
|
|
use callee;
|
2016-12-31 16:00:24 -07:00
|
|
|
use builder::Builder;
|
|
|
|
use common::{self, Funclet};
|
2016-06-08 00:35:01 +03:00
|
|
|
use common::{C_bool, C_str_slice, C_struct, C_u32, C_undef};
|
2016-05-25 08:39:32 +03:00
|
|
|
use consts;
|
2016-06-10 13:00:21 +03:00
|
|
|
use machine::llalign_of_min;
|
2016-03-22 19:23:36 +02:00
|
|
|
use meth;
|
2017-03-08 18:33:21 +02:00
|
|
|
use monomorphize;
|
2016-12-29 02:20:26 +01:00
|
|
|
use type_of::{self, align_of};
|
2016-03-22 19:23:36 +02:00
|
|
|
use glue;
|
|
|
|
use type_::Type;
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2016-12-11 22:19:39 -07:00
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
2016-11-16 10:52:37 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-12-29 02:20:26 +01:00
|
|
|
use std::cmp;
|
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
use super::{MirContext, LocalRef};
|
2016-05-29 22:01:06 +03:00
|
|
|
use super::analyze::CleanupKind;
|
2016-04-26 23:54:38 +03:00
|
|
|
use super::constant::Const;
|
2017-02-06 17:27:09 +01:00
|
|
|
use super::lvalue::{Alignment, LvalueRef};
|
2016-03-08 14:40:37 +02:00
|
|
|
use super::operand::OperandRef;
|
2016-10-31 02:16:21 +02:00
|
|
|
use super::operand::OperandValue::{Pair, Ref, Immediate};
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
impl<'a, 'tcx> MirContext<'a, 'tcx> {
|
2016-12-11 22:19:39 -07:00
|
|
|
pub fn trans_block(&mut self, bb: mir::BasicBlock,
|
2016-12-12 06:48:39 -07:00
|
|
|
funclets: &IndexVec<mir::BasicBlock, Option<Funclet>>) {
|
2017-01-01 01:29:23 -07:00
|
|
|
let mut bcx = self.get_builder(bb);
|
2016-12-17 12:56:33 -07:00
|
|
|
let data = &self.mir[bb];
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-05-29 22:01:06 +03:00
|
|
|
debug!("trans_block({:?}={:?})", bb, data);
|
|
|
|
|
2016-12-12 06:48:39 -07:00
|
|
|
let funclet = match self.cleanup_kinds[bb] {
|
|
|
|
CleanupKind::Internal { funclet } => funclets[funclet].as_ref(),
|
|
|
|
_ => funclets[bb].as_ref(),
|
2016-12-11 22:19:39 -07:00
|
|
|
};
|
|
|
|
|
2016-05-29 22:01:06 +03:00
|
|
|
// Create the cleanup bundle, if needed.
|
2016-12-12 06:48:39 -07:00
|
|
|
let cleanup_pad = funclet.map(|lp| lp.cleanuppad());
|
|
|
|
let cleanup_bundle = funclet.map(|l| l.bundle());
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2016-12-31 16:00:24 -07:00
|
|
|
let funclet_br = |this: &Self, bcx: Builder, bb: mir::BasicBlock| {
|
2016-12-11 22:19:39 -07:00
|
|
|
let lltarget = this.blocks[bb];
|
2016-05-29 22:01:06 +03:00
|
|
|
if let Some(cp) = cleanup_pad {
|
2016-06-07 17:28:36 +03:00
|
|
|
match this.cleanup_kinds[bb] {
|
2016-05-29 22:01:06 +03:00
|
|
|
CleanupKind::Funclet => {
|
|
|
|
// micro-optimization: generate a `ret` rather than a jump
|
|
|
|
// to a return block
|
|
|
|
bcx.cleanup_ret(cp, Some(lltarget));
|
|
|
|
}
|
|
|
|
CleanupKind::Internal { .. } => bcx.br(lltarget),
|
|
|
|
CleanupKind::NotCleanup => bug!("jump from cleanup bb to bb {:?}", bb)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bcx.br(lltarget);
|
|
|
|
}
|
2016-02-11 22:57:09 +02:00
|
|
|
};
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2017-01-01 01:29:23 -07:00
|
|
|
let llblock = |this: &mut Self, target: mir::BasicBlock| {
|
2016-12-11 22:19:39 -07:00
|
|
|
let lltarget = this.blocks[target];
|
2016-05-29 22:01:06 +03:00
|
|
|
|
|
|
|
if let Some(cp) = cleanup_pad {
|
2016-06-07 17:28:36 +03:00
|
|
|
match this.cleanup_kinds[target] {
|
2016-05-29 22:01:06 +03:00
|
|
|
CleanupKind::Funclet => {
|
|
|
|
// MSVC cross-funclet jump - need a trampoline
|
|
|
|
|
|
|
|
debug!("llblock: creating cleanup trampoline for {:?}", target);
|
|
|
|
let name = &format!("{:?}_cleanup_trampoline_{:?}", bb, target);
|
2017-01-01 01:29:23 -07:00
|
|
|
let trampoline = this.new_block(name);
|
2016-05-29 22:01:06 +03:00
|
|
|
trampoline.cleanup_ret(cp, Some(lltarget));
|
|
|
|
trampoline.llbb()
|
|
|
|
}
|
|
|
|
CleanupKind::Internal { .. } => lltarget,
|
|
|
|
CleanupKind::NotCleanup =>
|
|
|
|
bug!("jump from cleanup bb {:?} to bb {:?}", bb, target)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let (CleanupKind::NotCleanup, CleanupKind::Funclet) =
|
2016-06-07 17:28:36 +03:00
|
|
|
(this.cleanup_kinds[bb], this.cleanup_kinds[target])
|
2016-05-29 22:01:06 +03:00
|
|
|
{
|
|
|
|
// jump *into* cleanup - need a landing pad if GNU
|
2016-12-11 22:19:39 -07:00
|
|
|
this.landing_pad_to(target)
|
2016-05-29 22:01:06 +03:00
|
|
|
} else {
|
|
|
|
lltarget
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 22:57:09 +02:00
|
|
|
};
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
for statement in &data.statements {
|
|
|
|
bcx = self.trans_statement(bcx, statement);
|
|
|
|
}
|
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
let terminator = data.terminator();
|
|
|
|
debug!("trans_block: terminator: {:?}", terminator);
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
let span = terminator.source_info.span;
|
2016-12-19 14:38:16 -07:00
|
|
|
self.set_debug_loc(&bcx, terminator.source_info);
|
2016-04-07 22:35:11 +03:00
|
|
|
match terminator.kind {
|
2016-03-10 09:55:15 -05:00
|
|
|
mir::TerminatorKind::Resume => {
|
2016-02-11 22:57:09 +02:00
|
|
|
if let Some(cleanup_pad) = cleanup_pad {
|
|
|
|
bcx.cleanup_ret(cleanup_pad, None);
|
|
|
|
} else {
|
|
|
|
let ps = self.get_personality_slot(&bcx);
|
2017-02-06 17:27:09 +01:00
|
|
|
let lp = bcx.load(ps, None);
|
2016-12-11 15:03:52 -07:00
|
|
|
Lifetime::End.call(&bcx, ps);
|
2017-01-01 08:46:34 -07:00
|
|
|
if !bcx.sess().target.target.options.custom_unwind_resume {
|
2016-12-11 22:19:39 -07:00
|
|
|
bcx.resume(lp);
|
|
|
|
} else {
|
|
|
|
let exc_ptr = bcx.extract_value(lp, 0);
|
2016-12-19 21:26:49 -07:00
|
|
|
bcx.call(bcx.ccx.eh_unwind_resume(), &[exc_ptr], cleanup_bundle);
|
2016-12-19 20:09:51 -07:00
|
|
|
bcx.unreachable();
|
2016-12-11 22:19:39 -07:00
|
|
|
}
|
2016-02-11 22:57:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
mir::TerminatorKind::Goto { target } => {
|
2016-05-29 22:01:06 +03:00
|
|
|
funclet_br(self, bcx, target);
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
|
2017-02-02 06:44:30 +02:00
|
|
|
let discr = self.trans_operand(&bcx, discr);
|
|
|
|
if switch_ty == bcx.tcx().types.bool {
|
|
|
|
let lltrue = llblock(self, targets[0]);
|
|
|
|
let llfalse = llblock(self, targets[1]);
|
2017-02-15 15:00:20 +02:00
|
|
|
if let [ConstInt::U8(0)] = values[..] {
|
2017-02-02 06:44:30 +02:00
|
|
|
bcx.cond_br(discr.immediate(), llfalse, lltrue);
|
|
|
|
} else {
|
|
|
|
bcx.cond_br(discr.immediate(), lltrue, llfalse);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let (otherwise, targets) = targets.split_last().unwrap();
|
|
|
|
let switch = bcx.switch(discr.immediate(),
|
|
|
|
llblock(self, *otherwise), values.len());
|
|
|
|
for (value, target) in values.iter().zip(targets) {
|
|
|
|
let val = Const::from_constint(bcx.ccx, value);
|
|
|
|
let llbb = llblock(self, *target);
|
|
|
|
bcx.add_case(switch, val.llval, llbb)
|
|
|
|
}
|
2015-11-08 19:11:11 +01:00
|
|
|
}
|
2015-10-26 14:35:18 -04:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
mir::TerminatorKind::Return => {
|
2016-12-19 19:16:36 -07:00
|
|
|
let ret = self.fn_ty.ret;
|
2016-06-20 23:55:14 +03:00
|
|
|
if ret.is_ignore() || ret.is_indirect() {
|
|
|
|
bcx.ret_void();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let llval = if let Some(cast_ty) = ret.cast {
|
2016-09-25 01:38:27 +02:00
|
|
|
let op = match self.locals[mir::RETURN_POINTER] {
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(Some(op)) => op,
|
|
|
|
LocalRef::Operand(None) => bug!("use of return before def"),
|
|
|
|
LocalRef::Lvalue(tr_lvalue) => {
|
|
|
|
OperandRef {
|
2017-02-06 17:27:09 +01:00
|
|
|
val: Ref(tr_lvalue.llval, tr_lvalue.alignment),
|
2017-01-01 08:46:34 -07:00
|
|
|
ty: tr_lvalue.ty.to_ty(bcx.tcx())
|
2016-06-20 23:55:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let llslot = match op.val {
|
|
|
|
Immediate(_) | Pair(..) => {
|
2017-01-04 11:47:43 -07:00
|
|
|
let llscratch = bcx.alloca(ret.original_ty, "ret");
|
2017-02-06 17:27:09 +01:00
|
|
|
self.store_operand(&bcx, llscratch, None, op);
|
2016-06-20 23:55:14 +03:00
|
|
|
llscratch
|
|
|
|
}
|
2017-02-06 17:27:09 +01:00
|
|
|
Ref(llval, align) => {
|
2017-02-06 21:23:26 +01:00
|
|
|
assert_eq!(align, Alignment::AbiAligned,
|
|
|
|
"return pointer is unaligned!");
|
2017-02-06 17:27:09 +01:00
|
|
|
llval
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
};
|
2017-02-06 17:27:09 +01:00
|
|
|
let load = bcx.load(
|
|
|
|
bcx.pointercast(llslot, cast_ty.ptr_to()),
|
|
|
|
Some(llalign_of_min(bcx.ccx, ret.ty)));
|
2016-06-20 23:55:14 +03:00
|
|
|
load
|
|
|
|
} else {
|
2016-09-25 01:38:27 +02:00
|
|
|
let op = self.trans_consume(&bcx, &mir::Lvalue::Local(mir::RETURN_POINTER));
|
2017-02-06 17:27:09 +01:00
|
|
|
if let Ref(llval, align) = op.val {
|
|
|
|
base::load_ty(&bcx, llval, align, op.ty)
|
2017-01-05 12:22:51 -07:00
|
|
|
} else {
|
|
|
|
op.pack_if_pair(&bcx).immediate()
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
};
|
|
|
|
bcx.ret(llval);
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 19:26:19 +03:00
|
|
|
mir::TerminatorKind::Unreachable => {
|
|
|
|
bcx.unreachable();
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:06:52 +03:00
|
|
|
mir::TerminatorKind::Drop { ref location, target, unwind } => {
|
2017-01-01 08:46:34 -07:00
|
|
|
let ty = location.ty(&self.mir, bcx.tcx()).to_ty(bcx.tcx());
|
2016-12-18 16:05:40 -07:00
|
|
|
let ty = self.monomorphize(&ty);
|
2016-06-09 18:15:15 +03:00
|
|
|
|
2016-01-30 19:32:50 +02:00
|
|
|
// Double check for necessity to drop
|
2016-12-19 16:25:00 -07:00
|
|
|
if !bcx.ccx.shared().type_needs_drop(ty) {
|
2016-05-29 22:01:06 +03:00
|
|
|
funclet_br(self, bcx, target);
|
2016-01-30 19:32:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-09 18:15:15 +03:00
|
|
|
|
2017-01-02 14:47:15 -07:00
|
|
|
let mut lvalue = self.trans_lvalue(&bcx, location);
|
2016-12-19 16:25:00 -07:00
|
|
|
let drop_fn = glue::get_drop_glue(bcx.ccx, ty);
|
|
|
|
let drop_ty = glue::get_drop_glue_type(bcx.ccx.shared(), ty);
|
2017-01-02 14:47:15 -07:00
|
|
|
if bcx.ccx.shared().type_is_sized(ty) && drop_ty != ty {
|
|
|
|
lvalue.llval = bcx.pointercast(
|
|
|
|
lvalue.llval, type_of::type_of(bcx.ccx, drop_ty).ptr_to());
|
|
|
|
}
|
|
|
|
let args = &[lvalue.llval, lvalue.llextra][..1 + lvalue.has_extra() as usize];
|
2016-01-30 19:32:50 +02:00
|
|
|
if let Some(unwind) = unwind {
|
2016-12-20 10:46:44 -07:00
|
|
|
bcx.invoke(
|
|
|
|
drop_fn,
|
|
|
|
args,
|
|
|
|
self.blocks[target],
|
2017-01-01 01:29:23 -07:00
|
|
|
llblock(self, unwind),
|
2016-12-20 10:46:44 -07:00
|
|
|
cleanup_bundle
|
|
|
|
);
|
2016-01-30 19:32:50 +02:00
|
|
|
} else {
|
2016-12-20 10:46:44 -07:00
|
|
|
bcx.call(drop_fn, args, cleanup_bundle);
|
2016-05-29 22:01:06 +03:00
|
|
|
funclet_br(self, bcx, target);
|
2016-01-30 19:32:50 +02:00
|
|
|
}
|
2016-01-30 00:18:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => {
|
|
|
|
let cond = self.trans_operand(&bcx, cond).immediate();
|
2016-08-26 01:32:46 +03:00
|
|
|
let mut const_cond = common::const_to_opt_u128(cond, false).map(|c| c == 1);
|
2016-08-04 19:57:57 +03:00
|
|
|
|
|
|
|
// This case can currently arise only from functions marked
|
|
|
|
// with #[rustc_inherit_overflow_checks] and inlined from
|
|
|
|
// another crate (mostly core::num generic/#[inline] fns),
|
|
|
|
// while the current crate doesn't use overflow checks.
|
|
|
|
// NOTE: Unlike binops, negation doesn't have its own
|
|
|
|
// checked operation, just a comparison with the minimum
|
|
|
|
// value, so we have to check for the assert message.
|
2016-12-19 16:25:00 -07:00
|
|
|
if !bcx.ccx.check_overflow() {
|
2016-08-04 19:57:57 +03:00
|
|
|
use rustc_const_math::ConstMathErr::Overflow;
|
|
|
|
use rustc_const_math::Op::Neg;
|
|
|
|
|
|
|
|
if let mir::AssertMessage::Math(Overflow(Neg)) = *msg {
|
|
|
|
const_cond = Some(expected);
|
|
|
|
}
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
|
|
|
// Don't translate the panic block if success if known.
|
|
|
|
if const_cond == Some(expected) {
|
2016-06-05 14:38:29 +03:00
|
|
|
funclet_br(self, bcx, target);
|
2016-05-25 08:39:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-05 14:38:29 +03:00
|
|
|
// Pass the condition through llvm.expect for branch hinting.
|
2016-12-19 16:25:00 -07:00
|
|
|
let expect = bcx.ccx.get_intrinsic(&"llvm.expect.i1");
|
|
|
|
let cond = bcx.call(expect, &[cond, C_bool(bcx.ccx, expected)], None);
|
2016-06-05 14:38:29 +03:00
|
|
|
|
|
|
|
// Create the failure block and the conditional branch to it.
|
2017-01-01 01:29:23 -07:00
|
|
|
let lltarget = llblock(self, target);
|
|
|
|
let panic_block = self.new_block("panic");
|
2016-06-05 14:38:29 +03:00
|
|
|
if expected {
|
2016-12-11 22:19:39 -07:00
|
|
|
bcx.cond_br(cond, lltarget, panic_block.llbb());
|
2016-05-25 08:39:32 +03:00
|
|
|
} else {
|
2016-12-11 22:19:39 -07:00
|
|
|
bcx.cond_br(cond, panic_block.llbb(), lltarget);
|
2016-05-25 08:39:32 +03:00
|
|
|
}
|
|
|
|
|
2016-06-05 14:38:29 +03:00
|
|
|
// After this point, bcx is the block for the call to panic.
|
2016-12-11 22:19:39 -07:00
|
|
|
bcx = panic_block;
|
2016-12-19 14:38:16 -07:00
|
|
|
self.set_debug_loc(&bcx, terminator.source_info);
|
2016-06-05 14:38:29 +03:00
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
// Get the location information.
|
2017-01-01 08:46:34 -07:00
|
|
|
let loc = bcx.sess().codemap().lookup_char_pos(span.lo);
|
2016-11-16 10:52:37 +00:00
|
|
|
let filename = Symbol::intern(&loc.file.name).as_str();
|
2016-12-19 16:25:00 -07:00
|
|
|
let filename = C_str_slice(bcx.ccx, filename);
|
|
|
|
let line = C_u32(bcx.ccx, loc.line as u32);
|
2016-05-25 08:39:32 +03:00
|
|
|
|
|
|
|
// Put together the arguments to the panic entry point.
|
2016-06-05 14:38:29 +03:00
|
|
|
let (lang_item, args, const_err) = match *msg {
|
2016-05-25 08:39:32 +03:00
|
|
|
mir::AssertMessage::BoundsCheck { ref len, ref index } => {
|
2016-06-05 14:38:29 +03:00
|
|
|
let len = self.trans_operand(&mut bcx, len).immediate();
|
|
|
|
let index = self.trans_operand(&mut bcx, index).immediate();
|
|
|
|
|
2016-08-26 01:32:46 +03:00
|
|
|
let const_err = common::const_to_opt_u128(len, false)
|
|
|
|
.and_then(|len| common::const_to_opt_u128(index, false)
|
|
|
|
.map(|index| ErrKind::IndexOutOfBounds {
|
|
|
|
len: len as u64,
|
|
|
|
index: index as u64
|
|
|
|
}));
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2016-12-19 16:25:00 -07:00
|
|
|
let file_line = C_struct(bcx.ccx, &[filename, line], false);
|
|
|
|
let align = llalign_of_min(bcx.ccx, common::val_ty(file_line));
|
|
|
|
let file_line = consts::addr_of(bcx.ccx,
|
2016-05-25 08:39:32 +03:00
|
|
|
file_line,
|
|
|
|
align,
|
|
|
|
"panic_bounds_check_loc");
|
|
|
|
(lang_items::PanicBoundsCheckFnLangItem,
|
2016-06-05 14:38:29 +03:00
|
|
|
vec![file_line, index, len],
|
|
|
|
const_err)
|
2016-05-25 08:39:32 +03:00
|
|
|
}
|
|
|
|
mir::AssertMessage::Math(ref err) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
let msg_str = Symbol::intern(err.description()).as_str();
|
2016-12-19 16:25:00 -07:00
|
|
|
let msg_str = C_str_slice(bcx.ccx, msg_str);
|
|
|
|
let msg_file_line = C_struct(bcx.ccx,
|
2016-05-25 08:39:32 +03:00
|
|
|
&[msg_str, filename, line],
|
|
|
|
false);
|
2016-12-19 16:25:00 -07:00
|
|
|
let align = llalign_of_min(bcx.ccx, common::val_ty(msg_file_line));
|
|
|
|
let msg_file_line = consts::addr_of(bcx.ccx,
|
2016-05-25 08:39:32 +03:00
|
|
|
msg_file_line,
|
|
|
|
align,
|
|
|
|
"panic_loc");
|
2016-06-05 14:38:29 +03:00
|
|
|
(lang_items::PanicFnLangItem,
|
|
|
|
vec![msg_file_line],
|
|
|
|
Some(ErrKind::Math(err.clone())))
|
2016-05-25 08:39:32 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-05 14:38:29 +03:00
|
|
|
// If we know we always panic, and the error message
|
|
|
|
// is also constant, then we can produce a warning.
|
|
|
|
if const_cond == Some(!expected) {
|
|
|
|
if let Some(err) = const_err {
|
2016-08-16 17:41:38 +03:00
|
|
|
let err = ConstEvalErr{ span: span, kind: err };
|
2017-01-01 08:46:34 -07:00
|
|
|
let mut diag = bcx.tcx().sess.struct_span_warn(
|
2016-08-16 17:41:38 +03:00
|
|
|
span, "this expression will panic at run-time");
|
2017-01-01 08:46:34 -07:00
|
|
|
note_const_eval_err(bcx.tcx(), &err, span, "expression", &mut diag);
|
2016-08-16 17:41:38 +03:00
|
|
|
diag.emit();
|
2016-06-05 14:38:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
// Obtain the panic entry point.
|
2017-01-01 08:46:34 -07:00
|
|
|
let def_id = common::langcall(bcx.tcx(), Some(span), "", lang_item);
|
2017-03-08 18:33:21 +02:00
|
|
|
let instance = ty::Instance::mono(bcx.tcx(), def_id);
|
|
|
|
let llfn = callee::get_fn(bcx.ccx, instance);
|
2016-05-25 08:39:32 +03:00
|
|
|
|
|
|
|
// Translate the actual panic invoke/call.
|
|
|
|
if let Some(unwind) = cleanup {
|
|
|
|
bcx.invoke(llfn,
|
|
|
|
&args,
|
2016-12-11 22:19:39 -07:00
|
|
|
self.unreachable_block(),
|
2017-01-01 01:29:23 -07:00
|
|
|
llblock(self, unwind),
|
2016-06-05 14:38:29 +03:00
|
|
|
cleanup_bundle);
|
2016-05-25 08:39:32 +03:00
|
|
|
} else {
|
2016-06-05 14:38:29 +03:00
|
|
|
bcx.call(llfn, &args, cleanup_bundle);
|
2016-05-25 08:39:32 +03:00
|
|
|
bcx.unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:06:52 +03:00
|
|
|
mir::TerminatorKind::DropAndReplace { .. } => {
|
|
|
|
bug!("undesugared DropAndReplace in trans: {:?}", data);
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:55:15 -05:00
|
|
|
mir::TerminatorKind::Call { ref func, ref args, ref destination, ref cleanup } => {
|
2016-03-06 17:32:47 +02:00
|
|
|
// Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar.
|
2016-02-01 11:04:46 +01:00
|
|
|
let callee = self.trans_operand(&bcx, func);
|
2016-01-12 15:20:18 +02:00
|
|
|
|
2017-03-08 18:33:21 +02:00
|
|
|
let (instance, mut llfn, sig) = match callee.ty.sty {
|
2017-02-13 10:51:06 +02:00
|
|
|
ty::TyFnDef(def_id, substs, sig) => {
|
2017-03-08 18:33:21 +02:00
|
|
|
(Some(monomorphize::resolve(bcx.ccx.shared(), def_id, substs)),
|
|
|
|
None,
|
|
|
|
sig)
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
2017-02-13 10:51:06 +02:00
|
|
|
ty::TyFnPtr(sig) => {
|
2017-03-08 18:33:21 +02:00
|
|
|
(None,
|
|
|
|
Some(callee.immediate()),
|
|
|
|
sig)
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("{} is not callable", callee.ty)
|
2016-03-06 17:32:47 +02:00
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
let def = instance.map(|i| i.def);
|
2017-02-13 10:51:06 +02:00
|
|
|
let sig = bcx.tcx().erase_late_bound_regions_and_normalize(&sig);
|
|
|
|
let abi = sig.abi;
|
2016-04-08 15:37:56 +12:00
|
|
|
|
2016-03-08 14:40:37 +02:00
|
|
|
// Handle intrinsics old trans wants Expr's for, ourselves.
|
2017-03-08 18:33:21 +02:00
|
|
|
let intrinsic = match def {
|
|
|
|
Some(ty::InstanceDef::Intrinsic(def_id))
|
|
|
|
=> Some(bcx.tcx().item_name(def_id).as_str()),
|
2016-03-08 14:40:37 +02:00
|
|
|
_ => None
|
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
let intrinsic = intrinsic.as_ref().map(|s| &s[..]);
|
2016-03-08 14:40:37 +02:00
|
|
|
|
|
|
|
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]);
|
2017-02-06 17:27:09 +01:00
|
|
|
self.store_operand(&bcx, llptr, None, val);
|
2016-05-29 22:01:06 +03:00
|
|
|
funclet_br(self, bcx, target);
|
2016-03-08 14:40:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if intrinsic == Some("transmute") {
|
|
|
|
let &(ref dest, target) = destination.as_ref().unwrap();
|
2017-02-06 17:37:58 +01:00
|
|
|
self.trans_transmute(&bcx, &args[0], dest);
|
2016-05-29 22:01:06 +03:00
|
|
|
funclet_br(self, bcx, target);
|
2016-03-08 14:40:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
|
2016-11-28 19:35:38 -07:00
|
|
|
let extra_args = &args[sig.inputs().len()..];
|
2016-03-06 13:23:20 +02:00
|
|
|
let extra_args = extra_args.iter().map(|op_arg| {
|
2017-01-01 08:46:34 -07:00
|
|
|
let op_ty = op_arg.ty(&self.mir, bcx.tcx());
|
2016-12-18 16:05:40 -07:00
|
|
|
self.monomorphize(&op_ty)
|
2016-03-06 13:23:20 +02:00
|
|
|
}).collect::<Vec<_>>();
|
2017-03-08 18:33:21 +02:00
|
|
|
|
|
|
|
let fn_ty = match def {
|
|
|
|
Some(ty::InstanceDef::Virtual(..)) => {
|
|
|
|
FnType::new_vtable(bcx.ccx, sig, &extra_args)
|
|
|
|
}
|
|
|
|
_ => FnType::new(bcx.ccx, sig, &extra_args)
|
|
|
|
};
|
2016-03-06 13:23:20 +02:00
|
|
|
|
2016-12-20 10:46:44 -07:00
|
|
|
if intrinsic == Some("drop_in_place") {
|
|
|
|
let &(_, target) = destination.as_ref().unwrap();
|
2017-03-08 18:33:21 +02:00
|
|
|
let ty = instance.unwrap().substs.type_at(0);
|
2016-12-20 10:46:44 -07:00
|
|
|
|
|
|
|
// Double check for necessity to drop
|
|
|
|
if !bcx.ccx.shared().type_needs_drop(ty) {
|
|
|
|
funclet_br(self, bcx, target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let drop_fn = glue::get_drop_glue(bcx.ccx, ty);
|
|
|
|
let llty = fn_ty.llvm_type(bcx.ccx).ptr_to();
|
2017-03-08 18:33:21 +02:00
|
|
|
llfn = Some(bcx.pointercast(drop_fn, llty));
|
2016-12-20 10:46:44 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
// 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-22 01:46:56 +02:00
|
|
|
|
|
|
|
// Prepare the return value destination
|
2016-04-06 17:57:42 +12:00
|
|
|
let ret_dest = if let Some((ref dest, _)) = *destination {
|
2017-03-08 18:33:21 +02:00
|
|
|
let is_intrinsic = intrinsic.is_some();
|
|
|
|
self.make_return_dest(&bcx, dest, &fn_ty.ret, &mut llargs,
|
|
|
|
is_intrinsic)
|
2015-12-13 05:48:43 -08:00
|
|
|
} else {
|
2016-04-04 19:21:27 +12:00
|
|
|
ReturnDest::Nothing
|
2015-12-13 05:48:43 -08:00
|
|
|
};
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
// Split the rust-call tupled arguments off.
|
2016-03-08 14:31:48 +02:00
|
|
|
let (first_args, untuple) = if abi == Abi::RustCall && !args.is_empty() {
|
2016-03-06 17:32:47 +02:00
|
|
|
let (tup, args) = args.split_last().unwrap();
|
2016-03-06 13:23:20 +02:00
|
|
|
(args, Some(tup))
|
2016-03-06 17:32:47 +02:00
|
|
|
} else {
|
2016-03-06 13:23:20 +02:00
|
|
|
(&args[..], None)
|
2016-03-06 17:32:47 +02:00
|
|
|
};
|
|
|
|
|
2016-04-26 23:54:38 +03:00
|
|
|
let is_shuffle = intrinsic.map_or(false, |name| {
|
|
|
|
name.starts_with("simd_shuffle")
|
|
|
|
});
|
2016-03-06 13:23:20 +02:00
|
|
|
let mut idx = 0;
|
2016-03-08 14:31:48 +02:00
|
|
|
for arg in first_args {
|
2016-04-26 23:54:38 +03:00
|
|
|
// The indices passed to simd_shuffle* in the
|
|
|
|
// third argument must be constant. This is
|
|
|
|
// checked by const-qualification, which also
|
|
|
|
// promotes any complex rvalues to constants.
|
|
|
|
if is_shuffle && idx == 2 {
|
|
|
|
match *arg {
|
|
|
|
mir::Operand::Consume(_) => {
|
2016-06-07 19:21:56 +03:00
|
|
|
span_bug!(span, "shuffle indices must be constant");
|
2016-04-26 23:54:38 +03:00
|
|
|
}
|
|
|
|
mir::Operand::Constant(ref constant) => {
|
|
|
|
let val = self.trans_constant(&bcx, constant);
|
|
|
|
llargs.push(val.llval);
|
|
|
|
idx += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 11:55:44 +03:00
|
|
|
let op = self.trans_operand(&bcx, arg);
|
|
|
|
self.trans_argument(&bcx, op, &mut llargs, &fn_ty,
|
2017-03-08 18:33:21 +02:00
|
|
|
&mut idx, &mut llfn, &def);
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
if let Some(tup) = untuple {
|
|
|
|
self.trans_arguments_untupled(&bcx, tup, &mut llargs, &fn_ty,
|
2017-03-08 18:33:21 +02:00
|
|
|
&mut idx, &mut llfn, &def)
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
2016-02-26 01:10:40 +02:00
|
|
|
|
2017-03-08 18:33:21 +02:00
|
|
|
if intrinsic.is_some() && intrinsic != Some("drop_in_place") {
|
|
|
|
use intrinsic::trans_intrinsic_call;
|
2016-04-04 19:21:27 +12:00
|
|
|
|
2017-03-08 18:33:21 +02:00
|
|
|
let (dest, llargs) = match ret_dest {
|
|
|
|
_ if fn_ty.ret.is_indirect() => {
|
|
|
|
(llargs[0], &llargs[1..])
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
ReturnDest::Nothing => {
|
|
|
|
(C_undef(fn_ty.ret.original_ty.ptr_to()), &llargs[..])
|
2016-04-08 15:37:56 +12:00
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
ReturnDest::IndirectOperand(dst, _) |
|
|
|
|
ReturnDest::Store(dst) => (dst, &llargs[..]),
|
|
|
|
ReturnDest::DirectOperand(_) =>
|
|
|
|
bug!("Cannot use direct operand with an intrinsic call")
|
|
|
|
};
|
2016-04-08 15:37:56 +12:00
|
|
|
|
2017-03-08 18:33:21 +02:00
|
|
|
let callee_ty = common::instance_ty(
|
|
|
|
bcx.ccx.shared(), instance.as_ref().unwrap());
|
|
|
|
trans_intrinsic_call(&bcx, callee_ty, &fn_ty, &llargs, dest,
|
|
|
|
terminator.source_info.span);
|
|
|
|
|
|
|
|
if let ReturnDest::IndirectOperand(dst, _) = ret_dest {
|
|
|
|
// Make a fake operand for store_return
|
|
|
|
let op = OperandRef {
|
|
|
|
val: Ref(dst, Alignment::AbiAligned),
|
|
|
|
ty: sig.output(),
|
|
|
|
};
|
|
|
|
self.store_return(&bcx, ret_dest, fn_ty.ret, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((_, target)) = *destination {
|
|
|
|
funclet_br(self, bcx, target);
|
|
|
|
} else {
|
|
|
|
bcx.unreachable();
|
2016-03-08 14:40:37 +02:00
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fn_ptr = match (llfn, instance) {
|
|
|
|
(Some(llfn), _) => llfn,
|
|
|
|
(None, Some(instance)) => callee::get_fn(bcx.ccx, instance),
|
|
|
|
_ => span_bug!(span, "no llfn for call"),
|
2016-03-08 14:40:37 +02:00
|
|
|
};
|
2015-12-13 05:48:43 -08:00
|
|
|
|
2015-12-22 01:46:56 +02:00
|
|
|
// Many different ways to call a function handled here
|
2016-05-29 22:01:06 +03:00
|
|
|
if let &Some(cleanup) = cleanup {
|
2016-03-11 18:00:52 +13:00
|
|
|
let ret_bcx = if let Some((_, target)) = *destination {
|
2016-06-07 17:28:36 +03:00
|
|
|
self.blocks[target]
|
2016-03-08 14:40:04 +02:00
|
|
|
} else {
|
|
|
|
self.unreachable_block()
|
|
|
|
};
|
|
|
|
let invokeret = bcx.invoke(fn_ptr,
|
|
|
|
&llargs,
|
2016-12-11 22:19:39 -07:00
|
|
|
ret_bcx,
|
2017-01-01 01:29:23 -07:00
|
|
|
llblock(self, cleanup),
|
2016-05-29 22:01:06 +03:00
|
|
|
cleanup_bundle);
|
2016-03-08 14:40:04 +02:00
|
|
|
fn_ty.apply_attrs_callsite(invokeret);
|
|
|
|
|
2016-12-11 22:19:39 -07:00
|
|
|
if let Some((_, target)) = *destination {
|
2017-01-01 01:29:23 -07:00
|
|
|
let ret_bcx = self.get_builder(target);
|
2016-12-31 16:00:24 -07:00
|
|
|
self.set_debug_loc(&ret_bcx, terminator.source_info);
|
|
|
|
let op = OperandRef {
|
|
|
|
val: Immediate(invokeret),
|
|
|
|
ty: sig.output(),
|
|
|
|
};
|
|
|
|
self.store_return(&ret_bcx, ret_dest, fn_ty.ret, op);
|
2015-12-22 01:46:56 +02:00
|
|
|
}
|
2016-03-08 14:40:04 +02:00
|
|
|
} else {
|
2016-05-29 22:01:06 +03:00
|
|
|
let llret = bcx.call(fn_ptr, &llargs, cleanup_bundle);
|
2016-03-08 14:40:04 +02:00
|
|
|
fn_ty.apply_attrs_callsite(llret);
|
|
|
|
if let Some((_, target)) = *destination {
|
2016-04-04 19:21:27 +12:00
|
|
|
let op = OperandRef {
|
2016-05-25 11:55:44 +03:00
|
|
|
val: Immediate(llret),
|
2016-11-28 19:35:38 -07:00
|
|
|
ty: sig.output(),
|
2016-04-04 19:21:27 +12:00
|
|
|
};
|
|
|
|
self.store_return(&bcx, ret_dest, fn_ty.ret, op);
|
2016-05-29 22:01:06 +03:00
|
|
|
funclet_br(self, bcx, target);
|
2016-03-08 14:40:04 +02:00
|
|
|
} else {
|
|
|
|
bcx.unreachable();
|
2015-12-13 05:48:43 -08:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
fn trans_argument(&mut self,
|
2016-12-31 16:00:24 -07:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-06-20 23:55:14 +03:00
|
|
|
op: OperandRef<'tcx>,
|
2016-03-06 13:23:20 +02:00
|
|
|
llargs: &mut Vec<ValueRef>,
|
|
|
|
fn_ty: &FnType,
|
|
|
|
next_idx: &mut usize,
|
2017-03-08 18:33:21 +02:00
|
|
|
llfn: &mut Option<ValueRef>,
|
|
|
|
def: &Option<ty::InstanceDef<'tcx>>) {
|
2016-05-25 11:55:44 +03:00
|
|
|
if let Pair(a, b) = op.val {
|
|
|
|
// Treat the values in a fat pointer separately.
|
2016-12-19 16:25:00 -07:00
|
|
|
if common::type_is_fat_ptr(bcx.ccx, op.ty) {
|
2016-05-25 11:55:44 +03:00
|
|
|
let (ptr, meta) = (a, b);
|
|
|
|
if *next_idx == 0 {
|
2017-03-08 18:33:21 +02:00
|
|
|
if let Some(ty::InstanceDef::Virtual(_, idx)) = *def {
|
|
|
|
let llmeth = meth::get_virtual_method(bcx, meta, idx);
|
2016-12-19 16:25:00 -07:00
|
|
|
let llty = fn_ty.llvm_type(bcx.ccx).ptr_to();
|
2017-03-08 18:33:21 +02:00
|
|
|
*llfn = Some(bcx.pointercast(llmeth, llty));
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
2016-05-25 11:55:44 +03:00
|
|
|
|
|
|
|
let imm_op = |x| OperandRef {
|
|
|
|
val: Immediate(x),
|
|
|
|
// We won't be checking the type again.
|
2017-01-01 08:46:34 -07:00
|
|
|
ty: bcx.tcx().types.err
|
2016-05-25 11:55:44 +03:00
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
self.trans_argument(bcx, imm_op(ptr), llargs, fn_ty, next_idx, llfn, def);
|
|
|
|
self.trans_argument(bcx, imm_op(meta), llargs, fn_ty, next_idx, llfn, def);
|
2016-05-25 11:55:44 +03:00
|
|
|
return;
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-02-06 17:27:09 +01:00
|
|
|
let (mut llval, align, by_ref) = match op.val {
|
2016-06-20 23:55:14 +03:00
|
|
|
Immediate(_) | Pair(..) => {
|
|
|
|
if arg.is_indirect() || arg.cast.is_some() {
|
2017-01-04 11:47:43 -07:00
|
|
|
let llscratch = bcx.alloca(arg.original_ty, "arg");
|
2017-02-06 17:27:09 +01:00
|
|
|
self.store_operand(bcx, llscratch, None, op);
|
|
|
|
(llscratch, Alignment::AbiAligned, true)
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
2017-02-06 17:27:09 +01:00
|
|
|
(op.pack_if_pair(bcx).immediate(), Alignment::AbiAligned, false)
|
2016-06-20 23:55:14 +03:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
2017-02-06 17:27:09 +01:00
|
|
|
Ref(llval, Alignment::Packed) if arg.is_indirect() => {
|
|
|
|
// `foo(packed.large_field)`. We can't pass the (unaligned) field directly. I
|
|
|
|
// think that ATM (Rust 1.16) we only pass temporaries, but we shouldn't
|
|
|
|
// have scary latent bugs around.
|
|
|
|
|
|
|
|
let llscratch = bcx.alloca(arg.original_ty, "arg");
|
|
|
|
base::memcpy_ty(bcx, llscratch, llval, op.ty, Some(1));
|
|
|
|
(llscratch, Alignment::AbiAligned, true)
|
|
|
|
}
|
|
|
|
Ref(llval, align) => (llval, align, true)
|
2016-03-06 13:23:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if by_ref && !arg.is_indirect() {
|
|
|
|
// Have to load the argument, maybe while casting it.
|
2016-12-19 16:25:00 -07:00
|
|
|
if arg.original_ty == Type::i1(bcx.ccx) {
|
2016-03-06 13:23:20 +02:00
|
|
|
// We store bools as i8 so we need to truncate to i1.
|
2017-02-06 17:27:09 +01:00
|
|
|
llval = bcx.load_range_assert(llval, 0, 2, llvm::False, None);
|
2016-03-06 13:23:20 +02:00
|
|
|
llval = bcx.trunc(llval, arg.original_ty);
|
|
|
|
} else if let Some(ty) = arg.cast {
|
2017-02-06 17:27:09 +01:00
|
|
|
llval = bcx.load(bcx.pointercast(llval, ty.ptr_to()),
|
|
|
|
align.min_with(llalign_of_min(bcx.ccx, arg.ty)));
|
2016-03-06 13:23:20 +02:00
|
|
|
} else {
|
2017-02-06 17:27:09 +01:00
|
|
|
llval = bcx.load(llval, align.to_align());
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llargs.push(llval);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_arguments_untupled(&mut self,
|
2016-12-31 16:00:24 -07:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-03-06 13:23:20 +02:00
|
|
|
operand: &mir::Operand<'tcx>,
|
|
|
|
llargs: &mut Vec<ValueRef>,
|
|
|
|
fn_ty: &FnType,
|
|
|
|
next_idx: &mut usize,
|
2017-03-08 18:33:21 +02:00
|
|
|
llfn: &mut Option<ValueRef>,
|
|
|
|
def: &Option<ty::InstanceDef<'tcx>>) {
|
2016-04-20 15:27:15 +12:00
|
|
|
let tuple = self.trans_operand(bcx, operand);
|
2016-03-06 13:23:20 +02:00
|
|
|
|
2016-04-20 15:27:15 +12:00
|
|
|
let arg_types = match tuple.ty.sty {
|
2017-01-11 15:58:37 +08:00
|
|
|
ty::TyTuple(ref tys, _) => tys,
|
2016-04-20 15:27:15 +12:00
|
|
|
_ => span_bug!(self.mir.span,
|
|
|
|
"bad final argument to \"rust-call\" fn {:?}", tuple.ty)
|
2016-03-06 13:23:20 +02:00
|
|
|
};
|
|
|
|
|
2016-04-21 11:43:01 +12:00
|
|
|
// Handle both by-ref and immediate tuples.
|
2016-04-20 15:27:15 +12:00
|
|
|
match tuple.val {
|
2017-02-06 17:27:09 +01:00
|
|
|
Ref(llval, align) => {
|
2016-04-20 15:27:15 +12:00
|
|
|
for (n, &ty) in arg_types.iter().enumerate() {
|
2017-02-06 17:27:09 +01:00
|
|
|
let ptr = LvalueRef::new_sized_ty(llval, tuple.ty, align);
|
|
|
|
let (ptr, align) = ptr.trans_field_ptr(bcx, n);
|
2016-12-19 16:25:00 -07:00
|
|
|
let val = if common::type_is_fat_ptr(bcx.ccx, ty) {
|
2017-02-06 17:27:09 +01:00
|
|
|
let (lldata, llextra) = base::load_fat_ptr(bcx, ptr, align, ty);
|
2016-05-25 11:55:44 +03:00
|
|
|
Pair(lldata, llextra)
|
2016-04-20 15:27:15 +12:00
|
|
|
} else {
|
|
|
|
// trans_argument will load this if it needs to
|
2017-02-06 17:27:09 +01:00
|
|
|
Ref(ptr, align)
|
2016-04-20 15:27:15 +12:00
|
|
|
};
|
2016-05-25 11:55:44 +03:00
|
|
|
let op = OperandRef {
|
|
|
|
val: val,
|
|
|
|
ty: ty
|
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
self.trans_argument(bcx, op, llargs, fn_ty, next_idx, llfn, def);
|
2016-04-20 15:27:15 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
Immediate(llval) => {
|
2016-12-19 16:25:00 -07:00
|
|
|
let l = bcx.ccx.layout_of(tuple.ty);
|
2016-11-23 14:48:31 -05:00
|
|
|
let v = if let layout::Univariant { ref variant, .. } = *l {
|
|
|
|
variant
|
|
|
|
} else {
|
|
|
|
bug!("Not a tuple.");
|
|
|
|
};
|
2016-04-20 15:27:15 +12:00
|
|
|
for (n, &ty) in arg_types.iter().enumerate() {
|
2016-11-23 14:48:31 -05:00
|
|
|
let mut elem = bcx.extract_value(llval, v.memory_index[n] as usize);
|
2016-04-20 15:27:15 +12:00
|
|
|
// Truncate bools to i1, if needed
|
2016-12-19 16:25:00 -07:00
|
|
|
if ty.is_bool() && common::val_ty(elem) != Type::i1(bcx.ccx) {
|
|
|
|
elem = bcx.trunc(elem, Type::i1(bcx.ccx));
|
2016-04-20 15:27:15 +12:00
|
|
|
}
|
|
|
|
// If the tuple is immediate, the elements are as well
|
2016-05-25 11:55:44 +03:00
|
|
|
let op = OperandRef {
|
|
|
|
val: Immediate(elem),
|
|
|
|
ty: ty
|
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
self.trans_argument(bcx, op, llargs, fn_ty, next_idx, llfn, def);
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Pair(a, b) => {
|
|
|
|
let elems = [a, b];
|
|
|
|
for (n, &ty) in arg_types.iter().enumerate() {
|
|
|
|
let mut elem = elems[n];
|
|
|
|
// Truncate bools to i1, if needed
|
2016-12-19 16:25:00 -07:00
|
|
|
if ty.is_bool() && common::val_ty(elem) != Type::i1(bcx.ccx) {
|
|
|
|
elem = bcx.trunc(elem, Type::i1(bcx.ccx));
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
|
|
|
// Pair is always made up of immediates
|
|
|
|
let op = OperandRef {
|
|
|
|
val: Immediate(elem),
|
|
|
|
ty: ty
|
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
self.trans_argument(bcx, op, llargs, fn_ty, next_idx, llfn, def);
|
2016-04-20 15:27:15 +12:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
2016-04-20 15:27:15 +12:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 16:00:24 -07:00
|
|
|
fn get_personality_slot(&mut self, bcx: &Builder<'a, 'tcx>) -> ValueRef {
|
2016-12-19 16:25:00 -07:00
|
|
|
let ccx = bcx.ccx;
|
2016-01-02 00:45:21 +02:00
|
|
|
if let Some(slot) = self.llpersonalityslot {
|
|
|
|
slot
|
|
|
|
} else {
|
|
|
|
let llretty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false);
|
2016-12-31 16:00:24 -07:00
|
|
|
let slot = bcx.alloca(llretty, "personalityslot");
|
2016-12-10 20:32:44 -07:00
|
|
|
self.llpersonalityslot = Some(slot);
|
2016-12-11 15:03:52 -07:00
|
|
|
Lifetime::Start.call(bcx, slot);
|
2016-12-10 20:32:44 -07:00
|
|
|
slot
|
2016-01-02 00:45:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 22:01:06 +03:00
|
|
|
/// Return the landingpad wrapper around the given basic block
|
2016-02-11 22:57:09 +02:00
|
|
|
///
|
|
|
|
/// No-op in MSVC SEH scheme.
|
2016-12-11 22:19:39 -07:00
|
|
|
fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> BasicBlockRef {
|
2016-06-07 17:28:36 +03:00
|
|
|
if let Some(block) = self.landing_pads[target_bb] {
|
2016-05-29 22:01:06 +03:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2016-12-19 17:48:41 -07:00
|
|
|
if base::wants_msvc_seh(self.ccx.sess()) {
|
2016-06-07 17:28:36 +03:00
|
|
|
return self.blocks[target_bb];
|
2016-02-11 22:57:09 +02:00
|
|
|
}
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2017-01-01 01:29:23 -07:00
|
|
|
let target = self.get_builder(target_bb);
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2017-01-01 01:29:23 -07:00
|
|
|
let bcx = self.new_block("cleanup");
|
2016-12-11 22:19:39 -07:00
|
|
|
self.landing_pads[target_bb] = Some(bcx.llbb());
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2016-12-19 16:25:00 -07:00
|
|
|
let ccx = bcx.ccx;
|
2016-12-19 19:32:50 -07:00
|
|
|
let llpersonality = self.ccx.eh_personality();
|
2015-12-19 16:48:49 +02:00
|
|
|
let llretty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false);
|
2017-01-01 00:42:09 -07:00
|
|
|
let llretval = bcx.landing_pad(llretty, llpersonality, 1, self.llfn);
|
2016-02-01 11:04:46 +01:00
|
|
|
bcx.set_cleanup(llretval);
|
|
|
|
let slot = self.get_personality_slot(&bcx);
|
2016-12-29 02:20:26 +01:00
|
|
|
bcx.store(llretval, slot, None);
|
2016-05-29 22:01:06 +03:00
|
|
|
bcx.br(target.llbb());
|
2016-12-11 22:19:39 -07:00
|
|
|
bcx.llbb()
|
2015-12-19 16:48:49 +02:00
|
|
|
}
|
|
|
|
|
2016-12-11 22:19:39 -07:00
|
|
|
fn unreachable_block(&mut self) -> BasicBlockRef {
|
2016-02-01 11:04:46 +01:00
|
|
|
self.unreachable_block.unwrap_or_else(|| {
|
2017-01-01 01:29:23 -07:00
|
|
|
let bl = self.new_block("unreachable");
|
2016-12-11 22:19:39 -07:00
|
|
|
bl.unreachable();
|
|
|
|
self.unreachable_block = Some(bl.llbb());
|
|
|
|
bl.llbb()
|
2016-02-01 11:04:46 +01:00
|
|
|
})
|
2015-12-19 16:47:52 +02:00
|
|
|
}
|
|
|
|
|
2017-01-01 01:29:23 -07:00
|
|
|
pub fn new_block(&self, name: &str) -> Builder<'a, 'tcx> {
|
|
|
|
Builder::new_block(self.ccx, self.llfn, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_builder(&self, bb: mir::BasicBlock) -> Builder<'a, 'tcx> {
|
2017-01-01 00:42:09 -07:00
|
|
|
let builder = Builder::with_ccx(self.ccx);
|
2016-12-31 16:00:24 -07:00
|
|
|
builder.position_at_end(self.blocks[bb]);
|
|
|
|
builder
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2016-12-31 16:00:24 -07:00
|
|
|
fn make_return_dest(&mut self, bcx: &Builder<'a, 'tcx>,
|
2016-04-06 17:57:42 +12:00
|
|
|
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;
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
let dest = if let mir::Lvalue::Local(index) = *dest {
|
2016-08-05 15:59:51 -07:00
|
|
|
let ret_ty = self.monomorphized_lvalue_ty(dest);
|
2016-06-20 23:55:14 +03:00
|
|
|
match self.locals[index] {
|
|
|
|
LocalRef::Lvalue(dest) => dest,
|
|
|
|
LocalRef::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.
|
2017-02-06 17:27:09 +01:00
|
|
|
let tmp = LvalueRef::alloca(bcx, ret_ty, "tmp_ret");
|
|
|
|
llargs.push(tmp.llval);
|
|
|
|
ReturnDest::IndirectOperand(tmp.llval, index)
|
2016-06-20 23:55:14 +03:00
|
|
|
} else if is_intrinsic {
|
|
|
|
// Currently, intrinsics always need a location to store
|
|
|
|
// the result. so we create a temporary alloca for the
|
|
|
|
// result
|
2017-02-06 17:27:09 +01:00
|
|
|
let tmp = LvalueRef::alloca(bcx, ret_ty, "tmp_ret");
|
|
|
|
ReturnDest::IndirectOperand(tmp.llval, index)
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
|
|
|
ReturnDest::DirectOperand(index)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
LocalRef::Operand(Some(_)) => {
|
|
|
|
bug!("lvalue local already assigned to");
|
2016-04-06 17:57:42 +12:00
|
|
|
}
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
|
|
|
self.trans_lvalue(bcx, dest)
|
2016-04-06 17:57:42 +12:00
|
|
|
};
|
|
|
|
if fn_ret_ty.is_indirect() {
|
2017-03-09 13:28:26 +02:00
|
|
|
match dest.alignment {
|
|
|
|
Alignment::AbiAligned => {
|
|
|
|
llargs.push(dest.llval);
|
|
|
|
ReturnDest::Nothing
|
|
|
|
},
|
|
|
|
Alignment::Packed => {
|
|
|
|
// Currently, MIR code generation does not create calls
|
|
|
|
// that store directly to fields of packed structs (in
|
|
|
|
// fact, the calls it creates write only to temps),
|
|
|
|
//
|
|
|
|
// If someone changes that, please update this code path
|
|
|
|
// to create a temporary.
|
|
|
|
span_bug!(self.mir.span, "can't directly store to unaligned value");
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 17:57:42 +12:00
|
|
|
} else {
|
|
|
|
ReturnDest::Store(dest.llval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:00:24 -07:00
|
|
|
fn trans_transmute(&mut self, bcx: &Builder<'a, 'tcx>,
|
2017-02-06 17:37:58 +01:00
|
|
|
src: &mir::Operand<'tcx>,
|
|
|
|
dst: &mir::Lvalue<'tcx>) {
|
|
|
|
if let mir::Lvalue::Local(index) = *dst {
|
|
|
|
match self.locals[index] {
|
|
|
|
LocalRef::Lvalue(lvalue) => self.trans_transmute_into(bcx, src, &lvalue),
|
|
|
|
LocalRef::Operand(None) => {
|
|
|
|
let lvalue_ty = self.monomorphized_lvalue_ty(dst);
|
|
|
|
assert!(!lvalue_ty.has_erasable_regions());
|
|
|
|
let lvalue = LvalueRef::alloca(bcx, lvalue_ty, "transmute_temp");
|
|
|
|
self.trans_transmute_into(bcx, src, &lvalue);
|
|
|
|
let op = self.trans_load(bcx, lvalue.llval, lvalue.alignment, lvalue_ty);
|
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
|
|
|
}
|
|
|
|
LocalRef::Operand(Some(_)) => {
|
|
|
|
let ty = self.monomorphized_lvalue_ty(dst);
|
|
|
|
assert!(common::type_is_zero_size(bcx.ccx, ty),
|
|
|
|
"assigning to initialized SSAtemp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let dst = self.trans_lvalue(bcx, dst);
|
|
|
|
self.trans_transmute_into(bcx, src, &dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_transmute_into(&mut self, bcx: &Builder<'a, 'tcx>,
|
|
|
|
src: &mir::Operand<'tcx>,
|
|
|
|
dst: &LvalueRef<'tcx>) {
|
2016-06-10 13:00:21 +03:00
|
|
|
let val = self.trans_operand(bcx, src);
|
2016-12-19 16:25:00 -07:00
|
|
|
let llty = type_of::type_of(bcx.ccx, val.ty);
|
2016-04-04 19:21:27 +12:00
|
|
|
let cast_ptr = bcx.pointercast(dst.llval, llty.ptr_to());
|
2016-12-29 02:20:26 +01:00
|
|
|
let in_type = val.ty;
|
|
|
|
let out_type = dst.ty.to_ty(bcx.tcx());;
|
|
|
|
let llalign = cmp::min(align_of(bcx.ccx, in_type), align_of(bcx.ccx, out_type));
|
2017-02-06 17:27:09 +01:00
|
|
|
self.store_operand(bcx, cast_ptr, Some(llalign), val);
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
|
2016-04-04 19:21:27 +12:00
|
|
|
// Stores the return value of a function call into it's final location.
|
|
|
|
fn store_return(&mut self,
|
2016-12-31 16:00:24 -07:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-04-04 19:21:27 +12:00
|
|
|
dest: ReturnDest,
|
|
|
|
ret_ty: ArgType,
|
|
|
|
op: OperandRef<'tcx>) {
|
|
|
|
use self::ReturnDest::*;
|
|
|
|
|
2016-06-08 00:35:01 +03:00
|
|
|
match dest {
|
|
|
|
Nothing => (),
|
|
|
|
Store(dst) => ret_ty.store(bcx, op.immediate(), dst),
|
2016-06-20 23:55:14 +03:00
|
|
|
IndirectOperand(tmp, index) => {
|
2017-02-06 17:27:09 +01:00
|
|
|
let op = self.trans_load(bcx, tmp, Alignment::AbiAligned, op.ty);
|
2016-06-20 23:55:14 +03:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
DirectOperand(index) => {
|
2016-06-08 00:35:01 +03:00
|
|
|
// If there is a cast, we have to store and reload.
|
|
|
|
let op = if ret_ty.cast.is_some() {
|
2017-02-06 17:27:09 +01:00
|
|
|
let tmp = LvalueRef::alloca(bcx, op.ty, "tmp_ret");
|
|
|
|
ret_ty.store(bcx, op.immediate(), tmp.llval);
|
|
|
|
self.trans_load(bcx, tmp.llval, tmp.alignment, op.ty)
|
2016-04-08 15:37:56 +12:00
|
|
|
} else {
|
2016-06-08 00:35:01 +03:00
|
|
|
op.unpack_if_pair(bcx)
|
|
|
|
};
|
2016-06-20 23:55:14 +03:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ReturnDest {
|
|
|
|
// Do nothing, the return value is indirect or ignored
|
|
|
|
Nothing,
|
|
|
|
// Store the return value to the pointer
|
|
|
|
Store(ValueRef),
|
2016-06-20 23:55:14 +03:00
|
|
|
// Stores an indirect return value to an operand local lvalue
|
|
|
|
IndirectOperand(ValueRef, mir::Local),
|
|
|
|
// Stores a direct return value to an operand local lvalue
|
|
|
|
DirectOperand(mir::Local)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|