base.rs 50.7 KB
Newer Older
1
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9
// 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.
10

I
Irina Popa 已提交
11
//! Codegen the completed AST to the LLVM IR.
12
//!
I
Irina Popa 已提交
13 14 15
//! Some functions here, such as codegen_block and codegen_expr, return a value --
//! the result of the codegen to LLVM -- while others, such as codegen_fn
//! and mono_item, are called only for the side effect of adding a
16 17
//! particular definition to the LLVM IR output we're producing.
//!
I
Irina Popa 已提交
18
//! Hopefully useful general knowledge about codegen:
19 20 21 22 23 24
//!
//!   * There's no way to find out the Ty type of a ValueRef.  Doing so
//!     would be "trying to get the eggs out of an omelette" (credit:
//!     pcwalton).  You can, instead, find out its TypeRef by calling val_ty,
//!     but one TypeRef corresponds to many `Ty`s; for instance, tup(int, int,
//!     int) and rec(x=int, y=int, z=int) will have the same TypeRef.
25

26 27
use super::ModuleLlvm;
use super::ModuleSource;
I
Irina Popa 已提交
28
use super::ModuleCodegen;
29
use super::ModuleKind;
30

31
use abi;
32
use back::{link, lto};
I
Irina Popa 已提交
33
use back::write::{self, OngoingCodegen, create_target_machine};
34
use llvm::{ContextRef, ModuleRef, ValueRef, Vector, get_param};
35
use llvm;
36
use libc::c_uint;
37
use metadata;
38
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
39
use rustc::middle::lang_items::StartFnLangItem;
40
use rustc::middle::weak_lang_items;
41 42
use rustc::mir::mono::{Linkage, Visibility, Stats};
use rustc::middle::cstore::{EncodedMetadata};
43
use rustc::ty::{self, Ty, TyCtxt};
44
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf};
45
use rustc::ty::query::Providers;
46
use rustc::dep_graph::{DepNode, DepConstructor};
47
use rustc::middle::cstore::{self, LinkMeta, LinkagePreference};
48
use rustc::middle::exported_symbols;
49
use rustc::util::common::{time, print_time_passes_entry};
A
Alex Crichton 已提交
50
use rustc::session::config::{self, NoDebugInfo};
51
use rustc::session::Session;
52
use rustc_incremental;
53
use allocator;
54
use mir::place::PlaceRef;
55
use attributes;
56
use builder::{Builder, MemFlags};
57
use callee;
58
use common::{C_bool, C_bytes_in_context, C_i32, C_usize};
59
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
60
use rustc_mir::monomorphize::item::DefPathBasedNames;
61
use common::{self, C_struct_in_context, C_array, val_ty};
62
use consts;
63
use context::{self, CodegenCx};
M
Mark-Simulacrum 已提交
64
use debuginfo;
65 66 67
use declare;
use meth;
use mir;
68
use monomorphize::Instance;
M
Maik Klein 已提交
69
use monomorphize::partitioning::{self, PartitioningStrategy, CodegenUnit, CodegenUnitExt};
I
Irina Popa 已提交
70
use rustc_codegen_utils::symbol_names_test;
71
use time_graph;
72
use mono_item::{MonoItem, BaseMonoItemExt, MonoItemExt};
73
use type_::Type;
74
use type_of::LayoutLlvmExt;
75
use rustc::util::nodemap::{FxHashMap, FxHashSet, DefIdSet};
76
use CrateInfo;
77
use rustc_data_structures::sync::Lrc;
J
James Miller 已提交
78

79
use std::any::Any;
80
use std::ffi::CString;
A
Alex Crichton 已提交
81
use std::str;
82
use std::sync::Arc;
83
use std::time::{Instant, Duration};
V
varkor 已提交
84 85
use std::i32;
use std::cmp;
86
use std::sync::mpsc;
87
use syntax_pos::Span;
A
Alex Crichton 已提交
88
use syntax_pos::symbol::InternedString;
89
use syntax::attr;
90
use rustc::hir;
91
use syntax::ast;
T
Tim Chevalier 已提交
92

93
use mir::operand::OperandValue;
94

B
bjorn3 已提交
95 96
use rustc_codegen_utils::check_for_rustc_errors_attr;

97
pub struct StatRecorder<'a, 'tcx: 'a> {
98
    cx: &'a CodegenCx<'a, 'tcx>,
99
    name: Option<String>,
100
    istart: usize,
101 102
}

103
impl<'a, 'tcx> StatRecorder<'a, 'tcx> {
104 105
    pub fn new(cx: &'a CodegenCx<'a, 'tcx>, name: String) -> StatRecorder<'a, 'tcx> {
        let istart = cx.stats.borrow().n_llvm_insns;
106
        StatRecorder {
107
            cx,
108
            name: Some(name),
109
            istart,
110 111 112 113
        }
    }
}

114
impl<'a, 'tcx> Drop for StatRecorder<'a, 'tcx> {
D
Daniel Micay 已提交
115
    fn drop(&mut self) {
I
Irina Popa 已提交
116
        if self.cx.sess().codegen_stats() {
117
            let mut stats = self.cx.stats.borrow_mut();
A
Alex Crichton 已提交
118 119 120
            let iend = stats.n_llvm_insns;
            stats.fn_stats.push((self.name.take().unwrap(), iend - self.istart));
            stats.n_fns += 1;
121
            // Reset LLVM insn count to avoid compound costs.
A
Alex Crichton 已提交
122
            stats.n_llvm_insns = self.istart;
123 124 125 126
        }
    }
}

127
pub fn bin_op_to_icmp_predicate(op: hir::BinOp_,
M
Ms2ger 已提交
128
                                signed: bool)
129 130
                                -> llvm::IntPredicate {
    match op {
131 132 133 134 135 136
        hir::BiEq => llvm::IntEQ,
        hir::BiNe => llvm::IntNE,
        hir::BiLt => if signed { llvm::IntSLT } else { llvm::IntULT },
        hir::BiLe => if signed { llvm::IntSLE } else { llvm::IntULE },
        hir::BiGt => if signed { llvm::IntSGT } else { llvm::IntUGT },
        hir::BiGe => if signed { llvm::IntSGE } else { llvm::IntUGE },
137
        op => {
138 139 140
            bug!("comparison_op_to_icmp_predicate: expected comparison operator, \
                  found {:?}",
                 op)
141 142 143
        }
    }
}
144

145
pub fn bin_op_to_fcmp_predicate(op: hir::BinOp_) -> llvm::RealPredicate {
146
    match op {
147 148 149 150 151 152
        hir::BiEq => llvm::RealOEQ,
        hir::BiNe => llvm::RealUNE,
        hir::BiLt => llvm::RealOLT,
        hir::BiLe => llvm::RealOLE,
        hir::BiGt => llvm::RealOGT,
        hir::BiGe => llvm::RealOGE,
153
        op => {
154 155 156
            bug!("comparison_op_to_fcmp_predicate: expected comparison operator, \
                  found {:?}",
                 op);
157 158 159 160
        }
    }
}

M
Mark Simulacrum 已提交
161
pub fn compare_simd_types<'a, 'tcx>(
162
    bx: &Builder<'a, 'tcx>,
M
Mark Simulacrum 已提交
163 164 165 166 167 168
    lhs: ValueRef,
    rhs: ValueRef,
    t: Ty<'tcx>,
    ret_ty: Type,
    op: hir::BinOp_
) -> ValueRef {
169
    let signed = match t.sty {
170
        ty::TyFloat(_) => {
171
            let cmp = bin_op_to_fcmp_predicate(op);
172
            return bx.sext(bx.fcmp(cmp, lhs, rhs), ret_ty);
173
        },
174 175
        ty::TyUint(_) => false,
        ty::TyInt(_) => true,
176
        _ => bug!("compare_simd_types: invalid SIMD type"),
177
    };
178

179
    let cmp = bin_op_to_icmp_predicate(op, signed);
180 181 182 183
    // LLVM outputs an `< size x i1 >`, so we need to perform a sign extension
    // to get the correctly sized type. This will compile to a single instruction
    // once the IR is converted to assembly if the SIMD instruction is supported
    // by the target architecture.
184
    bx.sext(bx.icmp(cmp, lhs, rhs), ret_ty)
185 186
}

