mod.rs 25.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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;
M
Mark-Simulacrum 已提交
12
use llvm::{self, ValueRef, BasicBlockRef};
M
Mark-Simulacrum 已提交
13
use llvm::debuginfo::DIScope;
14
use rustc::ty::{self, Ty, TypeFoldable};
15
use rustc::ty::layout::{self, LayoutTyper};
M
Mark-Simulacrum 已提交
16
use rustc::mir::{self, Mir};
17
use rustc::mir::tcx::LvalueTy;
18 19
use rustc::ty::subst::Substs;
use rustc::infer::TransNormalize;
20
use rustc::session::config::FullDebugInfo;
21
use base;
22
use builder::Builder;
23
use common::{self, CrateContext, Funclet};
M
Mark-Simulacrum 已提交
24
use debuginfo::{self, declare_local, VariableAccess, VariableKind, FunctionDebugContext};
25
use monomorphize::Instance;
26
use abi::{ArgAttribute, FnType};
27 28
use type_of;

29
use syntax_pos::{DUMMY_SP, NO_EXPANSION, BytePos, Span};
30
use syntax::symbol::keywords;
31

32
use std::iter;
33

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

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

M
Mark Simulacrum 已提交
39
use self::analyze::CleanupKind;
40
use self::lvalue::{Alignment, LvalueRef};
41
use rustc::mir::traversal;
42

43
use self::operand::{OperandRef, OperandValue};
44 45

/// Master context for translating MIR.
M
Mark Simulacrum 已提交
46
pub struct MirContext<'a, 'tcx:'a> {
47
    mir: &'a mir::Mir<'tcx>,
48

49
    debug_context: debuginfo::FunctionDebugContext,
50

M
Mark Simulacrum 已提交
51
    llfn: ValueRef,
52

M
Mark Simulacrum 已提交
53 54
    ccx: &'a CrateContext<'a, 'tcx>,

55
    fn_ty: FnType<'tcx>,
56

57 58 59 60 61 62 63
    /// 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.
64
    personality_slot: Option<LvalueRef<'tcx>>,
65 66

    /// A `Block` for each MIR `BasicBlock`
M
Mark-Simulacrum 已提交
67
    blocks: IndexVec<mir::BasicBlock, BasicBlockRef>,
68

69
    /// The funclet status of each basic block
70
    cleanup_kinds: IndexVec<mir::BasicBlock, analyze::CleanupKind>,
71

72 73 74 75
    /// When targeting MSVC, this stores the cleanup info for each funclet
    /// BB. This is initialized as we compute the funclets' head block in RPO.
    funclets: &'a IndexVec<mir::BasicBlock, Option<Funclet>>,

76 77
    /// This stores the landing-pad block for a given BB, computed lazily on GNU
    /// and eagerly on MSVC.
M
Mark-Simulacrum 已提交
78
    landing_pads: IndexVec<mir::BasicBlock, Option<BasicBlockRef>>,
79

80
    /// Cached unreachable block
M
Mark-Simulacrum 已提交
81
    unreachable_block: Option<BasicBlockRef>,
82

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

    /// Debug information for MIR scopes.
101
    scopes: IndexVec<mir::VisibilityScope, debuginfo::MirDebugScope>,
102 103 104

    /// If this function is being monomorphized, this contains the type substitutions used.
    param_substs: &'tcx Substs<'tcx>,
105 106
}

