mod.rs 19.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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};
13
use llvm::debuginfo::DIScope;
14
use rustc::ty;
15 16
use rustc::mir::repr as mir;
use rustc::mir::tcx::LvalueTy;
17
use session::config::FullDebugInfo;
18
use base;
19
use common::{self, Block, BlockAndBuilder, CrateContext, FunctionContext, C_null};
20 21 22 23
use debuginfo::{self, declare_local, DebugLoc, VariableAccess, VariableKind};
use machine;
use type_of;

24
use syntax_pos::DUMMY_SP;
25
use syntax::parse::token::keywords;
26

27 28
use std::ops::Deref;
use std::rc::Rc;
29
use std::iter;
30

31
use basic_block::BasicBlock;
32

33
use rustc_data_structures::bitvec::BitVector;
34
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
35

36 37
pub use self::constant::trans_static_initializer;

38
use self::lvalue::{LvalueRef, get_dataptr, get_meta};
39
use rustc::mir::traversal;
40

41
use self::operand::{OperandRef, OperandValue};
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#[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
        }
    }
}

59 60
/// Master context for translating MIR.
pub struct MirContext<'bcx, 'tcx:'bcx> {
61
    mir: CachedMir<'bcx, 'tcx>,
62

63 64 65
    /// Function context
    fcx: &'bcx common::FunctionContext<'bcx, 'tcx>,

66 67 68 69 70 71 72 73 74 75
    /// 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`
76
    blocks: IndexVec<mir::BasicBlock, Block<'bcx, 'tcx>>,
77

78
    /// The funclet status of each basic block
79
    cleanup_kinds: IndexVec<mir::BasicBlock, analyze::CleanupKind>,
80 81 82

    /// This stores the landing-pad block for a given BB, computed lazily on GNU
    /// and eagerly on MSVC.
83
    landing_pads: IndexVec<mir::BasicBlock, Option<Block<'bcx, 'tcx>>>,
84

85 86 87
    /// Cached unreachable block
    unreachable_block: Option<Block<'bcx, 'tcx>>,

88
    /// The location where each MIR arg/var/tmp/ret is stored. This is
89 90 91 92 93
    /// 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:
    ///
94
    /// - the type of the local must be judged "immediate" by `type_is_immediate`
95 96 97 98 99
    /// - 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
N
Niko Matsakis 已提交
100 101 102
    ///
    /// Avoiding allocs can also be important for certain intrinsics,
    /// notably `expect`.
103
    locals: IndexVec<mir::Local, LocalRef<'tcx>>,
104 105

    /// Debug information for MIR scopes.
106
    scopes: IndexVec<mir::VisibilityScope, DIScope>
107 108
}

109 110
impl<'blk, 'tcx> MirContext<'blk, 'tcx> {
    pub fn debug_loc(&self, source_info: mir::SourceInfo) -> DebugLoc {
111
        DebugLoc::ScopeAt(self.scopes[source_info.scope], source_info.span)
112 113 114
    }
}

115
enum LocalRef<'tcx> {
116 117 118 119
    Lvalue(LvalueRef<'tcx>),
    Operand(Option<OperandRef<'tcx>>),
}

120
impl<'tcx> LocalRef<'tcx> {
121
    fn new_operand<'bcx>(ccx: &CrateContext<'bcx, 'tcx>,
122
                         ty: ty::Ty<'tcx>) -> LocalRef<'tcx> {
123 124 125 126
        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.
127
            let llty = type_of::type_of(ccx, ty);
128
            let val = if common::type_is_imm_pair(ccx, ty) {
129 130
                let fields = llty.field_types();
                OperandValue::Pair(C_null(fields[0]), C_null(fields[1]))
131
            } else {
132
                OperandValue::Immediate(C_null(llty))
133
            };
134 135 136 137
            let op = OperandRef {
                val: val,
                ty: ty
            };
138
            LocalRef::Operand(Some(op))
139
        } else {
140
            LocalRef::Operand(None)
141
        }
J
James Miller 已提交
142 143 144
    }
}