A
Ariel Ben-Yehuda 已提交
187 188 189 190
/// Retrieve the information we are losing (making dynamic) in an unsizing
/// adjustment.
///
/// The `old_info` argument is a bit funny. It is intended for use
B
Bastien Orivel 已提交
191
/// in an upcast, where the new vtable for an object will be derived
A
Ariel Ben-Yehuda 已提交
192
/// from the old one.
193
pub fn unsized_info<'cx, 'tcx>(cx: &CodegenCx<'cx, 'tcx>,
A
Ariel Ben-Yehuda 已提交
194 195
                                source: Ty<'tcx>,
                                target: Ty<'tcx>,
196
                                old_info: Option<ValueRef>)
A
Ariel Ben-Yehuda 已提交
197
                                -> ValueRef {
198
    let (source, target) = cx.tcx.struct_lockstep_tails(source, target);
A
Ariel Ben-Yehuda 已提交
199
    match (&source.sty, &target.sty) {
200
        (&ty::TyArray(_, len), &ty::TySlice(_)) => {
201
            C_usize(cx, len.unwrap_usize(cx.tcx))
202
        }
203
        (&ty::TyDynamic(..), &ty::TyDynamic(..)) => {
A
Ariel Ben-Yehuda 已提交
204 205 206 207 208
            // For now, upcasts are limited to changes in marker
            // traits, and hence never actually require an actual
            // change to the vtable.
            old_info.expect("unsized_info: missing old info for trait upcast")
        }
209
        (_, &ty::TyDynamic(ref data, ..)) => {
210 211 212 213
            let vtable_ptr = cx.layout_of(cx.tcx.mk_mut_ptr(target))
                .field(cx, abi::FAT_PTR_EXTRA);
            consts::ptrcast(meth::get_vtable(cx, source, data.principal()),
                            vtable_ptr.llvm_type(cx))
A
Ariel Ben-Yehuda 已提交
214
        }
215
        _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
A
Ariel Ben-Yehuda 已提交
216
                                     source,
217
                                     target),
A
Ariel Ben-Yehuda 已提交
218 219 220 221
    }
}

/// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
M
Mark Simulacrum 已提交
222
pub fn unsize_thin_ptr<'a, 'tcx>(
223
    bx: &Builder<'a, 'tcx>,
M
Mark Simulacrum 已提交
224 225 226 227
    src: ValueRef,
    src_ty: Ty<'tcx>,
    dst_ty: Ty<'tcx>
) -> (ValueRef, ValueRef) {
A
Ariel Ben-Yehuda 已提交
228 229
    debug!("unsize_thin_ptr: {:?} => {:?}", src_ty, dst_ty);
    match (&src_ty.sty, &dst_ty.sty) {
230 231 232
        (&ty::TyRef(_, a, _),
         &ty::TyRef(_, b, _)) |
        (&ty::TyRef(_, a, _),
A
Ariel Ben-Yehuda 已提交
233 234 235
         &ty::TyRawPtr(ty::TypeAndMut { ty: b, .. })) |
        (&ty::TyRawPtr(ty::TypeAndMut { ty: a, .. }),
         &ty::TyRawPtr(ty::TypeAndMut { ty: b, .. })) => {
236 237 238
            assert!(bx.cx.type_is_sized(a));
            let ptr_ty = bx.cx.layout_of(b).llvm_type(bx.cx).ptr_to();
            (bx.pointercast(src, ptr_ty), unsized_info(bx.cx, a, b, None))
A
Ariel Ben-Yehuda 已提交
239
        }
240 241
        (&ty::TyAdt(def_a, _), &ty::TyAdt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
            let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
242 243 244
            assert!(bx.cx.type_is_sized(a));
            let ptr_ty = bx.cx.layout_of(b).llvm_type(bx.cx).ptr_to();
            (bx.pointercast(src, ptr_ty), unsized_info(bx.cx, a, b, None))
245
        }
246 247 248
        (&ty::TyAdt(def_a, _), &ty::TyAdt(def_b, _)) => {
            assert_eq!(def_a, def_b);

249 250
            let src_layout = bx.cx.layout_of(src_ty);
            let dst_layout = bx.cx.layout_of(dst_ty);
251 252
            let mut result = None;
            for i in 0..src_layout.fields.count() {
253
                let src_f = src_layout.field(bx.cx, i);
254 255 256 257 258 259 260
                assert_eq!(src_layout.fields.offset(i).bytes(), 0);
                assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
                if src_f.is_zst() {
                    continue;
                }
                assert_eq!(src_layout.size, src_f.size);

261
                let dst_f = dst_layout.field(bx.cx, i);
262 263
                assert_ne!(src_f.ty, dst_f.ty);
                assert_eq!(result, None);
264
                result = Some(unsize_thin_ptr(bx, src, src_f.ty, dst_f.ty));
265 266 267
            }
            let (lldata, llextra) = result.unwrap();
            // HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
268 269
            (bx.bitcast(lldata, dst_layout.scalar_pair_element_llvm_type(bx.cx, 0, true)),
             bx.bitcast(llextra, dst_layout.scalar_pair_element_llvm_type(bx.cx, 1, true)))
270
        }
271
        _ => bug!("unsize_thin_ptr: called on bad types"),
A
Ariel Ben-Yehuda 已提交
272 273 274 275 276
    }
}

/// Coerce `src`, which is a reference to a value of type `src_ty`,
/// to a value of type `dst_ty` and store the result in `dst`
277
pub fn coerce_unsized_into<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
278 279
                                     src: PlaceRef<'tcx>,
                                     dst: PlaceRef<'tcx>) {
280 281
    let src_ty = src.layout.ty;
    let dst_ty = dst.layout.ty;
282
    let coerce_ptr = || {
283
        let (base, info) = match src.load(bx).val {
284 285 286 287 288
            OperandValue::Pair(base, info) => {
                // fat-ptr to fat-ptr unsize preserves the vtable
                // i.e. &'a fmt::Debug+Send => &'a fmt::Debug
                // So we need to pointercast the base to ensure
                // the types match up.
289 290
                let thin_ptr = dst.layout.field(bx.cx, abi::FAT_PTR_ADDR);
                (bx.pointercast(base, thin_ptr.llvm_type(bx.cx)), info)
291 292
            }
            OperandValue::Immediate(base) => {
293
                unsize_thin_ptr(bx, base, src_ty, dst_ty)
294 295
            }
            OperandValue::Ref(..) => bug!()
296
        };
297
        OperandValue::Pair(base, info).store(bx, dst);
298
    };
A
Ariel Ben-Yehuda 已提交
299 300 301 302
    match (&src_ty.sty, &dst_ty.sty) {
        (&ty::TyRef(..), &ty::TyRef(..)) |
        (&ty::TyRef(..), &ty::TyRawPtr(..)) |
        (&ty::TyRawPtr(..), &ty::TyRawPtr(..)) => {
303 304 305 306
            coerce_ptr()
        }
        (&ty::TyAdt(def_a, _), &ty::TyAdt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
            coerce_ptr()
A
Ariel Ben-Yehuda 已提交
307 308
        }

309
        (&ty::TyAdt(def_a, _), &ty::TyAdt(def_b, _)) => {
A
Ariel Ben-Yehuda 已提交
310 311
            assert_eq!(def_a, def_b);

312
            for i in 0..def_a.variants[0].fields.len() {
313 314
                let src_f = src.project_field(bx, i);
                let dst_f = dst.project_field(bx, i);
A
Ariel Ben-Yehuda 已提交
315

316
                if dst_f.layout.is_zst() {
M
Ms2ger 已提交
317 318
                    continue;
                }
A
Ariel Ben-Yehuda 已提交
319

320
                if src_f.layout.ty == dst_f.layout.ty {
321
                    memcpy_ty(bx, dst_f.llval, src_f.llval, src_f.layout,
322
                              src_f.align.min(dst_f.align), MemFlags::empty());
A
Ariel Ben-Yehuda 已提交
323
                } else {
324
                    coerce_unsized_into(bx, src_f, dst_f);
A
Ariel Ben-Yehuda 已提交
325 326 327
                }
            }
        }
328 329 330
        _ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}",
                  src_ty,
                  dst_ty),
