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-03-08 14:38:13 +02:00
|
|
|
use middle::ty;
|
2015-11-19 16:37:34 +01:00
|
|
|
use rustc::mir::repr as mir;
|
|
|
|
use rustc::mir::tcx::LvalueTy;
|
2015-10-21 17:42:25 -04:00
|
|
|
use trans::base;
|
2016-03-08 14:38:13 +02:00
|
|
|
use trans::build;
|
2016-03-06 16:30:21 +02:00
|
|
|
use trans::common::{self, Block, BlockAndBuilder, FunctionContext};
|
2015-10-21 17:42:25 -04:00
|
|
|
use trans::expr;
|
|
|
|
|
2016-03-08 14:38:13 +02:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
use self::lvalue::LvalueRef;
|
2015-11-02 09:39:59 -05:00
|
|
|
use self::operand::OperandRef;
|
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
|
|
|
// FIXME DebugLoc is always None right now
|
|
|
|
|
|
|
|
/// 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`
|
|
|
|
blocks: Vec<Block<'bcx, 'tcx>>,
|
|
|
|
|
2015-12-19 16:47:52 +02:00
|
|
|
/// Cached unreachable block
|
|
|
|
unreachable_block: Option<Block<'bcx, 'tcx>>,
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
/// An LLVM alloca for each MIR `VarDecl`
|
|
|
|
vars: Vec<LvalueRef<'tcx>>,
|
|
|
|
|
2015-11-02 09:39:59 -05:00
|
|
|
/// The location where each MIR `TempDecl` is stored. This is
|
|
|
|
/// 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:
|
|
|
|
///
|
|
|
|
/// - the type of the temporary must be judged "immediate" by `type_is_immediate`
|
|
|
|
/// - 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`.
|
2015-11-02 09:39:59 -05:00
|
|
|
temps: Vec<TempRef<'tcx>>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
|
|
/// The arguments to the function; as args are lvalues, these are
|
|
|
|
/// always indirect, though we try to avoid creating an alloca
|
|
|
|
/// when we can (and just reuse the pointer the caller provided).
|
|
|
|
args: Vec<LvalueRef<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2015-11-02 09:39:59 -05:00
|
|
|
enum TempRef<'tcx> {
|
|
|
|
Lvalue(LvalueRef<'tcx>),
|
|
|
|
Operand(Option<OperandRef<'tcx>>),
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
pub fn trans_mir<'blk, 'tcx>(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();
|
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
let mir_blocks = mir.all_basic_blocks();
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2015-11-02 09:39:59 -05:00
|
|
|
// Analyze the temps to determine which must be lvalues
|
|
|
|
// FIXME
|
2016-02-01 11:04:46 +01:00
|
|
|
let lvalue_temps = bcx.with_block(|bcx| {
|
2016-03-08 14:38:13 +02:00
|
|
|
analyze::lvalue_temps(bcx, &mir)
|
2016-02-01 11:04:46 +01:00
|
|
|
});
|
2015-11-02 09:39:59 -05:00
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
// Allocate variable and temp allocas
|
|
|
|
let vars = mir.var_decls.iter()
|
|
|
|
.map(|decl| (bcx.monomorphize(&decl.ty), decl.name))
|
2016-02-01 11:04:46 +01:00
|
|
|
.map(|(mty, name)| LvalueRef::alloca(&bcx, mty, &name.as_str()))
|
2015-10-21 17:42:25 -04:00
|
|
|
.collect();
|
|
|
|
let temps = mir.temp_decls.iter()
|
|
|
|
.map(|decl| bcx.monomorphize(&decl.ty))
|
|
|
|
.enumerate()
|
2016-01-21 18:57:43 +02:00
|
|
|
.map(|(i, mty)| if lvalue_temps.contains(i) {
|
2016-02-01 11:04:46 +01:00
|
|
|
TempRef::Lvalue(LvalueRef::alloca(&bcx,
|
2015-11-02 09:39:59 -05:00
|
|
|
mty,
|
|
|
|
&format!("temp{:?}", i)))
|
|
|
|
} else {
|
|
|
|
// If this is an immediate temp, we do not create an
|
|
|
|
// alloca in advance. Instead we wait until we see the
|
|
|
|
// definition and update the operand there.
|
|
|
|
TempRef::Operand(None)
|
|
|
|
})
|
2015-10-21 17:42:25 -04:00
|
|
|
.collect();
|
2016-03-08 14:38:13 +02:00
|
|
|
let args = arg_value_refs(&bcx, &mir);
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
|
|
// Allocate a `Block` for every basic block
|
2016-03-06 16:30:21 +02:00
|
|
|
let block_bcxs: Vec<Block<'blk,'tcx>> =
|
2015-10-21 17:42:25 -04:00
|
|
|
mir_blocks.iter()
|
2015-12-20 15:30:09 +02:00
|
|
|
.map(|&bb|{
|
2015-10-23 18:18:44 -07:00
|
|
|
// FIXME(#30941) this doesn't handle msvc-style exceptions
|
2016-02-08 23:08:47 +01:00
|
|
|
fcx.new_block(&format!("{:?}", bb), None)
|
2015-12-20 15:30:09 +02:00
|
|
|
})
|
2015-10-21 17:42:25 -04:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Branch to the START block
|
|
|
|
let start_bcx = block_bcxs[mir::START_BLOCK.index()];
|
2016-02-01 11:04:46 +01:00
|
|
|
bcx.br(start_bcx.llbb);
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
|
|
let mut mircx = MirContext {
|
|
|
|
mir: mir,
|
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,
|
2015-10-21 17:42:25 -04:00
|
|
|
vars: vars,
|
|
|
|
temps: temps,
|
|
|
|
args: args,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Translate the body of each block
|
|
|
|
for &bb in &mir_blocks {
|
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
|
|
|
|
|
|
|
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-02-01 11:04:46 +01:00
|
|
|
fn arg_value_refs<'bcx, 'tcx>(bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
2015-10-21 17:42:25 -04:00
|
|
|
mir: &mir::Mir<'tcx>)
|
|
|
|
-> Vec<LvalueRef<'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;
|
|
|
|
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);
|
|
|
|
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,
|
|
|
|
_ => unreachable!("spread argument isn't a tuple?!")
|
|
|
|
};
|
|
|
|
|
|
|
|
let llval = bcx.with_block(|bcx| {
|
|
|
|
let lltemp = base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index));
|
|
|
|
for (i, &tupled_arg_ty) in tupled_arg_tys.iter().enumerate() {
|
|
|
|
let dst = build::StructGEP(bcx, lltemp, i);
|
|
|
|
let arg = &fcx.fn_ty.args[idx];
|
|
|
|
idx += 1;
|
|
|
|
if common::type_is_fat_ptr(tcx, tupled_arg_ty) {
|
|
|
|
// We pass fat pointers as two words, but inside the tuple
|
|
|
|
// they are the two sub-fields of a single aggregate field.
|
|
|
|
let meta = &fcx.fn_ty.args[idx];
|
|
|
|
idx += 1;
|
|
|
|
arg.store_fn_arg(bcx, &mut llarg_idx, expr::get_dataptr(bcx, dst));
|
|
|
|
meta.store_fn_arg(bcx, &mut llarg_idx, expr::get_meta(bcx, dst));
|
|
|
|
} else {
|
|
|
|
arg.store_fn_arg(bcx, &mut llarg_idx, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lltemp
|
|
|
|
});
|
|
|
|
return LvalueRef::new_sized(llval, LvalueTy::from_ty(arg_ty));
|
|
|
|
}
|
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
let arg = &fcx.fn_ty.args[idx];
|
|
|
|
idx += 1;
|
|
|
|
let llval = if arg.is_indirect() {
|
|
|
|
// 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 :(.
|
|
|
|
// FIXME: lifetimes, debug info
|
|
|
|
let llarg = llvm::get_param(fcx.llfn, llarg_idx as c_uint);
|
|
|
|
llarg_idx += 1;
|
|
|
|
llarg
|
|
|
|
} else {
|
|
|
|
bcx.with_block(|bcx| {
|
|
|
|
let lltemp = base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index));
|
|
|
|
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, expr::get_dataptr(bcx, lltemp));
|
|
|
|
meta.store_fn_arg(bcx, &mut llarg_idx, expr::get_meta(bcx, lltemp));
|
|
|
|
} else {
|
|
|
|
// otherwise, arg is passed by value, so make a
|
|
|
|
// temporary and store it there
|
|
|
|
arg.store_fn_arg(bcx, &mut llarg_idx, lltemp);
|
|
|
|
}
|
|
|
|
lltemp
|
|
|
|
})
|
|
|
|
};
|
|
|
|
LvalueRef::new_sized(llval, LvalueTy::from_ty(arg_ty))
|
|
|
|
}).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;
|
2016-02-04 19:40:28 +02:00
|
|
|
mod drop;
|
2015-10-21 17:42:25 -04:00
|
|
|
mod lvalue;
|
|
|
|
mod operand;
|
2016-01-30 19:32:50 +02:00
|
|
|
mod rvalue;
|
2015-10-21 17:42:25 -04:00
|
|
|
mod statement;
|