M
Mark Simulacrum 已提交
107
impl<'a, 'tcx> MirContext<'a, 'tcx> {
108
    pub fn monomorphize<T>(&self, value: &T) -> T
109 110 111
        where T: TransNormalize<'tcx>
    {
        self.ccx.tcx().trans_apply_param_substs(self.param_substs, value)
112 113
    }

114
    pub fn set_debug_loc(&mut self, bcx: &Builder, source_info: mir::SourceInfo) {
115 116 117 118
        let (scope, span) = self.debug_loc(source_info);
        debuginfo::set_source_location(&self.debug_context, bcx, scope, span);
    }

M
Mark-Simulacrum 已提交
119
    pub fn debug_loc(&mut self, source_info: mir::SourceInfo) -> (DIScope, Span) {
120
        // Bail out if debug info emission is not enabled.
121
        match self.debug_context {
122 123
            FunctionDebugContext::DebugInfoDisabled |
            FunctionDebugContext::FunctionWithoutDebugInfo => {
M
Mark-Simulacrum 已提交
124
                return (self.scopes[source_info.scope].scope_metadata, source_info.span);
125 126 127 128 129 130 131
            }
            FunctionDebugContext::RegularContext(_) =>{}
        }

        // In order to have a good line stepping behavior in debugger, we overwrite debug
        // locations of macro expansions with that of the outermost expansion site
        // (unless the crate is being compiled with `-Z debug-macros`).
132
        if source_info.span.ctxt() == NO_EXPANSION ||
133
           self.ccx.sess().opts.debugging_opts.debug_macros {
134
            let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo());
135
            (scope, source_info.span)
136 137
        } else {
            // Walk up the macro expansion chain until we reach a non-expanded span.
B
Bastien Orivel 已提交
138
            // We also stop at the function body level because no line stepping can occur
V
Vadim Chugunov 已提交
139
            // at the level above that.
140
            let mut span = source_info.span;
141 142
            while span.ctxt() != NO_EXPANSION && span.ctxt() != self.mir.span.ctxt() {
                if let Some(info) = span.ctxt().outer().expn_info() {
143
                    span = info.call_site;
144 145 146 147
                } else {
                    break;
                }
            }
148
            let scope = self.scope_metadata_for_loc(source_info.scope, span.lo());
149
            // Use span of the outermost expansion site, while keeping the original lexical scope.
150
            (scope, span)
151 152 153 154 155 156 157 158 159 160 161 162
        }
    }

    // DILocations inherit source file name from the parent DIScope.  Due to macro expansions
    // it may so happen that the current span belongs to a different file than the DIScope
    // corresponding to span's containing visibility scope.  If so, we need to create a DIScope
    // "extension" into that file.
    fn scope_metadata_for_loc(&self, scope_id: mir::VisibilityScope, pos: BytePos)
                               -> llvm::debuginfo::DIScope {
        let scope_metadata = self.scopes[scope_id].scope_metadata;
        if pos < self.scopes[scope_id].file_start_pos ||
           pos >= self.scopes[scope_id].file_end_pos {
M
Mark Simulacrum 已提交
163
            let cm = self.ccx.sess().codemap();
164 165 166 167 168
            let defining_crate = self.debug_context.get_ref(DUMMY_SP).defining_crate;
            debuginfo::extend_scope_to_file(self.ccx,
                                            scope_metadata,
                                            &cm.lookup_char_pos(pos).file,
                                            defining_crate)
169 170 171
        } else {
            scope_metadata
        }
172 173 174
    }
}

175
enum LocalRef<'tcx> {
176 177 178 179
    Lvalue(LvalueRef<'tcx>),
    Operand(Option<OperandRef<'tcx>>),
}

180 181
impl<'a, 'tcx> LocalRef<'tcx> {
    fn new_operand(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> LocalRef<'tcx> {
182 183 184 185
        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.
186
            LocalRef::Operand(Some(OperandRef::new_zst(ccx, ty)))
187
        } else {
188
            LocalRef::Operand(None)
189
        }
J
James Miller 已提交
190 191 192
    }
}

193 194
///////////////////////////////////////////////////////////////////////////

195
pub fn trans_mir<'a, 'tcx: 'a>(
M
Mark Simulacrum 已提交
196 197
    ccx: &'a CrateContext<'a, 'tcx>,
    llfn: ValueRef,
198 199
    mir: &'a Mir<'tcx>,
    instance: Instance<'tcx>,
200
    sig: ty::FnSig<'tcx>,