A
Ariel Ben-Yehuda 已提交
331 332 333
    }
}

334
pub fn cast_shift_expr_rhs(
335
    cx: &Builder, op: hir::BinOp_, lhs: ValueRef, rhs: ValueRef
336 337
) -> ValueRef {
    cast_shift_rhs(op, lhs, rhs, |a, b| cx.trunc(a, b), |a, b| cx.zext(a, b))
338 339
}

340
fn cast_shift_rhs<F, G>(op: hir::BinOp_,
341 342 343 344
                        lhs: ValueRef,
                        rhs: ValueRef,
                        trunc: F,
                        zext: G)
M
Ms2ger 已提交
345 346 347
                        -> ValueRef
    where F: FnOnce(ValueRef, Type) -> ValueRef,
          G: FnOnce(ValueRef, Type) -> ValueRef
348
{
349
    // Shifts may have any size int on the rhs
350
    if op.is_shift() {
351 352
        let mut rhs_llty = val_ty(rhs);
        let mut lhs_llty = val_ty(lhs);
M
Ms2ger 已提交
353 354 355 356 357 358
        if rhs_llty.kind() == Vector {
            rhs_llty = rhs_llty.element_type()
        }
        if lhs_llty.kind() == Vector {
            lhs_llty = lhs_llty.element_type()
        }
359 360 361 362 363 364 365 366
        let rhs_sz = rhs_llty.int_width();
        let lhs_sz = lhs_llty.int_width();
        if lhs_sz < rhs_sz {
            trunc(rhs, lhs_llty)
        } else if lhs_sz > rhs_sz {
            // FIXME (#1877: If shifting by negative
            // values becomes not undefined then this is wrong.
            zext(rhs, lhs_llty)
367 368 369
        } else {
            rhs
        }
370 371
    } else {
        rhs
372 373 374
    }
}

375 376 377 378 379 380
/// Returns whether this session's target will use SEH-based unwinding.
///
/// This is only true for MSVC targets, and even then the 64-bit MSVC target
/// currently uses SEH-ish unwinding with DWARF info tables to the side (same as
/// 64-bit MinGW) instead of "full SEH".
pub fn wants_msvc_seh(sess: &Session) -> bool {
381
    sess.target.target.options.is_like_msvc
382 383
}

384 385 386
pub fn call_assume<'a, 'tcx>(bx: &Builder<'a, 'tcx>, val: ValueRef) {
    let assume_intrinsic = bx.cx.get_intrinsic("llvm.assume");
    bx.call(assume_intrinsic, &[val], None);
387 388
}

389 390 391
pub fn from_immediate(bx: &Builder, val: ValueRef) -> ValueRef {
    if val_ty(val) == Type::i1(bx.cx) {
        bx.zext(val, Type::i8(bx.cx))
392 393 394 395 396
    } else {
        val
    }
}

397
pub fn to_immediate(bx: &Builder, val: ValueRef, layout: layout::TyLayout) -> ValueRef {
398
    if let layout::Abi::Scalar(ref scalar) = layout.abi {
399 400 401 402 403 404 405 406
        return to_immediate_scalar(bx, val, scalar);
    }
    val
}

pub fn to_immediate_scalar(bx: &Builder, val: ValueRef, scalar: &layout::Scalar) -> ValueRef {
    if scalar.is_bool() {
        return bx.trunc(val, Type::i1(bx.cx));
407
    }
408
    val
409 410
}

411
pub fn call_memcpy(bx: &Builder,
412 413 414
                   dst: ValueRef,
                   src: ValueRef,
                   n_bytes: ValueRef,
415
                   align: Align,
416 417 418 419 420 421 422 423
                   flags: MemFlags) {
    if flags.contains(MemFlags::NONTEMPORAL) {
        // HACK(nox): This is inefficient but there is no nontemporal memcpy.
        let val = bx.load(src, align);
        let ptr = bx.pointercast(dst, val_ty(val).ptr_to());
        bx.store_with_flags(val, ptr, align, flags);
        return;
    }
424
    let cx = bx.cx;
425
    let ptr_width = &cx.sess().target.target.target_pointer_width;
426
    let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
427
    let memcpy = cx.get_intrinsic(&key);
428 429 430
    let src_ptr = bx.pointercast(src, Type::i8p(cx));
    let dst_ptr = bx.pointercast(dst, Type::i8p(cx));
    let size = bx.intcast(n_bytes, cx.isize_ty, false);
431
    let align = C_i32(cx, align.abi() as i32);
432
    let volatile = C_bool(cx, flags.contains(MemFlags::VOLATILE));
433
    bx.call(memcpy, &[dst_ptr, src_ptr, size, align, volatile], None);
434 435
}

436
pub fn memcpy_ty<'a, 'tcx>(
437
    bx: &Builder<'a, 'tcx>,
438 439
    dst: ValueRef,
    src: ValueRef,
440
    layout: TyLayout<'tcx>,
441
    align: Align,
442
    flags: MemFlags,
443
) {
444
    let size = layout.size.bytes();
445
    if size == 0 {
446 447 448
        return;
    }

449
    call_memcpy(bx, dst, src, C_usize(bx.cx, size), align, flags);
450 451
}

452
pub fn call_memset<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
453 454 455 456 457
                             ptr: ValueRef,
                             fill_byte: ValueRef,
                             size: ValueRef,
                             align: ValueRef,
                             volatile: bool) -> ValueRef {
458
    let ptr_width = &bx.cx.sess().target.target.target_pointer_width;
459
    let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
460 461 462
    let llintrinsicfn = bx.cx.get_intrinsic(&intrinsic_key);
    let volatile = C_bool(bx.cx, volatile);
    bx.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None)
463 464
}

I
Irina Popa 已提交
465 466
pub fn codegen_instance<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, instance: Instance<'tcx>) {
    let _s = if cx.sess().codegen_stats() {
467
        let mut instance_name = String::new();
468
        DefPathBasedNames::new(cx.tcx, true, true)
469
            .push_def_path(instance.def_id(), &mut instance_name);
470
        Some(StatRecorder::new(cx, instance_name))
471 472 473 474
    } else {
        None
    };

475 476 477
    // this is an info! to allow collecting monomorphization statistics
    // and to allow finding the last function before LLVM aborts from
    // release builds.
I
Irina Popa 已提交
478
    info!("codegen_instance({})", instance);
479

480 481
    let fn_ty = instance.ty(cx.tcx);
    let sig = common::ty_fn_sig(cx, fn_ty);
482
    let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
483

484
    let lldecl = match cx.instances.borrow().get(&instance) {
485 486 487 488
        Some(&val) => val,
        None => bug!("Instance `{:?}` not already declared", instance)
    };

