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