201
) {
202
    let fn_ty = FnType::new(ccx, sig, &[]);
203
    debug!("fn_ty: {:?}", fn_ty);
204
    let debug_context =
205
        debuginfo::create_function_debug_context(ccx, instance, sig, llfn, mir);
206
    let bcx = Builder::new_block(ccx, llfn, "start");
207

208 209 210
    if mir.basic_blocks().iter().any(|bb| bb.is_cleanup) {
        bcx.set_personality_fn(ccx.eh_personality());
    }
211

212
    let cleanup_kinds = analyze::cleanup_kinds(&mir);
213 214 215
    // Allocate a `Block` for every basic block, except
    // the start block, if nothing loops back to it.
    let reentrant_start_block = !mir.predecessors_for(mir::START_BLOCK).is_empty();
M
Mark-Simulacrum 已提交
216
    let block_bcxs: IndexVec<mir::BasicBlock, BasicBlockRef> =
217
        mir.basic_blocks().indices().map(|bb| {
218 219
            if bb == mir::START_BLOCK && !reentrant_start_block {
                bcx.llbb()
220
            } else {
221
                bcx.build_sibling_block(&format!("{:?}", bb)).llbb()
222 223 224
            }
        }).collect();

225
    // Compute debuginfo scopes from MIR scopes.
M
Mark Simulacrum 已提交
226
    let scopes = debuginfo::create_mir_scopes(ccx, mir, &debug_context);
227
    let (landing_pads, funclets) = create_funclets(&bcx, &cleanup_kinds, &block_bcxs);
228

229
    let mut mircx = MirContext {
230 231 232 233
        mir,
        llfn,
        fn_ty,
        ccx,
234
        personality_slot: None,
235 236
        blocks: block_bcxs,
        unreachable_block: None,
237 238
        cleanup_kinds,
        landing_pads,
239
        funclets: &funclets,
240
        scopes,
241
        locals: IndexVec::new(),
242
        debug_context,
243 244 245 246
        param_substs: {
            assert!(!instance.substs.needs_infer());
            instance.substs
        },
247 248
    };

249
    let lvalue_locals = analyze::lvalue_locals(&mircx);
250

251
    // Allocate variable and temp allocas
252
    mircx.locals = {
253
        let args = arg_local_refs(&bcx, &mircx, &mircx.scopes, &lvalue_locals);
254 255 256

        let mut allocate_local = |local| {
            let decl = &mir.local_decls[local];
257
            let ty = mircx.monomorphize(&decl.ty);
258

259 260
            if let Some(name) = decl.name {
                // User variable
261
                let debug_scope = mircx.scopes[decl.source_info.scope];
262
                let dbg = debug_scope.is_valid() && bcx.sess().opts.debuginfo == FullDebugInfo;
263

264 265
                if !lvalue_locals.contains(local.index()) && !dbg {
                    debug!("alloc: {:?} ({}) -> operand", local, name);
M
Mark Simulacrum 已提交
266
                    return LocalRef::new_operand(bcx.ccx, ty);
267
                }
268 269

                debug!("alloc: {:?} ({}) -> lvalue", local, name);
270
                assert!(!ty.has_erasable_regions());
271
                let lvalue = LvalueRef::alloca(&bcx, ty, &name.as_str());
272
                if dbg {
273
                    let (scope, span) = mircx.debug_loc(decl.source_info);
274
                    declare_local(&bcx, &mircx.debug_context, name, ty, scope,
M
Mark-Simulacrum 已提交
275 276
                        VariableAccess::DirectVariable { alloca: lvalue.llval },
                        VariableKind::LocalVariable, span);
277 278
                }
                LocalRef::Lvalue(lvalue)
279
            } else {
280
                // Temporary or return pointer
281
                if local == mir::RETURN_POINTER && mircx.fn_ty.ret.is_indirect() {
282
                    debug!("alloc: {:?} (return pointer) -> lvalue", local);
M
Mark Simulacrum 已提交
283
                    let llretptr = llvm::get_param(llfn, 0);
284 285
                    LocalRef::Lvalue(LvalueRef::new_sized(llretptr, LvalueTy::from_ty(ty),
                                                          Alignment::AbiAligned))
286 287
                } else if lvalue_locals.contains(local.index()) {
                    debug!("alloc: {:?} -> lvalue", local);
288
                    assert!(!ty.has_erasable_regions());
289
                    LocalRef::Lvalue(LvalueRef::alloca(&bcx, ty,  &format!("{:?}", local)))
290 291 292 293 294
                } 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.
                    debug!("alloc: {:?} -> operand", local);
M
Mark Simulacrum 已提交
295
                    LocalRef::new_operand(bcx.ccx, ty)
296
                }
297
            }
298 299 300 301 302
        };

        let retptr = allocate_local(mir::RETURN_POINTER);
        iter::once(retptr)
            .chain(args.into_iter())
303
            .chain(mir.vars_and_temps_iter().map(allocate_local))
304
            .collect()
305
    };
306

307 308 309 310
    // Branch to the START block, if it's not the entry block.
    if reentrant_start_block {
        bcx.br(mircx.blocks[mir::START_BLOCK]);
    }
311

312 313 314
    // 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.
315
    debuginfo::start_emitting_source_locations(&mircx.debug_context);
316

M
Mark Simulacrum 已提交
317 318
    let rpo = traversal::reverse_postorder(&mir);
    let mut visited = BitVector::new(mir.basic_blocks().len());