489
    cx.stats.borrow_mut().n_closures += 1;
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    // The `uwtable` attribute according to LLVM is:
    //
    //     This attribute indicates that the ABI being targeted requires that an
    //     unwind table entry be produced for this function even if we can show
    //     that no exceptions passes by it. This is normally the case for the
    //     ELF x86-64 abi, but it can be disabled for some compilation units.
    //
    // Typically when we're compiling with `-C panic=abort` (which implies this
    // `no_landing_pads` check) we don't need `uwtable` because we can't
    // generate any exceptions! On Windows, however, exceptions include other
    // events such as illegal instructions, segfaults, etc. This means that on
    // Windows we end up still needing the `uwtable` attribute even if the `-C
    // panic=abort` flag is passed.
    //
    // You can also find more info on why Windows is whitelisted here in:
    //      https://bugzilla.mozilla.org/show_bug.cgi?id=1302078
507
    if !cx.sess().no_landing_pads() ||
508
       cx.sess().target.target.options.requires_uwtable {
509
        attributes::emit_uwtable(lldecl, true);
510
    }
511

512
    let mir = cx.tcx.instance_mir(instance.def);
I
Irina Popa 已提交
513
    mir::codegen_mir(cx, lldecl, &mir, instance, sig);
514 515
}

516
pub fn set_link_section(cx: &CodegenCx,
517 518 519
                        llval: ValueRef,
                        attrs: &[ast::Attribute]) {
    if let Some(sect) = attr::first_attr_value_str_by_name(attrs, "link_section") {
520
        if contains_null(&sect.as_str()) {
521
            cx.sess().fatal(&format!("Illegal null byte in link_section value: `{}`", &sect));
522 523
        }
        unsafe {
524
            let buf = CString::new(sect.as_str().as_bytes()).unwrap();
525 526
            llvm::LLVMSetSection(llval, buf.as_ptr());
        }
E
Eli Friedman 已提交
527 528 529
    }
}

F
Fourchaux 已提交
530
/// Create the `main` function which will initialize the rust runtime and call
531
/// users main function.
532 533
fn maybe_create_entry_wrapper(cx: &CodegenCx) {
    let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
534
        Some((id, span, _)) => {
535
            (cx.tcx.hir.local_def_id(id), span)
536 537 538 539
        }
        None => return,
    };

540
    let instance = Instance::mono(cx.tcx, main_def_id);
541

542
    if !cx.codegen_unit.contains_item(&MonoItem::Fn(instance)) {
543 544 545
        // We want to create the wrapper in the same codegen unit as Rust's main
        // function.
        return;
546 547
    }

548
    let main_llfn = callee::get_fn(cx, instance);
549

550
    let et = cx.sess().entry_fn.get().map(|e| e.2);
551
    match et {
552 553 554
        Some(config::EntryMain) => create_entry_fn(cx, span, main_llfn, main_def_id, true),
        Some(config::EntryStart) => create_entry_fn(cx, span, main_llfn, main_def_id, false),
        None => {}    // Do nothing.
555
    }
556

557
    fn create_entry_fn<'cx>(cx: &'cx CodegenCx,
S
Simonas Kazlauskas 已提交
558
                       sp: Span,
559
                       rust_main: ValueRef,
560
                       rust_main_def_id: DefId,
561
                       use_start_lang_item: bool) {
562
        let llfty = Type::func(&[Type::c_int(cx), Type::i8p(cx).ptr_to()], &Type::c_int(cx));
563

564
        let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
565 566 567 568 569
        // Given that `main()` has no arguments,
        // then its return type cannot have
        // late-bound regions, since late-bound
        // regions must appear in the argument
        // listing.
570 571 572
        let main_ret_ty = cx.tcx.erase_regions(
            &main_ret_ty.no_late_bound_regions().unwrap(),
        );
K
kyeongwoon 已提交
573

574
        if declare::get_defined_value(cx, "main").is_some() {
S
Simonas Kazlauskas 已提交
575
            // FIXME: We should be smart and show a better diagnostic here.
576
            cx.sess().struct_span_err(sp, "entry symbol `main` defined multiple times")
N
Nick Cameron 已提交
577 578
                      .help("did you use #[no_mangle] on `fn main`? Use #[start] instead")
                      .emit();
579
            cx.sess().abort_if_errors();
580
            bug!();
581
        }
582
        let llfn = declare::declare_cfn(cx, "main", llfty);
583

584
        // `main` should respect same config for frame pointer elimination as rest of code
585
        attributes::set_frame_pointer_elimination(cx, llfn);
586

587
        let bx = Builder::new_block(cx, llfn, "top");
588

589
        debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(&bx);
590

591 592 593
        // Params from native main() used as args for rust start function
        let param_argc = get_param(llfn, 0);
        let param_argv = get_param(llfn, 1);
594
        let arg_argc = bx.intcast(param_argc, cx.isize_ty, true);
595 596
        let arg_argv = param_argv;

M
Mark-Simulacrum 已提交
597
        let (start_fn, args) = if use_start_lang_item {
598
            let start_def_id = cx.tcx.require_lang_item(StartFnLangItem);
599 600 601
            let start_fn = callee::resolve_and_get_fn(
                cx,
                start_def_id,
602
                cx.tcx.intern_substs(&[main_ret_ty.into()]),
603
            );
604
            (start_fn, vec![bx.pointercast(rust_main, Type::i8p(cx).ptr_to()),
605
                            arg_argc, arg_argv])
M
Mark-Simulacrum 已提交
606 607
        } else {
            debug!("using user-defined start fn");
608
            (rust_main, vec![arg_argc, arg_argv])
M
Mark-Simulacrum 已提交
609
        };
610

611 612
        let result = bx.call(start_fn, &args, None);
        bx.ret(bx.intcast(result, Type::c_int(cx), true));
M
Marijn Haverbeke 已提交
613
    }
614 615
}

616
fn contains_null(s: &str) -> bool {
617
    s.bytes().any(|b| b == 0)
618 619
}

620
fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
621
                            llmod_id: &str,
622
                            link_meta: &LinkMeta)
623
                            -> (ContextRef, ModuleRef, EncodedMetadata) {
A
Alex Crichton 已提交
624 625
    use std::io::Write;
    use flate2::Compression;
626
    use flate2::write::DeflateEncoder;
627

628
    let (metadata_llcx, metadata_llmod) = unsafe {
629
        context::create_context_and_module(tcx.sess, llmod_id)
630 631
    };

N
Nicholas Nethercote 已提交
632 633 634 635 636 637 638
    #[derive(PartialEq, Eq, PartialOrd, Ord)]
    enum MetadataKind {
        None,
        Uncompressed,
        Compressed
    }

639
    let kind = tcx.sess.crate_types.borrow().iter().map(|ty| {
N
Nicholas Nethercote 已提交
640 641 642 643 644
        match *ty {
            config::CrateTypeExecutable |
            config::CrateTypeStaticlib |
            config::CrateTypeCdylib => MetadataKind::None,

645
            config::CrateTypeRlib => MetadataKind::Uncompressed,
N
Nicholas Nethercote 已提交
646 647 648 649

            config::CrateTypeDylib |
            config::CrateTypeProcMacro => MetadataKind::Compressed,
        }
650
    }).max().unwrap_or(MetadataKind::None);
N
Nicholas Nethercote 已提交
651 652

    if kind == MetadataKind::None {
653 654
        return (metadata_llcx,
                metadata_llmod,
655
                EncodedMetadata::new());
656
    }
J
James Miller 已提交
657

658
    let metadata = tcx.encode_metadata(link_meta);
N
Nicholas Nethercote 已提交
659
    if kind == MetadataKind::Uncompressed {
660
        return (metadata_llcx, metadata_llmod, metadata);
N
Nicholas Nethercote 已提交
661 662 663
    }

    assert!(kind == MetadataKind::Compressed);
664
    let mut compressed = tcx.metadata_encoding_version();
665
    DeflateEncoder::new(&mut compressed, Compression::fast())
A
Alex Crichton 已提交
666
        .write_all(&metadata.raw_data).unwrap();
667

668 669
    let llmeta = C_bytes_in_context(metadata_llcx, &compressed);
    let llconst = C_struct_in_context(metadata_llcx, &[llmeta], false);
670
    let name = exported_symbols::metadata_symbol_name(tcx);
671
    let buf = CString::new(name).unwrap();
A
Alex Crichton 已提交
672
    let llglobal = unsafe {
673
        llvm::LLVMAddGlobal(metadata_llmod, val_ty(llconst).to_ref(), buf.as_ptr())
A
Alex Crichton 已提交
674
    };
675 676
    unsafe {
        llvm::LLVMSetInitializer(llglobal, llconst);
677
        let section_name = metadata::metadata_section_name(&tcx.sess.target.target);
678 679 680 681 682 683 684 685
        let name = CString::new(section_name).unwrap();
        llvm::LLVMSetSection(llglobal, name.as_ptr());

        // Also generate a .section directive to force no
        // flags, at least for ELF outputs, so that the
        // metadata doesn't get loaded into memory.
        let directive = format!(".section {}", section_name);
        let directive = CString::new(directive).unwrap();
686
        llvm::LLVMSetModuleInlineAsm(metadata_llmod, directive.as_ptr())
687
    }
688
    return (metadata_llcx, metadata_llmod, metadata);
689 690
}