145 146
///////////////////////////////////////////////////////////////////////////

147
pub fn trans_mir<'blk, 'tcx: 'blk>(fcx: &'blk FunctionContext<'blk, 'tcx>) {
148
    let bcx = fcx.init(true).build();
149 150
    let mir = bcx.mir();

151 152
    // Analyze the temps to determine which must be lvalues
    // FIXME
153 154
    let (lvalue_locals, cleanup_kinds) = bcx.with_block(|bcx| {
        (analyze::lvalue_locals(bcx, &mir),
155
         analyze::cleanup_kinds(bcx, &mir))
156
    });
157

158 159 160
    // Compute debuginfo scopes from MIR scopes.
    let scopes = debuginfo::create_mir_scopes(fcx);

161
    // Allocate variable and temp allocas
162 163 164 165 166 167 168 169 170 171 172
    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);
            }
173

174 175 176 177 178 179 180 181 182 183 184 185 186
            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)
187
        }).chain(iter::once((mir::Lvalue::ReturnPointer, mir.return_ty)));
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

        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()
    };
205 206

    // Allocate a `Block` for every basic block
207
    let block_bcxs: IndexVec<mir::BasicBlock, Block<'blk,'tcx>> =
208 209
        mir.basic_blocks().indices().map(|bb| {
            if bb == mir::START_BLOCK {
210
                fcx.new_block("start")
211
            } else {
212
                fcx.new_block(&format!("{:?}", bb))
213 214
            }
        }).collect();
215 216

    // Branch to the START block
217
    let start_bcx = block_bcxs[mir::START_BLOCK];
218
    bcx.br(start_bcx.llbb);
219

220 221 222 223 224
    // 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);

225
    let mut mircx = MirContext {
226
        mir: mir.clone(),
227
        fcx: fcx,
228 229
        llpersonalityslot: None,
        blocks: block_bcxs,
230
        unreachable_block: None,
231
        cleanup_kinds: cleanup_kinds,
232
        landing_pads: IndexVec::from_elem(None, mir.basic_blocks()),
233
        locals: locals,
234
        scopes: scopes
235 236
    };

237
    let mut visited = BitVector::new(mir.basic_blocks().len());
238

239 240 241 242 243 244 245 246
    let mut rpo = traversal::reverse_postorder(&mir);

    // Prepare each block for translation.
    for (bb, _) in rpo.by_ref() {
        mircx.init_cpad(bb);
    }
    rpo.reset();

247 248
    // Translate the body of each block using reverse postorder
    for (bb, _) in rpo {
249
        visited.insert(bb.index());
S
Simonas Kazlauskas 已提交
250
        mircx.trans_block(bb);
251
    }
252

253 254
    // Remove blocks that haven't been visited, or have no
    // predecessors.
255
    for bb in mir.basic_blocks().indices() {
256
        let block = mircx.blocks[bb];
257 258
        let block = BasicBlock(block.llbb);
        // Unreachable block
259
        if !visited.contains(bb.index()) {
260
            debug!("trans_mir: block {:?} was not visited", bb);
261
            block.delete();
262 263 264
        }
    }

265
    DebugLoc::None.apply(fcx);
266
    fcx.cleanup();
267 268 269 270 271
}

/// Produce, for each argument, a `ValueRef` pointing at the
/// argument's value. As arguments are lvalues, these are always
/// indirect.
272
fn arg_local_refs<'bcx, 'tcx>(bcx: &BlockAndBuilder<'bcx, 'tcx>,
273
                              mir: &mir::Mir<'tcx>,
274 275 276
                              scopes: &IndexVec<mir::VisibilityScope, DIScope>,
                              lvalue_locals: &BitVector)
                              -> Vec<LocalRef<'tcx>> {
277
    let fcx = bcx.fcx();
278
    let tcx = bcx.tcx();
279 280
    let mut idx = 0;
    let mut llarg_idx = fcx.fn_ty.ret.is_indirect() as usize;