319

320 321
    // Translate the body of each block using reverse postorder
    for (bb, _) in rpo {
322
        visited.insert(bb.index());
323
        mircx.trans_block(bb);
324
    }
325

326 327
    // Remove blocks that haven't been visited, or have no
    // predecessors.
328
    for bb in mir.basic_blocks().indices() {
329
        // Unreachable block
330
        if !visited.contains(bb.index()) {
331
            debug!("trans_mir: block {:?} was not visited", bb);
332 333 334
            unsafe {
                llvm::LLVMDeleteBasicBlock(mircx.blocks[bb]);
            }
335 336
        }
    }
337 338
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
fn create_funclets<'a, 'tcx>(
    bcx: &Builder<'a, 'tcx>,
    cleanup_kinds: &IndexVec<mir::BasicBlock, CleanupKind>,
    block_bcxs: &IndexVec<mir::BasicBlock, BasicBlockRef>)
    -> (IndexVec<mir::BasicBlock, Option<BasicBlockRef>>,
        IndexVec<mir::BasicBlock, Option<Funclet>>)
{
    block_bcxs.iter_enumerated().zip(cleanup_kinds).map(|((bb, &llbb), cleanup_kind)| {
        match *cleanup_kind {
            CleanupKind::Funclet if base::wants_msvc_seh(bcx.sess()) => {
                let cleanup_bcx = bcx.build_sibling_block(&format!("funclet_{:?}", bb));
                let cleanup = cleanup_bcx.cleanup_pad(None, &[]);
                cleanup_bcx.br(llbb);
                (Some(cleanup_bcx.llbb()), Some(Funclet::new(cleanup)))
            }
            _ => (None, None)
        }
    }).unzip()
}

359 360 361
/// Produce, for each argument, a `ValueRef` pointing at the
/// argument's value. As arguments are lvalues, these are always
/// indirect.
362
fn arg_local_refs<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
363
                            mircx: &MirContext<'a, 'tcx>,
M
Mark Simulacrum 已提交
364 365 366
                            scopes: &IndexVec<mir::VisibilityScope, debuginfo::MirDebugScope>,
                            lvalue_locals: &BitVector)
                            -> Vec<LocalRef<'tcx>> {
367
    let mir = mircx.mir;
368
    let tcx = bcx.tcx();
369
    let mut idx = 0;
370
    let mut llarg_idx = mircx.fn_ty.ret.is_indirect() as usize;
371

372
    // Get the argument scope, if it exists and if we need it.
373
    let arg_scope = scopes[mir::ARGUMENT_VISIBILITY_SCOPE];
374
    let arg_scope = if arg_scope.is_valid() && bcx.sess().opts.debuginfo == FullDebugInfo {
375
        Some(arg_scope.scope_metadata)
376 377 378
    } else {
        None
    };
379

380 381 382 383
    let deref_op = unsafe {
        [llvm::LLVMRustDIBuilderCreateOpDeref()]
    };