691
pub struct ValueIter {
692 693 694
    cur: ValueRef,
    step: unsafe extern "C" fn(ValueRef) -> ValueRef,
}
695

696 697
impl Iterator for ValueIter {
    type Item = ValueRef;
698

699 700 701
    fn next(&mut self) -> Option<ValueRef> {
        let old = self.cur;
        if !old.is_null() {
702
            self.cur = unsafe { (self.step)(old) };
703 704 705 706
            Some(old)
        } else {
            None
        }
707
    }
708
}
709

710
pub fn iter_globals(llmod: llvm::ModuleRef) -> ValueIter {
711 712 713 714 715 716 717
    unsafe {
        ValueIter {
            cur: llvm::LLVMGetFirstGlobal(llmod),
            step: llvm::LLVMGetNextGlobal,
        }
    }
}
J
Jorge Aparicio 已提交
718

I
Irina Popa 已提交
719
pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
720
                             rx: mpsc::Receiver<Box<dyn Any + Send>>)
I
Irina Popa 已提交
721
                             -> OngoingCodegen {
B
bjorn3 已提交
722

B
bjorn3 已提交
723
    check_for_rustc_errors_attr(tcx);
724

725
    if let Some(true) = tcx.sess.opts.debugging_opts.thinlto {
A
Alex Crichton 已提交
726 727 728 729
        if unsafe { !llvm::LLVMRustThinLTOAvailable() } {
            tcx.sess.fatal("this compiler's LLVM does not support ThinLTO");
        }
    }
730

731 732 733 734 735 736 737
    if (tcx.sess.opts.debugging_opts.pgo_gen.is_some() ||
        !tcx.sess.opts.debugging_opts.pgo_use.is_empty()) &&
        unsafe { !llvm::LLVMRustPGOAvailable() }
    {
        tcx.sess.fatal("this compiler's LLVM does not support PGO");
    }

738
    let crate_hash = tcx.crate_hash(LOCAL_CRATE);
739
    let link_meta = link::build_link_meta(crate_hash);
740

I
Irina Popa 已提交
741
    // Codegen the metadata.
742 743 744 745 746
    let metadata_cgu_name = CodegenUnit::build_cgu_name(tcx,
                                                        LOCAL_CRATE,
                                                        &["crate"],
                                                        Some("metadata")).as_str()
                                                                         .to_string();
747
    let (metadata_llcx, metadata_llmod, metadata) =
748
        time(tcx.sess, "write metadata", || {
749
            write_metadata(tcx, &metadata_cgu_name, &link_meta)
750
        });
751

I
Irina Popa 已提交
752
    let metadata_module = ModuleCodegen {
753
        name: metadata_cgu_name,
I
Irina Popa 已提交
754
        source: ModuleSource::Codegened(ModuleLlvm {
755 756
            llcx: metadata_llcx,
            llmod: metadata_llmod,
757
            tm: create_target_machine(tcx.sess, false),
758
        }),
759
        kind: ModuleKind::Metadata,
760
    };
761

I
Irina Popa 已提交
762
    let time_graph = if tcx.sess.opts.debugging_opts.codegen_time_graph {
763 764 765 766
        Some(time_graph::TimeGraph::new())
    } else {
        None
    };
767

I
Irina Popa 已提交
768 769 770 771
    // Skip crate items and just output metadata in -Z no-codegen mode.
    if tcx.sess.opts.debugging_opts.no_codegen ||
       !tcx.sess.opts.output_types.should_codegen() {
        let ongoing_codegen = write::start_async_codegen(
772
            tcx,
773
            time_graph.clone(),
774
            link_meta,
775
            metadata,
A
Alex Crichton 已提交
776 777
            rx,
            1);
778

I
Irina Popa 已提交
779 780
        ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
        ongoing_codegen.codegen_finished(tcx);
781

782
        assert_and_save_dep_graph(tcx);
783

I
Irina Popa 已提交
784
        ongoing_codegen.check_for_errors(tcx.sess);
785

I
Irina Popa 已提交
786
        return ongoing_codegen;
787 788
    }

I
Irina Popa 已提交
789
    // Run the monomorphization collector and partition the collected items into
790
    // codegen units.
791
    let codegen_units =
I
Irina Popa 已提交
792
        tcx.collect_and_partition_mono_items(LOCAL_CRATE).1;
793
    let codegen_units = (*codegen_units).clone();
794

795 796 797 798 799 800 801 802 803 804 805
    // Force all codegen_unit queries so they are already either red or green
    // when compile_codegen_unit accesses them. We are not able to re-execute
    // the codegen_unit query from just the DepNode, so an unknown color would
    // lead to having to re-execute compile_codegen_unit, possibly
    // unnecessarily.
    if tcx.dep_graph.is_fully_enabled() {
        for cgu in &codegen_units {
            tcx.codegen_unit(cgu.name().clone());
        }
    }

I
Irina Popa 已提交
806
    let ongoing_codegen = write::start_async_codegen(
807
        tcx,
808
        time_graph.clone(),
809
        link_meta,
810
        metadata,
A
Alex Crichton 已提交
811 812
        rx,
        codegen_units.len());
813

I
Irina Popa 已提交
814
    // Codegen an allocator shim, if any
815
    let allocator_module = if let Some(kind) = *tcx.sess.allocator_kind.get() {
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
        let llmod_id = CodegenUnit::build_cgu_name(tcx,
                                                   LOCAL_CRATE,
                                                   &["crate"],
                                                   Some("allocator")).as_str()
                                                                     .to_string();
        let (llcx, llmod) = unsafe {
            context::create_context_and_module(tcx.sess, &llmod_id)
        };
        let modules = ModuleLlvm {
            llmod,
            llcx,
            tm: create_target_machine(tcx.sess, false),
        };
        time(tcx.sess, "write allocator module", || {
            unsafe {
I
Irina Popa 已提交
831
                allocator::codegen(tcx, &modules, kind)
832 833
            }
        });
834

835 836 837 838 839
        Some(ModuleCodegen {
            name: llmod_id,
            source: ModuleSource::Codegened(modules),
            kind: ModuleKind::Allocator,
        })
840 841 842 843 844
    } else {
        None
    };

    if let Some(allocator_module) = allocator_module {
I
Irina Popa 已提交
845
        ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, allocator_module);
846 847
    }

I
Irina Popa 已提交
848
    ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
849

850
    // We sort the codegen units by size. This way we can schedule work for LLVM
851
    // a bit more efficiently.
852 853
    let codegen_units = {
        let mut codegen_units = codegen_units;
V
varkor 已提交
854
        codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
855 856 857
        codegen_units
    };

I
Irina Popa 已提交
858
    let mut total_codegen_time = Duration::new(0, 0);
A
Alex Crichton 已提交
859
    let mut all_stats = Stats::default();
860

