]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/lib.rs
incr.comp.: Use red/green tracking for CGU re-use.
[rust.git] / src / librustc_trans / lib.rs
1 // Copyright 2012-2013 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 //! The Rust compiler.
12 //!
13 //! # Note
14 //!
15 //! This API is completely unstable and subject to change.
16
17 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
18       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
19       html_root_url = "https://doc.rust-lang.org/nightly/")]
20 #![deny(warnings)]
21
22 #![feature(box_patterns)]
23 #![feature(box_syntax)]
24 #![feature(custom_attribute)]
25 #![allow(unused_attributes)]
26 #![feature(i128_type)]
27 #![feature(libc)]
28 #![feature(quote)]
29 #![feature(rustc_diagnostic_macros)]
30 #![feature(slice_patterns)]
31 #![feature(conservative_impl_trait)]
32
33 #![cfg_attr(stage0, feature(const_fn))]
34 #![cfg_attr(not(stage0), feature(const_atomic_bool_new))]
35 #![cfg_attr(not(stage0), feature(const_once_new))]
36
37 use rustc::dep_graph::WorkProduct;
38 use syntax_pos::symbol::Symbol;
39
40 #[macro_use]
41 extern crate bitflags;
42 extern crate flate2;
43 extern crate libc;
44 extern crate owning_ref;
45 #[macro_use] extern crate rustc;
46 extern crate rustc_allocator;
47 extern crate rustc_back;
48 extern crate rustc_data_structures;
49 extern crate rustc_incremental;
50 extern crate rustc_llvm as llvm;
51 extern crate rustc_platform_intrinsics as intrinsics;
52 extern crate rustc_const_math;
53 extern crate rustc_trans_utils;
54 extern crate rustc_demangle;
55 extern crate jobserver;
56 extern crate num_cpus;
57
58 #[macro_use] extern crate log;
59 #[macro_use] extern crate syntax;
60 extern crate syntax_pos;
61 extern crate rustc_errors as errors;
62 extern crate serialize;
63 #[cfg(windows)]
64 extern crate cc; // Used to locate MSVC
65
66 pub use base::trans_crate;
67
68 pub use metadata::LlvmMetadataLoader;
69 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
70
71 use std::any::Any;
72 use std::path::PathBuf;
73 use std::rc::Rc;
74 use std::sync::mpsc;
75
76 use rustc::dep_graph::DepGraph;
77 use rustc::hir::def_id::CrateNum;
78 use rustc::middle::cstore::MetadataLoader;
79 use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
80 use rustc::session::Session;
81 use rustc::session::config::{OutputFilenames, OutputType};
82 use rustc::ty::maps::Providers;
83 use rustc::ty::{self, TyCtxt};
84 use rustc::util::nodemap::{FxHashSet, FxHashMap};
85
86 mod diagnostics;
87
88 pub mod back {
89     mod archive;
90     mod bytecode;
91     mod command;
92     pub(crate) mod linker;
93     pub mod link;
94     mod lto;
95     pub(crate) mod symbol_export;
96     pub(crate) mod symbol_names;
97     pub mod write;
98     mod rpath;
99 }
100
101 mod abi;
102 mod adt;
103 mod allocator;
104 mod asm;
105 mod assert_module_sources;
106 mod attributes;
107 mod base;
108 mod builder;
109 mod cabi_aarch64;
110 mod cabi_arm;
111 mod cabi_asmjs;
112 mod cabi_hexagon;
113 mod cabi_mips;
114 mod cabi_mips64;
115 mod cabi_msp430;
116 mod cabi_nvptx;
117 mod cabi_nvptx64;
118 mod cabi_powerpc;
119 mod cabi_powerpc64;
120 mod cabi_s390x;
121 mod cabi_sparc;
122 mod cabi_sparc64;
123 mod cabi_x86;
124 mod cabi_x86_64;
125 mod cabi_x86_win64;
126 mod callee;
127 mod collector;
128 mod common;
129 mod consts;
130 mod context;
131 mod debuginfo;
132 mod declare;
133 mod glue;
134 mod intrinsic;
135 mod llvm_util;
136 mod machine;
137 mod metadata;
138 mod meth;
139 mod mir;
140 mod monomorphize;
141 mod partitioning;
142 mod symbol_names_test;
143 mod time_graph;
144 mod trans_item;
145 mod tvec;
146 mod type_;
147 mod type_of;
148 mod value;
149
150 pub struct LlvmTransCrate(());
151
152 impl LlvmTransCrate {
153     pub fn new() -> Self {
154         LlvmTransCrate(())
155     }
156 }
157
158 impl rustc_trans_utils::trans_crate::TransCrate for LlvmTransCrate {
159     type MetadataLoader = metadata::LlvmMetadataLoader;
160     type OngoingCrateTranslation = back::write::OngoingCrateTranslation;
161     type TranslatedCrate = CrateTranslation;
162
163     fn metadata_loader() -> Box<MetadataLoader> {
164         box metadata::LlvmMetadataLoader
165     }
166
167     fn provide_local(providers: &mut ty::maps::Providers) {
168         provide_local(providers);
169     }
170
171     fn provide_extern(providers: &mut ty::maps::Providers) {
172         provide_extern(providers);
173     }
174
175     fn trans_crate<'a, 'tcx>(
176         tcx: TyCtxt<'a, 'tcx, 'tcx>,
177         rx: mpsc::Receiver<Box<Any + Send>>
178     ) -> Self::OngoingCrateTranslation {
179         base::trans_crate(tcx, rx)
180     }
181
182     fn join_trans(
183         trans: Self::OngoingCrateTranslation,
184         sess: &Session,
185         dep_graph: &DepGraph
186     ) -> Self::TranslatedCrate {
187         trans.join(sess, dep_graph)
188     }
189
190     fn link_binary(sess: &Session, trans: &Self::TranslatedCrate, outputs: &OutputFilenames) {
191         back::link::link_binary(sess, trans, outputs, &trans.crate_name.as_str());
192     }
193
194     fn dump_incremental_data(trans: &Self::TranslatedCrate) {
195         back::write::dump_incremental_data(trans);
196     }
197 }
198
199 pub struct ModuleTranslation {
200     /// The name of the module. When the crate may be saved between
201     /// compilations, incremental compilation requires that name be
202     /// unique amongst **all** crates.  Therefore, it should contain
203     /// something unique to this crate (e.g., a module path) as well
204     /// as the crate name and disambiguator.
205     name: String,
206     llmod_id: String,
207     pub source: ModuleSource,
208     pub kind: ModuleKind,
209 }
210
211 #[derive(Copy, Clone, Debug, PartialEq)]
212 pub enum ModuleKind {
213     Regular,
214     Metadata,
215     Allocator,
216 }
217
218 impl ModuleTranslation {
219     pub fn llvm(&self) -> Option<&ModuleLlvm> {
220         match self.source {
221             ModuleSource::Translated(ref llvm) => Some(llvm),
222             ModuleSource::Preexisting(_) => None,
223         }
224     }
225
226     pub fn into_compiled_module(self,
227                                 emit_obj: bool,
228                                 emit_bc: bool,
229                                 outputs: &OutputFilenames) -> CompiledModule {
230         let pre_existing = match self.source {
231             ModuleSource::Preexisting(_) => true,
232             ModuleSource::Translated(_) => false,
233         };
234         let object = outputs.temp_path(OutputType::Object, Some(&self.name));
235
236         CompiledModule {
237             llmod_id: self.llmod_id,
238             name: self.name.clone(),
239             kind: self.kind,
240             pre_existing,
241             emit_obj,
242             emit_bc,
243             object,
244         }
245     }
246 }
247
248 #[derive(Debug)]
249 pub struct CompiledModule {
250     pub name: String,
251     pub llmod_id: String,
252     pub object: PathBuf,
253     pub kind: ModuleKind,
254     pub pre_existing: bool,
255     pub emit_obj: bool,
256     pub emit_bc: bool,
257 }
258
259 pub enum ModuleSource {
260     /// Copy the `.o` files or whatever from the incr. comp. directory.
261     Preexisting(WorkProduct),
262
263     /// Rebuild from this LLVM module.
264     Translated(ModuleLlvm),
265 }
266
267 #[derive(Debug)]
268 pub struct ModuleLlvm {
269     llcx: llvm::ContextRef,
270     pub llmod: llvm::ModuleRef,
271     tm: llvm::TargetMachineRef,
272 }
273
274 unsafe impl Send for ModuleLlvm { }
275 unsafe impl Sync for ModuleLlvm { }
276
277 impl Drop for ModuleLlvm {
278     fn drop(&mut self) {
279         unsafe {
280             llvm::LLVMDisposeModule(self.llmod);
281             llvm::LLVMContextDispose(self.llcx);
282             llvm::LLVMRustDisposeTargetMachine(self.tm);
283         }
284     }
285 }
286
287 pub struct CrateTranslation {
288     pub crate_name: Symbol,
289     pub modules: Vec<CompiledModule>,
290     allocator_module: Option<CompiledModule>,
291     pub link: rustc::middle::cstore::LinkMeta,
292     pub metadata: rustc::middle::cstore::EncodedMetadata,
293     windows_subsystem: Option<String>,
294     linker_info: back::linker::LinkerInfo,
295     crate_info: CrateInfo,
296 }
297
298 // Misc info we load from metadata to persist beyond the tcx
299 pub struct CrateInfo {
300     panic_runtime: Option<CrateNum>,
301     compiler_builtins: Option<CrateNum>,
302     profiler_runtime: Option<CrateNum>,
303     sanitizer_runtime: Option<CrateNum>,
304     is_no_builtins: FxHashSet<CrateNum>,
305     native_libraries: FxHashMap<CrateNum, Rc<Vec<NativeLibrary>>>,
306     crate_name: FxHashMap<CrateNum, String>,
307     used_libraries: Rc<Vec<NativeLibrary>>,
308     link_args: Rc<Vec<String>>,
309     used_crate_source: FxHashMap<CrateNum, Rc<CrateSource>>,
310     used_crates_static: Vec<(CrateNum, LibSource)>,
311     used_crates_dynamic: Vec<(CrateNum, LibSource)>,
312 }
313
314 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }
315
316 pub fn provide_local(providers: &mut Providers) {
317     back::symbol_names::provide(providers);
318     back::symbol_export::provide_local(providers);
319     base::provide_local(providers);
320 }
321
322 pub fn provide_extern(providers: &mut Providers) {
323     back::symbol_names::provide(providers);
324     back::symbol_export::provide_extern(providers);
325     base::provide_extern(providers);
326 }