384
    mir.args_iter().enumerate().map(|(arg_index, local)| {
385
        let arg_decl = &mir.local_decls[local];
386
        let arg_ty = mircx.monomorphize(&arg_decl.ty);
387

388 389 390 391 392 393
        let name = if let Some(name) = arg_decl.name {
            name.as_str().to_string()
        } else {
            format!("arg{}", arg_index)
        };

J
Jonas Schievink 已提交
394 395 396 397 398 399 400
        if Some(local) == mir.spread_arg {
            // 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 {
A
Andrew Cann 已提交
401
                ty::TyTuple(ref tys, _) => tys,
J
Jonas Schievink 已提交
402 403
                _ => bug!("spread argument isn't a tuple?!")
            };
404

405
            let lvalue = LvalueRef::alloca(bcx, arg_ty, &name);
J
Jonas Schievink 已提交
406
            for (i, &tupled_arg_ty) in tupled_arg_tys.iter().enumerate() {
407
                let (dst, _) = lvalue.trans_field_ptr(bcx, i);
408
                let arg = &mircx.fn_ty.args[idx];
J
Jonas Schievink 已提交
409
                idx += 1;
M
Mark Simulacrum 已提交
410
                if common::type_is_fat_ptr(bcx.ccx, tupled_arg_ty) {
J
Jonas Schievink 已提交
411 412
                    // We pass fat pointers as two words, but inside the tuple
                    // they are the two sub-fields of a single aggregate field.
413
                    let meta = &mircx.fn_ty.args[idx];
J
Jonas Schievink 已提交
414
                    idx += 1;
M
Mark-Simulacrum 已提交
415 416
                    arg.store_fn_arg(bcx, &mut llarg_idx, base::get_dataptr(bcx, dst));
                    meta.store_fn_arg(bcx, &mut llarg_idx, base::get_meta(bcx, dst));
J
Jonas Schievink 已提交
417 418
                } else {
                    arg.store_fn_arg(bcx, &mut llarg_idx, dst);
419
                }
420
            }
421 422 423

            // Now that we have one alloca that contains the aggregate value,
            // we can create one debuginfo entry for the argument.
424
            arg_scope.map(|scope| {
425
                let variable_access = VariableAccess::DirectVariable {
426
                    alloca: lvalue.llval
427
                };
428 429 430 431 432 433 434 435 436
                declare_local(
                    bcx,
                    &mircx.debug_context,
                    arg_decl.name.unwrap_or(keywords::Invalid.name()),
                    arg_ty, scope,
                    variable_access,
                    VariableKind::ArgumentVariable(arg_index + 1),
                    DUMMY_SP
                );
437
            });
438

439
            return LocalRef::Lvalue(lvalue);
440 441
        }

442
        let arg = &mircx.fn_ty.args[idx];
443
        idx += 1;
444
        let llval = if arg.is_indirect() {
445
            // Don't copy an indirect argument to an alloca, the caller
446
            // already put it in a temporary alloca and gave it up
447
            // FIXME: lifetimes
448 449 450
            if arg.pad.is_some() {
                llarg_idx += 1;
            }
M
Mark Simulacrum 已提交
451
            let llarg = llvm::get_param(bcx.llfn(), llarg_idx as c_uint);
452
            bcx.set_value_name(llarg, &name);
453 454
            llarg_idx += 1;
            llarg
455
        } else if !lvalue_locals.contains(local.index()) &&
456
                  arg.cast.is_none() && arg_scope.is_none() {
457
            if arg.is_ignore() {
M
Mark Simulacrum 已提交
458
                return LocalRef::new_operand(bcx.ccx, arg_ty);
459 460 461 462 463 464 465 466
            }

            // 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;
            }
M
Mark Simulacrum 已提交
467
            let llarg = llvm::get_param(bcx.llfn(), llarg_idx as c_uint);
468
            llarg_idx += 1;
M
Mark Simulacrum 已提交
469
            let val = if common::type_is_fat_ptr(bcx.ccx, arg_ty) {
470
                let meta = &mircx.fn_ty.args[idx];
471
                idx += 1;
472
                assert!(meta.cast.is_none() && meta.pad.is_none());
M
Mark Simulacrum 已提交
473
                let llmeta = llvm::get_param(bcx.llfn(), llarg_idx as c_uint);
474
                llarg_idx += 1;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

                // FIXME(eddyb) As we can't perfectly represent the data and/or
                // vtable pointer in a fat pointers in Rust's typesystem, and
                // because we split fat pointers into two ArgType's, they're
                // not the right type so we have to cast them for now.
                let pointee = match arg_ty.sty {
                    ty::TyRef(_, ty::TypeAndMut{ty, ..}) |
                    ty::TyRawPtr(ty::TypeAndMut{ty, ..}) => ty,
                    ty::TyAdt(def, _) if def.is_box() => arg_ty.boxed_ty(),
                    _ => bug!()
                };
                let data_llty = type_of::in_memory_type_of(bcx.ccx, pointee);
                let meta_llty = type_of::unsized_info_ty(bcx.ccx, pointee);

                let llarg = bcx.pointercast(llarg, data_llty.ptr_to());
490
                bcx.set_value_name(llarg, &(name.clone() + ".ptr"));
491
                let llmeta = bcx.pointercast(llmeta, meta_llty);
492
                bcx.set_value_name(llmeta, &(name + ".meta"));
493

494 495
                OperandValue::Pair(llarg, llmeta)
            } else {
496
                bcx.set_value_name(llarg, &name);
497 498 499
                OperandValue::Immediate(llarg)
            };
            let operand = OperandRef {
500
                val,
501 502 503
                ty: arg_ty
            };
            return LocalRef::Operand(Some(operand.unpack_if_pair(bcx)));
504
        } else {
505
            let lltemp = LvalueRef::alloca(bcx, arg_ty, &name);
M
Mark Simulacrum 已提交
506
            if common::type_is_fat_ptr(bcx.ccx, arg_ty) {
507 508 509
                // 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.
510
                let meta = &mircx.fn_ty.args[idx];
511
                idx += 1;
512 513
                arg.store_fn_arg(bcx, &mut llarg_idx, base::get_dataptr(bcx, lltemp.llval));
                meta.store_fn_arg(bcx, &mut llarg_idx, base::get_meta(bcx, lltemp.llval));
514
            } else  {
515 516
                // otherwise, arg is passed by value, so make a
                // temporary and store it there
517
                arg.store_fn_arg(bcx, &mut llarg_idx, lltemp.llval);
518
            }
519
            lltemp.llval
520
        };