A
Alex Crichton 已提交
861
    for cgu in codegen_units.into_iter() {
I
Irina Popa 已提交
862 863
        ongoing_codegen.wait_for_signal_to_codegen_item();
        ongoing_codegen.check_for_errors(tcx.sess);
864

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
        // First, if incremental compilation is enabled, we try to re-use the
        // codegen unit from the cache.
        if tcx.dep_graph.is_fully_enabled() {
            let cgu_id = cgu.work_product_id();

            // Check whether there is a previous work-product we can
            // re-use.  Not only must the file exist, and the inputs not
            // be dirty, but the hash of the symbols we will generate must
            // be the same.
            if let Some(buf) = tcx.dep_graph.previous_work_product(&cgu_id) {
                let dep_node = &DepNode::new(tcx,
                    DepConstructor::CompileCodegenUnit(cgu.name().clone()));

                // We try to mark the DepNode::CompileCodegenUnit green. If we
                // succeed it means that none of the dependencies has changed
                // and we can safely re-use.
                if let Some(dep_node_index) = tcx.dep_graph.try_mark_green(tcx, dep_node) {
I
Irina Popa 已提交
882
                    let module = ModuleCodegen {
883 884 885 886 887
                        name: cgu.name().to_string(),
                        source: ModuleSource::Preexisting(buf),
                        kind: ModuleKind::Regular,
                    };
                    tcx.dep_graph.mark_loaded_from_cache(dep_node_index, true);
I
Irina Popa 已提交
888
                    write::submit_codegened_module_to_llvm(tcx, module, 0);
889 890 891 892 893 894 895 896 897
                    // Continue to next cgu, this one is done.
                    continue
                }
            } else {
                // This can happen if files were  deleted from the cache
                // directory for some reason. We just re-compile then.
            }
        }

898
        let _timing_guard = time_graph.as_ref().map(|time_graph| {
I
Irina Popa 已提交
899 900
            time_graph.start(write::CODEGEN_WORKER_TIMELINE,
                             write::CODEGEN_WORK_PACKAGE_KIND,
901 902
                             &format!("codegen {}", cgu.name()))
        });
903
        let start_time = Instant::now();
A
Alex Crichton 已提交
904
        all_stats.extend(tcx.compile_codegen_unit(*cgu.name()));
I
Irina Popa 已提交
905 906
        total_codegen_time += start_time.elapsed();
        ongoing_codegen.check_for_errors(tcx.sess);
907 908
    }

I
Irina Popa 已提交
909
    ongoing_codegen.codegen_finished(tcx);
A
Alex Crichton 已提交
910

I
Irina Popa 已提交
911
    // Since the main thread is sometimes blocked during codegen, we keep track
912 913
    // -Ztime-passes output manually.
    print_time_passes_entry(tcx.sess.time_passes(),
I
Irina Popa 已提交
914 915
                            "codegen to LLVM IR",
                            total_codegen_time);
916

A
Alex Crichton 已提交
917
    if tcx.sess.opts.incremental.is_some() {
918
        ::rustc_incremental::assert_module_sources::assert_module_sources(tcx);
919 920
    }

N
Niko Matsakis 已提交
921
    symbol_names_test::report_symbol_names(tcx);
922

I
Irina Popa 已提交
923 924
    if tcx.sess.codegen_stats() {
        println!("--- codegen stats ---");
A
Alex Crichton 已提交
925 926 927
        println!("n_glues_created: {}", all_stats.n_glues_created);
        println!("n_null_glues: {}", all_stats.n_null_glues);
        println!("n_real_glues: {}", all_stats.n_real_glues);
928

A
Alex Crichton 已提交
929 930 931
        println!("n_fns: {}", all_stats.n_fns);
        println!("n_inlines: {}", all_stats.n_inlines);
        println!("n_closures: {}", all_stats.n_closures);
932
        println!("fn stats:");
A
Alex Crichton 已提交
933 934 935
        all_stats.fn_stats.sort_by_key(|&(_, insns)| insns);
        for &(ref name, insns) in all_stats.fn_stats.iter() {
            println!("{} insns, {}", insns, *name);
936
        }
J
James Miller 已提交
937
    }
938

939
    if tcx.sess.count_llvm_insns() {
A
Alex Crichton 已提交
940
        for (k, v) in all_stats.llvm_insns.iter() {
A
Alex Crichton 已提交
941
            println!("{:7} {}", *v, *k);
942 943 944
        }
    }

I
Irina Popa 已提交
945
    ongoing_codegen.check_for_errors(tcx.sess);
946

947
    assert_and_save_dep_graph(tcx);
I
Irina Popa 已提交
948
    ongoing_codegen
949
}
950

951
fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
952
    time(tcx.sess,
953 954 955
         "assert dep graph",
         || rustc_incremental::assert_dep_graph(tcx));

956
    time(tcx.sess,
957
         "serialize dep graph",
958
         || rustc_incremental::save_dep_graph(tcx));
959 960
}

I
Irina Popa 已提交
961
fn collect_and_partition_mono_items<'a, 'tcx>(
962 963
    tcx: TyCtxt<'a, 'tcx, 'tcx>,
    cnum: CrateNum,
964
) -> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>)
965 966
{
    assert_eq!(cnum, LOCAL_CRATE);
967

I
Irina Popa 已提交
968
    let collection_mode = match tcx.sess.opts.debugging_opts.print_mono_items {
969 970 971 972
        Some(ref s) => {
            let mode_string = s.to_lowercase();
            let mode_string = mode_string.trim();
            if mode_string == "eager" {
973
                MonoItemCollectionMode::Eager
974 975 976 977 978
            } else {
                if mode_string != "lazy" {
                    let message = format!("Unknown codegen-item collection mode '{}'. \
                                           Falling back to 'lazy' mode.",
                                           mode_string);
979
                    tcx.sess.warn(&message);
980 981
                }

982
                MonoItemCollectionMode::Lazy
983 984
            }
        }
985 986 987 988 989 990 991
        None => {
            if tcx.sess.opts.cg.link_dead_code {
                MonoItemCollectionMode::Eager
            } else {
                MonoItemCollectionMode::Lazy
            }
        }
992 993
    };

994
    let (items, inlining_map) =
I
Irina Popa 已提交
995
        time(tcx.sess, "monomorphization collection", || {
996
            collector::collect_crate_mono_items(tcx, collection_mode)
997 998
    });

999 1000
    tcx.sess.abort_if_errors();

1001
    ::rustc_mir::monomorphize::assert_symbols_are_distinct(tcx, items.iter());
1002

1003
    let strategy = if tcx.sess.opts.incremental.is_some() {
1004 1005
        PartitioningStrategy::PerModule
    } else {
1006
        PartitioningStrategy::FixedUnitCount(tcx.sess.codegen_units())
1007 1008
    };

1009
    let codegen_units = time(tcx.sess, "codegen unit partitioning", || {
1010
        partitioning::partition(tcx,
1011 1012
                                items.iter().cloned(),
                                strategy,
1013
                                &inlining_map)
1014 1015 1016
            .into_iter()
            .map(Arc::new)
            .collect::<Vec<_>>()
1017 1018
    });

I
Irina Popa 已提交
1019 1020
    let mono_items: DefIdSet = items.iter().filter_map(|mono_item| {
        match *mono_item {
M
Maik Klein 已提交
1021
            MonoItem::Fn(ref instance) => Some(instance.def_id()),
1022
            MonoItem::Static(def_id) => Some(def_id),
1023 1024 1025
            _ => None,
        }
    }).collect();
1026

I
Irina Popa 已提交
1027
    if tcx.sess.opts.debugging_opts.print_mono_items.is_some() {
1028
        let mut item_to_cgus = FxHashMap();
1029

1030
        for cgu in &codegen_units {
I
Irina Popa 已提交
1031 1032
            for (&mono_item, &linkage) in cgu.items() {
                item_to_cgus.entry(mono_item)
1033
                            .or_insert(Vec::new())
1034
                            .push((cgu.name().clone(), linkage));
1035 1036 1037 1038 1039 1040
            }
        }

        let mut item_keys: Vec<_> = items
            .iter()
            .map(|i| {
1041
                let mut output = i.to_string(tcx);
1042 1043
                output.push_str(" @@");
                let mut empty = Vec::new();
1044
                let cgus = item_to_cgus.get_mut(i).unwrap_or(&mut empty);
1045 1046
                cgus.as_mut_slice().sort_by_key(|&(ref name, _)| name.clone());
                cgus.dedup();
1047
                for &(ref cgu_name, (linkage, _)) in cgus.iter() {
1048
                    output.push_str(" ");
1049
                    output.push_str(&cgu_name.as_str());
1050 1051

                    let linkage_abbrev = match linkage {
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
                        Linkage::External => "External",
                        Linkage::AvailableExternally => "Available",
                        Linkage::LinkOnceAny => "OnceAny",
                        Linkage::LinkOnceODR => "OnceODR",
                        Linkage::WeakAny => "WeakAny",
                        Linkage::WeakODR => "WeakODR",
                        Linkage::Appending => "Appending",
                        Linkage::Internal => "Internal",
                        Linkage::Private => "Private",
                        Linkage::ExternalWeak => "ExternalWeak",
                        Linkage::Common => "Common",
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
                    };

                    output.push_str("[");
                    output.push_str(linkage_abbrev);
                    output.push_str("]");
                }
                output
            })
            .collect();

1073 1074 1075
        item_keys.sort();

        for item in item_keys {
I
Irina Popa 已提交
1076
            println!("MONO_ITEM {}", item);
1077 1078
        }
    }
1079

I
Irina Popa 已提交
1080
    (Arc::new(mono_items), Arc::new(codegen_units))
1081
}
1082 1083

