2020-01-01 19:25:28 +01:00
|
|
|
use super::operand::OperandRef;
|
|
|
|
use super::operand::OperandValue::{Immediate, Pair, Ref};
|
|
|
|
use super::place::PlaceRef;
|
|
|
|
use super::{FunctionCx, LocalRef};
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::base;
|
|
|
|
use crate::common::{self, IntPredicate};
|
|
|
|
use crate::meth;
|
2020-01-01 19:25:28 +01:00
|
|
|
use crate::traits::*;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::MemFlags;
|
2020-01-01 19:25:28 +01:00
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_index::vec::Idx;
|
2020-07-31 13:27:54 +02:00
|
|
|
use rustc_middle::mir::interpret::ConstValue;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::AssertKind;
|
2020-10-10 17:36:04 +02:00
|
|
|
use rustc_middle::mir::{self, SwitchTargets};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt};
|
2020-09-02 10:40:56 +03:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, Instance, Ty, TypeFoldable};
|
2020-07-08 11:04:10 +10:00
|
|
|
use rustc_span::source_map::Span;
|
|
|
|
use rustc_span::{sym, Symbol};
|
2019-10-29 16:35:26 +02:00
|
|
|
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::{self, LayoutOf};
|
2018-10-03 13:49:57 +02:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Used by `FunctionCx::codegen_terminator` for emitting common patterns
|
|
|
|
/// e.g., creating a basic block, calling a function, etc.
|
2019-10-29 16:24:25 +02:00
|
|
|
struct TerminatorCodegenHelper<'tcx> {
|
|
|
|
bb: mir::BasicBlock,
|
|
|
|
terminator: &'tcx mir::Terminator<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
funclet_bb: Option<mir::BasicBlock>,
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:11:11 +02:00
|
|
|
impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Returns the associated funclet from `FunctionCx::funclets` for the
|
|
|
|
/// `funclet_bb` member if it is not `None`.
|
2019-10-29 18:11:11 +02:00
|
|
|
fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2020-10-11 08:32:19 +02:00
|
|
|
fx: &'b FunctionCx<'a, 'tcx, Bx>,
|
2019-10-29 18:11:11 +02:00
|
|
|
) -> Option<&'b Bx::Funclet> {
|
2020-10-11 08:32:19 +02:00
|
|
|
self.funclet_bb.and_then(|funcl| fx.funclets[funcl].as_ref())
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2019-10-29 18:11:11 +02:00
|
|
|
fn lltarget<Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2019-10-29 18:11:11 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
2019-11-04 19:52:19 -05:00
|
|
|
target: mir::BasicBlock,
|
2019-02-02 16:34:09 +00:00
|
|
|
) -> (Bx::BasicBlock, bool) {
|
|
|
|
let span = self.terminator.source_info.span;
|
|
|
|
let lltarget = fx.blocks[target];
|
|
|
|
let target_funclet = fx.cleanup_kinds[target].funclet_bb(target);
|
|
|
|
match (self.funclet_bb, target_funclet) {
|
|
|
|
(None, None) => (lltarget, false),
|
2019-12-22 17:42:04 -05:00
|
|
|
(Some(f), Some(t_f)) if f == t_f || !base::wants_msvc_seh(fx.cx.tcx().sess) => {
|
|
|
|
(lltarget, false)
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
// jump *into* cleanup - need a landing pad if GNU
|
2019-10-14 01:38:38 -04:00
|
|
|
(None, Some(_)) => (fx.landing_pad_to(target), false),
|
2019-02-02 16:34:09 +00:00
|
|
|
(Some(_), None) => span_bug!(span, "{:?} - jump out of cleanup?", self.terminator),
|
2019-10-14 01:38:38 -04:00
|
|
|
(Some(_), Some(_)) => (fx.landing_pad_to(target), true),
|
2017-05-11 02:01:25 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2017-05-11 02:01:25 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Create a basic block.
|
2019-10-29 18:11:11 +02:00
|
|
|
fn llblock<Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2019-10-29 18:11:11 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
2019-11-04 19:52:19 -05:00
|
|
|
target: mir::BasicBlock,
|
2019-02-02 16:34:09 +00:00
|
|
|
) -> Bx::BasicBlock {
|
2019-10-14 01:38:38 -04:00
|
|
|
let (lltarget, is_cleanupret) = self.lltarget(fx, target);
|
2019-02-02 16:34:09 +00:00
|
|
|
if is_cleanupret {
|
|
|
|
// MSVC cross-funclet jump - need a trampoline
|
|
|
|
|
|
|
|
debug!("llblock: creating cleanup trampoline for {:?}", target);
|
|
|
|
let name = &format!("{:?}_cleanup_trampoline_{:?}", self.bb, target);
|
|
|
|
let mut trampoline = fx.new_block(name);
|
2019-12-22 17:42:04 -05:00
|
|
|
trampoline.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget));
|
2019-02-02 16:34:09 +00:00
|
|
|
trampoline.llbb()
|
|
|
|
} else {
|
|
|
|
lltarget
|
|
|
|
}
|
2017-05-11 02:01:25 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:11:11 +02:00
|
|
|
fn funclet_br<Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2019-10-29 18:11:11 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
bx: &mut Bx,
|
|
|
|
target: mir::BasicBlock,
|
2018-09-20 15:47:22 +02:00
|
|
|
) {
|
2019-10-14 01:38:38 -04:00
|
|
|
let (lltarget, is_cleanupret) = self.lltarget(fx, target);
|
2019-02-02 16:34:09 +00:00
|
|
|
if is_cleanupret {
|
|
|
|
// micro-optimization: generate a `ret` rather than a jump
|
|
|
|
// to a trampoline.
|
|
|
|
bx.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget));
|
|
|
|
} else {
|
|
|
|
bx.br(lltarget);
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 23:47:15 +03:00
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
/// Call `fn_ptr` of `fn_abi` with the arguments `llargs`, the optional
|
2019-02-02 16:34:09 +00:00
|
|
|
/// return destination `destination` and the cleanup function `cleanup`.
|
2019-10-29 18:11:11 +02:00
|
|
|
fn do_call<Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2019-10-29 18:11:11 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
bx: &mut Bx,
|
2019-10-29 16:35:26 +02:00
|
|
|
fn_abi: FnAbi<'tcx, Ty<'tcx>>,
|
2019-02-02 16:34:09 +00:00
|
|
|
fn_ptr: Bx::Value,
|
|
|
|
llargs: &[Bx::Value],
|
|
|
|
destination: Option<(ReturnDest<'tcx, Bx::Value>, mir::BasicBlock)>,
|
|
|
|
cleanup: Option<mir::BasicBlock>,
|
|
|
|
) {
|
2020-03-26 05:32:52 -04:00
|
|
|
// If there is a cleanup block and the function we're calling can unwind, then
|
|
|
|
// do an invoke, otherwise do a call.
|
|
|
|
if let Some(cleanup) = cleanup.filter(|_| fn_abi.can_unwind) {
|
2019-02-02 16:34:09 +00:00
|
|
|
let ret_bx = if let Some((_, target)) = destination {
|
|
|
|
fx.blocks[target]
|
|
|
|
} else {
|
|
|
|
fx.unreachable_block()
|
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
let invokeret =
|
|
|
|
bx.invoke(fn_ptr, &llargs, ret_bx, self.llblock(fx, cleanup), self.funclet(fx));
|
2019-10-29 16:35:26 +02:00
|
|
|
bx.apply_attrs_callsite(&fn_abi, invokeret);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
if let Some((ret_dest, target)) = destination {
|
|
|
|
let mut ret_bx = fx.build_block(target);
|
2019-10-14 01:38:38 -04:00
|
|
|
fx.set_debug_loc(&mut ret_bx, self.terminator.source_info);
|
2019-10-29 16:35:26 +02:00
|
|
|
fx.store_return(&mut ret_bx, ret_dest, &fn_abi.ret, invokeret);
|
2018-09-20 15:47:22 +02:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
let llret = bx.call(fn_ptr, &llargs, self.funclet(fx));
|
2019-10-29 16:35:26 +02:00
|
|
|
bx.apply_attrs_callsite(&fn_abi, llret);
|
2019-10-29 16:24:25 +02:00
|
|
|
if fx.mir[self.bb].is_cleanup {
|
2019-02-02 16:34:09 +00:00
|
|
|
// Cleanup is always the cold path. Don't inline
|
|
|
|
// drop glue. Also, when there is a deeply-nested
|
|
|
|
// struct, there are "symmetry" issues that cause
|
|
|
|
// exponential inlining - see issue #41696.
|
|
|
|
bx.do_not_inline(llret);
|
2016-05-29 22:01:06 +03:00
|
|
|
}
|
2017-05-23 23:47:15 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
if let Some((ret_dest, target)) = destination {
|
2019-10-29 16:35:26 +02:00
|
|
|
fx.store_return(bx, ret_dest, &fn_abi.ret, llret);
|
2019-10-14 01:38:38 -04:00
|
|
|
self.funclet_br(fx, bx, target);
|
2017-05-23 23:47:15 +03:00
|
|
|
} else {
|
2019-02-02 16:34:09 +00:00
|
|
|
bx.unreachable();
|
2017-05-23 23:47:15 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 22:57:09 +02:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Codegen implementations for some terminator variants.
|
2019-10-26 01:41:17 -04:00
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Generates code for a `Resume` terminator.
|
2019-12-22 17:42:04 -05:00
|
|
|
fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, mut bx: Bx) {
|
2019-02-02 16:34:09 +00:00
|
|
|
if let Some(funclet) = helper.funclet(self) {
|
|
|
|
bx.cleanup_ret(funclet, None);
|
|
|
|
} else {
|
|
|
|
let slot = self.get_personality_slot(&mut bx);
|
|
|
|
let lp0 = slot.project_field(&mut bx, 0);
|
|
|
|
let lp0 = bx.load_operand(lp0).immediate();
|
|
|
|
let lp1 = slot.project_field(&mut bx, 1);
|
|
|
|
let lp1 = bx.load_operand(lp1).immediate();
|
|
|
|
slot.storage_dead(&mut bx);
|
|
|
|
|
2020-01-27 06:10:10 +00:00
|
|
|
let mut lp = bx.const_undef(self.landing_pad_type());
|
|
|
|
lp = bx.insert_value(lp, lp0, 0);
|
|
|
|
lp = bx.insert_value(lp, lp1, 1);
|
|
|
|
bx.resume(lp);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_switchint_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
mut bx: Bx,
|
|
|
|
discr: &mir::Operand<'tcx>,
|
|
|
|
switch_ty: Ty<'tcx>,
|
2020-10-11 01:14:12 +02:00
|
|
|
targets: &SwitchTargets,
|
2019-02-02 16:34:09 +00:00
|
|
|
) {
|
2019-10-14 01:38:38 -04:00
|
|
|
let discr = self.codegen_operand(&mut bx, &discr);
|
2020-06-21 18:22:30 +02:00
|
|
|
// `switch_ty` is redundant, sanity-check that.
|
|
|
|
assert_eq!(discr.layout.ty, switch_ty);
|
2020-10-10 17:36:04 +02:00
|
|
|
let mut target_iter = targets.iter();
|
|
|
|
if target_iter.len() == 1 {
|
|
|
|
// If there are two targets (one conditional, one fallback), emit br instead of switch
|
|
|
|
let (test_value, target) = target_iter.next().unwrap();
|
|
|
|
let lltrue = helper.llblock(self, target);
|
|
|
|
let llfalse = helper.llblock(self, targets.otherwise());
|
2019-02-02 16:34:09 +00:00
|
|
|
if switch_ty == bx.tcx().types.bool {
|
|
|
|
// Don't generate trivial icmps when switching on bool
|
2020-10-10 17:36:04 +02:00
|
|
|
match test_value {
|
|
|
|
0 => bx.cond_br(discr.immediate(), llfalse, lltrue),
|
|
|
|
1 => bx.cond_br(discr.immediate(), lltrue, llfalse),
|
|
|
|
_ => bug!(),
|
2017-05-11 02:01:25 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
let switch_llty = bx.immediate_backend_type(bx.layout_of(switch_ty));
|
2020-10-10 17:36:04 +02:00
|
|
|
let llval = bx.const_uint_big(switch_llty, test_value);
|
2019-02-02 16:34:09 +00:00
|
|
|
let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval);
|
|
|
|
bx.cond_br(cmp, lltrue, llfalse);
|
2017-05-11 02:01:25 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
2018-12-08 18:42:31 +01:00
|
|
|
bx.switch(
|
|
|
|
discr.immediate(),
|
2020-10-10 17:36:04 +02:00
|
|
|
helper.llblock(self, targets.otherwise()),
|
|
|
|
target_iter.map(|(value, target)| (value, helper.llblock(self, target))),
|
2019-02-02 16:34:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2019-10-14 01:38:38 -04:00
|
|
|
fn codegen_return_terminator(&mut self, mut bx: Bx) {
|
2019-08-12 15:27:31 +03:00
|
|
|
// Call `va_end` if this is the definition of a C-variadic function.
|
2019-10-29 16:35:26 +02:00
|
|
|
if self.fn_abi.c_variadic {
|
2019-08-12 15:27:31 +03:00
|
|
|
// The `VaList` "spoofed" argument is just after all the real arguments.
|
2019-10-29 16:35:26 +02:00
|
|
|
let va_list_arg_idx = self.fn_abi.args.len();
|
2019-08-12 15:27:31 +03:00
|
|
|
match self.locals[mir::Local::new(1 + va_list_arg_idx)] {
|
|
|
|
LocalRef::Place(va_list) => {
|
2019-03-07 03:50:50 +00:00
|
|
|
bx.va_end(va_list.llval);
|
|
|
|
}
|
2019-08-12 15:27:31 +03:00
|
|
|
_ => bug!("C-variadic function must have a `VaList` place"),
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 16:35:26 +02:00
|
|
|
if self.fn_abi.ret.layout.abi.is_uninhabited() {
|
2019-04-03 15:44:49 -07:00
|
|
|
// Functions with uninhabited return values are marked `noreturn`,
|
|
|
|
// so we should make sure that we never actually do.
|
2019-12-05 23:59:30 +01:00
|
|
|
// We play it safe by using a well-defined `abort`, but we could go for immediate UB
|
|
|
|
// if that turns out to be helpful.
|
2019-04-03 15:44:49 -07:00
|
|
|
bx.abort();
|
2019-12-05 23:59:30 +01:00
|
|
|
// `abort` does not terminate the block, so we still need to generate
|
|
|
|
// an `unreachable` terminator after it.
|
2019-04-03 15:44:49 -07:00
|
|
|
bx.unreachable();
|
|
|
|
return;
|
|
|
|
}
|
2019-10-29 16:35:26 +02:00
|
|
|
let llval = match self.fn_abi.ret.mode {
|
2020-11-14 14:29:40 +01:00
|
|
|
PassMode::Ignore | PassMode::Indirect { .. } => {
|
2019-02-02 16:34:09 +00:00
|
|
|
bx.ret_void();
|
|
|
|
return;
|
2016-02-11 22:57:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
PassMode::Direct(_) | PassMode::Pair(..) => {
|
2020-01-14 01:51:59 -03:00
|
|
|
let op = self.codegen_consume(&mut bx, mir::Place::return_place().as_ref());
|
2019-02-02 16:34:09 +00:00
|
|
|
if let Ref(llval, _, align) = op.val {
|
|
|
|
bx.load(llval, align)
|
2017-02-02 06:44:30 +02:00
|
|
|
} else {
|
2019-02-02 16:34:09 +00:00
|
|
|
op.immediate_or_packed_pair(&mut bx)
|
2015-11-08 19:11:11 +01:00
|
|
|
}
|
2015-10-26 14:35:18 -04:00
|
|
|
}
|
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
PassMode::Cast(cast_ty) => {
|
|
|
|
let op = match self.locals[mir::RETURN_PLACE] {
|
|
|
|
LocalRef::Operand(Some(op)) => op,
|
|
|
|
LocalRef::Operand(None) => bug!("use of return before def"),
|
2019-12-22 17:42:04 -05:00
|
|
|
LocalRef::Place(cg_place) => OperandRef {
|
|
|
|
val: Ref(cg_place.llval, None, cg_place.align),
|
|
|
|
layout: cg_place.layout,
|
|
|
|
},
|
2019-02-02 16:34:09 +00:00
|
|
|
LocalRef::UnsizedPlace(_) => bug!("return type must be sized"),
|
|
|
|
};
|
|
|
|
let llslot = match op.val {
|
|
|
|
Immediate(_) | Pair(..) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let scratch = PlaceRef::alloca(&mut bx, self.fn_abi.ret.layout);
|
2019-02-02 16:34:09 +00:00
|
|
|
op.val.store(&mut bx, scratch);
|
|
|
|
scratch.llval
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
Ref(llval, _, align) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
assert_eq!(align, op.layout.align.abi, "return place is unaligned!");
|
2019-02-02 16:34:09 +00:00
|
|
|
llval
|
2018-11-30 15:53:44 +00:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
let addr = bx.pointercast(llslot, bx.type_ptr_to(bx.cast_backend_type(&cast_ty)));
|
2019-10-29 16:35:26 +02:00
|
|
|
bx.load(addr, self.fn_abi.ret.layout.align.abi)
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
bx.ret(llval);
|
|
|
|
}
|
2018-11-30 15:53:44 +00:00
|
|
|
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_drop_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
mut bx: Bx,
|
2020-03-31 14:27:48 -03:00
|
|
|
location: mir::Place<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
target: mir::BasicBlock,
|
|
|
|
unwind: Option<mir::BasicBlock>,
|
|
|
|
) {
|
2020-04-12 10:28:41 -07:00
|
|
|
let ty = location.ty(self.mir, bx.tcx()).ty;
|
2020-10-24 02:21:18 +02:00
|
|
|
let ty = self.monomorphize(ty);
|
2019-05-23 12:45:22 -05:00
|
|
|
let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
|
|
|
|
// we don't actually need to drop anything.
|
2019-10-14 01:38:38 -04:00
|
|
|
helper.funclet_br(self, &mut bx, target);
|
2019-12-22 17:42:04 -05:00
|
|
|
return;
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 01:51:59 -03:00
|
|
|
let place = self.codegen_place(&mut bx, location.as_ref());
|
2019-02-02 16:34:09 +00:00
|
|
|
let (args1, args2);
|
|
|
|
let mut args = if let Some(llextra) = place.llextra {
|
|
|
|
args2 = [place.llval, llextra];
|
|
|
|
&args2[..]
|
|
|
|
} else {
|
|
|
|
args1 = [place.llval];
|
|
|
|
&args1[..]
|
|
|
|
};
|
2020-08-03 00:49:11 +02:00
|
|
|
let (drop_fn, fn_abi) = match ty.kind() {
|
2019-10-29 21:46:25 +02:00
|
|
|
// FIXME(eddyb) perhaps move some of this logic into
|
|
|
|
// `Instance::resolve_drop_in_place`?
|
2019-02-02 16:34:09 +00:00
|
|
|
ty::Dynamic(..) => {
|
2019-10-29 21:46:25 +02:00
|
|
|
let virtual_drop = Instance {
|
|
|
|
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
|
|
|
|
substs: drop_fn.substs,
|
|
|
|
};
|
|
|
|
let fn_abi = FnAbi::of_instance(&bx, virtual_drop, &[]);
|
2019-02-02 16:34:09 +00:00
|
|
|
let vtable = args[1];
|
|
|
|
args = &args[..1];
|
2019-10-29 16:35:26 +02:00
|
|
|
(meth::DESTRUCTOR.get_fn(&mut bx, vtable, &fn_abi), fn_abi)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => (bx.get_fn_addr(drop_fn), FnAbi::of_instance(&bx, drop_fn, &[])),
|
2019-02-02 16:34:09 +00:00
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
helper.do_call(
|
|
|
|
self,
|
|
|
|
&mut bx,
|
|
|
|
fn_abi,
|
|
|
|
drop_fn,
|
|
|
|
args,
|
|
|
|
Some((ReturnDest::Nothing, target)),
|
|
|
|
unwind,
|
|
|
|
);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_assert_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
mut bx: Bx,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
cond: &mir::Operand<'tcx>,
|
|
|
|
expected: bool,
|
|
|
|
msg: &mir::AssertMessage<'tcx>,
|
|
|
|
target: mir::BasicBlock,
|
|
|
|
cleanup: Option<mir::BasicBlock>,
|
|
|
|
) {
|
|
|
|
let span = terminator.source_info.span;
|
2019-10-14 01:38:38 -04:00
|
|
|
let cond = self.codegen_operand(&mut bx, cond).immediate();
|
2019-02-02 16:34:09 +00:00
|
|
|
let mut const_cond = bx.const_to_opt_u128(cond, false).map(|c| c == 1);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if !bx.check_overflow() {
|
2020-06-19 18:57:15 +02:00
|
|
|
if let AssertKind::OverflowNeg(_) = *msg {
|
2019-02-02 16:34:09 +00:00
|
|
|
const_cond = Some(expected);
|
2016-06-08 19:26:19 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-06-08 19:26:19 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Don't codegen the panic block if success if known.
|
|
|
|
if const_cond == Some(expected) {
|
2019-10-14 01:38:38 -04:00
|
|
|
helper.funclet_br(self, &mut bx, target);
|
2019-02-02 16:34:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-09 18:15:15 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Pass the condition through llvm.expect for branch hinting.
|
|
|
|
let cond = bx.expect(cond, expected);
|
|
|
|
|
|
|
|
// Create the failure block and the conditional branch to it.
|
2019-10-14 01:38:38 -04:00
|
|
|
let lltarget = helper.llblock(self, target);
|
2019-02-02 16:34:09 +00:00
|
|
|
let panic_block = self.new_block("panic");
|
|
|
|
if expected {
|
|
|
|
bx.cond_br(cond, lltarget, panic_block.llbb());
|
|
|
|
} else {
|
|
|
|
bx.cond_br(cond, panic_block.llbb(), lltarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// After this point, bx is the block for the call to panic.
|
|
|
|
bx = panic_block;
|
2019-10-14 01:38:38 -04:00
|
|
|
self.set_debug_loc(&mut bx, terminator.source_info);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
// Get the location information.
|
2020-09-21 06:52:37 +03:00
|
|
|
let location = self.get_caller_location(&mut bx, terminator.source_info).immediate();
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
// Put together the arguments to the panic entry point.
|
2019-07-24 10:24:55 +02:00
|
|
|
let (lang_item, args) = match msg {
|
2020-02-12 19:40:31 +01:00
|
|
|
AssertKind::BoundsCheck { ref len, ref index } => {
|
2019-10-14 01:38:38 -04:00
|
|
|
let len = self.codegen_operand(&mut bx, len).immediate();
|
|
|
|
let index = self.codegen_operand(&mut bx, index).immediate();
|
2020-03-09 16:56:45 +01:00
|
|
|
// It's `fn panic_bounds_check(index: usize, len: usize)`,
|
|
|
|
// and `#[track_caller]` adds an implicit third argument.
|
2020-08-18 11:47:27 +01:00
|
|
|
(LangItem::PanicBoundsCheck, vec![index, len, location])
|
2016-01-30 00:18:47 +02:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => {
|
2019-09-03 16:23:21 +10:00
|
|
|
let msg_str = Symbol::intern(msg.description());
|
2019-10-23 19:30:21 -07:00
|
|
|
let msg = bx.const_str(msg_str);
|
2020-03-09 11:16:23 +01:00
|
|
|
// It's `pub fn panic(expr: &str)`, with the wide reference being passed
|
|
|
|
// as two arguments, and `#[track_caller]` adds an implicit third argument.
|
2020-08-18 11:47:27 +01:00
|
|
|
(LangItem::Panic, vec![msg.0, msg.1, location])
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
};
|
2016-01-30 00:18:47 +02:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Obtain the panic entry point.
|
|
|
|
let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item);
|
|
|
|
let instance = ty::Instance::mono(bx.tcx(), def_id);
|
2019-10-29 21:46:25 +02:00
|
|
|
let fn_abi = FnAbi::of_instance(&bx, instance, &[]);
|
2019-10-13 12:05:40 +02:00
|
|
|
let llfn = bx.get_fn_addr(instance);
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Codegen the actual panic invoke/call.
|
2019-10-14 01:38:38 -04:00
|
|
|
helper.do_call(self, &mut bx, fn_abi, llfn, &args, None, cleanup);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2020-02-29 09:40:40 +01:00
|
|
|
/// Returns `true` if this is indeed a panic intrinsic and codegen is done.
|
|
|
|
fn codegen_panic_intrinsic(
|
|
|
|
&mut self,
|
|
|
|
helper: &TerminatorCodegenHelper<'tcx>,
|
|
|
|
bx: &mut Bx,
|
2020-07-08 11:04:10 +10:00
|
|
|
intrinsic: Option<Symbol>,
|
2020-02-29 09:40:40 +01:00
|
|
|
instance: Option<Instance<'tcx>>,
|
2020-09-21 06:52:37 +03:00
|
|
|
source_info: mir::SourceInfo,
|
2020-02-29 09:40:40 +01:00
|
|
|
destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>,
|
|
|
|
cleanup: Option<mir::BasicBlock>,
|
|
|
|
) -> bool {
|
2020-03-12 19:38:09 +01:00
|
|
|
// Emit a panic or a no-op for `assert_*` intrinsics.
|
2020-02-29 09:40:40 +01:00
|
|
|
// These are intrinsics that compile to panics so that we can get a message
|
|
|
|
// which mentions the offending type, even from a const context.
|
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-13 08:43:27 +01:00
|
|
|
enum AssertIntrinsic {
|
|
|
|
Inhabited,
|
|
|
|
ZeroValid,
|
|
|
|
UninitValid,
|
2020-11-25 17:00:28 -05:00
|
|
|
}
|
2020-02-29 09:40:40 +01:00
|
|
|
let panic_intrinsic = intrinsic.and_then(|i| match i {
|
2020-07-08 11:04:10 +10:00
|
|
|
sym::assert_inhabited => Some(AssertIntrinsic::Inhabited),
|
|
|
|
sym::assert_zero_valid => Some(AssertIntrinsic::ZeroValid),
|
|
|
|
sym::assert_uninit_valid => Some(AssertIntrinsic::UninitValid),
|
2020-02-29 09:40:40 +01:00
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
if let Some(intrinsic) = panic_intrinsic {
|
2020-03-13 08:43:27 +01:00
|
|
|
use AssertIntrinsic::*;
|
2020-02-29 09:40:40 +01:00
|
|
|
let ty = instance.unwrap().substs.type_at(0);
|
|
|
|
let layout = bx.layout_of(ty);
|
|
|
|
let do_panic = match intrinsic {
|
2020-03-13 08:43:27 +01:00
|
|
|
Inhabited => layout.abi.is_uninhabited(),
|
2020-02-29 09:40:40 +01:00
|
|
|
// We unwrap as the error type is `!`.
|
2020-03-13 08:43:27 +01:00
|
|
|
ZeroValid => !layout.might_permit_raw_init(bx, /*zero:*/ true).unwrap(),
|
2020-02-29 09:40:40 +01:00
|
|
|
// We unwrap as the error type is `!`.
|
2020-03-13 08:43:27 +01:00
|
|
|
UninitValid => !layout.might_permit_raw_init(bx, /*zero:*/ false).unwrap(),
|
2020-02-29 09:40:40 +01:00
|
|
|
};
|
|
|
|
if do_panic {
|
2020-09-02 10:40:56 +03:00
|
|
|
let msg_str = with_no_trimmed_paths(|| {
|
|
|
|
if layout.abi.is_uninhabited() {
|
|
|
|
// Use this error even for the other intrinsics as it is more precise.
|
|
|
|
format!("attempted to instantiate uninhabited type `{}`", ty)
|
|
|
|
} else if intrinsic == ZeroValid {
|
|
|
|
format!("attempted to zero-initialize type `{}`, which is invalid", ty)
|
|
|
|
} else {
|
|
|
|
format!("attempted to leave type `{}` uninitialized, which is invalid", ty)
|
|
|
|
}
|
|
|
|
});
|
2020-02-29 09:40:40 +01:00
|
|
|
let msg = bx.const_str(Symbol::intern(&msg_str));
|
2020-09-21 06:52:37 +03:00
|
|
|
let location = self.get_caller_location(bx, source_info).immediate();
|
2020-02-29 09:40:40 +01:00
|
|
|
|
|
|
|
// Obtain the panic entry point.
|
|
|
|
// FIXME: dedup this with `codegen_assert_terminator` above.
|
2020-09-21 06:52:37 +03:00
|
|
|
let def_id =
|
|
|
|
common::langcall(bx.tcx(), Some(source_info.span), "", LangItem::Panic);
|
2020-02-29 09:40:40 +01:00
|
|
|
let instance = ty::Instance::mono(bx.tcx(), def_id);
|
|
|
|
let fn_abi = FnAbi::of_instance(bx, instance, &[]);
|
|
|
|
let llfn = bx.get_fn_addr(instance);
|
|
|
|
|
|
|
|
// Codegen the actual panic invoke/call.
|
|
|
|
helper.do_call(
|
|
|
|
self,
|
|
|
|
bx,
|
|
|
|
fn_abi,
|
|
|
|
llfn,
|
|
|
|
&[msg.0, msg.1, location],
|
|
|
|
destination.as_ref().map(|(_, bb)| (ReturnDest::Nothing, *bb)),
|
|
|
|
cleanup,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// a NOP
|
|
|
|
let target = destination.as_ref().unwrap().1;
|
|
|
|
helper.funclet_br(self, bx, target)
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_call_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
mut bx: Bx,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
func: &mir::Operand<'tcx>,
|
2020-12-30 12:59:07 +01:00
|
|
|
args: &[mir::Operand<'tcx>],
|
2019-02-02 16:34:09 +00:00
|
|
|
destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>,
|
|
|
|
cleanup: Option<mir::BasicBlock>,
|
2020-06-09 15:34:23 -04:00
|
|
|
fn_span: Span,
|
2019-02-02 16:34:09 +00:00
|
|
|
) {
|
2020-09-21 06:52:37 +03:00
|
|
|
let source_info = terminator.source_info;
|
|
|
|
let span = source_info.span;
|
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar.
|
2019-10-14 01:38:38 -04:00
|
|
|
let callee = self.codegen_operand(&mut bx, func);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
let (instance, mut llfn) = match *callee.layout.ty.kind() {
|
2019-12-22 17:42:04 -05:00
|
|
|
ty::FnDef(def_id, substs) => (
|
|
|
|
Some(
|
|
|
|
ty::Instance::resolve(bx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs)
|
2020-04-10 05:13:29 +03:00
|
|
|
.unwrap()
|
2020-06-22 13:57:03 +01:00
|
|
|
.unwrap()
|
|
|
|
.polymorphize(bx.tcx()),
|
2019-12-22 17:42:04 -05:00
|
|
|
),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
ty::FnPtr(_) => (None, Some(callee.immediate())),
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => bug!("{} is not callable", callee.layout.ty),
|
|
|
|
};
|
|
|
|
let def = instance.map(|i| i.def);
|
2019-10-29 21:46:25 +02:00
|
|
|
|
|
|
|
if let Some(ty::InstanceDef::DropGlue(_, None)) = def {
|
|
|
|
// Empty drop glue; a no-op.
|
|
|
|
let &(_, target) = destination.as_ref().unwrap();
|
|
|
|
helper.funclet_br(self, &mut bx, target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(eddyb) avoid computing this if possible, when `instance` is
|
|
|
|
// available - right now `sig` is only needed for getting the `abi`
|
|
|
|
// and figuring out how many extra args were passed to a C-variadic `fn`.
|
2019-02-02 16:34:09 +00:00
|
|
|
let sig = callee.layout.ty.fn_sig(bx.tcx());
|
2019-10-29 22:08:50 +02:00
|
|
|
let abi = sig.abi();
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
// Handle intrinsics old codegen wants Expr's for, ourselves.
|
|
|
|
let intrinsic = match def {
|
2020-07-08 11:04:10 +10:00
|
|
|
Some(ty::InstanceDef::Intrinsic(def_id)) => Some(bx.tcx().item_name(def_id)),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
2019-02-02 16:34:09 +00:00
|
|
|
};
|
2016-06-05 14:38:29 +03:00
|
|
|
|
2019-10-29 22:08:50 +02:00
|
|
|
let extra_args = &args[sig.inputs().skip_binder().len()..];
|
2019-12-22 17:42:04 -05:00
|
|
|
let extra_args = extra_args
|
|
|
|
.iter()
|
|
|
|
.map(|op_arg| {
|
2020-04-12 10:28:41 -07:00
|
|
|
let op_ty = op_arg.ty(self.mir, bx.tcx());
|
2020-10-24 02:21:18 +02:00
|
|
|
self.monomorphize(op_ty)
|
2019-12-22 17:42:04 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2019-10-29 22:08:50 +02:00
|
|
|
|
|
|
|
let fn_abi = match instance {
|
|
|
|
Some(instance) => FnAbi::of_instance(&bx, instance, &extra_args),
|
2019-12-22 17:42:04 -05:00
|
|
|
None => FnAbi::of_fn_ptr(&bx, sig, &extra_args),
|
2019-10-29 22:08:50 +02:00
|
|
|
};
|
|
|
|
|
2020-07-08 11:04:10 +10:00
|
|
|
if intrinsic == Some(sym::transmute) {
|
2019-02-02 16:34:09 +00:00
|
|
|
if let Some(destination_ref) = destination.as_ref() {
|
2020-03-31 14:35:01 -03:00
|
|
|
let &(dest, target) = destination_ref;
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_transmute(&mut bx, &args[0], dest);
|
|
|
|
helper.funclet_br(self, &mut bx, target);
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
// If we are trying to transmute to an uninhabited type,
|
|
|
|
// it is likely there is no allotted destination. In fact,
|
|
|
|
// transmuting to an uninhabited type is UB, which means
|
|
|
|
// we can do what we like. Here, we declare that transmuting
|
|
|
|
// into an uninhabited type is impossible, so anything following
|
|
|
|
// it must be unreachable.
|
2020-03-31 18:16:47 +02:00
|
|
|
assert_eq!(fn_abi.ret.layout.abi, abi::Abi::Uninhabited);
|
2019-02-02 16:34:09 +00:00
|
|
|
bx.unreachable();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2020-02-29 09:40:40 +01:00
|
|
|
if self.codegen_panic_intrinsic(
|
|
|
|
&helper,
|
|
|
|
&mut bx,
|
|
|
|
intrinsic,
|
|
|
|
instance,
|
2020-09-21 06:52:37 +03:00
|
|
|
source_info,
|
2020-02-29 09:40:40 +01:00
|
|
|
destination,
|
|
|
|
cleanup,
|
|
|
|
) {
|
2019-02-02 16:34:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-17 01:06:52 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// The arguments we'll be passing. Plus one to account for outptr, if used.
|
2019-10-29 16:35:26 +02:00
|
|
|
let arg_count = fn_abi.args.len() + fn_abi.ret.is_indirect() as usize;
|
2019-02-02 16:34:09 +00:00
|
|
|
let mut llargs = Vec::with_capacity(arg_count);
|
|
|
|
|
|
|
|
// Prepare the return value destination
|
2020-03-31 14:31:34 -03:00
|
|
|
let ret_dest = if let Some((dest, _)) = *destination {
|
2019-02-02 16:34:09 +00:00
|
|
|
let is_intrinsic = intrinsic.is_some();
|
2019-12-22 17:42:04 -05:00
|
|
|
self.make_return_dest(&mut bx, dest, &fn_abi.ret, &mut llargs, is_intrinsic)
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
ReturnDest::Nothing
|
|
|
|
};
|
2016-04-08 15:37:56 +12:00
|
|
|
|
2020-07-08 11:04:10 +10:00
|
|
|
if intrinsic == Some(sym::caller_location) {
|
2019-10-09 08:25:41 -07:00
|
|
|
if let Some((_, target)) = destination.as_ref() {
|
2020-09-21 06:52:37 +03:00
|
|
|
let location = self
|
|
|
|
.get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info });
|
2019-10-09 08:25:41 -07:00
|
|
|
|
|
|
|
if let ReturnDest::IndirectOperand(tmp, _) = ret_dest {
|
2019-10-24 17:35:02 -07:00
|
|
|
location.val.store(&mut bx, tmp);
|
2019-10-09 08:25:41 -07:00
|
|
|
}
|
2019-10-29 16:35:26 +02:00
|
|
|
self.store_return(&mut bx, ret_dest, &fn_abi.ret, location.immediate());
|
2019-10-09 08:25:41 -07:00
|
|
|
helper.funclet_br(self, &mut bx, *target);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
match intrinsic {
|
|
|
|
None | Some(sym::drop_in_place) => {}
|
2021-01-23 08:57:04 +00:00
|
|
|
Some(sym::copy_nonoverlapping) => unreachable!(),
|
2021-01-23 03:55:41 +00:00
|
|
|
Some(intrinsic) => {
|
|
|
|
let dest = match ret_dest {
|
|
|
|
_ if fn_abi.ret.is_indirect() => llargs[0],
|
|
|
|
ReturnDest::Nothing => {
|
|
|
|
bx.const_undef(bx.type_ptr_to(bx.arg_memory_ty(&fn_abi.ret)))
|
|
|
|
}
|
|
|
|
ReturnDest::IndirectOperand(dst, _) | ReturnDest::Store(dst) => dst.llval,
|
|
|
|
ReturnDest::DirectOperand(_) => {
|
|
|
|
bug!("Cannot use direct operand with an intrinsic call")
|
|
|
|
}
|
|
|
|
};
|
2016-03-06 13:23:20 +02:00
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
let args: Vec<_> = args
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, arg)| {
|
|
|
|
// 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 i == 2 && intrinsic.as_str().starts_with("simd_shuffle") {
|
|
|
|
if let mir::Operand::Constant(constant) = arg {
|
|
|
|
let c = self.eval_mir_constant(constant);
|
2021-03-08 14:14:11 +00:00
|
|
|
let (llval, ty) =
|
|
|
|
self.simd_shuffle_indices(&bx, constant.span, constant.ty(), c);
|
2021-01-23 03:55:41 +00:00
|
|
|
return OperandRef {
|
|
|
|
val: Immediate(llval),
|
|
|
|
layout: bx.layout_of(ty),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "shuffle indices must be constant");
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2018-08-19 17:36:04 +02:00
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
self.codegen_operand(&mut bx, arg)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-01-23 08:57:04 +00:00
|
|
|
Self::codegen_intrinsic_call(
|
2021-01-23 03:55:41 +00:00
|
|
|
&mut bx,
|
|
|
|
*instance.as_ref().unwrap(),
|
|
|
|
&fn_abi,
|
|
|
|
&args,
|
|
|
|
dest,
|
|
|
|
span,
|
|
|
|
);
|
2017-09-20 02:32:22 +03:00
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
if let ReturnDest::IndirectOperand(dst, _) = ret_dest {
|
|
|
|
self.store_return(&mut bx, ret_dest, &fn_abi.ret, dst.llval);
|
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
if let Some((_, target)) = *destination {
|
|
|
|
helper.funclet_br(self, &mut bx, target);
|
|
|
|
} else {
|
|
|
|
bx.unreachable();
|
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Split the rust-call tupled arguments off.
|
|
|
|
let (first_args, untuple) = if abi == Abi::RustCall && !args.is_empty() {
|
|
|
|
let (tup, args) = args.split_last().unwrap();
|
|
|
|
(args, Some(tup))
|
|
|
|
} else {
|
2021-02-16 00:30:06 +01:00
|
|
|
(args, None)
|
2019-02-02 16:34:09 +00:00
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
'make_args: for (i, arg) in first_args.iter().enumerate() {
|
2019-10-14 01:38:38 -04:00
|
|
|
let mut op = self.codegen_operand(&mut bx, arg);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
if let (0, Some(ty::InstanceDef::Virtual(_, idx))) = (i, def) {
|
|
|
|
if let Pair(..) = op.val {
|
|
|
|
// In the case of Rc<Self>, we need to explicitly pass a
|
|
|
|
// *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack
|
|
|
|
// that is understood elsewhere in the compiler as a method on
|
|
|
|
// `dyn Trait`.
|
|
|
|
// To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until
|
|
|
|
// we get a value of a built-in pointer type
|
|
|
|
'descend_newtypes: while !op.layout.ty.is_unsafe_ptr()
|
2019-12-22 17:42:04 -05:00
|
|
|
&& !op.layout.ty.is_region_ptr()
|
2019-02-02 16:34:09 +00:00
|
|
|
{
|
2019-11-12 03:19:40 -08:00
|
|
|
for i in 0..op.layout.fields.count() {
|
2019-02-02 16:34:09 +00:00
|
|
|
let field = op.extract_field(&mut bx, i);
|
|
|
|
if !field.layout.is_zst() {
|
|
|
|
// we found the one non-zero-sized field that is allowed
|
|
|
|
// now find *its* non-zero-sized field, or stop if it's a
|
|
|
|
// pointer
|
|
|
|
op = field;
|
2019-12-22 17:42:04 -05:00
|
|
|
continue 'descend_newtypes;
|
2018-09-20 03:16:10 -04:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2018-09-20 03:16:10 -04:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
span_bug!(span, "receiver has no non-zero-sized fields {:?}", op);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that we have `*dyn Trait` or `&dyn Trait`, split it up into its
|
|
|
|
// data pointer and vtable. Look up the method in the vtable, and pass
|
|
|
|
// the data pointer as the first argument
|
|
|
|
match op.val {
|
|
|
|
Pair(data_ptr, meta) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
llfn = Some(
|
|
|
|
meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi),
|
|
|
|
);
|
2017-10-10 20:54:50 +03:00
|
|
|
llargs.push(data_ptr);
|
2019-12-22 17:42:04 -05:00
|
|
|
continue 'make_args;
|
2017-09-20 05:16:06 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
other => bug!("expected a Pair, got {:?}", other),
|
2017-09-20 05:16:06 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
} else if let Ref(data_ptr, Some(meta), _) = op.val {
|
|
|
|
// by-value dynamic dispatch
|
2019-12-22 17:42:04 -05:00
|
|
|
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi));
|
2019-02-02 16:34:09 +00:00
|
|
|
llargs.push(data_ptr);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "can't codegen a virtual call on {:?}", op);
|
2017-09-20 02:32:22 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The callee needs to own the argument memory if we pass it
|
|
|
|
// by-ref, so make a local copy of non-immediate constants.
|
|
|
|
match (arg, op.val) {
|
2019-12-22 17:42:04 -05:00
|
|
|
(&mir::Operand::Copy(_), Ref(_, None, _))
|
|
|
|
| (&mir::Operand::Constant(_), Ref(_, None, _)) => {
|
2019-09-12 19:04:30 +03:00
|
|
|
let tmp = PlaceRef::alloca(&mut bx, op.layout);
|
2019-02-02 16:34:09 +00:00
|
|
|
op.val.store(&mut bx, tmp);
|
|
|
|
op.val = Ref(tmp.llval, None, tmp.align);
|
2017-09-20 02:32:22 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2017-09-20 02:32:22 +03:00
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
self.codegen_argument(&mut bx, op, &mut llargs, &fn_abi.args[i]);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
if let Some(tup) = untuple {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.codegen_arguments_untupled(
|
|
|
|
&mut bx,
|
|
|
|
tup,
|
|
|
|
&mut llargs,
|
|
|
|
&fn_abi.args[first_args.len()..],
|
|
|
|
)
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 06:04:14 -08:00
|
|
|
let needs_location =
|
2019-12-06 17:05:51 -08:00
|
|
|
instance.map_or(false, |i| i.def.requires_caller_location(self.cx.tcx()));
|
2019-11-07 06:04:14 -08:00
|
|
|
if needs_location {
|
|
|
|
assert_eq!(
|
2019-12-22 17:42:04 -05:00
|
|
|
fn_abi.args.len(),
|
|
|
|
args.len() + 1,
|
2019-11-07 06:04:14 -08:00
|
|
|
"#[track_caller] fn's must have 1 more argument in their ABI than in their MIR",
|
|
|
|
);
|
2020-09-21 06:52:37 +03:00
|
|
|
let location =
|
|
|
|
self.get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info });
|
2020-06-09 15:34:23 -04:00
|
|
|
debug!(
|
|
|
|
"codegen_call_terminator({:?}): location={:?} (fn_span {:?})",
|
|
|
|
terminator, location, fn_span
|
|
|
|
);
|
|
|
|
|
2019-12-06 17:05:51 -08:00
|
|
|
let last_arg = fn_abi.args.last().unwrap();
|
2019-11-07 06:04:14 -08:00
|
|
|
self.codegen_argument(&mut bx, location, &mut llargs, last_arg);
|
|
|
|
}
|
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
let fn_ptr = match (llfn, instance) {
|
|
|
|
(Some(llfn), _) => llfn,
|
2019-10-13 12:05:40 +02:00
|
|
|
(None, Some(instance)) => bx.get_fn_addr(instance),
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => span_bug!(span, "no llfn for call"),
|
|
|
|
};
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
helper.do_call(
|
|
|
|
self,
|
|
|
|
&mut bx,
|
|
|
|
fn_abi,
|
|
|
|
fn_ptr,
|
|
|
|
&llargs,
|
|
|
|
destination.as_ref().map(|&(_, target)| (ret_dest, target)),
|
|
|
|
cleanup,
|
|
|
|
);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2020-05-11 17:53:32 +01:00
|
|
|
|
|
|
|
fn codegen_asm_terminator(
|
|
|
|
&mut self,
|
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
|
|
|
mut bx: Bx,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
template: &[ast::InlineAsmTemplatePiece],
|
|
|
|
operands: &[mir::InlineAsmOperand<'tcx>],
|
|
|
|
options: ast::InlineAsmOptions,
|
2020-05-26 20:07:59 +01:00
|
|
|
line_spans: &[Span],
|
2020-05-11 17:53:32 +01:00
|
|
|
destination: Option<mir::BasicBlock>,
|
|
|
|
) {
|
|
|
|
let span = terminator.source_info.span;
|
|
|
|
|
|
|
|
let operands: Vec<_> = operands
|
|
|
|
.iter()
|
|
|
|
.map(|op| match *op {
|
|
|
|
mir::InlineAsmOperand::In { reg, ref value } => {
|
|
|
|
let value = self.codegen_operand(&mut bx, value);
|
|
|
|
InlineAsmOperandRef::In { reg, value }
|
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::Out { reg, late, ref place } => {
|
|
|
|
let place = place.map(|place| self.codegen_place(&mut bx, place.as_ref()));
|
|
|
|
InlineAsmOperandRef::Out { reg, late, place }
|
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::InOut { reg, late, ref in_value, ref out_place } => {
|
|
|
|
let in_value = self.codegen_operand(&mut bx, in_value);
|
|
|
|
let out_place =
|
|
|
|
out_place.map(|out_place| self.codegen_place(&mut bx, out_place.as_ref()));
|
|
|
|
InlineAsmOperandRef::InOut { reg, late, in_value, out_place }
|
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::Const { ref value } => {
|
2021-04-06 05:50:55 +01:00
|
|
|
let const_value = self
|
|
|
|
.eval_mir_constant(value)
|
|
|
|
.unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved"));
|
|
|
|
let ty = value.ty();
|
|
|
|
let size = bx.layout_of(ty).size;
|
|
|
|
let scalar = match const_value {
|
|
|
|
ConstValue::Scalar(s) => s,
|
|
|
|
_ => span_bug!(
|
|
|
|
span,
|
|
|
|
"expected Scalar for promoted asm const, but got {:#?}",
|
|
|
|
const_value
|
|
|
|
),
|
|
|
|
};
|
|
|
|
let value = scalar.assert_bits(size);
|
|
|
|
let string = match ty.kind() {
|
|
|
|
ty::Uint(_) => value.to_string(),
|
|
|
|
ty::Int(int_ty) => {
|
|
|
|
match int_ty.normalize(bx.tcx().sess.target.pointer_width) {
|
|
|
|
ty::IntTy::I8 => (value as i8).to_string(),
|
|
|
|
ty::IntTy::I16 => (value as i16).to_string(),
|
|
|
|
ty::IntTy::I32 => (value as i32).to_string(),
|
|
|
|
ty::IntTy::I64 => (value as i64).to_string(),
|
|
|
|
ty::IntTy::I128 => (value as i128).to_string(),
|
|
|
|
ty::IntTy::Isize => unreachable!(),
|
2020-05-11 17:53:32 +01:00
|
|
|
}
|
2021-04-06 05:50:55 +01:00
|
|
|
}
|
|
|
|
ty::Float(ty::FloatTy::F32) => f32::from_bits(value as u32).to_string(),
|
|
|
|
ty::Float(ty::FloatTy::F64) => f64::from_bits(value as u64).to_string(),
|
|
|
|
_ => span_bug!(span, "asm const has bad type {}", ty),
|
|
|
|
};
|
|
|
|
InlineAsmOperandRef::Const { string }
|
2020-05-11 17:53:32 +01:00
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::SymFn { ref value } => {
|
2020-10-24 02:21:18 +02:00
|
|
|
let literal = self.monomorphize(value.literal);
|
2021-03-08 16:18:03 +00:00
|
|
|
if let ty::FnDef(def_id, substs) = *literal.ty().kind() {
|
2020-05-24 02:04:49 +01:00
|
|
|
let instance = ty::Instance::resolve_for_fn_ptr(
|
2020-05-11 17:53:32 +01:00
|
|
|
bx.tcx(),
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
def_id,
|
|
|
|
substs,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
InlineAsmOperandRef::SymFn { instance }
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "invalid type for asm sym (fn)");
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 15:52:38 +01:00
|
|
|
mir::InlineAsmOperand::SymStatic { def_id } => {
|
|
|
|
InlineAsmOperandRef::SymStatic { def_id }
|
2020-05-11 17:53:32 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-05-26 20:07:59 +01:00
|
|
|
bx.codegen_inline_asm(template, &operands, options, line_spans);
|
2020-05-11 17:53:32 +01:00
|
|
|
|
|
|
|
if let Some(target) = destination {
|
|
|
|
helper.funclet_br(self, &mut bx, target);
|
|
|
|
} else {
|
|
|
|
bx.unreachable();
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 01:41:17 -04:00
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn codegen_block(&mut self, bb: mir::BasicBlock) {
|
2019-02-02 16:34:09 +00:00
|
|
|
let mut bx = self.build_block(bb);
|
2019-11-06 00:04:53 -05:00
|
|
|
let mir = self.mir;
|
|
|
|
let data = &mir[bb];
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
debug!("codegen_block({:?}={:?})", bb, data);
|
|
|
|
|
|
|
|
for statement in &data.statements {
|
2019-10-14 01:38:38 -04:00
|
|
|
bx = self.codegen_statement(bx, statement);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_terminator(bx, bb, data.terminator());
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn codegen_terminator(
|
|
|
|
&mut self,
|
|
|
|
mut bx: Bx,
|
|
|
|
bb: mir::BasicBlock,
|
2019-12-22 17:42:04 -05:00
|
|
|
terminator: &'tcx mir::Terminator<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
) {
|
|
|
|
debug!("codegen_terminator: {:?}", terminator);
|
|
|
|
|
|
|
|
// Create the cleanup bundle, if needed.
|
|
|
|
let funclet_bb = self.cleanup_kinds[bb].funclet_bb(bb);
|
2019-12-22 17:42:04 -05:00
|
|
|
let helper = TerminatorCodegenHelper { bb, terminator, funclet_bb };
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2019-10-14 01:38:38 -04:00
|
|
|
self.set_debug_loc(&mut bx, terminator.source_info);
|
2019-02-02 16:34:09 +00:00
|
|
|
match terminator.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
mir::TerminatorKind::Resume => self.codegen_resume_terminator(helper, bx),
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
mir::TerminatorKind::Abort => {
|
|
|
|
bx.abort();
|
2019-12-05 23:59:30 +01:00
|
|
|
// `abort` does not terminate the block, so we still need to generate
|
|
|
|
// an `unreachable` terminator after it.
|
2019-02-02 16:34:09 +00:00
|
|
|
bx.unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Goto { target } => {
|
2020-10-15 09:28:27 -04:00
|
|
|
if bb == target {
|
2021-03-08 01:59:10 +02:00
|
|
|
// This is an unconditional branch back to this same basic block. That means we
|
|
|
|
// have something like a `loop {}` statement. LLVM versions before 12.0
|
|
|
|
// miscompile this because they assume forward progress. For older versions
|
|
|
|
// try to handle just this specific case which comes up commonly in practice
|
|
|
|
// (e.g., in embedded code).
|
2020-10-15 09:28:27 -04:00
|
|
|
//
|
2021-03-08 01:59:10 +02:00
|
|
|
// NB: the `sideeffect` currently checks for the LLVM version used internally.
|
|
|
|
bx.sideeffect();
|
2020-10-15 09:28:27 -04:00
|
|
|
}
|
2021-03-08 01:59:10 +02:00
|
|
|
|
2019-10-14 01:38:38 -04:00
|
|
|
helper.funclet_br(self, &mut bx, target);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 17:36:04 +02:00
|
|
|
mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref targets } => {
|
|
|
|
self.codegen_switchint_terminator(helper, bx, discr, switch_ty, targets);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2015-12-13 05:48:43 -08:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
mir::TerminatorKind::Return => {
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_return_terminator(bx);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Unreachable => {
|
|
|
|
bx.unreachable();
|
|
|
|
}
|
|
|
|
|
2020-06-10 09:56:54 +02:00
|
|
|
mir::TerminatorKind::Drop { place, target, unwind } => {
|
|
|
|
self.codegen_drop_terminator(helper, bx, place, target, unwind);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.codegen_assert_terminator(
|
|
|
|
helper, bx, terminator, cond, expected, msg, target, cleanup,
|
|
|
|
);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::DropAndReplace { .. } => {
|
|
|
|
bug!("undesugared DropAndReplace in codegen: {:?}", terminator);
|
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
|
|
|
ref destination,
|
|
|
|
cleanup,
|
2019-12-22 17:42:04 -05:00
|
|
|
from_hir_call: _,
|
2020-06-09 15:34:23 -04:00
|
|
|
fn_span,
|
2019-02-02 16:34:09 +00:00
|
|
|
} => {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.codegen_call_terminator(
|
|
|
|
helper,
|
|
|
|
bx,
|
|
|
|
terminator,
|
|
|
|
func,
|
|
|
|
args,
|
|
|
|
destination,
|
|
|
|
cleanup,
|
2020-06-09 15:34:23 -04:00
|
|
|
fn_span,
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
mir::TerminatorKind::GeneratorDrop | mir::TerminatorKind::Yield { .. } => {
|
|
|
|
bug!("generator ops in codegen")
|
|
|
|
}
|
2020-06-02 09:15:24 +02:00
|
|
|
mir::TerminatorKind::FalseEdge { .. } | mir::TerminatorKind::FalseUnwind { .. } => {
|
2019-12-22 17:42:04 -05:00
|
|
|
bug!("borrowck false edges in codegen")
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
2020-02-17 21:36:01 +00:00
|
|
|
|
2020-05-26 20:07:59 +01:00
|
|
|
mir::TerminatorKind::InlineAsm {
|
|
|
|
template,
|
|
|
|
ref operands,
|
|
|
|
options,
|
|
|
|
line_spans,
|
|
|
|
destination,
|
|
|
|
} => {
|
2020-05-11 17:53:32 +01:00
|
|
|
self.codegen_asm_terminator(
|
|
|
|
helper,
|
|
|
|
bx,
|
|
|
|
terminator,
|
|
|
|
template,
|
|
|
|
operands,
|
|
|
|
options,
|
2020-05-26 20:07:59 +01:00
|
|
|
line_spans,
|
2020-05-11 17:53:32 +01:00
|
|
|
destination,
|
|
|
|
);
|
2020-02-17 21:36:01 +00:00
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn codegen_argument(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
op: OperandRef<'tcx, Bx::Value>,
|
|
|
|
llargs: &mut Vec<Bx::Value>,
|
2019-12-22 17:42:04 -05:00
|
|
|
arg: &ArgAbi<'tcx, Ty<'tcx>>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) {
|
2016-03-06 13:23:20 +02:00
|
|
|
// Fill padding with undef value, where applicable.
|
|
|
|
if let Some(ty) = arg.pad {
|
2018-11-27 19:00:25 +01:00
|
|
|
llargs.push(bx.const_undef(bx.reg_backend_type(&ty)))
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if arg.is_ignore() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:54:50 +03:00
|
|
|
if let PassMode::Pair(..) = arg.mode {
|
|
|
|
match op.val {
|
|
|
|
Pair(a, b) => {
|
|
|
|
llargs.push(a);
|
|
|
|
llargs.push(b);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("codegen_argument: {:?} invalid for pair argument", op),
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
2018-08-03 23:32:21 +09:00
|
|
|
} else if arg.is_unsized_indirect() {
|
2018-05-29 00:12:55 +09:00
|
|
|
match op.val {
|
2018-08-03 23:50:13 +09:00
|
|
|
Ref(a, Some(b), _) => {
|
2018-05-29 00:12:55 +09:00
|
|
|
llargs.push(a);
|
|
|
|
llargs.push(b);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("codegen_argument: {:?} invalid for unsized indirect argument", op),
|
2018-05-29 00:12:55 +09:00
|
|
|
}
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
// 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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
Immediate(_) | Pair(..) => match arg.mode {
|
2020-11-14 14:29:40 +01:00
|
|
|
PassMode::Indirect { .. } | PassMode::Cast(_) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let scratch = PlaceRef::alloca(bx, arg.layout);
|
|
|
|
op.val.store(bx, scratch);
|
|
|
|
(scratch.llval, scratch.align, true)
|
2016-06-20 23:55:14 +03:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => (op.immediate_or_packed_pair(bx), arg.layout.align.abi, false),
|
|
|
|
},
|
2018-08-03 23:50:13 +09:00
|
|
|
Ref(llval, _, align) => {
|
2018-09-09 01:16:45 +03:00
|
|
|
if arg.is_indirect() && align < arg.layout.align.abi {
|
2017-12-01 19:16:39 +02:00
|
|
|
// `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.
|
|
|
|
|
2019-09-12 19:04:30 +03:00
|
|
|
let scratch = PlaceRef::alloca(bx, arg.layout);
|
2019-12-22 17:42:04 -05:00
|
|
|
base::memcpy_ty(
|
|
|
|
bx,
|
|
|
|
scratch.llval,
|
|
|
|
scratch.align,
|
|
|
|
llval,
|
|
|
|
align,
|
|
|
|
op.layout,
|
|
|
|
MemFlags::empty(),
|
|
|
|
);
|
2017-12-01 19:16:39 +02:00
|
|
|
(scratch.llval, scratch.align, true)
|
|
|
|
} else {
|
|
|
|
(llval, align, true)
|
|
|
|
}
|
2017-02-06 17:27:09 +01:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if by_ref && !arg.is_indirect() {
|
|
|
|
// Have to load the argument, maybe while casting it.
|
2017-10-10 20:54:50 +03:00
|
|
|
if let PassMode::Cast(ty) = arg.mode {
|
2019-12-22 17:42:04 -05:00
|
|
|
let addr = bx.pointercast(llval, bx.type_ptr_to(bx.cast_backend_type(&ty)));
|
2018-09-09 01:16:45 +03:00
|
|
|
llval = bx.load(addr, align.min(arg.layout.align.abi));
|
2016-03-06 13:23:20 +02:00
|
|
|
} else {
|
2017-12-01 14:31:47 +02:00
|
|
|
// We can't use `PlaceRef::load` here because the argument
|
2017-09-26 14:41:06 +03:00
|
|
|
// may have a type we don't treat as immediate, but the ABI
|
|
|
|
// used for this call is passing it by-value. In that case,
|
|
|
|
// the load would just produce `OperandValue::Ref` instead
|
|
|
|
// of the `OperandValue::Immediate` we need for the call.
|
2018-01-05 07:12:32 +02:00
|
|
|
llval = bx.load(llval, align);
|
2020-03-31 18:16:47 +02:00
|
|
|
if let abi::Abi::Scalar(ref scalar) = arg.layout.abi {
|
2017-09-26 14:41:06 +03:00
|
|
|
if scalar.is_bool() {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.range_metadata(llval, 0..2);
|
2017-09-26 14:41:06 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-28 22:43:53 +00:00
|
|
|
// We store bools as `i8` so we need to truncate to `i1`.
|
2020-08-29 18:10:01 +02:00
|
|
|
llval = bx.to_immediate(llval, arg.layout);
|
2017-09-23 15:04:37 +03:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llargs.push(llval);
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn codegen_arguments_untupled(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
operand: &mir::Operand<'tcx>,
|
|
|
|
llargs: &mut Vec<Bx::Value>,
|
2019-12-22 17:42:04 -05:00
|
|
|
args: &[ArgAbi<'tcx, Ty<'tcx>>],
|
2018-09-20 15:47:22 +02:00
|
|
|
) {
|
2019-10-14 01:38:38 -04:00
|
|
|
let tuple = self.codegen_operand(bx, operand);
|
2016-03-06 13:23:20 +02:00
|
|
|
|
2016-04-21 11:43:01 +12:00
|
|
|
// Handle both by-ref and immediate tuples.
|
2018-08-03 23:50:13 +09:00
|
|
|
if let Ref(llval, None, align) = tuple.val {
|
2019-08-29 14:24:50 -04:00
|
|
|
let tuple_ptr = PlaceRef::new_sized_aligned(llval, tuple.layout, align);
|
2017-10-09 19:56:41 +03:00
|
|
|
for i in 0..tuple.layout.fields.count() {
|
2018-01-05 07:12:32 +02:00
|
|
|
let field_ptr = tuple_ptr.project_field(bx, i);
|
2018-10-05 15:08:49 +02:00
|
|
|
let field = bx.load_operand(field_ptr);
|
|
|
|
self.codegen_argument(bx, field, llargs, &args[i]);
|
2016-04-20 15:27:15 +12:00
|
|
|
}
|
2018-08-03 23:50:13 +09:00
|
|
|
} else if let Ref(_, Some(_), _) = tuple.val {
|
2018-05-29 00:12:55 +09:00
|
|
|
bug!("closure arguments must be sized")
|
2017-10-09 19:56:41 +03:00
|
|
|
} else {
|
|
|
|
// If the tuple is immediate, the elements are as well.
|
|
|
|
for i in 0..tuple.layout.fields.count() {
|
2018-01-05 07:12:32 +02:00
|
|
|
let op = tuple.extract_field(bx, i);
|
2018-05-08 16:10:16 +03:00
|
|
|
self.codegen_argument(bx, op, llargs, &args[i]);
|
2016-04-20 15:27:15 +12:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 06:52:37 +03:00
|
|
|
fn get_caller_location(
|
|
|
|
&mut self,
|
|
|
|
bx: &mut Bx,
|
|
|
|
mut source_info: mir::SourceInfo,
|
|
|
|
) -> OperandRef<'tcx, Bx::Value> {
|
|
|
|
let tcx = bx.tcx();
|
|
|
|
|
|
|
|
let mut span_to_caller_location = |span: Span| {
|
2019-10-27 17:31:12 -07:00
|
|
|
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
|
2020-09-21 06:52:37 +03:00
|
|
|
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
|
|
|
|
let const_loc = tcx.const_caller_location((
|
2021-04-19 23:27:02 +01:00
|
|
|
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
|
2019-10-27 17:31:12 -07:00
|
|
|
caller.line as u32,
|
|
|
|
caller.col_display as u32 + 1,
|
|
|
|
));
|
2020-02-15 12:57:46 +13:00
|
|
|
OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty())
|
2020-09-21 06:52:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Walk up the `SourceScope`s, in case some of them are from MIR inlining.
|
|
|
|
// If so, the starting `source_info.span` is in the innermost inlined
|
|
|
|
// function, and will be replaced with outer callsite spans as long
|
|
|
|
// as the inlined functions were `#[track_caller]`.
|
|
|
|
loop {
|
|
|
|
let scope_data = &self.mir.source_scopes[source_info.scope];
|
|
|
|
|
|
|
|
if let Some((callee, callsite_span)) = scope_data.inlined {
|
|
|
|
// Stop inside the most nested non-`#[track_caller]` function,
|
|
|
|
// before ever reaching its caller (which is irrelevant).
|
|
|
|
if !callee.def.requires_caller_location(tcx) {
|
|
|
|
return span_to_caller_location(source_info.span);
|
|
|
|
}
|
|
|
|
source_info.span = callsite_span;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip past all of the parents with `inlined: None`.
|
|
|
|
match scope_data.inlined_parent_scope {
|
|
|
|
Some(parent) => source_info.scope = parent,
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No inlined `SourceScope`s, or all of them were `#[track_caller]`.
|
|
|
|
self.caller_location.unwrap_or_else(|| span_to_caller_location(source_info.span))
|
2019-10-24 17:35:02 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn get_personality_slot(&mut self, bx: &mut Bx) -> PlaceRef<'tcx, Bx::Value> {
|
2018-08-28 17:50:57 +02:00
|
|
|
let cx = bx.cx();
|
2017-06-01 21:50:53 +03:00
|
|
|
if let Some(slot) = self.personality_slot {
|
2016-01-02 00:45:21 +02:00
|
|
|
slot
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
let layout = cx.layout_of(
|
|
|
|
cx.tcx().intern_tup(&[cx.tcx().mk_mut_ptr(cx.tcx().types.u8), cx.tcx().types.i32]),
|
|
|
|
);
|
2019-09-12 19:04:30 +03:00
|
|
|
let slot = PlaceRef::alloca(bx, layout);
|
2017-06-01 21:50:53 +03:00
|
|
|
self.personality_slot = Some(slot);
|
2016-12-10 20:32:44 -07:00
|
|
|
slot
|
2016-01-02 00:45:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns the landing-pad wrapper around the given basic block.
|
2016-02-11 22:57:09 +02:00
|
|
|
///
|
|
|
|
/// No-op in MSVC SEH scheme.
|
2019-12-22 17:42:04 -05:00
|
|
|
fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> Bx::BasicBlock {
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-14 01:08:21 +02:00
|
|
|
let block = self.blocks[target_bb];
|
2019-10-14 01:38:38 -04:00
|
|
|
let landing_pad = self.landing_pad_uncached(block);
|
2017-03-14 01:08:21 +02:00
|
|
|
self.landing_pads[target_bb] = Some(landing_pad);
|
|
|
|
landing_pad
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn landing_pad_uncached(&mut self, target_bb: Bx::BasicBlock) -> Bx::BasicBlock {
|
2018-01-05 07:04:08 +02:00
|
|
|
if base::wants_msvc_seh(self.cx.sess()) {
|
2019-10-14 01:38:38 -04:00
|
|
|
span_bug!(self.mir.span, "landing pad was not inserted?")
|
2016-02-11 22:57:09 +02:00
|
|
|
}
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2018-10-04 15:23:10 +02:00
|
|
|
let mut bx = self.new_block("cleanup");
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
let llpersonality = self.cx.eh_personality();
|
2017-06-01 21:50:53 +03:00
|
|
|
let llretty = self.landing_pad_type();
|
2018-01-05 07:12:32 +02:00
|
|
|
let lp = bx.landing_pad(llretty, llpersonality, 1);
|
|
|
|
bx.set_cleanup(lp);
|
2017-06-01 21:50:53 +03:00
|
|
|
|
2018-10-05 15:08:49 +02:00
|
|
|
let slot = self.get_personality_slot(&mut bx);
|
|
|
|
slot.storage_live(&mut bx);
|
|
|
|
Pair(bx.extract_value(lp, 0), bx.extract_value(lp, 1)).store(&mut bx, slot);
|
2017-06-01 21:50:53 +03:00
|
|
|
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.br(target_bb);
|
|
|
|
bx.llbb()
|
2015-12-19 16:48:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn landing_pad_type(&self) -> Bx::Type {
|
2018-01-05 07:04:08 +02:00
|
|
|
let cx = self.cx;
|
2018-09-20 15:47:22 +02:00
|
|
|
cx.type_struct(&[cx.type_i8p(), cx.type_i32()], false)
|
2017-06-01 21:50:53 +03:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn unreachable_block(&mut self) -> Bx::BasicBlock {
|
2016-02-01 11:04:46 +01:00
|
|
|
self.unreachable_block.unwrap_or_else(|| {
|
2018-10-05 15:08:49 +02:00
|
|
|
let mut bx = self.new_block("unreachable");
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.unreachable();
|
|
|
|
self.unreachable_block = Some(bx.llbb());
|
|
|
|
bx.llbb()
|
2016-02-01 11:04:46 +01:00
|
|
|
})
|
2015-12-19 16:47:52 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn new_block(&self, name: &str) -> Bx {
|
|
|
|
Bx::new_block(self.cx, self.llfn, name)
|
2017-01-01 01:29:23 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn build_block(&self, bb: mir::BasicBlock) -> Bx {
|
2018-10-04 15:23:10 +02:00
|
|
|
let mut bx = Bx::with_cx(self.cx);
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.position_at_end(self.blocks[bb]);
|
|
|
|
bx
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn make_return_dest(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2020-03-31 14:31:34 -03:00
|
|
|
dest: mir::Place<'tcx>,
|
2019-10-29 16:35:26 +02:00
|
|
|
fn_ret: &ArgAbi<'tcx, Ty<'tcx>>,
|
2019-12-22 17:42:04 -05:00
|
|
|
llargs: &mut Vec<Bx::Value>,
|
|
|
|
is_intrinsic: bool,
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> ReturnDest<'tcx, Bx::Value> {
|
2019-02-28 22:43:53 +00:00
|
|
|
// If the return is ignored, we can just return a do-nothing `ReturnDest`.
|
2017-09-20 18:17:23 +03:00
|
|
|
if fn_ret.is_ignore() {
|
2016-04-06 17:57:42 +12:00
|
|
|
return ReturnDest::Nothing;
|
|
|
|
}
|
2019-10-20 16:09:36 -04:00
|
|
|
let dest = if let Some(index) = dest.as_local() {
|
2016-06-20 23:55:14 +03:00
|
|
|
match self.locals[index] {
|
2017-12-01 14:31:47 +02:00
|
|
|
LocalRef::Place(dest) => dest,
|
2018-05-29 00:12:55 +09:00
|
|
|
LocalRef::UnsizedPlace(_) => bug!("return type must be sized"),
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(None) => {
|
2019-02-28 22:43:53 +00:00
|
|
|
// Handle temporary places, specifically `Operand` ones, as
|
|
|
|
// they don't have `alloca`s.
|
2017-09-20 18:17:23 +03:00
|
|
|
return if fn_ret.is_indirect() {
|
2016-06-20 23:55:14 +03:00
|
|
|
// Odd, but possible, case, we have an operand temporary,
|
|
|
|
// but the calling convention has an indirect return.
|
2019-09-12 19:04:30 +03:00
|
|
|
let tmp = PlaceRef::alloca(bx, fn_ret.layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_live(bx);
|
2017-02-06 17:27:09 +01:00
|
|
|
llargs.push(tmp.llval);
|
2017-06-01 21:50:53 +03:00
|
|
|
ReturnDest::IndirectOperand(tmp, index)
|
2016-06-20 23:55:14 +03:00
|
|
|
} else if is_intrinsic {
|
|
|
|
// Currently, intrinsics always need a location to store
|
2019-02-28 22:43:53 +00:00
|
|
|
// the result, so we create a temporary `alloca` for the
|
|
|
|
// result.
|
2019-09-12 19:04:30 +03:00
|
|
|
let tmp = PlaceRef::alloca(bx, fn_ret.layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_live(bx);
|
2017-06-01 21:50:53 +03:00
|
|
|
ReturnDest::IndirectOperand(tmp, index)
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
|
|
|
ReturnDest::DirectOperand(index)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
LocalRef::Operand(Some(_)) => {
|
2017-12-01 14:39:51 +02:00
|
|
|
bug!("place local already assigned to");
|
2016-04-06 17:57:42 +12:00
|
|
|
}
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.codegen_place(
|
|
|
|
bx,
|
2020-01-14 02:10:05 -03:00
|
|
|
mir::PlaceRef { local: dest.local, projection: &dest.projection },
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2016-04-06 17:57:42 +12:00
|
|
|
};
|
2017-09-20 18:17:23 +03:00
|
|
|
if fn_ret.is_indirect() {
|
2018-09-09 01:16:45 +03:00
|
|
|
if dest.align < dest.layout.align.abi {
|
2017-12-01 19:16:39 +02:00
|
|
|
// Currently, MIR code generation does not create calls
|
|
|
|
// that store directly to fields of packed structs (in
|
2019-02-28 22:43:53 +00:00
|
|
|
// fact, the calls it creates write only to temps).
|
2017-12-01 19:16:39 +02:00
|
|
|
//
|
|
|
|
// If someone changes that, please update this code path
|
|
|
|
// to create a temporary.
|
2019-10-14 01:38:38 -04:00
|
|
|
span_bug!(self.mir.span, "can't directly store to unaligned value");
|
2017-03-09 13:28:26 +02:00
|
|
|
}
|
2017-12-01 19:16:39 +02:00
|
|
|
llargs.push(dest.llval);
|
|
|
|
ReturnDest::Nothing
|
2016-04-06 17:57:42 +12:00
|
|
|
} else {
|
2017-06-25 12:41:24 +03:00
|
|
|
ReturnDest::Store(dest)
|
2016-04-06 17:57:42 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:35:01 -03:00
|
|
|
fn codegen_transmute(&mut self, bx: &mut Bx, src: &mir::Operand<'tcx>, dst: mir::Place<'tcx>) {
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Some(index) = dst.as_local() {
|
2017-02-06 17:37:58 +01:00
|
|
|
match self.locals[index] {
|
2019-10-14 01:38:38 -04:00
|
|
|
LocalRef::Place(place) => self.codegen_transmute_into(bx, src, place),
|
2018-05-29 00:12:55 +09:00
|
|
|
LocalRef::UnsizedPlace(_) => bug!("transmute must not involve unsized locals"),
|
2017-02-06 17:37:58 +01:00
|
|
|
LocalRef::Operand(None) => {
|
2020-01-14 01:51:59 -03:00
|
|
|
let dst_layout = bx.layout_of(self.monomorphized_place_ty(dst.as_ref()));
|
2017-09-20 18:17:23 +03:00
|
|
|
assert!(!dst_layout.ty.has_erasable_regions());
|
2019-09-12 19:04:30 +03:00
|
|
|
let place = PlaceRef::alloca(bx, dst_layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
place.storage_live(bx);
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_transmute_into(bx, src, place);
|
2018-09-14 17:48:57 +02:00
|
|
|
let op = bx.load_operand(place);
|
2018-01-05 07:12:32 +02:00
|
|
|
place.storage_dead(bx);
|
2017-02-06 17:37:58 +01:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2020-02-08 19:20:45 +02:00
|
|
|
self.debug_introduce_local(bx, index);
|
2017-02-06 17:37:58 +01:00
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
LocalRef::Operand(Some(op)) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
assert!(op.layout.is_zst(), "assigning to initialized SSAtemp");
|
2017-02-06 17:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-14 01:51:59 -03:00
|
|
|
let dst = self.codegen_place(bx, dst.as_ref());
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_transmute_into(bx, src, dst);
|
2017-02-06 17:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn codegen_transmute_into(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
src: &mir::Operand<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
dst: PlaceRef<'tcx, Bx::Value>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) {
|
2019-10-14 01:38:38 -04:00
|
|
|
let src = self.codegen_operand(bx, src);
|
2020-12-07 17:33:43 +02:00
|
|
|
|
|
|
|
// Special-case transmutes between scalars as simple bitcasts.
|
|
|
|
match (&src.layout.abi, &dst.layout.abi) {
|
|
|
|
(abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => {
|
|
|
|
// HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers.
|
|
|
|
if (src_scalar.value == abi::Pointer) == (dst_scalar.value == abi::Pointer) {
|
|
|
|
assert_eq!(src.layout.size, dst.layout.size);
|
|
|
|
|
|
|
|
// NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
|
|
|
|
// conversions allow handling `bool`s the same as `u8`s.
|
|
|
|
let src = bx.from_immediate(src.immediate());
|
|
|
|
let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout));
|
|
|
|
Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2018-11-27 19:00:25 +01:00
|
|
|
let llty = bx.backend_type(src.layout);
|
|
|
|
let cast_ptr = bx.pointercast(dst.llval, bx.type_ptr_to(llty));
|
2018-09-09 01:16:45 +03:00
|
|
|
let align = src.layout.align.abi.min(dst.align);
|
2019-08-29 14:24:50 -04:00
|
|
|
src.val.store(bx, PlaceRef::new_sized_aligned(cast_ptr, src.layout, align));
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stores the return value of a function call into it's final location.
|
2018-09-20 15:47:22 +02:00
|
|
|
fn store_return(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
dest: ReturnDest<'tcx, Bx::Value>,
|
2019-10-29 16:35:26 +02:00
|
|
|
ret_abi: &ArgAbi<'tcx, Ty<'tcx>>,
|
2019-12-22 17:42:04 -05:00
|
|
|
llval: Bx::Value,
|
2018-09-20 15:47:22 +02:00
|
|
|
) {
|
2016-04-04 19:21:27 +12:00
|
|
|
use self::ReturnDest::*;
|
|
|
|
|
2016-06-08 00:35:01 +03:00
|
|
|
match dest {
|
|
|
|
Nothing => (),
|
2019-10-29 16:35:26 +02:00
|
|
|
Store(dst) => bx.store_arg(&ret_abi, llval, dst),
|
2016-06-20 23:55:14 +03:00
|
|
|
IndirectOperand(tmp, index) => {
|
2018-09-14 17:48:57 +02:00
|
|
|
let op = bx.load_operand(tmp);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_dead(bx);
|
2016-06-20 23:55:14 +03:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2020-02-08 19:20:45 +02:00
|
|
|
self.debug_introduce_local(bx, index);
|
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.
|
2019-10-29 16:35:26 +02:00
|
|
|
let op = if let PassMode::Cast(_) = ret_abi.mode {
|
|
|
|
let tmp = PlaceRef::alloca(bx, ret_abi.layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_live(bx);
|
2019-10-29 16:35:26 +02:00
|
|
|
bx.store_arg(&ret_abi, llval, tmp);
|
2018-09-14 17:48:57 +02:00
|
|
|
let op = bx.load_operand(tmp);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_dead(bx);
|
2017-06-01 21:50:53 +03:00
|
|
|
op
|
2016-04-08 15:37:56 +12:00
|
|
|
} else {
|
2019-10-29 16:35:26 +02:00
|
|
|
OperandRef::from_immediate_or_packed_pair(bx, llval, ret_abi.layout)
|
2016-06-08 00:35:01 +03:00
|
|
|
};
|
2016-06-20 23:55:14 +03:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2020-02-08 19:20:45 +02:00
|
|
|
self.debug_introduce_local(bx, index);
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:48:44 +03:00
|
|
|
enum ReturnDest<'tcx, V> {
|
2019-02-28 22:43:53 +00:00
|
|
|
// Do nothing; the return value is indirect or ignored.
|
2016-04-04 19:21:27 +12:00
|
|
|
Nothing,
|
2019-02-28 22:43:53 +00:00
|
|
|
// Store the return value to the pointer.
|
2018-08-02 17:48:44 +03:00
|
|
|
Store(PlaceRef<'tcx, V>),
|
2019-02-28 22:43:53 +00:00
|
|
|
// Store an indirect return value to an operand local place.
|
2018-08-02 17:48:44 +03:00
|
|
|
IndirectOperand(PlaceRef<'tcx, V>, mir::Local),
|
2019-02-28 22:43:53 +00:00
|
|
|
// Store a direct return value to an operand local place.
|
2019-12-22 17:42:04 -05:00
|
|
|
DirectOperand(mir::Local),
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|