521
        arg_scope.map(|scope| {
522 523
            // Is this a regular argument?
            if arg_index > 0 || mir.upvar_decls.is_empty() {
524 525 526 527 528 529 530 531 532 533 534 535 536
                // The Rust ABI passes indirect variables using a pointer and a manual copy, so we
                // need to insert a deref here, but the C ABI uses a pointer and a copy using the
                // byval attribute, for which LLVM does the deref itself, so we must not add it.
                let variable_access = if arg.is_indirect() &&
                    !arg.attrs.contains(ArgAttribute::ByVal) {
                    VariableAccess::IndirectVariable {
                        alloca: llval,
                        address_operations: &deref_op,
                    }
                } else {
                    VariableAccess::DirectVariable { alloca: llval }
                };

537 538 539 540 541 542
                declare_local(
                    bcx,
                    &mircx.debug_context,
                    arg_decl.name.unwrap_or(keywords::Invalid.name()),
                    arg_ty,
                    scope,
543
                    variable_access,
544 545 546
                    VariableKind::ArgumentVariable(arg_index + 1),
                    DUMMY_SP
                );
547 548 549 550
                return;
            }

            // Or is it the closure environment?
J
John Kåre Alsaker 已提交
551 552 553
            let (closure_ty, env_ref) = match arg_ty.sty {
                ty::TyRef(_, mt) | ty::TyRawPtr(mt) => (mt.ty, true),
                _ => (arg_ty, false)
554
            };
A
Alex Crichton 已提交
555

J
John Kåre Alsaker 已提交
556 557 558 559
            let upvar_tys = match closure_ty.sty {
                ty::TyClosure(def_id, substs) |
                ty::TyGenerator(def_id, substs, _) => substs.upvar_tys(def_id, tcx),
                _ => bug!("upvar_decls with non-closure arg0 type `{}`", closure_ty)
560 561 562 563 564 565 566 567 568 569
            };

            // 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 {
570
                let alloc = bcx.alloca(common::val_ty(llval), "__debuginfo_env_ptr", None);
571
                bcx.store(llval, alloc, None);
572 573 574 575 576
                alloc
            } else {
                llval
            };

M
Mark Simulacrum 已提交
577
            let layout = bcx.ccx.layout_of(closure_ty);
A
Austin Hicks 已提交
578 579 580 581 582
            let offsets = match *layout {
                layout::Univariant { ref variant, .. } => &variant.offsets[..],
                _ => bug!("Closures are only supposed to be Univariant")
            };

583
            for (i, (decl, ty)) in mir.upvar_decls.iter().zip(upvar_tys).enumerate() {
A
Austin Hicks 已提交
584 585
                let byte_offset_of_var_in_env = offsets[i].bytes();

586
                let ops = unsafe {
587 588
                    [llvm::LLVMRustDIBuilderCreateOpDeref(),
                     llvm::LLVMRustDIBuilderCreateOpPlus(),
589
                     byte_offset_of_var_in_env as i64,
590
                     llvm::LLVMRustDIBuilderCreateOpDeref()]
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
                };

                // 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
                };
610 611 612 613 614 615 616 617 618 619
                declare_local(
                    bcx,
                    &mircx.debug_context,
                    decl.debug_name,
                    ty,
                    scope,
                    variable_access,
                    VariableKind::CapturedVariable,
                    DUMMY_SP
                );
620
            }
621
        });
622 623
        LocalRef::Lvalue(LvalueRef::new_sized(llval, LvalueTy::from_ty(arg_ty),
                                              Alignment::AbiAligned))
624
    }).collect()
625 626
}

627
mod analyze;
628 629
mod block;
mod constant;
630
pub mod lvalue;
631
mod operand;
632
mod rvalue;
633
mod statement;