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.
|
|
|
|
|
|
|
|
use libc::c_uint;
|
|
|
|
use llvm::{self, ValueRef};
|
2016-04-07 22:35:11 +03:00
|
|
|
use llvm::debuginfo::DIScope;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty;
|
2015-11-19 16:37:34 +01:00
|
|
|
use rustc::mir::repr as mir;
|
|
|
|
use rustc::mir::tcx::LvalueTy;
|
2016-04-07 22:35:11 +03:00
|
|
|
use session::config::FullDebugInfo;
|
2016-03-22 19:23:36 +02:00
|
|
|
use base;
|
2016-06-09 00:33:57 +03:00
|
|
|
use common::{self, Block, BlockAndBuilder, CrateContext, FunctionContext, C_null};
|
2016-04-07 22:35:11 +03:00
|
|
|
use debuginfo::{self, declare_local, DebugLoc, VariableAccess, VariableKind};
|
|
|
|
use machine;
|
|
|
|
use type_of;
|
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::DUMMY_SP;
|
2016-04-18 22:53:50 +03:00
|
|
|
use syntax::parse::token::keywords;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-03-08 14:38:13 +02:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2016-03-30 14:46:02 +13:00
|
|
|
use basic_block::BasicBlock;
|
2016-03-11 19:42:47 +13:00
|
|
|
|
2016-03-11 18:00:52 +13:00
|
|
|
use rustc_data_structures::bitvec::BitVector;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
2016-03-11 18:00:52 +13:00
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
pub use self::constant::trans_static_initializer;
|
|
|
|
|
2016-03-09 14:20:22 +02:00
|
|
|
use self::lvalue::{LvalueRef, get_dataptr, get_meta};
|
2016-06-05 19:38:22 +03:00
|
|
|
use rustc::mir::traversal;
|
2016-03-11 13:14:51 +13:00
|
|
|
|
Various improvements to MIR and LLVM IR Construction
Primarily affects the MIR construction, which indirectly improves LLVM
IR generation, but some LLVM IR changes have been made too.
* Handle "statement expressions" more intelligently. These are
expressions that always evaluate to `()`. Previously a temporary would
be generated as a destination to translate into, which is unnecessary.
This affects assignment, augmented assignment, `return`, `break` and
`continue`.
* Avoid inserting drops for non-drop types in more places. Scheduled
drops were already skipped for types that we knew wouldn't need
dropping at construction time. However manually-inserted drops like
those for `x` in `x = y;` were still generated. `build_drop` now takes
a type parameter like its `schedule_drop` counterpart and checks to
see if the type needs dropping.
* Avoid generating an extra temporary for an assignment where the types
involved don't need dropping. Previously an expression like
`a = b + 1;` would result in a temporary for `b + 1`. This is so the
RHS can be evaluated, then the LHS evaluated and dropped and have
everything work correctly. However, this isn't necessary if the `LHS`
doesn't need a drop, as we can just overwrite the existing value.
* Improves lvalue analysis to allow treating an `Rvalue::Use` as an
operand in certain conditions. The reason for it never being an
operand is so it can be zeroed/drop-filled, but this is only true for
types that need dropping.
The first two changes result in significantly fewer MIR blocks being
generated, as previously almost every statement would end up generating
a new block due to the drop of the `()` temporary being generated.
2016-04-15 12:36:16 +12:00
|
|
|
use self::operand::{OperandRef, OperandValue};
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-03-08 14:38:13 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum CachedMir<'mir, 'tcx: 'mir> {
|
|
|
|
Ref(&'mir mir::Mir<'tcx>),
|
|
|
|
Owned(Rc<mir::Mir<'tcx>>)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'mir, 'tcx: 'mir> Deref for CachedMir<'mir, 'tcx> {
|
|
|
|
type Target = mir::Mir<'tcx>;
|
|
|
|
fn deref(&self) -> &mir::Mir<'tcx> {
|
|
|
|
match *self {
|
|
|
|
CachedMir::Ref(r) => r,
|
|
|
|
CachedMir::Owned(ref rc) => rc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
/// Master context for translating MIR.
|
|
|
|
pub struct MirContext<'bcx, 'tcx:'bcx> {
|
2016-03-08 14:38:13 +02:00
|
|
|
mir: CachedMir<'bcx, 'tcx>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2015-12-19 16:47:52 +02:00
|
|
|
/// Function context
|
|
|
|
fcx: &'bcx common::FunctionContext<'bcx, 'tcx>,
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
/// When unwinding is initiated, we have to store this personality
|
|
|
|
/// value somewhere so that we can load it and re-use it in the
|
|
|
|
/// resume instruction. The personality is (afaik) some kind of
|
|
|
|
/// value used for C++ unwinding, which must filter by type: we
|
|
|
|
/// don't really care about it very much. Anyway, this value
|
|
|
|
/// contains an alloca into which the personality is stored and
|
|
|
|
/// then later loaded when generating the DIVERGE_BLOCK.
|
|
|
|
llpersonalityslot: Option<ValueRef>,
|
|
|
|
|
|
|
|
/// A `Block` for each MIR `BasicBlock`
|
2016-06-07 17:28:36 +03:00
|
|
|
blocks: IndexVec<mir::BasicBlock, Block<'bcx, 'tcx>>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-05-29 22:01:06 +03:00
|
|
|
/// The funclet status of each basic block
|
2016-06-07 17:28:36 +03:00
|
|
|
cleanup_kinds: IndexVec<mir::BasicBlock, analyze::CleanupKind>,
|
2016-05-29 22:01:06 +03:00
|
|
|
|
|
|
|
/// This stores the landing-pad block for a given BB, computed lazily on GNU
|
|
|
|
/// and eagerly on MSVC.
|
2016-06-07 17:28:36 +03:00
|
|
|
landing_pads: IndexVec<mir::BasicBlock, Option<Block<'bcx, 'tcx>>>,
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2015-12-19 16:47:52 +02:00
|
|
|
/// Cached unreachable block
|
|
|
|
unreachable_block: Option<Block<'bcx, 'tcx>>,
|
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
/// The location where each MIR arg/var/tmp/ret is stored. This is
|
2015-11-02 09:39:59 -05:00
|
|
|
/// usually an `LvalueRef` representing an alloca, but not always:
|
|
|
|
/// sometimes we can skip the alloca and just store the value
|
|
|
|
/// directly using an `OperandRef`, which makes for tighter LLVM
|
|
|
|
/// IR. The conditions for using an `OperandRef` are as follows:
|
|
|
|
///
|
2016-06-20 23:55:14 +03:00
|
|
|
/// - the type of the local must be judged "immediate" by `type_is_immediate`
|
2015-11-02 09:39:59 -05:00
|
|
|
/// - the operand must never be referenced indirectly
|
|
|
|
/// - we should not take its address using the `&` operator
|
|
|
|
/// - nor should it appear in an lvalue path like `tmp.a`
|
|
|
|
/// - the operand must be defined by an rvalue that can generate immediate
|
|
|
|
/// values
|
2015-11-03 15:50:04 -05:00
|
|
|
///
|
|
|
|
/// Avoiding allocs can also be important for certain intrinsics,
|
|
|
|
/// notably `expect`.
|
2016-06-20 23:55:14 +03:00
|
|
|
locals: IndexVec<mir::Local, LocalRef<'tcx>>,
|
2016-04-07 22:35:11 +03:00
|
|
|
|
|
|
|
/// Debug information for MIR scopes.
|
2016-06-07 17:28:36 +03:00
|
|
|
scopes: IndexVec<mir::VisibilityScope, DIScope>
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
impl<'blk, 'tcx> MirContext<'blk, 'tcx> {
|
|
|
|
pub fn debug_loc(&self, source_info: mir::SourceInfo) -> DebugLoc {
|
2016-06-07 17:28:36 +03:00
|
|
|
DebugLoc::ScopeAt(self.scopes[source_info.scope], source_info.span)
|
2016-06-07 19:21:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
enum LocalRef<'tcx> {
|
2015-11-02 09:39:59 -05:00
|
|
|
Lvalue(LvalueRef<'tcx>),
|
|
|
|
Operand(Option<OperandRef<'tcx>>),
|
|
|
|
}
|
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
impl<'tcx> LocalRef<'tcx> {
|
2016-04-17 14:37:52 +12:00
|
|
|
fn new_operand<'bcx>(ccx: &CrateContext<'bcx, 'tcx>,
|
2016-06-20 23:55:14 +03:00
|
|
|
ty: ty::Ty<'tcx>) -> LocalRef<'tcx> {
|
2016-04-17 14:37:52 +12:00
|
|
|
if common::type_is_zero_size(ccx, ty) {
|
|
|
|
// Zero-size temporaries aren't always initialized, which
|
|
|
|
// doesn't matter because they don't contain data, but
|
|
|
|
// we need something in the operand.
|
2016-06-09 00:33:57 +03:00
|
|
|
let llty = type_of::type_of(ccx, ty);
|
2016-06-07 00:24:21 +03:00
|
|
|
let val = if common::type_is_imm_pair(ccx, ty) {
|
2016-06-09 00:33:57 +03:00
|
|
|
let fields = llty.field_types();
|
|
|
|
OperandValue::Pair(C_null(fields[0]), C_null(fields[1]))
|
2016-06-07 00:24:21 +03:00
|
|
|
} else {
|
2016-06-09 00:33:57 +03:00
|
|
|
OperandValue::Immediate(C_null(llty))
|
2016-06-07 00:24:21 +03:00
|
|
|
};
|
2016-04-17 14:37:52 +12:00
|
|
|
let op = OperandRef {
|
|
|
|
val: val,
|
|
|
|
ty: ty
|
|
|
|
};
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(Some(op))
|
2016-04-17 14:37:52 +12:00
|
|
|
} else {
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(None)
|
2016-04-17 14:37:52 +12:00
|
|
|
}
|
2016-04-16 17:38:18 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-03-11 18:00:52 +13:00
|
|
|
pub fn trans_mir<'blk, 'tcx: 'blk>(fcx: &'blk FunctionContext<'blk, 'tcx>) {
|
2016-03-08 14:38:13 +02:00
|
|
|
let bcx = fcx.init(false, None).build();
|
2015-10-21 17:42:25 -04:00
|
|
|
let mir = bcx.mir();
|
|
|
|
|
2015-11-02 09:39:59 -05:00
|
|
|
// Analyze the temps to determine which must be lvalues
|
|
|
|
// FIXME
|
2016-06-20 23:55:14 +03:00
|
|
|
let (lvalue_locals, cleanup_kinds) = bcx.with_block(|bcx| {
|
|
|
|
(analyze::lvalue_locals(bcx, &mir),
|
2016-05-29 22:01:06 +03:00
|
|
|
analyze::cleanup_kinds(bcx, &mir))
|
2016-02-01 11:04:46 +01:00
|
|
|
});
|
2015-11-02 09:39:59 -05:00
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
// Compute debuginfo scopes from MIR scopes.
|
|
|
|
let scopes = debuginfo::create_mir_scopes(fcx);
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
// Allocate variable and temp allocas
|
2016-06-20 23:55:14 +03:00
|
|
|
let locals = {
|
|
|
|
let args = arg_local_refs(&bcx, &mir, &scopes, &lvalue_locals);
|
|
|
|
let vars = mir.var_decls.iter().enumerate().map(|(i, decl)| {
|
|
|
|
let ty = bcx.monomorphize(&decl.ty);
|
|
|
|
let scope = scopes[decl.source_info.scope];
|
|
|
|
let dbg = !scope.is_null() && bcx.sess().opts.debuginfo == FullDebugInfo;
|
|
|
|
|
|
|
|
let local = mir.local_index(&mir::Lvalue::Var(mir::Var::new(i))).unwrap();
|
|
|
|
if !lvalue_locals.contains(local.index()) && !dbg {
|
|
|
|
return LocalRef::new_operand(bcx.ccx(), ty);
|
|
|
|
}
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
let lvalue = LvalueRef::alloca(&bcx, ty, &decl.name.as_str());
|
|
|
|
if dbg {
|
|
|
|
bcx.with_block(|bcx| {
|
|
|
|
declare_local(bcx, decl.name, ty, scope,
|
|
|
|
VariableAccess::DirectVariable { alloca: lvalue.llval },
|
|
|
|
VariableKind::LocalVariable, decl.source_info.span);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
LocalRef::Lvalue(lvalue)
|
|
|
|
});
|
|
|
|
|
|
|
|
let locals = mir.temp_decls.iter().enumerate().map(|(i, decl)| {
|
|
|
|
(mir::Lvalue::Temp(mir::Temp::new(i)), decl.ty)
|
|
|
|
}).chain(mir.return_ty.maybe_converging().map(|ty| (mir::Lvalue::ReturnPointer, ty)));
|
|
|
|
|
|
|
|
args.into_iter().chain(vars).chain(locals.map(|(lvalue, ty)| {
|
|
|
|
let ty = bcx.monomorphize(&ty);
|
|
|
|
let local = mir.local_index(&lvalue).unwrap();
|
|
|
|
if lvalue == mir::Lvalue::ReturnPointer && fcx.fn_ty.ret.is_indirect() {
|
|
|
|
let llretptr = llvm::get_param(fcx.llfn, 0);
|
|
|
|
LocalRef::Lvalue(LvalueRef::new_sized(llretptr, LvalueTy::from_ty(ty)))
|
|
|
|
} else if lvalue_locals.contains(local.index()) {
|
|
|
|
LocalRef::Lvalue(LvalueRef::alloca(&bcx, ty, &format!("{:?}", lvalue)))
|
|
|
|
} else {
|
|
|
|
// If this is an immediate local, we do not create an
|
|
|
|
// alloca in advance. Instead we wait until we see the
|
|
|
|
// definition and update the operand there.
|
|
|
|
LocalRef::new_operand(bcx.ccx(), ty)
|
|
|
|
}
|
|
|
|
})).collect()
|
|
|
|
};
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
|
|
// Allocate a `Block` for every basic block
|
2016-06-07 17:28:36 +03:00
|
|
|
let block_bcxs: IndexVec<mir::BasicBlock, Block<'blk,'tcx>> =
|
2016-06-07 21:20:50 +03:00
|
|
|
mir.basic_blocks().indices().map(|bb| {
|
|
|
|
if bb == mir::START_BLOCK {
|
|
|
|
fcx.new_block("start", None)
|
|
|
|
} else {
|
|
|
|
fcx.new_block(&format!("{:?}", bb), None)
|
|
|
|
}
|
|
|
|
}).collect();
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
|
|
// Branch to the START block
|
2016-06-07 17:28:36 +03:00
|
|
|
let start_bcx = block_bcxs[mir::START_BLOCK];
|
2016-02-01 11:04:46 +01:00
|
|
|
bcx.br(start_bcx.llbb);
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
// Up until here, IR instructions for this function have explicitly not been annotated with
|
|
|
|
// source code location, so we don't step into call setup code. From here on, source location
|
|
|
|
// emitting should be enabled.
|
|
|
|
debuginfo::start_emitting_source_locations(fcx);
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
let mut mircx = MirContext {
|
2016-03-11 18:00:52 +13:00
|
|
|
mir: mir.clone(),
|
2015-12-19 16:47:52 +02:00
|
|
|
fcx: fcx,
|
2015-10-21 17:42:25 -04:00
|
|
|
llpersonalityslot: None,
|
|
|
|
blocks: block_bcxs,
|
2015-12-19 16:47:52 +02:00
|
|
|
unreachable_block: None,
|
2016-05-29 22:01:06 +03:00
|
|
|
cleanup_kinds: cleanup_kinds,
|
2016-06-07 21:20:50 +03:00
|
|
|
landing_pads: IndexVec::from_elem(None, mir.basic_blocks()),
|
2016-06-20 23:55:14 +03:00
|
|
|
locals: locals,
|
2016-04-07 22:35:11 +03:00
|
|
|
scopes: scopes
|
2015-10-21 17:42:25 -04:00
|
|
|
};
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
let mut visited = BitVector::new(mir.basic_blocks().len());
|
2016-03-11 18:00:52 +13:00
|
|
|
|
2016-05-29 22:01:06 +03:00
|
|
|
let mut rpo = traversal::reverse_postorder(&mir);
|
|
|
|
|
|
|
|
// Prepare each block for translation.
|
|
|
|
for (bb, _) in rpo.by_ref() {
|
|
|
|
mircx.init_cpad(bb);
|
|
|
|
}
|
|
|
|
rpo.reset();
|
|
|
|
|
2016-03-11 13:14:51 +13:00
|
|
|
// Translate the body of each block using reverse postorder
|
|
|
|
for (bb, _) in rpo {
|
2016-03-11 18:00:52 +13:00
|
|
|
visited.insert(bb.index());
|
2015-12-19 00:44:32 +02:00
|
|
|
mircx.trans_block(bb);
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
2016-03-06 16:30:21 +02:00
|
|
|
|
2016-03-11 19:42:47 +13:00
|
|
|
// Remove blocks that haven't been visited, or have no
|
|
|
|
// predecessors.
|
2016-06-07 21:20:50 +03:00
|
|
|
for bb in mir.basic_blocks().indices() {
|
2016-06-07 17:28:36 +03:00
|
|
|
let block = mircx.blocks[bb];
|
2016-03-11 19:42:47 +13:00
|
|
|
let block = BasicBlock(block.llbb);
|
|
|
|
// Unreachable block
|
2016-03-11 18:00:52 +13:00
|
|
|
if !visited.contains(bb.index()) {
|
2016-05-29 22:01:06 +03:00
|
|
|
debug!("trans_mir: block {:?} was not visited", bb);
|
2016-03-11 19:42:47 +13:00
|
|
|
block.delete();
|
2016-03-11 18:00:52 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
DebugLoc::None.apply(fcx);
|
2016-03-06 16:30:21 +02:00
|
|
|
fcx.cleanup();
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce, for each argument, a `ValueRef` pointing at the
|
|
|
|
/// argument's value. As arguments are lvalues, these are always
|
|
|
|
/// indirect.
|
2016-06-20 23:55:14 +03:00
|
|
|
fn arg_local_refs<'bcx, 'tcx>(bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
2016-04-07 22:35:11 +03:00
|
|
|
mir: &mir::Mir<'tcx>,
|
2016-06-20 23:55:14 +03:00
|
|
|
scopes: &IndexVec<mir::VisibilityScope, DIScope>,
|
|
|
|
lvalue_locals: &BitVector)
|
|
|
|
-> Vec<LocalRef<'tcx>> {
|
2016-02-01 11:04:46 +01:00
|
|
|
let fcx = bcx.fcx();
|
2015-10-21 17:42:25 -04:00
|
|
|
let tcx = bcx.tcx();
|
2016-03-06 16:30:21 +02:00
|
|
|
let mut idx = 0;
|
|
|
|
let mut llarg_idx = fcx.fn_ty.ret.is_indirect() as usize;
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2016-05-31 20:27:36 +03:00
|
|
|
// Get the argument scope, if it exists and if we need it.
|
2016-06-07 17:28:36 +03:00
|
|
|
let arg_scope = scopes[mir::ARGUMENT_VISIBILITY_SCOPE];
|
2016-05-31 20:27:36 +03:00
|
|
|
let arg_scope = if !arg_scope.is_null() && bcx.sess().opts.debuginfo == FullDebugInfo {
|
|
|
|
Some(arg_scope)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
mir.arg_decls.iter().enumerate().map(|(arg_index, arg_decl)| {
|
2016-03-08 14:24:44 +02:00
|
|
|
let arg_ty = bcx.monomorphize(&arg_decl.ty);
|
2016-06-20 23:55:14 +03:00
|
|
|
let local = mir.local_index(&mir::Lvalue::Arg(mir::Arg::new(arg_index))).unwrap();
|
2016-03-08 14:24:44 +02:00
|
|
|
if arg_decl.spread {
|
|
|
|
// This argument (e.g. the last argument in the "rust-call" ABI)
|
|
|
|
// is a tuple that was spread at the ABI level and now we have
|
|
|
|
// to reconstruct it into a tuple local variable, from multiple
|
|
|
|
// individual LLVM function arguments.
|
|
|
|
|
|
|
|
let tupled_arg_tys = match arg_ty.sty {
|
|
|
|
ty::TyTuple(ref tys) => tys,
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("spread argument isn't a tuple?!")
|
2016-03-08 14:24:44 +02:00
|
|
|
};
|
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
let lltuplety = type_of::type_of(bcx.ccx(), arg_ty);
|
2016-03-09 14:20:22 +02:00
|
|
|
let lltemp = bcx.with_block(|bcx| {
|
|
|
|
base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index))
|
|
|
|
});
|
|
|
|
for (i, &tupled_arg_ty) in tupled_arg_tys.iter().enumerate() {
|
|
|
|
let dst = bcx.struct_gep(lltemp, i);
|
|
|
|
let arg = &fcx.fn_ty.args[idx];
|
2016-04-07 22:35:11 +03:00
|
|
|
idx += 1;
|
2016-03-09 14:20:22 +02:00
|
|
|
if common::type_is_fat_ptr(tcx, tupled_arg_ty) {
|
2016-06-20 23:55:14 +03:00
|
|
|
// We pass fat pointers as two words, but inside the tuple
|
|
|
|
// they are the two sub-fields of a single aggregate field.
|
2016-03-09 14:20:22 +02:00
|
|
|
let meta = &fcx.fn_ty.args[idx];
|
|
|
|
idx += 1;
|
|
|
|
arg.store_fn_arg(bcx, &mut llarg_idx, get_dataptr(bcx, dst));
|
|
|
|
meta.store_fn_arg(bcx, &mut llarg_idx, get_meta(bcx, dst));
|
|
|
|
} else {
|
|
|
|
arg.store_fn_arg(bcx, &mut llarg_idx, dst);
|
2016-03-08 14:24:44 +02:00
|
|
|
}
|
2016-04-07 22:35:11 +03:00
|
|
|
|
|
|
|
bcx.with_block(|bcx| arg_scope.map(|scope| {
|
|
|
|
let byte_offset_of_var_in_tuple =
|
|
|
|
machine::llelement_offset(bcx.ccx(), lltuplety, i);
|
|
|
|
|
2016-04-16 21:51:26 +03:00
|
|
|
let ops = unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
[llvm::LLVMRustDIBuilderCreateOpDeref(),
|
|
|
|
llvm::LLVMRustDIBuilderCreateOpPlus(),
|
2016-04-07 22:35:11 +03:00
|
|
|
byte_offset_of_var_in_tuple as i64]
|
|
|
|
};
|
|
|
|
|
|
|
|
let variable_access = VariableAccess::IndirectVariable {
|
|
|
|
alloca: lltemp,
|
2016-04-16 21:51:26 +03:00
|
|
|
address_operations: &ops
|
2016-04-07 22:35:11 +03:00
|
|
|
};
|
2016-04-18 22:53:50 +03:00
|
|
|
declare_local(bcx, keywords::Invalid.name(),
|
2016-04-07 22:35:11 +03:00
|
|
|
tupled_arg_ty, scope, variable_access,
|
|
|
|
VariableKind::ArgumentVariable(arg_index + i + 1),
|
|
|
|
bcx.fcx().span.unwrap_or(DUMMY_SP));
|
|
|
|
}));
|
2016-03-09 14:20:22 +02:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
return LocalRef::Lvalue(LvalueRef::new_sized(lltemp, LvalueTy::from_ty(arg_ty)));
|
2016-03-08 14:24:44 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
let arg = &fcx.fn_ty.args[idx];
|
|
|
|
idx += 1;
|
2016-04-07 22:35:11 +03:00
|
|
|
let llval = if arg.is_indirect() && bcx.sess().opts.debuginfo != FullDebugInfo {
|
2016-03-06 16:30:21 +02:00
|
|
|
// Don't copy an indirect argument to an alloca, the caller
|
|
|
|
// already put it in a temporary alloca and gave it up, unless
|
|
|
|
// we emit extra-debug-info, which requires local allocas :(.
|
2016-04-07 22:35:11 +03:00
|
|
|
// FIXME: lifetimes
|
2016-06-20 23:55:14 +03:00
|
|
|
if arg.pad.is_some() {
|
|
|
|
llarg_idx += 1;
|
|
|
|
}
|
2016-03-06 16:30:21 +02:00
|
|
|
let llarg = llvm::get_param(fcx.llfn, llarg_idx as c_uint);
|
|
|
|
llarg_idx += 1;
|
|
|
|
llarg
|
2016-06-20 23:55:14 +03:00
|
|
|
} else if !lvalue_locals.contains(local.index()) &&
|
|
|
|
!arg.is_indirect() && arg.cast.is_none() &&
|
|
|
|
arg_scope.is_none() {
|
|
|
|
if arg.is_ignore() {
|
|
|
|
return LocalRef::new_operand(bcx.ccx(), arg_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have to cast or keep the argument in the alloca.
|
|
|
|
// FIXME(eddyb): We should figure out how to use llvm.dbg.value instead
|
|
|
|
// of putting everything in allocas just so we can use llvm.dbg.declare.
|
|
|
|
if arg.pad.is_some() {
|
|
|
|
llarg_idx += 1;
|
|
|
|
}
|
|
|
|
let llarg = llvm::get_param(fcx.llfn, llarg_idx as c_uint);
|
|
|
|
llarg_idx += 1;
|
|
|
|
let val = if common::type_is_fat_ptr(tcx, arg_ty) {
|
|
|
|
let meta = &fcx.fn_ty.args[idx];
|
|
|
|
idx += 1;
|
|
|
|
assert_eq!((meta.cast, meta.pad), (None, None));
|
|
|
|
let llmeta = llvm::get_param(fcx.llfn, llarg_idx as c_uint);
|
|
|
|
llarg_idx += 1;
|
|
|
|
OperandValue::Pair(llarg, llmeta)
|
|
|
|
} else {
|
|
|
|
OperandValue::Immediate(llarg)
|
|
|
|
};
|
|
|
|
let operand = OperandRef {
|
|
|
|
val: val,
|
|
|
|
ty: arg_ty
|
|
|
|
};
|
|
|
|
return LocalRef::Operand(Some(operand.unpack_if_pair(bcx)));
|
2016-03-06 16:30:21 +02:00
|
|
|
} else {
|
2016-06-08 00:35:01 +03:00
|
|
|
let lltemp = bcx.with_block(|bcx| {
|
|
|
|
base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index))
|
|
|
|
});
|
2016-03-09 14:20:22 +02:00
|
|
|
if common::type_is_fat_ptr(tcx, arg_ty) {
|
|
|
|
// we pass fat pointers as two words, but we want to
|
|
|
|
// represent them internally as a pointer to two words,
|
|
|
|
// so make an alloca to store them in.
|
|
|
|
let meta = &fcx.fn_ty.args[idx];
|
|
|
|
idx += 1;
|
|
|
|
arg.store_fn_arg(bcx, &mut llarg_idx, get_dataptr(bcx, lltemp));
|
|
|
|
meta.store_fn_arg(bcx, &mut llarg_idx, get_meta(bcx, lltemp));
|
|
|
|
} else {
|
2016-06-08 00:35:01 +03:00
|
|
|
// otherwise, arg is passed by value, so make a
|
|
|
|
// temporary and store it there
|
2016-03-09 14:20:22 +02:00
|
|
|
arg.store_fn_arg(bcx, &mut llarg_idx, lltemp);
|
|
|
|
}
|
2016-06-08 00:35:01 +03:00
|
|
|
lltemp
|
2016-03-06 16:30:21 +02:00
|
|
|
};
|
2016-04-07 22:35:11 +03:00
|
|
|
bcx.with_block(|bcx| arg_scope.map(|scope| {
|
2016-04-16 21:51:26 +03:00
|
|
|
// Is this a regular argument?
|
|
|
|
if arg_index > 0 || mir.upvar_decls.is_empty() {
|
|
|
|
declare_local(bcx, arg_decl.debug_name, arg_ty, scope,
|
|
|
|
VariableAccess::DirectVariable { alloca: llval },
|
|
|
|
VariableKind::ArgumentVariable(arg_index + 1),
|
|
|
|
bcx.fcx().span.unwrap_or(DUMMY_SP));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or is it the closure environment?
|
|
|
|
let (closure_ty, env_ref) = if let ty::TyRef(_, mt) = arg_ty.sty {
|
|
|
|
(mt.ty, true)
|
|
|
|
} else {
|
|
|
|
(arg_ty, false)
|
|
|
|
};
|
|
|
|
let upvar_tys = if let ty::TyClosure(_, ref substs) = closure_ty.sty {
|
|
|
|
&substs.upvar_tys[..]
|
|
|
|
} else {
|
|
|
|
bug!("upvar_decls with non-closure arg0 type `{}`", closure_ty);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Store the pointer to closure data in an alloca for debuginfo
|
|
|
|
// because that's what the llvm.dbg.declare intrinsic expects.
|
|
|
|
|
|
|
|
// FIXME(eddyb) this shouldn't be necessary but SROA seems to
|
|
|
|
// mishandle DW_OP_plus not preceded by DW_OP_deref, i.e. it
|
|
|
|
// doesn't actually strip the offset when splitting the closure
|
|
|
|
// environment into its components so it ends up out of bounds.
|
|
|
|
let env_ptr = if !env_ref {
|
|
|
|
use base::*;
|
|
|
|
use build::*;
|
|
|
|
use common::*;
|
|
|
|
let alloc = alloca(bcx, val_ty(llval), "__debuginfo_env_ptr");
|
|
|
|
Store(bcx, llval, alloc);
|
|
|
|
alloc
|
|
|
|
} else {
|
|
|
|
llval
|
|
|
|
};
|
|
|
|
|
|
|
|
let llclosurety = type_of::type_of(bcx.ccx(), closure_ty);
|
|
|
|
for (i, (decl, ty)) in mir.upvar_decls.iter().zip(upvar_tys).enumerate() {
|
|
|
|
let byte_offset_of_var_in_env =
|
|
|
|
machine::llelement_offset(bcx.ccx(), llclosurety, i);
|
|
|
|
|
|
|
|
let ops = unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
[llvm::LLVMRustDIBuilderCreateOpDeref(),
|
|
|
|
llvm::LLVMRustDIBuilderCreateOpPlus(),
|
2016-04-16 21:51:26 +03:00
|
|
|
byte_offset_of_var_in_env as i64,
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateOpDeref()]
|
2016-04-16 21:51:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// The environment and the capture can each be indirect.
|
|
|
|
|
|
|
|
// FIXME(eddyb) see above why we have to keep
|
|
|
|
// a pointer in an alloca for debuginfo atm.
|
|
|
|
let mut ops = if env_ref || true { &ops[..] } else { &ops[1..] };
|
|
|
|
|
|
|
|
let ty = if let (true, &ty::TyRef(_, mt)) = (decl.by_ref, &ty.sty) {
|
|
|
|
mt.ty
|
|
|
|
} else {
|
|
|
|
ops = &ops[..ops.len() - 1];
|
|
|
|
ty
|
|
|
|
};
|
|
|
|
|
|
|
|
let variable_access = VariableAccess::IndirectVariable {
|
|
|
|
alloca: env_ptr,
|
|
|
|
address_operations: &ops
|
|
|
|
};
|
|
|
|
declare_local(bcx, decl.debug_name, ty, scope, variable_access,
|
|
|
|
VariableKind::CapturedVariable,
|
|
|
|
bcx.fcx().span.unwrap_or(DUMMY_SP));
|
|
|
|
}
|
2016-04-07 22:35:11 +03:00
|
|
|
}));
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Lvalue(LvalueRef::new_sized(llval, LvalueTy::from_ty(arg_ty)))
|
2016-03-06 16:30:21 +02:00
|
|
|
}).collect()
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2015-11-03 06:35:09 -05:00
|
|
|
mod analyze;
|
2015-10-21 17:42:25 -04:00
|
|
|
mod block;
|
|
|
|
mod constant;
|
|
|
|
mod lvalue;
|
|
|
|
mod operand;
|
2016-01-30 19:32:50 +02:00
|
|
|
mod rvalue;
|
2015-10-21 17:42:25 -04:00
|
|
|
mod statement;
|