]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/mir/mod.rs
Split out growth functionality into BitVector type
[rust.git] / src / librustc_codegen_llvm / mir / mod.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use common::{C_i32, C_null};
12 use libc::c_uint;
13 use llvm::{self, BasicBlock};
14 use llvm::debuginfo::DIScope;
15 use rustc::ty::{self, Ty, TypeFoldable, UpvarSubsts};
16 use rustc::ty::layout::{LayoutOf, TyLayout};
17 use rustc::mir::{self, Mir};
18 use rustc::ty::subst::Substs;
19 use rustc::session::config::FullDebugInfo;
20 use base;
21 use builder::Builder;
22 use common::{CodegenCx, Funclet};
23 use debuginfo::{self, declare_local, VariableAccess, VariableKind, FunctionDebugContext};
24 use monomorphize::Instance;
25 use abi::{ArgTypeExt, FnType, FnTypeExt, PassMode};
26 use type_::Type;
27 use value::Value;
28
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::BitArray;
35 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
36
37 pub use self::constant::codegen_static_initializer;
38
39 use self::analyze::CleanupKind;
40 use self::place::PlaceRef;
41 use rustc::mir::traversal;
42
43 use self::operand::{OperandRef, OperandValue};
44
45 /// Master context for codegenning from MIR.
46 pub struct FunctionCx<'a, 'll: 'a, 'tcx: 'll> {
47     instance: Instance<'tcx>,
48
49     mir: &'a mir::Mir<'tcx>,
50
51     debug_context: FunctionDebugContext<'ll>,
52
53     llfn: &'ll Value,
54
55     cx: &'a CodegenCx<'ll, 'tcx>,
56
57     fn_ty: FnType<'tcx, Ty<'tcx>>,
58
59     /// When unwinding is initiated, we have to store this personality
60     /// value somewhere so that we can load it and re-use it in the
61     /// resume instruction. The personality is (afaik) some kind of
62     /// value used for C++ unwinding, which must filter by type: we
63     /// don't really care about it very much. Anyway, this value
64     /// contains an alloca into which the personality is stored and
65     /// then later loaded when generating the DIVERGE_BLOCK.
66     personality_slot: Option<PlaceRef<'ll, 'tcx>>,
67
68     /// A `Block` for each MIR `BasicBlock`
69     blocks: IndexVec<mir::BasicBlock, &'ll BasicBlock>,
70
71     /// The funclet status of each basic block
72     cleanup_kinds: IndexVec<mir::BasicBlock, analyze::CleanupKind>,
73
74     /// When targeting MSVC, this stores the cleanup info for each funclet
75     /// BB. This is initialized as we compute the funclets' head block in RPO.
76     funclets: &'a IndexVec<mir::BasicBlock, Option<Funclet<'ll>>>,
77
78     /// This stores the landing-pad block for a given BB, computed lazily on GNU
79     /// and eagerly on MSVC.
80     landing_pads: IndexVec<mir::BasicBlock, Option<&'ll BasicBlock>>,
81
82     /// Cached unreachable block
83     unreachable_block: Option<&'ll BasicBlock>,
84
85     /// The location where each MIR arg/var/tmp/ret is stored. This is
86     /// usually an `PlaceRef` representing an alloca, but not always:
87     /// sometimes we can skip the alloca and just store the value
88     /// directly using an `OperandRef`, which makes for tighter LLVM
89     /// IR. The conditions for using an `OperandRef` are as follows:
90     ///
91     /// - the type of the local must be judged "immediate" by `is_llvm_immediate`
92     /// - the operand must never be referenced indirectly
93     ///     - we should not take its address using the `&` operator
94     ///     - nor should it appear in a place path like `tmp.a`
95     /// - the operand must be defined by an rvalue that can generate immediate
96     ///   values
97     ///
98     /// Avoiding allocs can also be important for certain intrinsics,
99     /// notably `expect`.
100     locals: IndexVec<mir::Local, LocalRef<'ll, 'tcx>>,
101
102     /// Debug information for MIR scopes.
103     scopes: IndexVec<mir::SourceScope, debuginfo::MirDebugScope<'ll>>,
104
105     /// If this function is being monomorphized, this contains the type substitutions used.
106     param_substs: &'tcx Substs<'tcx>,
107 }
108
109 impl FunctionCx<'a, 'll, 'tcx> {
110     pub fn monomorphize<T>(&self, value: &T) -> T
111         where T: TypeFoldable<'tcx>
112     {
113         self.cx.tcx.subst_and_normalize_erasing_regions(
114             self.param_substs,
115             ty::ParamEnv::reveal_all(),
116             value,
117         )
118     }
119
120     pub fn set_debug_loc(&mut self, bx: &Builder<'_, 'll, '_>, source_info: mir::SourceInfo) {
121         let (scope, span) = self.debug_loc(source_info);
122         debuginfo::set_source_location(&self.debug_context, bx, scope, span);
123     }
124
125     pub fn debug_loc(&mut self, source_info: mir::SourceInfo) -> (Option<&'ll DIScope>, Span) {
126         // Bail out if debug info emission is not enabled.
127         match self.debug_context {
128             FunctionDebugContext::DebugInfoDisabled |
129             FunctionDebugContext::FunctionWithoutDebugInfo => {
130                 return (self.scopes[source_info.scope].scope_metadata, source_info.span);
131             }
132             FunctionDebugContext::RegularContext(_) =>{}
133         }
134
135         // In order to have a good line stepping behavior in debugger, we overwrite debug
136         // locations of macro expansions with that of the outermost expansion site
137         // (unless the crate is being compiled with `-Z debug-macros`).
138         if source_info.span.ctxt() == NO_EXPANSION ||
139            self.cx.sess().opts.debugging_opts.debug_macros {
140             let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo());
141             (scope, source_info.span)
142         } else {
143             // Walk up the macro expansion chain until we reach a non-expanded span.
144             // We also stop at the function body level because no line stepping can occur
145             // at the level above that.
146             let mut span = source_info.span;
147             while span.ctxt() != NO_EXPANSION && span.ctxt() != self.mir.span.ctxt() {
148                 if let Some(info) = span.ctxt().outer().expn_info() {
149                     span = info.call_site;
150                 } else {
151                     break;
152                 }
153             }
154             let scope = self.scope_metadata_for_loc(source_info.scope, span.lo());
155             // Use span of the outermost expansion site, while keeping the original lexical scope.
156             (scope, span)
157         }
158     }
159
160     // DILocations inherit source file name from the parent DIScope.  Due to macro expansions
161     // it may so happen that the current span belongs to a different file than the DIScope
162     // corresponding to span's containing source scope.  If so, we need to create a DIScope
163     // "extension" into that file.
164     fn scope_metadata_for_loc(&self, scope_id: mir::SourceScope, pos: BytePos)
165                                -> Option<&'ll DIScope> {
166         let scope_metadata = self.scopes[scope_id].scope_metadata;
167         if pos < self.scopes[scope_id].file_start_pos ||
168            pos >= self.scopes[scope_id].file_end_pos {
169             let cm = self.cx.sess().codemap();
170             let defining_crate = self.debug_context.get_ref(DUMMY_SP).defining_crate;
171             Some(debuginfo::extend_scope_to_file(self.cx,
172                                             scope_metadata.unwrap(),
173                                             &cm.lookup_char_pos(pos).file,
174                                             defining_crate))
175         } else {
176             scope_metadata
177         }
178     }
179 }
180
181 enum LocalRef<'ll, 'tcx> {
182     Place(PlaceRef<'ll, 'tcx>),
183     Operand(Option<OperandRef<'ll, 'tcx>>),
184 }
185
186 impl LocalRef<'ll, 'tcx> {
187     fn new_operand(cx: &CodegenCx<'ll, 'tcx>, layout: TyLayout<'tcx>) -> LocalRef<'ll, 'tcx> {
188         if layout.is_zst() {
189             // Zero-size temporaries aren't always initialized, which
190             // doesn't matter because they don't contain data, but
191             // we need something in the operand.
192             LocalRef::Operand(Some(OperandRef::new_zst(cx, layout)))
193         } else {
194             LocalRef::Operand(None)
195         }
196     }
197 }
198
199 ///////////////////////////////////////////////////////////////////////////
200
201 pub fn codegen_mir(
202     cx: &'a CodegenCx<'ll, 'tcx>,
203     llfn: &'ll Value,
204     mir: &'a Mir<'tcx>,
205     instance: Instance<'tcx>,
206     sig: ty::FnSig<'tcx>,
207 ) {
208     let fn_ty = FnType::new(cx, sig, &[]);
209     debug!("fn_ty: {:?}", fn_ty);
210     let debug_context =
211         debuginfo::create_function_debug_context(cx, instance, sig, llfn, mir);
212     let bx = Builder::new_block(cx, llfn, "start");
213
214     if mir.basic_blocks().iter().any(|bb| bb.is_cleanup) {
215         bx.set_personality_fn(cx.eh_personality());
216     }
217
218     let cleanup_kinds = analyze::cleanup_kinds(&mir);
219     // Allocate a `Block` for every basic block, except
220     // the start block, if nothing loops back to it.
221     let reentrant_start_block = !mir.predecessors_for(mir::START_BLOCK).is_empty();
222     let block_bxs: IndexVec<mir::BasicBlock, &'ll BasicBlock> =
223         mir.basic_blocks().indices().map(|bb| {
224             if bb == mir::START_BLOCK && !reentrant_start_block {
225                 bx.llbb()
226             } else {
227                 bx.build_sibling_block(&format!("{:?}", bb)).llbb()
228             }
229         }).collect();
230
231     // Compute debuginfo scopes from MIR scopes.
232     let scopes = debuginfo::create_mir_scopes(cx, mir, &debug_context);
233     let (landing_pads, funclets) = create_funclets(mir, &bx, &cleanup_kinds, &block_bxs);
234
235     let mut fx = FunctionCx {
236         instance,
237         mir,
238         llfn,
239         fn_ty,
240         cx,
241         personality_slot: None,
242         blocks: block_bxs,
243         unreachable_block: None,
244         cleanup_kinds,
245         landing_pads,
246         funclets: &funclets,
247         scopes,
248         locals: IndexVec::new(),
249         debug_context,
250         param_substs: {
251             assert!(!instance.substs.needs_infer());
252             instance.substs
253         },
254     };
255
256     let memory_locals = analyze::non_ssa_locals(&fx);
257
258     // Allocate variable and temp allocas
259     fx.locals = {
260         let args = arg_local_refs(&bx, &fx, &fx.scopes, &memory_locals);
261
262         let mut allocate_local = |local| {
263             let decl = &mir.local_decls[local];
264             let layout = bx.cx.layout_of(fx.monomorphize(&decl.ty));
265             assert!(!layout.ty.has_erasable_regions());
266
267             if let Some(name) = decl.name {
268                 // User variable
269                 let debug_scope = fx.scopes[decl.visibility_scope];
270                 let dbg = debug_scope.is_valid() && bx.sess().opts.debuginfo == FullDebugInfo;
271
272                 if !memory_locals.contains(local) && !dbg {
273                     debug!("alloc: {:?} ({}) -> operand", local, name);
274                     return LocalRef::new_operand(bx.cx, layout);
275                 }
276
277                 debug!("alloc: {:?} ({}) -> place", local, name);
278                 let place = PlaceRef::alloca(&bx, layout, &name.as_str());
279                 if dbg {
280                     let (scope, span) = fx.debug_loc(mir::SourceInfo {
281                         span: decl.source_info.span,
282                         scope: decl.visibility_scope,
283                     });
284                     declare_local(&bx, &fx.debug_context, name, layout.ty, scope.unwrap(),
285                         VariableAccess::DirectVariable { alloca: place.llval },
286                         VariableKind::LocalVariable, span);
287                 }
288                 LocalRef::Place(place)
289             } else {
290                 // Temporary or return place
291                 if local == mir::RETURN_PLACE && fx.fn_ty.ret.is_indirect() {
292                     debug!("alloc: {:?} (return place) -> place", local);
293                     let llretptr = llvm::get_param(llfn, 0);
294                     LocalRef::Place(PlaceRef::new_sized(llretptr, layout, layout.align))
295                 } else if memory_locals.contains(local) {
296                     debug!("alloc: {:?} -> place", local);
297                     LocalRef::Place(PlaceRef::alloca(&bx, layout, &format!("{:?}", local)))
298                 } else {
299                     // If this is an immediate local, we do not create an
300                     // alloca in advance. Instead we wait until we see the
301                     // definition and update the operand there.
302                     debug!("alloc: {:?} -> operand", local);
303                     LocalRef::new_operand(bx.cx, layout)
304                 }
305             }
306         };
307
308         let retptr = allocate_local(mir::RETURN_PLACE);
309         iter::once(retptr)
310             .chain(args.into_iter())
311             .chain(mir.vars_and_temps_iter().map(allocate_local))
312             .collect()
313     };
314
315     // Branch to the START block, if it's not the entry block.
316     if reentrant_start_block {
317         bx.br(fx.blocks[mir::START_BLOCK]);
318     }
319
320     // Up until here, IR instructions for this function have explicitly not been annotated with
321     // source code location, so we don't step into call setup code. From here on, source location
322     // emitting should be enabled.
323     debuginfo::start_emitting_source_locations(&fx.debug_context);
324
325     let rpo = traversal::reverse_postorder(&mir);
326     let mut visited = BitArray::new(mir.basic_blocks().len());
327
328     // Codegen the body of each block using reverse postorder
329     for (bb, _) in rpo {
330         visited.insert(bb.index());
331         fx.codegen_block(bb);
332     }
333
334     // Remove blocks that haven't been visited, or have no
335     // predecessors.
336     for bb in mir.basic_blocks().indices() {
337         // Unreachable block
338         if !visited.contains(bb.index()) {
339             debug!("codegen_mir: block {:?} was not visited", bb);
340             unsafe {
341                 llvm::LLVMDeleteBasicBlock(fx.blocks[bb]);
342             }
343         }
344     }
345 }
346
347 fn create_funclets(
348     mir: &'a Mir<'tcx>,
349     bx: &Builder<'a, 'll, 'tcx>,
350     cleanup_kinds: &IndexVec<mir::BasicBlock, CleanupKind>,
351     block_bxs: &IndexVec<mir::BasicBlock, &'ll BasicBlock>)
352     -> (IndexVec<mir::BasicBlock, Option<&'ll BasicBlock>>,
353         IndexVec<mir::BasicBlock, Option<Funclet<'ll>>>)
354 {
355     block_bxs.iter_enumerated().zip(cleanup_kinds).map(|((bb, &llbb), cleanup_kind)| {
356         match *cleanup_kind {
357             CleanupKind::Funclet if base::wants_msvc_seh(bx.sess()) => {}
358             _ => return (None, None)
359         }
360
361         let cleanup;
362         let ret_llbb;
363         match mir[bb].terminator.as_ref().map(|t| &t.kind) {
364             // This is a basic block that we're aborting the program for,
365             // notably in an `extern` function. These basic blocks are inserted
366             // so that we assert that `extern` functions do indeed not panic,
367             // and if they do we abort the process.
368             //
369             // On MSVC these are tricky though (where we're doing funclets). If
370             // we were to do a cleanuppad (like below) the normal functions like
371             // `longjmp` would trigger the abort logic, terminating the
372             // program. Instead we insert the equivalent of `catch(...)` for C++
373             // which magically doesn't trigger when `longjmp` files over this
374             // frame.
375             //
376             // Lots more discussion can be found on #48251 but this codegen is
377             // modeled after clang's for:
378             //
379             //      try {
380             //          foo();
381             //      } catch (...) {
382             //          bar();
383             //      }
384             Some(&mir::TerminatorKind::Abort) => {
385                 let cs_bx = bx.build_sibling_block(&format!("cs_funclet{:?}", bb));
386                 let cp_bx = bx.build_sibling_block(&format!("cp_funclet{:?}", bb));
387                 ret_llbb = cs_bx.llbb();
388
389                 let cs = cs_bx.catch_switch(None, None, 1);
390                 cs_bx.add_handler(cs, cp_bx.llbb());
391
392                 // The "null" here is actually a RTTI type descriptor for the
393                 // C++ personality function, but `catch (...)` has no type so
394                 // it's null. The 64 here is actually a bitfield which
395                 // represents that this is a catch-all block.
396                 let null = C_null(Type::i8p(bx.cx));
397                 let sixty_four = C_i32(bx.cx, 64);
398                 cleanup = cp_bx.catch_pad(cs, &[null, sixty_four, null]);
399                 cp_bx.br(llbb);
400             }
401             _ => {
402                 let cleanup_bx = bx.build_sibling_block(&format!("funclet_{:?}", bb));
403                 ret_llbb = cleanup_bx.llbb();
404                 cleanup = cleanup_bx.cleanup_pad(None, &[]);
405                 cleanup_bx.br(llbb);
406             }
407         };
408
409         (Some(ret_llbb), Some(Funclet::new(cleanup)))
410     }).unzip()
411 }
412
413 /// Produce, for each argument, a `Value` pointing at the
414 /// argument's value. As arguments are places, these are always
415 /// indirect.
416 fn arg_local_refs(
417     bx: &Builder<'a, 'll, 'tcx>,
418     fx: &FunctionCx<'a, 'll, 'tcx>,
419     scopes: &IndexVec<mir::SourceScope, debuginfo::MirDebugScope<'ll>>,
420     memory_locals: &BitArray<mir::Local>,
421 ) -> Vec<LocalRef<'ll, 'tcx>> {
422     let mir = fx.mir;
423     let tcx = bx.tcx();
424     let mut idx = 0;
425     let mut llarg_idx = fx.fn_ty.ret.is_indirect() as usize;
426
427     // Get the argument scope, if it exists and if we need it.
428     let arg_scope = scopes[mir::OUTERMOST_SOURCE_SCOPE];
429     let arg_scope = if bx.sess().opts.debuginfo == FullDebugInfo {
430         arg_scope.scope_metadata
431     } else {
432         None
433     };
434
435     mir.args_iter().enumerate().map(|(arg_index, local)| {
436         let arg_decl = &mir.local_decls[local];
437
438         let name = if let Some(name) = arg_decl.name {
439             name.as_str().to_string()
440         } else {
441             format!("arg{}", arg_index)
442         };
443
444         if Some(local) == mir.spread_arg {
445             // This argument (e.g. the last argument in the "rust-call" ABI)
446             // is a tuple that was spread at the ABI level and now we have
447             // to reconstruct it into a tuple local variable, from multiple
448             // individual LLVM function arguments.
449
450             let arg_ty = fx.monomorphize(&arg_decl.ty);
451             let tupled_arg_tys = match arg_ty.sty {
452                 ty::TyTuple(ref tys) => tys,
453                 _ => bug!("spread argument isn't a tuple?!")
454             };
455
456             let place = PlaceRef::alloca(bx, bx.cx.layout_of(arg_ty), &name);
457             for i in 0..tupled_arg_tys.len() {
458                 let arg = &fx.fn_ty.args[idx];
459                 idx += 1;
460                 if arg.pad.is_some() {
461                     llarg_idx += 1;
462                 }
463                 arg.store_fn_arg(bx, &mut llarg_idx, place.project_field(bx, i));
464             }
465
466             // Now that we have one alloca that contains the aggregate value,
467             // we can create one debuginfo entry for the argument.
468             arg_scope.map(|scope| {
469                 let variable_access = VariableAccess::DirectVariable {
470                     alloca: place.llval
471                 };
472                 declare_local(
473                     bx,
474                     &fx.debug_context,
475                     arg_decl.name.unwrap_or(keywords::Invalid.name()),
476                     arg_ty, scope,
477                     variable_access,
478                     VariableKind::ArgumentVariable(arg_index + 1),
479                     DUMMY_SP
480                 );
481             });
482
483             return LocalRef::Place(place);
484         }
485
486         let arg = &fx.fn_ty.args[idx];
487         idx += 1;
488         if arg.pad.is_some() {
489             llarg_idx += 1;
490         }
491
492         if arg_scope.is_none() && !memory_locals.contains(local) {
493             // We don't have to cast or keep the argument in the alloca.
494             // FIXME(eddyb): We should figure out how to use llvm.dbg.value instead
495             // of putting everything in allocas just so we can use llvm.dbg.declare.
496             let local = |op| LocalRef::Operand(Some(op));
497             match arg.mode {
498                 PassMode::Ignore => {
499                     return local(OperandRef::new_zst(bx.cx, arg.layout));
500                 }
501                 PassMode::Direct(_) => {
502                     let llarg = llvm::get_param(bx.llfn(), llarg_idx as c_uint);
503                     bx.set_value_name(llarg, &name);
504                     llarg_idx += 1;
505                     return local(
506                         OperandRef::from_immediate_or_packed_pair(bx, llarg, arg.layout));
507                 }
508                 PassMode::Pair(..) => {
509                     let a = llvm::get_param(bx.llfn(), llarg_idx as c_uint);
510                     bx.set_value_name(a, &(name.clone() + ".0"));
511                     llarg_idx += 1;
512
513                     let b = llvm::get_param(bx.llfn(), llarg_idx as c_uint);
514                     bx.set_value_name(b, &(name + ".1"));
515                     llarg_idx += 1;
516
517                     return local(OperandRef {
518                         val: OperandValue::Pair(a, b),
519                         layout: arg.layout
520                     });
521                 }
522                 _ => {}
523             }
524         }
525
526         let place = if arg.is_indirect() {
527             // Don't copy an indirect argument to an alloca, the caller
528             // already put it in a temporary alloca and gave it up.
529             // FIXME: lifetimes
530             let llarg = llvm::get_param(bx.llfn(), llarg_idx as c_uint);
531             bx.set_value_name(llarg, &name);
532             llarg_idx += 1;
533             PlaceRef::new_sized(llarg, arg.layout, arg.layout.align)
534         } else {
535             let tmp = PlaceRef::alloca(bx, arg.layout, &name);
536             arg.store_fn_arg(bx, &mut llarg_idx, tmp);
537             tmp
538         };
539         arg_scope.map(|scope| {
540             // Is this a regular argument?
541             if arg_index > 0 || mir.upvar_decls.is_empty() {
542                 // The Rust ABI passes indirect variables using a pointer and a manual copy, so we
543                 // need to insert a deref here, but the C ABI uses a pointer and a copy using the
544                 // byval attribute, for which LLVM always does the deref itself,
545                 // so we must not add it.
546                 let variable_access = VariableAccess::DirectVariable {
547                     alloca: place.llval
548                 };
549
550                 declare_local(
551                     bx,
552                     &fx.debug_context,
553                     arg_decl.name.unwrap_or(keywords::Invalid.name()),
554                     arg.layout.ty,
555                     scope,
556                     variable_access,
557                     VariableKind::ArgumentVariable(arg_index + 1),
558                     DUMMY_SP
559                 );
560                 return;
561             }
562
563             // Or is it the closure environment?
564             let (closure_layout, env_ref) = match arg.layout.ty.sty {
565                 ty::TyRawPtr(ty::TypeAndMut { ty, .. }) |
566                 ty::TyRef(_, ty, _)  => (bx.cx.layout_of(ty), true),
567                 _ => (arg.layout, false)
568             };
569
570             let (def_id, upvar_substs) = match closure_layout.ty.sty {
571                 ty::TyClosure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs)),
572                 ty::TyGenerator(def_id, substs, _) => (def_id, UpvarSubsts::Generator(substs)),
573                 _ => bug!("upvar_decls with non-closure arg0 type `{}`", closure_layout.ty)
574             };
575             let upvar_tys = upvar_substs.upvar_tys(def_id, tcx);
576
577             for (i, (decl, ty)) in mir.upvar_decls.iter().zip(upvar_tys).enumerate() {
578                 let byte_offset_of_var_in_env = closure_layout.fields.offset(i).bytes();
579
580                 let ops = unsafe {
581                     [llvm::LLVMRustDIBuilderCreateOpDeref(),
582                      llvm::LLVMRustDIBuilderCreateOpPlusUconst(),
583                      byte_offset_of_var_in_env as i64,
584                      llvm::LLVMRustDIBuilderCreateOpDeref()]
585                 };
586
587                 // The environment and the capture can each be indirect.
588                 let mut ops = if env_ref { &ops[..] } else { &ops[1..] };
589
590                 let ty = if let (true, &ty::TyRef(_, ty, _)) = (decl.by_ref, &ty.sty) {
591                     ty
592                 } else {
593                     ops = &ops[..ops.len() - 1];
594                     ty
595                 };
596
597                 let variable_access = VariableAccess::IndirectVariable {
598                     alloca: place.llval,
599                     address_operations: &ops
600                 };
601                 declare_local(
602                     bx,
603                     &fx.debug_context,
604                     decl.debug_name,
605                     ty,
606                     scope,
607                     variable_access,
608                     VariableKind::LocalVariable,
609                     DUMMY_SP
610                 );
611             }
612         });
613         LocalRef::Place(place)
614     }).collect()
615 }
616
617 mod analyze;
618 mod block;
619 mod constant;
620 pub mod place;
621 pub mod operand;
622 mod rvalue;
623 mod statement;