281

282
    // Get the argument scope, if it exists and if we need it.
283
    let arg_scope = scopes[mir::ARGUMENT_VISIBILITY_SCOPE];
284 285 286 287 288
    let arg_scope = if !arg_scope.is_null() && bcx.sess().opts.debuginfo == FullDebugInfo {
        Some(arg_scope)
    } else {
        None
    };
289

290
    mir.arg_decls.iter().enumerate().map(|(arg_index, arg_decl)| {
291
        let arg_ty = bcx.monomorphize(&arg_decl.ty);
292
        let local = mir.local_index(&mir::Lvalue::Arg(mir::Arg::new(arg_index))).unwrap();
293 294 295 296 297 298 299 300
        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,
301
                _ => bug!("spread argument isn't a tuple?!")
302 303
            };

304
            let lltuplety = type_of::type_of(bcx.ccx(), arg_ty);
305 306 307 308 309 310
            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];
311
                idx += 1;
312
                if common::type_is_fat_ptr(tcx, tupled_arg_ty) {
313 314
                    // We pass fat pointers as two words, but inside the tuple
                    // they are the two sub-fields of a single aggregate field.
315 316 317 318 319 320
                    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);
321
                }
322 323 324 325 326

                bcx.with_block(|bcx| arg_scope.map(|scope| {
                    let byte_offset_of_var_in_tuple =
                        machine::llelement_offset(bcx.ccx(), lltuplety, i);

327
                    let ops = unsafe {
328 329
                        [llvm::LLVMRustDIBuilderCreateOpDeref(),
                         llvm::LLVMRustDIBuilderCreateOpPlus(),
330 331 332 333 334
                         byte_offset_of_var_in_tuple as i64]
                    };

                    let variable_access = VariableAccess::IndirectVariable {
                        alloca: lltemp,
335
                        address_operations: &ops
336
                    };
337
                    declare_local(bcx, keywords::Invalid.name(),
338 339 340 341
                                  tupled_arg_ty, scope, variable_access,
                                  VariableKind::ArgumentVariable(arg_index + i + 1),
                                  bcx.fcx().span.unwrap_or(DUMMY_SP));
                }));
342
            }
343
            return LocalRef::Lvalue(LvalueRef::new_sized(lltemp, LvalueTy::from_ty(arg_ty)));
344 345
        }

346 347
        let arg = &fcx.fn_ty.args[idx];
        idx += 1;
348
        let llval = if arg.is_indirect() && bcx.sess().opts.debuginfo != FullDebugInfo {
349 350 351
            // 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 :(.
352
            // FIXME: lifetimes
353 354 355
            if arg.pad.is_some() {
                llarg_idx += 1;
            }
356 357 358
            let llarg = llvm::get_param(fcx.llfn, llarg_idx as c_uint);
            llarg_idx += 1;
            llarg
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        } 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)));
389
        } else {
390 391 392
            let lltemp = bcx.with_block(|bcx| {
                base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index))
            });
393 394 395 396 397 398 399 400 401
            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  {
402 403
                // otherwise, arg is passed by value, so make a
                // temporary and store it there
404 405
                arg.store_fn_arg(bcx, &mut llarg_idx, lltemp);
            }
406
            lltemp
407
        };
408
        bcx.with_block(|bcx| arg_scope.map(|scope| {
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
            // 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 {
454 455
                    [llvm::LLVMRustDIBuilderCreateOpDeref(),
                     llvm::LLVMRustDIBuilderCreateOpPlus(),
456
                     byte_offset_of_var_in_env as i64,
457
                     llvm::LLVMRustDIBuilderCreateOpDeref()]
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
                };

                // 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));
            }
481
        }));
482
        LocalRef::Lvalue(LvalueRef::new_sized(llval, LvalueTy::from_ty(arg_ty)))
483
    }).collect()
484 485
}

486
mod analyze;
487 488 489 490
mod block;
mod constant;
mod lvalue;
mod operand;
491
mod rvalue;
492
mod statement;