impl CrateInfo {
1084
    pub fn new(tcx: TyCtxt) -> CrateInfo {
1085 1086 1087 1088 1089 1090
        let mut info = CrateInfo {
            panic_runtime: None,
            compiler_builtins: None,
            profiler_runtime: None,
            sanitizer_runtime: None,
            is_no_builtins: FxHashSet(),
1091
            native_libraries: FxHashMap(),
1092 1093
            used_libraries: tcx.native_libraries(LOCAL_CRATE),
            link_args: tcx.link_args(LOCAL_CRATE),
1094
            crate_name: FxHashMap(),
1095 1096 1097
            used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
            used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
            used_crate_source: FxHashMap(),
1098
            wasm_imports: FxHashMap(),
1099 1100
            lang_item_to_crate: FxHashMap(),
            missing_lang_items: FxHashMap(),
1101
        };
1102
        let lang_items = tcx.lang_items();
1103

1104
        let load_wasm_items = tcx.sess.crate_types.borrow()
1105 1106
            .iter()
            .any(|c| *c != config::CrateTypeRlib) &&
1107
            tcx.sess.opts.target_triple.triple() == "wasm32-unknown-unknown";
1108

1109 1110
        if load_wasm_items {
            info.load_wasm_imports(tcx, LOCAL_CRATE);
1111 1112
        }

1113
        for &cnum in tcx.crates().iter() {
1114
            info.native_libraries.insert(cnum, tcx.native_libraries(cnum));
1115
            info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
1116
            info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum));
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
            if tcx.is_panic_runtime(cnum) {
                info.panic_runtime = Some(cnum);
            }
            if tcx.is_compiler_builtins(cnum) {
                info.compiler_builtins = Some(cnum);
            }
            if tcx.is_profiler_runtime(cnum) {
                info.profiler_runtime = Some(cnum);
            }
            if tcx.is_sanitizer_runtime(cnum) {
                info.sanitizer_runtime = Some(cnum);
            }
            if tcx.is_no_builtins(cnum) {
                info.is_no_builtins.insert(cnum);
            }
1132 1133
            if load_wasm_items {
                info.load_wasm_imports(tcx, cnum);
1134
            }
1135 1136 1137 1138 1139 1140
            let missing = tcx.missing_lang_items(cnum);
            for &item in missing.iter() {
                if let Ok(id) = lang_items.require(item) {
                    info.lang_item_to_crate.insert(item, id.krate);
                }
            }
1141 1142 1143 1144 1145 1146 1147

            // No need to look for lang items that are whitelisted and don't
            // actually need to exist.
            let missing = missing.iter()
                .cloned()
                .filter(|&l| !weak_lang_items::whitelisted(tcx, l))
                .collect();
1148
            info.missing_lang_items.insert(cnum, missing);
1149 1150 1151 1152
        }

        return info
    }
1153 1154 1155 1156 1157 1158 1159 1160

    fn load_wasm_imports(&mut self, tcx: TyCtxt, cnum: CrateNum) {
        for (&id, module) in tcx.wasm_import_module_map(cnum).iter() {
            let instance = Instance::mono(tcx, id);
            let import_name = tcx.symbol_name(instance);
            self.wasm_imports.insert(import_name.to_string(), module.clone());
        }
    }
1161
}
1162

I
Irina Popa 已提交
1163 1164 1165 1166
fn is_codegened_item(tcx: TyCtxt, id: DefId) -> bool {
    let (all_mono_items, _) =
        tcx.collect_and_partition_mono_items(LOCAL_CRATE);
    all_mono_items.contains(&id)
1167 1168
}

A
Alex Crichton 已提交
1169 1170
fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                                  cgu: InternedString) -> Stats {
1171
    let cgu = tcx.codegen_unit(cgu);
A
Alex Crichton 已提交
1172 1173

    let start_time = Instant::now();
I
Irina Popa 已提交
1174 1175
    let (stats, module) = module_codegen(tcx, cgu);
    let time_to_codegen = start_time.elapsed();
A
Alex Crichton 已提交
1176 1177

    // We assume that the cost to run LLVM on a CGU is proportional to
I
Irina Popa 已提交
1178 1179 1180
    // the time we needed for codegenning it.
    let cost = time_to_codegen.as_secs() * 1_000_000_000 +
               time_to_codegen.subsec_nanos() as u64;
A
Alex Crichton 已提交
1181

I
Irina Popa 已提交
1182
    write::submit_codegened_module_to_llvm(tcx,
A
Alex Crichton 已提交
1183 1184 1185 1186
                                            module,
                                            cost);
    return stats;

I
Irina Popa 已提交
1187
    fn module_codegen<'a, 'tcx>(
1188 1189
        tcx: TyCtxt<'a, 'tcx, 'tcx>,
        cgu: Arc<CodegenUnit<'tcx>>)
I
Irina Popa 已提交
1190
        -> (Stats, ModuleCodegen)
A
Alex Crichton 已提交
1191 1192 1193
    {
        let cgu_name = cgu.name().to_string();

I
Irina Popa 已提交
1194
        // Instantiate monomorphizations without filling out definitions yet...
1195
        let cx = CodegenCx::new(tcx, cgu);
A
Alex Crichton 已提交
1196
        let module = {
I
Irina Popa 已提交
1197
            let mono_items = cx.codegen_unit
1198
                                 .items_in_deterministic_order(cx.tcx);
I
Irina Popa 已提交
1199 1200
            for &(mono_item, (linkage, visibility)) in &mono_items {
                mono_item.predefine(&cx, linkage, visibility);
A
Alex Crichton 已提交
1201 1202 1203
            }

            // ... and now that we have everything pre-defined, fill out those definitions.
I
Irina Popa 已提交
1204 1205
            for &(mono_item, _) in &mono_items {
                mono_item.define(&cx);
A
Alex Crichton 已提交
1206 1207 1208 1209
            }

            // If this codegen unit contains the main function, also create the
            // wrapper here
1210
            maybe_create_entry_wrapper(&cx);
A
Alex Crichton 已提交
1211 1212

            // Run replace-all-uses-with for statics that need it
1213
            for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
A
Alex Crichton 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222
                unsafe {
                    let bitcast = llvm::LLVMConstPointerCast(new_g, llvm::LLVMTypeOf(old_g));
                    llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
                    llvm::LLVMDeleteGlobal(old_g);
                }
            }

            // Create the llvm.used variable
            // This variable has type [N x i8*] and is stored in the llvm.metadata section
1223
            if !cx.used_statics.borrow().is_empty() {
A
Alex Crichton 已提交
1224 1225
                let name = CString::new("llvm.used").unwrap();
                let section = CString::new("llvm.metadata").unwrap();
1226
                let array = C_array(Type::i8(&cx).ptr_to(), &*cx.used_statics.borrow());
A
Alex Crichton 已提交
1227 1228

                unsafe {
1229
                    let g = llvm::LLVMAddGlobal(cx.llmod,
A
Alex Crichton 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238
                                                val_ty(array).to_ref(),
                                                name.as_ptr());
                    llvm::LLVMSetInitializer(g, array);
                    llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
                    llvm::LLVMSetSection(g, section.as_ptr());
                }
            }

            // Finalize debuginfo
1239 1240
            if cx.sess().opts.debuginfo != NoDebugInfo {
                debuginfo::finalize(&cx);
A
Alex Crichton 已提交
1241 1242 1243
            }

            let llvm_module = ModuleLlvm {
1244 1245
                llcx: cx.llcx,
                llmod: cx.llmod,
1246
                tm: create_target_machine(cx.sess(), false),
A
Alex Crichton 已提交
1247 1248
            };

I
Irina Popa 已提交
1249
            ModuleCodegen {
A
Alex Crichton 已提交
1250
                name: cgu_name,
I
Irina Popa 已提交
1251
                source: ModuleSource::Codegened(llvm_module),
A
Alex Crichton 已提交
1252 1253 1254 1255
                kind: ModuleKind::Regular,
            }
        };

1256
        (cx.into_stats(), module)
A
Alex Crichton 已提交
1257 1258 1259
    }
}

