summaryrefslogtreecommitdiff
path: root/src/librustc_trans/trans/mir/mod.rs
blob: 4ad2e035945f3baaac56dbfaf2487732ece9a781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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};
use rustc::mir::repr as mir;
use rustc::mir::tcx::LvalueTy;
use trans::base;
use trans::common::{self, Block, BlockAndBuilder};
use trans::expr;
use trans::type_of;

use self::lvalue::LvalueRef;
use self::operand::OperandRef;

// FIXME DebugLoc is always None right now

/// Master context for translating MIR.
pub struct MirContext<'bcx, 'tcx:'bcx> {
    mir: &'bcx mir::Mir<'tcx>,

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

    /// 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>>,

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

    /// An LLVM alloca for each MIR `VarDecl`
    vars: Vec<LvalueRef<'tcx>>,

    /// 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
    ///
    /// Avoiding allocs can also be important for certain intrinsics,
    /// notably `expect`.
    temps: Vec<TempRef<'tcx>>,

    /// 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>>,
}

enum TempRef<'tcx> {
    Lvalue(LvalueRef<'tcx>),
    Operand(Option<OperandRef<'tcx>>),
}

///////////////////////////////////////////////////////////////////////////

pub fn trans_mir<'bcx, 'tcx>(bcx: BlockAndBuilder<'bcx, 'tcx>) {
    let fcx = bcx.fcx();
    let mir = bcx.mir();

    let mir_blocks = bcx.mir().all_basic_blocks();

    // Analyze the temps to determine which must be lvalues
    // FIXME
    let lvalue_temps = bcx.with_block(|bcx| {
      analyze::lvalue_temps(bcx, mir)
    });

    // Allocate variable and temp allocas
    let vars = mir.var_decls.iter()
                            .map(|decl| (bcx.monomorphize(&decl.ty), decl.name))
                            .map(|(mty, name)| LvalueRef::alloca(&bcx, mty, &name.as_str()))
                            .collect();
    let temps = mir.temp_decls.iter()
                              .map(|decl| bcx.monomorphize(&decl.ty))
                              .enumerate()
                              .map(|(i, mty)| if lvalue_temps.contains(i) {
                                  TempRef::Lvalue(LvalueRef::alloca(&bcx,
                                                                    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)
                              })
                              .collect();
    let args = arg_value_refs(&bcx, mir);

    // Allocate a `Block` for every basic block
    let block_bcxs: Vec<Block<'bcx,'tcx>> =
        mir_blocks.iter()
                  .map(|&bb|{
                      // FIXME(#30941) this doesn't handle msvc-style exceptions
                      fcx.new_block(&format!("{:?}", bb), None)
                  })
                  .collect();

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

    let mut mircx = MirContext {
        mir: mir,
        fcx: fcx,
        llpersonalityslot: None,
        blocks: block_bcxs,
        unreachable_block: None,
        vars: vars,
        temps: temps,
        args: args,
    };

    // Translate the body of each block
    for &bb in &mir_blocks {
        mircx.trans_block(bb);
    }
}

/// Produce, for each argument, a `ValueRef` pointing at the
/// argument's value. As arguments are lvalues, these are always
/// indirect.
fn arg_value_refs<'bcx, 'tcx>(bcx: &BlockAndBuilder<'bcx, 'tcx>,
                              mir: &mir::Mir<'tcx>)
                              -> Vec<LvalueRef<'tcx>> {
    // FIXME tupled_args? I think I'd rather that mapping is done in MIR land though
    let fcx = bcx.fcx();
    let tcx = bcx.tcx();
    let mut idx = fcx.arg_offset() as c_uint;
    mir.arg_decls
       .iter()
       .enumerate()
       .map(|(arg_index, arg_decl)| {
           let arg_ty = bcx.monomorphize(&arg_decl.ty);
           let llval = if type_of::arg_is_indirect(bcx.ccx(), arg_ty) {
               // 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, idx);
               idx += 1;
               llarg
           } else 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 lldata = llvm::get_param(fcx.llfn, idx);
               let llextra = llvm::get_param(fcx.llfn, idx + 1);
               idx += 2;
               let (lltemp, dataptr, meta) = bcx.with_block(|bcx| {
                   let lltemp = base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index));
                   (lltemp, expr::get_dataptr(bcx, lltemp), expr::get_meta(bcx, lltemp))
               });
               bcx.store(lldata, dataptr);
               bcx.store(llextra, meta);
               lltemp
           } else {
               // otherwise, arg is passed by value, so make a
               // temporary and store it there
               let llarg = llvm::get_param(fcx.llfn, idx);
               idx += 1;
               bcx.with_block(|bcx| {
                   let lltemp = base::alloc_ty(bcx, arg_ty, &format!("arg{}", arg_index));
                   base::store_ty(bcx, llarg, lltemp, arg_ty);
                   lltemp
               })
           };
           LvalueRef::new_sized(llval, LvalueTy::from_ty(arg_ty))
       })
       .collect()
}

mod analyze;
mod block;
mod constant;
mod drop;
mod lvalue;
mod operand;
mod rvalue;
mod statement;