]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/base.rs
5fd413b4c7babf1d33bb5ec4fa3376c696e97393
[rust.git] / src / librustc_codegen_llvm / base.rs
1 // Copyright 2012-2015 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 //! Codegen the completed AST to the LLVM IR.
12 //!
13 //! Some functions here, such as codegen_block and codegen_expr, return a value --
14 //! the result of the codegen to LLVM -- while others, such as codegen_fn
15 //! and mono_item, are called only for the side effect of adding a
16 //! particular definition to the LLVM IR output we're producing.
17 //!
18 //! Hopefully useful general knowledge about codegen:
19 //!
20 //!   * There's no way to find out the Ty type of a Value.  Doing so
21 //!     would be "trying to get the eggs out of an omelette" (credit:
22 //!     pcwalton).  You can, instead, find out its llvm::Type by calling val_ty,
23 //!     but one llvm::Type corresponds to many `Ty`s; for instance, tup(int, int,
24 //!     int) and rec(x=int, y=int, z=int) will have the same llvm::Type.
25
26 use super::ModuleLlvm;
27 use super::ModuleCodegen;
28 use super::ModuleKind;
29 use super::CachedModuleCodegen;
30
31 use abi;
32 use back::write;
33 use llvm;
34 use metadata;
35 use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
36 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
37 use rustc::middle::lang_items::StartFnLangItem;
38 use rustc::middle::weak_lang_items;
39 use rustc::mir::mono::{Linkage, Visibility, Stats, CodegenUnitNameBuilder};
40 use rustc::middle::cstore::{EncodedMetadata};
41 use rustc::ty::{self, Ty, TyCtxt};
42 use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
43 use rustc::ty::query::Providers;
44 use rustc::middle::cstore::{self, LinkagePreference};
45 use rustc::middle::exported_symbols;
46 use rustc::util::common::{time, print_time_passes_entry};
47 use rustc::util::profiling::ProfileCategory;
48 use rustc::session::config::{self, DebugInfo, EntryFnType, Lto};
49 use rustc::session::Session;
50 use rustc_incremental;
51 use mir::place::PlaceRef;
52 use builder::{Builder, MemFlags};
53 use callee;
54 use rustc_mir::monomorphize::item::DefPathBasedNames;
55 use common::{self, IntPredicate, RealPredicate, TypeKind};
56 use context::CodegenCx;
57 use debuginfo;
58 use meth;
59 use mir;
60 use monomorphize::Instance;
61 use monomorphize::partitioning::{CodegenUnit, CodegenUnitExt};
62 use rustc_codegen_utils::symbol_names_test;
63 use time_graph;
64 use mono_item::{MonoItem, MonoItemExt};
65
66 use rustc::util::nodemap::FxHashMap;
67 use CrateInfo;
68 use rustc_data_structures::small_c_str::SmallCStr;
69 use rustc_data_structures::sync::Lrc;
70 use rustc_data_structures::indexed_vec::Idx;
71
72 use interfaces::*;
73
74 use std::any::Any;
75 use std::cmp;
76 use std::ffi::CString;
77 use std::marker;
78 use std::ops::{Deref, DerefMut};
79 use std::sync::mpsc;
80 use std::time::{Instant, Duration};
81 use syntax_pos::Span;
82 use syntax_pos::symbol::InternedString;
83 use syntax::attr;
84 use rustc::hir::{self, CodegenFnAttrs};
85
86 use value::Value;
87
88 use mir::operand::OperandValue;
89
90 use rustc_codegen_utils::check_for_rustc_errors_attr;
91
92 pub struct StatRecorder<'a, 'tcx, Cx: 'a + CodegenMethods<'tcx>> {
93     cx: &'a Cx,
94     name: Option<String>,
95     istart: usize,
96     _marker: marker::PhantomData<&'tcx ()>,
97 }
98
99 impl<'a, 'tcx, Cx: CodegenMethods<'tcx>> StatRecorder<'a, 'tcx, Cx> {
100     pub fn new(cx: &'a Cx, name: String) -> Self {
101         let istart = cx.stats().borrow().n_llvm_insns;
102         StatRecorder {
103             cx,
104             name: Some(name),
105             istart,
106             _marker: marker::PhantomData,
107         }
108     }
109 }
110
111 impl<'a, 'tcx, Cx: CodegenMethods<'tcx>> Drop for StatRecorder<'a, 'tcx, Cx> {
112     fn drop(&mut self) {
113         if self.cx.sess().codegen_stats() {
114             let mut stats = self.cx.stats().borrow_mut();
115             let iend = stats.n_llvm_insns;
116             stats.fn_stats.push((self.name.take().unwrap(), iend - self.istart));
117             stats.n_fns += 1;
118             // Reset LLVM insn count to avoid compound costs.
119             stats.n_llvm_insns = self.istart;
120         }
121     }
122 }
123
124 pub fn bin_op_to_icmp_predicate(op: hir::BinOpKind,
125                                 signed: bool)
126                                 -> IntPredicate {
127     match op {
128         hir::BinOpKind::Eq => IntPredicate::IntEQ,
129         hir::BinOpKind::Ne => IntPredicate::IntNE,
130         hir::BinOpKind::Lt => if signed { IntPredicate::IntSLT } else { IntPredicate::IntULT },
131         hir::BinOpKind::Le => if signed { IntPredicate::IntSLE } else { IntPredicate::IntULE },
132         hir::BinOpKind::Gt => if signed { IntPredicate::IntSGT } else { IntPredicate::IntUGT },
133         hir::BinOpKind::Ge => if signed { IntPredicate::IntSGE } else { IntPredicate::IntUGE },
134         op => {
135             bug!("comparison_op_to_icmp_predicate: expected comparison operator, \
136                   found {:?}",
137                  op)
138         }
139     }
140 }
141
142 pub fn bin_op_to_fcmp_predicate(op: hir::BinOpKind) -> RealPredicate {
143     match op {
144         hir::BinOpKind::Eq => RealPredicate::RealOEQ,
145         hir::BinOpKind::Ne => RealPredicate::RealUNE,
146         hir::BinOpKind::Lt => RealPredicate::RealOLT,
147         hir::BinOpKind::Le => RealPredicate::RealOLE,
148         hir::BinOpKind::Gt => RealPredicate::RealOGT,
149         hir::BinOpKind::Ge => RealPredicate::RealOGE,
150         op => {
151             bug!("comparison_op_to_fcmp_predicate: expected comparison operator, \
152                   found {:?}",
153                  op);
154         }
155     }
156 }
157
158 pub fn compare_simd_types<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
159     bx: &Bx,
160     lhs: Bx::Value,
161     rhs: Bx::Value,
162     t: Ty<'tcx>,
163     ret_ty: Bx::Type,
164     op: hir::BinOpKind
165 ) -> Bx::Value {
166     let signed = match t.sty {
167         ty::Float(_) => {
168             let cmp = bin_op_to_fcmp_predicate(op);
169             return bx.sext(bx.fcmp(cmp, lhs, rhs), ret_ty);
170         },
171         ty::Uint(_) => false,
172         ty::Int(_) => true,
173         _ => bug!("compare_simd_types: invalid SIMD type"),
174     };
175
176     let cmp = bin_op_to_icmp_predicate(op, signed);
177     // LLVM outputs an `< size x i1 >`, so we need to perform a sign extension
178     // to get the correctly sized type. This will compile to a single instruction
179     // once the IR is converted to assembly if the SIMD instruction is supported
180     // by the target architecture.
181     bx.sext(bx.icmp(cmp, lhs, rhs), ret_ty)
182 }
183
184 /// Retrieve the information we are losing (making dynamic) in an unsizing
185 /// adjustment.
186 ///
187 /// The `old_info` argument is a bit funny. It is intended for use
188 /// in an upcast, where the new vtable for an object will be derived
189 /// from the old one.
190 pub fn unsized_info<'tcx, Cx: CodegenMethods<'tcx>>(
191     cx: &Cx,
192     source: Ty<'tcx>,
193     target: Ty<'tcx>,
194     old_info: Option<Cx::Value>,
195 ) -> Cx::Value {
196     let (source, target) = cx.tcx().struct_lockstep_tails(source, target);
197     match (&source.sty, &target.sty) {
198         (&ty::Array(_, len), &ty::Slice(_)) => {
199             cx.const_usize(len.unwrap_usize(cx.tcx()))
200         }
201         (&ty::Dynamic(..), &ty::Dynamic(..)) => {
202             // For now, upcasts are limited to changes in marker
203             // traits, and hence never actually require an actual
204             // change to the vtable.
205             old_info.expect("unsized_info: missing old info for trait upcast")
206         }
207         (_, &ty::Dynamic(ref data, ..)) => {
208             let vtable_ptr = cx.layout_of(cx.tcx().mk_mut_ptr(target))
209                 .field(cx, abi::FAT_PTR_EXTRA);
210             cx.static_ptrcast(meth::get_vtable(cx, source, data.principal()),
211                             cx.backend_type(vtable_ptr))
212         }
213         _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
214                   source,
215                   target),
216     }
217 }
218
219 /// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
220 pub fn unsize_thin_ptr<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
221     bx: &Bx,
222     src: Bx::Value,
223     src_ty: Ty<'tcx>,
224     dst_ty: Ty<'tcx>
225 ) -> (Bx::Value, Bx::Value) {
226     debug!("unsize_thin_ptr: {:?} => {:?}", src_ty, dst_ty);
227     match (&src_ty.sty, &dst_ty.sty) {
228         (&ty::Ref(_, a, _),
229          &ty::Ref(_, b, _)) |
230         (&ty::Ref(_, a, _),
231          &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) |
232         (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
233          &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
234             assert!(bx.cx().type_is_sized(a));
235             let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(bx.cx().layout_of(b)));
236             (bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
237         }
238         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
239             let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
240             assert!(bx.cx().type_is_sized(a));
241             let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(bx.cx().layout_of(b)));
242             (bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
243         }
244         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
245             assert_eq!(def_a, def_b);
246
247             let src_layout = bx.cx().layout_of(src_ty);
248             let dst_layout = bx.cx().layout_of(dst_ty);
249             let mut result = None;
250             for i in 0..src_layout.fields.count() {
251                 let src_f = src_layout.field(bx.cx(), i);
252                 assert_eq!(src_layout.fields.offset(i).bytes(), 0);
253                 assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
254                 if src_f.is_zst() {
255                     continue;
256                 }
257                 assert_eq!(src_layout.size, src_f.size);
258
259                 let dst_f = dst_layout.field(bx.cx(), i);
260                 assert_ne!(src_f.ty, dst_f.ty);
261                 assert_eq!(result, None);
262                 result = Some(unsize_thin_ptr(bx, src, src_f.ty, dst_f.ty));
263             }
264             let (lldata, llextra) = result.unwrap();
265             // HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
266             (bx.bitcast(lldata, bx.cx().scalar_pair_element_backend_type(dst_layout, 0, true)),
267              bx.bitcast(llextra, bx.cx().scalar_pair_element_backend_type(dst_layout, 1, true)))
268         }
269         _ => bug!("unsize_thin_ptr: called on bad types"),
270     }
271 }
272
273 /// Coerce `src`, which is a reference to a value of type `src_ty`,
274 /// to a value of type `dst_ty` and store the result in `dst`
275 pub fn coerce_unsized_into<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
276     bx: &Bx,
277     src: PlaceRef<'tcx, Bx::Value>,
278     dst: PlaceRef<'tcx, Bx::Value>
279 )  {
280     let src_ty = src.layout.ty;
281     let dst_ty = dst.layout.ty;
282     let coerce_ptr = || {
283         let (base, info) = match bx.load_operand(src).val {
284             OperandValue::Pair(base, info) => {
285                 // fat-ptr to fat-ptr unsize preserves the vtable
286                 // i.e. &'a fmt::Debug+Send => &'a fmt::Debug
287                 // So we need to pointercast the base to ensure
288                 // the types match up.
289                 let thin_ptr = dst.layout.field(bx.cx(), abi::FAT_PTR_ADDR);
290                 (bx.pointercast(base, bx.cx().backend_type(thin_ptr)), 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     };
299     match (&src_ty.sty, &dst_ty.sty) {
300         (&ty::Ref(..), &ty::Ref(..)) |
301         (&ty::Ref(..), &ty::RawPtr(..)) |
302         (&ty::RawPtr(..), &ty::RawPtr(..)) => {
303             coerce_ptr()
304         }
305         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
306             coerce_ptr()
307         }
308
309         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
310             assert_eq!(def_a, def_b);
311
312             for i in 0..def_a.variants[VariantIdx::new(0)].fields.len() {
313                 let src_f = src.project_field(bx, i);
314                 let dst_f = dst.project_field(bx, i);
315
316                 if dst_f.layout.is_zst() {
317                     continue;
318                 }
319
320                 if src_f.layout.ty == dst_f.layout.ty {
321                     memcpy_ty(bx, dst_f.llval, dst_f.align, src_f.llval, src_f.align,
322                               src_f.layout, MemFlags::empty());
323                 } else {
324                     coerce_unsized_into(bx, src_f, dst_f);
325                 }
326             }
327         }
328         _ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}",
329                   src_ty,
330                   dst_ty),
331     }
332 }
333
334 pub fn cast_shift_expr_rhs<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
335     bx: &Bx,
336     op: hir::BinOpKind,
337     lhs: Bx::Value,
338     rhs: Bx::Value
339 ) -> Bx::Value {
340     cast_shift_rhs(bx, op, lhs, rhs, |a, b| bx.trunc(a, b), |a, b| bx.zext(a, b))
341 }
342
343 fn cast_shift_rhs<'a, 'tcx: 'a, F, G, Bx: BuilderMethods<'a, 'tcx>>(
344     bx: &Bx,
345     op: hir::BinOpKind,
346     lhs: Bx::Value,
347     rhs: Bx::Value,
348     trunc: F,
349     zext: G
350 ) -> Bx::Value
351     where F: FnOnce(
352         Bx::Value,
353         Bx::Type
354     ) -> Bx::Value,
355     G: FnOnce(
356         Bx::Value,
357         Bx::Type
358     ) -> Bx::Value
359 {
360     // Shifts may have any size int on the rhs
361     if op.is_shift() {
362         let mut rhs_llty = bx.cx().val_ty(rhs);
363         let mut lhs_llty = bx.cx().val_ty(lhs);
364         if bx.cx().type_kind(rhs_llty) == TypeKind::Vector {
365             rhs_llty = bx.cx().element_type(rhs_llty)
366         }
367         if bx.cx().type_kind(lhs_llty) == TypeKind::Vector {
368             lhs_llty = bx.cx().element_type(lhs_llty)
369         }
370         let rhs_sz = bx.cx().int_width(rhs_llty);
371         let lhs_sz = bx.cx().int_width(lhs_llty);
372         if lhs_sz < rhs_sz {
373             trunc(rhs, lhs_llty)
374         } else if lhs_sz > rhs_sz {
375             // FIXME (#1877: If in the future shifting by negative
376             // values is no longer undefined then this is wrong.
377             zext(rhs, lhs_llty)
378         } else {
379             rhs
380         }
381     } else {
382         rhs
383     }
384 }
385
386 /// Returns whether this session's target will use SEH-based unwinding.
387 ///
388 /// This is only true for MSVC targets, and even then the 64-bit MSVC target
389 /// currently uses SEH-ish unwinding with DWARF info tables to the side (same as
390 /// 64-bit MinGW) instead of "full SEH".
391 pub fn wants_msvc_seh(sess: &Session) -> bool {
392     sess.target.target.options.is_like_msvc
393 }
394
395 pub fn call_assume<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
396     bx: &Bx,
397     val: Bx::Value
398 ) {
399     let assume_intrinsic = bx.cx().get_intrinsic("llvm.assume");
400     bx.call(assume_intrinsic, &[val], None);
401 }
402
403 pub fn from_immediate<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
404     bx: &Bx,
405     val: Bx::Value
406 ) -> Bx::Value {
407     if bx.cx().val_ty(val) == bx.cx().type_i1() {
408         bx.zext(val, bx.cx().type_i8())
409     } else {
410         val
411     }
412 }
413
414 pub fn to_immediate<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
415     bx: &Bx,
416     val: Bx::Value,
417     layout: layout::TyLayout,
418 ) -> Bx::Value {
419     if let layout::Abi::Scalar(ref scalar) = layout.abi {
420         return to_immediate_scalar(bx, val, scalar);
421     }
422     val
423 }
424
425 pub fn to_immediate_scalar<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
426     bx: &Bx,
427     val: Bx::Value,
428     scalar: &layout::Scalar,
429 ) -> Bx::Value {
430     if scalar.is_bool() {
431         return bx.trunc(val, bx.cx().type_i1());
432     }
433     val
434 }
435
436 pub fn memcpy_ty<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
437     bx: &Bx,
438     dst: Bx::Value,
439     dst_align: Align,
440     src: Bx::Value,
441     src_align: Align,
442     layout: TyLayout<'tcx>,
443     flags: MemFlags,
444 ) {
445     let size = layout.size.bytes();
446     if size == 0 {
447         return;
448     }
449
450     bx.memcpy(dst, dst_align, src, src_align, bx.cx().const_usize(size), flags);
451 }
452
453 pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
454     cx: &'a Bx::CodegenCx,
455     instance: Instance<'tcx>,
456 ) {
457     let _s = if cx.sess().codegen_stats() {
458         let mut instance_name = String::new();
459         DefPathBasedNames::new(cx.tcx(), true, true)
460             .push_def_path(instance.def_id(), &mut instance_name);
461         Some(StatRecorder::new(cx, instance_name))
462     } else {
463         None
464     };
465
466     // this is an info! to allow collecting monomorphization statistics
467     // and to allow finding the last function before LLVM aborts from
468     // release builds.
469     info!("codegen_instance({})", instance);
470
471     let sig = instance.fn_sig(cx.tcx());
472     let sig = cx.tcx().normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
473
474     let lldecl = cx.instances().borrow().get(&instance).cloned().unwrap_or_else(||
475         bug!("Instance `{:?}` not already declared", instance));
476
477     cx.stats().borrow_mut().n_closures += 1;
478
479     let mir = cx.tcx().instance_mir(instance.def);
480     mir::codegen_mir::<Bx>(cx, lldecl, &mir, instance, sig);
481 }
482
483 pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
484     let sect = match attrs.link_section {
485         Some(name) => name,
486         None => return,
487     };
488     unsafe {
489         let buf = SmallCStr::new(&sect.as_str());
490         llvm::LLVMSetSection(llval, buf.as_ptr());
491     }
492 }
493
494 /// Create the `main` function which will initialize the rust runtime and call
495 /// users main function.
496 fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
497     cx: &'a Bx::CodegenCx
498 ) {
499     let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
500         Some((id, span, _)) => {
501             (cx.tcx().hir.local_def_id(id), span)
502         }
503         None => return,
504     };
505
506     let instance = Instance::mono(cx.tcx(), main_def_id);
507
508     if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
509         // We want to create the wrapper in the same codegen unit as Rust's main
510         // function.
511         return;
512     }
513
514     let main_llfn = cx.get_fn(instance);
515
516     let et = cx.sess().entry_fn.get().map(|e| e.2);
517     match et {
518         Some(EntryFnType::Main) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, true),
519         Some(EntryFnType::Start) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, false),
520         None => {}    // Do nothing.
521     }
522
523     fn create_entry_fn<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
524         cx: &'a Bx::CodegenCx,
525         sp: Span,
526         rust_main: Bx::Value,
527         rust_main_def_id: DefId,
528         use_start_lang_item: bool,
529     ) {
530         let llfty =
531             cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int());
532
533         let main_ret_ty = cx.tcx().fn_sig(rust_main_def_id).output();
534         // Given that `main()` has no arguments,
535         // then its return type cannot have
536         // late-bound regions, since late-bound
537         // regions must appear in the argument
538         // listing.
539         let main_ret_ty = cx.tcx().erase_regions(
540             &main_ret_ty.no_bound_vars().unwrap(),
541         );
542
543         if cx.get_defined_value("main").is_some() {
544             // FIXME: We should be smart and show a better diagnostic here.
545             cx.sess().struct_span_err(sp, "entry symbol `main` defined multiple times")
546                      .help("did you use #[no_mangle] on `fn main`? Use #[start] instead")
547                      .emit();
548             cx.sess().abort_if_errors();
549             bug!();
550         }
551         let llfn = cx.declare_cfn("main", llfty);
552
553         // `main` should respect same config for frame pointer elimination as rest of code
554         cx.set_frame_pointer_elimination(llfn);
555         cx.apply_target_cpu_attr(llfn);
556
557         let bx = Bx::new_block(&cx, llfn, "top");
558
559         bx.insert_reference_to_gdb_debug_scripts_section_global();
560
561         // Params from native main() used as args for rust start function
562         let param_argc = cx.get_param(llfn, 0);
563         let param_argv = cx.get_param(llfn, 1);
564         let arg_argc = bx.intcast(param_argc, cx.type_isize(), true);
565         let arg_argv = param_argv;
566
567         let (start_fn, args) = if use_start_lang_item {
568             let start_def_id = cx.tcx().require_lang_item(StartFnLangItem);
569             let start_fn = callee::resolve_and_get_fn(
570                 cx,
571                 start_def_id,
572                 cx.tcx().intern_substs(&[main_ret_ty.into()]),
573             );
574             (start_fn, vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())),
575                             arg_argc, arg_argv])
576         } else {
577             debug!("using user-defined start fn");
578             (rust_main, vec![arg_argc, arg_argv])
579         };
580
581         let result = bx.call(start_fn, &args, None);
582         bx.ret(bx.intcast(result, cx.type_int(), true));
583     }
584 }
585
586 pub(crate) fn write_metadata<'a, 'gcx>(
587     tcx: TyCtxt<'a, 'gcx, 'gcx>,
588     llvm_module: &ModuleLlvm
589 ) -> EncodedMetadata {
590     use std::io::Write;
591     use flate2::Compression;
592     use flate2::write::DeflateEncoder;
593
594     let (metadata_llcx, metadata_llmod) = (&*llvm_module.llcx, llvm_module.llmod());
595
596     #[derive(PartialEq, Eq, PartialOrd, Ord)]
597     enum MetadataKind {
598         None,
599         Uncompressed,
600         Compressed
601     }
602
603     let kind = tcx.sess.crate_types.borrow().iter().map(|ty| {
604         match *ty {
605             config::CrateType::Executable |
606             config::CrateType::Staticlib |
607             config::CrateType::Cdylib => MetadataKind::None,
608
609             config::CrateType::Rlib => MetadataKind::Uncompressed,
610
611             config::CrateType::Dylib |
612             config::CrateType::ProcMacro => MetadataKind::Compressed,
613         }
614     }).max().unwrap_or(MetadataKind::None);
615
616     if kind == MetadataKind::None {
617         return EncodedMetadata::new();
618     }
619
620     let metadata = tcx.encode_metadata();
621     if kind == MetadataKind::Uncompressed {
622         return metadata;
623     }
624
625     assert!(kind == MetadataKind::Compressed);
626     let mut compressed = tcx.metadata_encoding_version();
627     DeflateEncoder::new(&mut compressed, Compression::fast())
628         .write_all(&metadata.raw_data).unwrap();
629
630     let llmeta = common::bytes_in_context(metadata_llcx, &compressed);
631     let llconst = common::struct_in_context(metadata_llcx, &[llmeta], false);
632     let name = exported_symbols::metadata_symbol_name(tcx);
633     let buf = CString::new(name).unwrap();
634     let llglobal = unsafe {
635         llvm::LLVMAddGlobal(metadata_llmod, common::val_ty(llconst), buf.as_ptr())
636     };
637     unsafe {
638         llvm::LLVMSetInitializer(llglobal, llconst);
639         let section_name = metadata::metadata_section_name(&tcx.sess.target.target);
640         let name = SmallCStr::new(section_name);
641         llvm::LLVMSetSection(llglobal, name.as_ptr());
642
643         // Also generate a .section directive to force no
644         // flags, at least for ELF outputs, so that the
645         // metadata doesn't get loaded into memory.
646         let directive = format!(".section {}", section_name);
647         let directive = CString::new(directive).unwrap();
648         llvm::LLVMSetModuleInlineAsm(metadata_llmod, directive.as_ptr())
649     }
650     return metadata;
651 }
652
653 pub struct ValueIter<'ll> {
654     cur: Option<&'ll Value>,
655     step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
656 }
657
658 impl Iterator for ValueIter<'ll> {
659     type Item = &'ll Value;
660
661     fn next(&mut self) -> Option<&'ll Value> {
662         let old = self.cur;
663         if let Some(old) = old {
664             self.cur = unsafe { (self.step)(old) };
665         }
666         old
667     }
668 }
669
670 pub fn iter_globals(llmod: &'ll llvm::Module) -> ValueIter<'ll> {
671     unsafe {
672         ValueIter {
673             cur: llvm::LLVMGetFirstGlobal(llmod),
674             step: llvm::LLVMGetNextGlobal,
675         }
676     }
677 }
678
679 fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
680                                  cgu: &CodegenUnit<'tcx>)
681                                  -> CguReuse {
682     if !tcx.dep_graph.is_fully_enabled() {
683         return CguReuse::No
684     }
685
686     let work_product_id = &cgu.work_product_id();
687     if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
688         // We don't have anything cached for this CGU. This can happen
689         // if the CGU did not exist in the previous session.
690         return CguReuse::No
691     }
692
693     // Try to mark the CGU as green. If it we can do so, it means that nothing
694     // affecting the LLVM module has changed and we can re-use a cached version.
695     // If we compile with any kind of LTO, this means we can re-use the bitcode
696     // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
697     // know that later). If we are not doing LTO, there is only one optimized
698     // version of each module, so we re-use that.
699     let dep_node = cgu.codegen_dep_node(tcx);
700     assert!(!tcx.dep_graph.dep_node_exists(&dep_node),
701         "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
702         cgu.name());
703
704     if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() {
705         // We can re-use either the pre- or the post-thinlto state
706         if tcx.sess.lto() != Lto::No {
707             CguReuse::PreLto
708         } else {
709             CguReuse::PostLto
710         }
711     } else {
712         CguReuse::No
713     }
714 }
715
716 pub fn codegen_crate<'a, 'tcx, B: BackendMethods>(
717     backend: B,
718     tcx: TyCtxt<'a, 'tcx, 'tcx>,
719     rx: mpsc::Receiver<Box<dyn Any + Send>>
720 ) -> B::OngoingCodegen {
721
722     check_for_rustc_errors_attr(tcx);
723
724     let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
725
726     // Codegen the metadata.
727     tcx.sess.profiler(|p| p.start_activity(ProfileCategory::Codegen));
728
729     let metadata_cgu_name = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
730                                                             &["crate"],
731                                                             Some("metadata")).as_str()
732                                                                              .to_string();
733     let metadata_llvm_module = backend.new_metadata(tcx.sess, &metadata_cgu_name);
734     let metadata = time(tcx.sess, "write metadata", || {
735         backend.write_metadata(tcx, &metadata_llvm_module)
736     });
737     tcx.sess.profiler(|p| p.end_activity(ProfileCategory::Codegen));
738
739     let metadata_module = ModuleCodegen {
740         name: metadata_cgu_name,
741         module_llvm: metadata_llvm_module,
742         kind: ModuleKind::Metadata,
743     };
744
745     let time_graph = if tcx.sess.opts.debugging_opts.codegen_time_graph {
746         Some(time_graph::TimeGraph::new())
747     } else {
748         None
749     };
750
751     // Skip crate items and just output metadata in -Z no-codegen mode.
752     if tcx.sess.opts.debugging_opts.no_codegen ||
753        !tcx.sess.opts.output_types.should_codegen() {
754         let ongoing_codegen = backend.start_async_codegen(
755             tcx,
756             time_graph,
757             metadata,
758             rx,
759             1);
760
761         backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, metadata_module);
762         backend.codegen_finished(&ongoing_codegen, tcx);
763
764         assert_and_save_dep_graph(tcx);
765
766         backend.check_for_errors(&ongoing_codegen, tcx.sess);
767
768         return ongoing_codegen;
769     }
770
771     // Run the monomorphization collector and partition the collected items into
772     // codegen units.
773     let codegen_units = tcx.collect_and_partition_mono_items(LOCAL_CRATE).1;
774     let codegen_units = (*codegen_units).clone();
775
776     // Force all codegen_unit queries so they are already either red or green
777     // when compile_codegen_unit accesses them. We are not able to re-execute
778     // the codegen_unit query from just the DepNode, so an unknown color would
779     // lead to having to re-execute compile_codegen_unit, possibly
780     // unnecessarily.
781     if tcx.dep_graph.is_fully_enabled() {
782         for cgu in &codegen_units {
783             tcx.codegen_unit(cgu.name().clone());
784         }
785     }
786
787     let ongoing_codegen = backend.start_async_codegen(
788         tcx,
789         time_graph.clone(),
790         metadata,
791         rx,
792         codegen_units.len());
793     let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen));
794
795     // Codegen an allocator shim, if necessary.
796     //
797     // If the crate doesn't have an `allocator_kind` set then there's definitely
798     // no shim to generate. Otherwise we also check our dependency graph for all
799     // our output crate types. If anything there looks like its a `Dynamic`
800     // linkage, then it's already got an allocator shim and we'll be using that
801     // one instead. If nothing exists then it's our job to generate the
802     // allocator!
803     let any_dynamic_crate = tcx.sess.dependency_formats.borrow()
804         .iter()
805         .any(|(_, list)| {
806             use rustc::middle::dependency_format::Linkage;
807             list.iter().any(|&linkage| linkage == Linkage::Dynamic)
808         });
809     let allocator_module = if any_dynamic_crate {
810         None
811     } else if let Some(kind) = *tcx.sess.allocator_kind.get() {
812         let llmod_id = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
813                                                        &["crate"],
814                                                        Some("allocator")).as_str()
815                                                                          .to_string();
816         let modules = backend.new_metadata(tcx.sess, &llmod_id);
817         time(tcx.sess, "write allocator module", || {
818             backend.codegen_allocator(tcx, &modules, kind)
819         });
820
821         Some(ModuleCodegen {
822             name: llmod_id,
823             module_llvm: modules,
824             kind: ModuleKind::Allocator,
825         })
826     } else {
827         None
828     };
829
830     if let Some(allocator_module) = allocator_module {
831         backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, allocator_module);
832     }
833
834     backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, metadata_module);
835
836     // We sort the codegen units by size. This way we can schedule work for LLVM
837     // a bit more efficiently.
838     let codegen_units = {
839         let mut codegen_units = codegen_units;
840         codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
841         codegen_units
842     };
843
844     let mut total_codegen_time = Duration::new(0, 0);
845     let mut all_stats = Stats::default();
846
847     for cgu in codegen_units.into_iter() {
848         backend.wait_for_signal_to_codegen_item(&ongoing_codegen);
849         backend.check_for_errors(&ongoing_codegen, tcx.sess);
850
851         let cgu_reuse = determine_cgu_reuse(tcx, &cgu);
852         tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
853
854         match cgu_reuse {
855             CguReuse::No => {
856                 let _timing_guard = time_graph.as_ref().map(|time_graph| {
857                     time_graph.start(write::CODEGEN_WORKER_TIMELINE,
858                                      write::CODEGEN_WORK_PACKAGE_KIND,
859                                      &format!("codegen {}", cgu.name()))
860                 });
861                 let start_time = Instant::now();
862                 let stats = compile_codegen_unit(tcx, *cgu.name());
863                 all_stats.extend(stats);
864                 total_codegen_time += start_time.elapsed();
865                 false
866             }
867             CguReuse::PreLto => {
868                 write::submit_pre_lto_module_to_llvm(tcx, CachedModuleCodegen {
869                     name: cgu.name().to_string(),
870                     source: cgu.work_product(tcx),
871                 });
872                 true
873             }
874             CguReuse::PostLto => {
875                 write::submit_post_lto_module_to_llvm(tcx, CachedModuleCodegen {
876                     name: cgu.name().to_string(),
877                     source: cgu.work_product(tcx),
878                 });
879                 true
880             }
881         };
882     }
883
884     backend.codegen_finished(&ongoing_codegen, tcx);
885
886     // Since the main thread is sometimes blocked during codegen, we keep track
887     // -Ztime-passes output manually.
888     print_time_passes_entry(tcx.sess.time_passes(),
889                             "codegen to LLVM IR",
890                             total_codegen_time);
891
892     rustc_incremental::assert_module_sources::assert_module_sources(tcx);
893
894     symbol_names_test::report_symbol_names(tcx);
895
896     if tcx.sess.codegen_stats() {
897         println!("--- codegen stats ---");
898         println!("n_glues_created: {}", all_stats.n_glues_created);
899         println!("n_null_glues: {}", all_stats.n_null_glues);
900         println!("n_real_glues: {}", all_stats.n_real_glues);
901
902         println!("n_fns: {}", all_stats.n_fns);
903         println!("n_inlines: {}", all_stats.n_inlines);
904         println!("n_closures: {}", all_stats.n_closures);
905         println!("fn stats:");
906         all_stats.fn_stats.sort_by_key(|&(_, insns)| insns);
907         for &(ref name, insns) in all_stats.fn_stats.iter() {
908             println!("{} insns, {}", insns, *name);
909         }
910     }
911
912     if tcx.sess.count_llvm_insns() {
913         for (k, v) in all_stats.llvm_insns.iter() {
914             println!("{:7} {}", *v, *k);
915         }
916     }
917
918     backend.check_for_errors(&ongoing_codegen, tcx.sess);
919
920     assert_and_save_dep_graph(tcx);
921     ongoing_codegen.into_inner()
922 }
923
924 /// A curious wrapper structure whose only purpose is to call `codegen_aborted`
925 /// when it's dropped abnormally.
926 ///
927 /// In the process of working on rust-lang/rust#55238 a mysterious segfault was
928 /// stumbled upon. The segfault was never reproduced locally, but it was
929 /// suspected to be related to the fact that codegen worker threads were
930 /// sticking around by the time the main thread was exiting, causing issues.
931 ///
932 /// This structure is an attempt to fix that issue where the `codegen_aborted`
933 /// message will block until all workers have finished. This should ensure that
934 /// even if the main codegen thread panics we'll wait for pending work to
935 /// complete before returning from the main thread, hopefully avoiding
936 /// segfaults.
937 ///
938 /// If you see this comment in the code, then it means that this workaround
939 /// worked! We may yet one day track down the mysterious cause of that
940 /// segfault...
941 struct AbortCodegenOnDrop<B: BackendMethods>(Option<B::OngoingCodegen>);
942
943 impl<B: BackendMethods> AbortCodegenOnDrop<B> {
944     fn into_inner(mut self) -> B::OngoingCodegen {
945         self.0.take().unwrap()
946     }
947 }
948
949 impl<B: BackendMethods> Deref for AbortCodegenOnDrop<B> {
950     type Target = B::OngoingCodegen;
951
952     fn deref(&self) -> &B::OngoingCodegen {
953         self.0.as_ref().unwrap()
954     }
955 }
956
957 impl<B: BackendMethods> DerefMut for AbortCodegenOnDrop<B> {
958     fn deref_mut(&mut self) -> &mut B::OngoingCodegen {
959         self.0.as_mut().unwrap()
960     }
961 }
962
963 impl<B: BackendMethods> Drop for AbortCodegenOnDrop<B> {
964     fn drop(&mut self) {
965         if let Some(codegen) = self.0.take() {
966             B::codegen_aborted(codegen);
967         }
968     }
969 }
970
971 fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
972     time(tcx.sess,
973          "assert dep graph",
974          || rustc_incremental::assert_dep_graph(tcx));
975
976     time(tcx.sess,
977          "serialize dep graph",
978          || rustc_incremental::save_dep_graph(tcx));
979 }
980
981 impl CrateInfo {
982     pub fn new(tcx: TyCtxt) -> CrateInfo {
983         let mut info = CrateInfo {
984             panic_runtime: None,
985             compiler_builtins: None,
986             profiler_runtime: None,
987             sanitizer_runtime: None,
988             is_no_builtins: Default::default(),
989             native_libraries: Default::default(),
990             used_libraries: tcx.native_libraries(LOCAL_CRATE),
991             link_args: tcx.link_args(LOCAL_CRATE),
992             crate_name: Default::default(),
993             used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
994             used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
995             used_crate_source: Default::default(),
996             wasm_imports: Default::default(),
997             lang_item_to_crate: Default::default(),
998             missing_lang_items: Default::default(),
999         };
1000         let lang_items = tcx.lang_items();
1001
1002         let load_wasm_items = tcx.sess.crate_types.borrow()
1003             .iter()
1004             .any(|c| *c != config::CrateType::Rlib) &&
1005             tcx.sess.opts.target_triple.triple() == "wasm32-unknown-unknown";
1006
1007         if load_wasm_items {
1008             info.load_wasm_imports(tcx, LOCAL_CRATE);
1009         }
1010
1011         let crates = tcx.crates();
1012
1013         let n_crates = crates.len();
1014         info.native_libraries.reserve(n_crates);
1015         info.crate_name.reserve(n_crates);
1016         info.used_crate_source.reserve(n_crates);
1017         info.missing_lang_items.reserve(n_crates);
1018
1019         for &cnum in crates.iter() {
1020             info.native_libraries.insert(cnum, tcx.native_libraries(cnum));
1021             info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
1022             info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum));
1023             if tcx.is_panic_runtime(cnum) {
1024                 info.panic_runtime = Some(cnum);
1025             }
1026             if tcx.is_compiler_builtins(cnum) {
1027                 info.compiler_builtins = Some(cnum);
1028             }
1029             if tcx.is_profiler_runtime(cnum) {
1030                 info.profiler_runtime = Some(cnum);
1031             }
1032             if tcx.is_sanitizer_runtime(cnum) {
1033                 info.sanitizer_runtime = Some(cnum);
1034             }
1035             if tcx.is_no_builtins(cnum) {
1036                 info.is_no_builtins.insert(cnum);
1037             }
1038             if load_wasm_items {
1039                 info.load_wasm_imports(tcx, cnum);
1040             }
1041             let missing = tcx.missing_lang_items(cnum);
1042             for &item in missing.iter() {
1043                 if let Ok(id) = lang_items.require(item) {
1044                     info.lang_item_to_crate.insert(item, id.krate);
1045                 }
1046             }
1047
1048             // No need to look for lang items that are whitelisted and don't
1049             // actually need to exist.
1050             let missing = missing.iter()
1051                 .cloned()
1052                 .filter(|&l| !weak_lang_items::whitelisted(tcx, l))
1053                 .collect();
1054             info.missing_lang_items.insert(cnum, missing);
1055         }
1056
1057         return info
1058     }
1059
1060     fn load_wasm_imports(&mut self, tcx: TyCtxt, cnum: CrateNum) {
1061         self.wasm_imports.extend(tcx.wasm_import_module_map(cnum).iter().map(|(&id, module)| {
1062             let instance = Instance::mono(tcx, id);
1063             let import_name = tcx.symbol_name(instance);
1064
1065             (import_name.to_string(), module.clone())
1066         }));
1067     }
1068 }
1069
1070 fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1071                                   cgu_name: InternedString)
1072                                   -> Stats {
1073     let start_time = Instant::now();
1074
1075     let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
1076     let ((stats, module), _) = tcx.dep_graph.with_task(dep_node,
1077                                                        tcx,
1078                                                        cgu_name,
1079                                                        module_codegen);
1080     let time_to_codegen = start_time.elapsed();
1081
1082     // We assume that the cost to run LLVM on a CGU is proportional to
1083     // the time we needed for codegenning it.
1084     let cost = time_to_codegen.as_secs() * 1_000_000_000 +
1085                time_to_codegen.subsec_nanos() as u64;
1086
1087     write::submit_codegened_module_to_llvm(tcx,
1088                                            module,
1089                                            cost);
1090     return stats;
1091
1092     fn module_codegen<'a, 'tcx>(
1093         tcx: TyCtxt<'a, 'tcx, 'tcx>,
1094         cgu_name: InternedString)
1095         -> (Stats, ModuleCodegen<ModuleLlvm>)
1096     {
1097         let cgu = tcx.codegen_unit(cgu_name);
1098
1099         // Instantiate monomorphizations without filling out definitions yet...
1100         let llvm_module = ModuleLlvm::new(tcx.sess, &cgu_name.as_str());
1101         let stats = {
1102             let cx = CodegenCx::new(tcx, cgu, &llvm_module);
1103             let mono_items = cx.codegen_unit
1104                                .items_in_deterministic_order(cx.tcx);
1105             for &(mono_item, (linkage, visibility)) in &mono_items {
1106                 mono_item.predefine(&cx, linkage, visibility);
1107             }
1108
1109             // ... and now that we have everything pre-defined, fill out those definitions.
1110             for &(mono_item, _) in &mono_items {
1111                 mono_item.define(&cx);
1112             }
1113
1114             // If this codegen unit contains the main function, also create the
1115             // wrapper here
1116             maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
1117
1118             // Run replace-all-uses-with for statics that need it
1119             for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
1120                 unsafe {
1121                     let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
1122                     llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
1123                     llvm::LLVMDeleteGlobal(old_g);
1124                 }
1125             }
1126
1127             // Create the llvm.used variable
1128             // This variable has type [N x i8*] and is stored in the llvm.metadata section
1129             if !cx.used_statics.borrow().is_empty() {
1130                 let name = const_cstr!("llvm.used");
1131                 let section = const_cstr!("llvm.metadata");
1132                 let array = cx.const_array(
1133                     &cx.type_ptr_to(cx.type_i8()),
1134                     &*cx.used_statics.borrow()
1135                 );
1136
1137                 unsafe {
1138                     let g = llvm::LLVMAddGlobal(cx.llmod,
1139                                                 cx.val_ty(array),
1140                                                 name.as_ptr());
1141                     llvm::LLVMSetInitializer(g, array);
1142                     llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
1143                     llvm::LLVMSetSection(g, section.as_ptr());
1144                 }
1145             }
1146
1147             // Finalize debuginfo
1148             if cx.sess().opts.debuginfo != DebugInfo::None {
1149                 debuginfo::finalize(&cx);
1150             }
1151
1152             cx.stats.into_inner()
1153         };
1154
1155         (stats, ModuleCodegen {
1156             name: cgu_name.to_string(),
1157             module_llvm: llvm_module,
1158             kind: ModuleKind::Regular,
1159         })
1160     }
1161 }
1162
1163 pub fn provide_both(providers: &mut Providers) {
1164     providers.dllimport_foreign_items = |tcx, krate| {
1165         let module_map = tcx.foreign_modules(krate);
1166         let module_map = module_map.iter()
1167             .map(|lib| (lib.def_id, lib))
1168             .collect::<FxHashMap<_, _>>();
1169
1170         let dllimports = tcx.native_libraries(krate)
1171             .iter()
1172             .filter(|lib| {
1173                 if lib.kind != cstore::NativeLibraryKind::NativeUnknown {
1174                     return false
1175                 }
1176                 let cfg = match lib.cfg {
1177                     Some(ref cfg) => cfg,
1178                     None => return true,
1179                 };
1180                 attr::cfg_matches(cfg, &tcx.sess.parse_sess, None)
1181             })
1182             .filter_map(|lib| lib.foreign_module)
1183             .map(|id| &module_map[&id])
1184             .flat_map(|module| module.foreign_items.iter().cloned())
1185             .collect();
1186         Lrc::new(dllimports)
1187     };
1188
1189     providers.is_dllimport_foreign_item = |tcx, def_id| {
1190         tcx.dllimport_foreign_items(def_id.krate).contains(&def_id)
1191     };
1192 }
1193
1194 pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
1195     match linkage {
1196         Linkage::External => llvm::Linkage::ExternalLinkage,
1197         Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
1198         Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
1199         Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
1200         Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
1201         Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
1202         Linkage::Appending => llvm::Linkage::AppendingLinkage,
1203         Linkage::Internal => llvm::Linkage::InternalLinkage,
1204         Linkage::Private => llvm::Linkage::PrivateLinkage,
1205         Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
1206         Linkage::Common => llvm::Linkage::CommonLinkage,
1207     }
1208 }
1209
1210 pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
1211     match linkage {
1212         Visibility::Default => llvm::Visibility::Default,
1213         Visibility::Hidden => llvm::Visibility::Hidden,
1214         Visibility::Protected => llvm::Visibility::Protected,
1215     }
1216 }
1217
1218 // FIXME(mw): Anything that is produced via DepGraph::with_task() must implement
1219 //            the HashStable trait. Normally DepGraph::with_task() calls are
1220 //            hidden behind queries, but CGU creation is a special case in two
1221 //            ways: (1) it's not a query and (2) CGU are output nodes, so their
1222 //            Fingerprints are not actually needed. It remains to be clarified
1223 //            how exactly this case will be handled in the red/green system but
1224 //            for now we content ourselves with providing a no-op HashStable
1225 //            implementation for CGUs.
1226 mod temp_stable_hash_impls {
1227     use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher,
1228                                                HashStable};
1229     use {ModuleCodegen, ModuleLlvm};
1230
1231     impl<HCX> HashStable<HCX> for ModuleCodegen<ModuleLlvm> {
1232         fn hash_stable<W: StableHasherResult>(&self,
1233                                               _: &mut HCX,
1234                                               _: &mut StableHasher<W>) {
1235             // do nothing
1236         }
1237     }
1238 }