1260
pub fn provide(providers: &mut Providers) {
I
Irina Popa 已提交
1261 1262
    providers.collect_and_partition_mono_items =
        collect_and_partition_mono_items;
1263

I
Irina Popa 已提交
1264
    providers.is_codegened_item = is_codegened_item;
A
Alex Crichton 已提交
1265 1266

    providers.codegen_unit = |tcx, name| {
I
Irina Popa 已提交
1267
        let (_, all) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
A
Alex Crichton 已提交
1268 1269 1270 1271 1272 1273
        all.iter()
            .find(|cgu| *cgu.name() == name)
            .cloned()
            .expect(&format!("failed to find cgu with name {:?}", name))
    };
    providers.compile_codegen_unit = compile_codegen_unit;
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

    provide_extern(providers);
}

pub fn provide_extern(providers: &mut Providers) {
    providers.dllimport_foreign_items = |tcx, krate| {
        let module_map = tcx.foreign_modules(krate);
        let module_map = module_map.iter()
            .map(|lib| (lib.def_id, lib))
            .collect::<FxHashMap<_, _>>();

        let dllimports = tcx.native_libraries(krate)
            .iter()
            .filter(|lib| {
                if lib.kind != cstore::NativeLibraryKind::NativeUnknown {
                    return false
                }
                let cfg = match lib.cfg {
                    Some(ref cfg) => cfg,
                    None => return true,
                };
                attr::cfg_matches(cfg, &tcx.sess.parse_sess, None)
            })
            .filter_map(|lib| lib.foreign_module)
            .map(|id| &module_map[&id])
            .flat_map(|module| module.foreign_items.iter().cloned())
            .collect();
        Lrc::new(dllimports)
    };

    providers.is_dllimport_foreign_item = |tcx, def_id| {
        tcx.dllimport_foreign_items(def_id.krate).contains(&def_id)
    };
1307 1308
}

1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
    match linkage {
        Linkage::External => llvm::Linkage::ExternalLinkage,
        Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
        Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
        Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
        Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
        Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
        Linkage::Appending => llvm::Linkage::AppendingLinkage,
        Linkage::Internal => llvm::Linkage::InternalLinkage,
        Linkage::Private => llvm::Linkage::PrivateLinkage,
        Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
        Linkage::Common => llvm::Linkage::CommonLinkage,
    }
}

pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
    match linkage {
        Visibility::Default => llvm::Visibility::Default,
        Visibility::Hidden => llvm::Visibility::Hidden,
        Visibility::Protected => llvm::Visibility::Protected,
1330
    }
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
}

// FIXME(mw): Anything that is produced via DepGraph::with_task() must implement
//            the HashStable trait. Normally DepGraph::with_task() calls are
//            hidden behind queries, but CGU creation is a special case in two
//            ways: (1) it's not a query and (2) CGU are output nodes, so their
//            Fingerprints are not actually needed. It remains to be clarified
//            how exactly this case will be handled in the red/green system but
//            for now we content ourselves with providing a no-op HashStable
//            implementation for CGUs.
mod temp_stable_hash_impls {
    use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher,
                                               HashStable};
I
Irina Popa 已提交
1344
    use ModuleCodegen;
1345

I
Irina Popa 已提交
1346
    impl<HCX> HashStable<HCX> for ModuleCodegen {
1347 1348 1349 1350 1351
        fn hash_stable<W: StableHasherResult>(&self,
                                              _: &mut HCX,
                                              _: &mut StableHasher<W>) {
            // do nothing
        }
1352 1353
    }
}
1354

1355 1356 1357 1358 1359 1360 1361 1362
#[allow(unused)]
fn load_thin_lto_imports(sess: &Session) -> lto::ThinLTOImports {
    let path = rustc_incremental::in_incr_comp_dir_sess(
        sess,
        lto::THIN_LTO_IMPORTS_INCR_COMP_FILE_NAME
    );

    if !path.exists() {
1363
        return lto::ThinLTOImports::new_empty();
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
    }

    match lto::ThinLTOImports::load_from_file(&path) {
        Ok(imports) => imports,
        Err(e) => {
            let msg = format!("Error while trying to load ThinLTO import data \
                               for incremental compilation: {}", e);
            sess.fatal(&msg)
        }
    }
}

1376
pub fn define_custom_section(cx: &CodegenCx, def_id: DefId) {
1377
    use rustc::mir::interpret::GlobalId;
1378

1379 1380 1381
    assert!(cx.tcx.sess.opts.target_triple.triple().starts_with("wasm32"));

    info!("loading wasm section {:?}", def_id);
1382

1383
    let section = cx.tcx.codegen_fn_attrs(def_id).wasm_custom_section.unwrap();
1384

1385
    let instance = ty::Instance::mono(cx.tcx, def_id);
1386 1387 1388 1389 1390
    let cid = GlobalId {
        instance,
        promoted: None
    };
    let param_env = ty::ParamEnv::reveal_all();
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
    let val = cx.tcx.const_eval(param_env.and(cid)).unwrap();
    let alloc = cx.tcx.const_value_to_allocation(val);

    unsafe {
        let section = llvm::LLVMMDStringInContext(
            cx.llcx,
            section.as_str().as_ptr() as *const _,
            section.as_str().len() as c_uint,
        );
        let alloc = llvm::LLVMMDStringInContext(
            cx.llcx,
            alloc.bytes.as_ptr() as *const _,
            alloc.bytes.len() as c_uint,
        );
        let data = [section, alloc];
        let meta = llvm::LLVMMDNodeInContext(cx.llcx, data.as_ptr(), 2);
        llvm::LLVMAddNamedMetadataOperand(
            cx.llmod,
            "wasm.custom_sections\0".as_ptr() as *const _,
            meta,
        );
    }
1413
}
1414