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.
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.
11 //! The Rust compiler.
15 //! This API is completely unstable and subject to change.
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/")]
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)]
29 #![feature(inclusive_range)]
30 #![feature(inclusive_range_syntax)]
33 #![feature(rustc_diagnostic_macros)]
34 #![feature(slice_patterns)]
35 #![feature(conservative_impl_trait)]
37 use rustc::dep_graph::WorkProduct;
38 use syntax_pos::symbol::Symbol;
41 extern crate bitflags;
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;
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;
66 extern crate cc; // Used to locate MSVC
69 pub use base::trans_crate;
70 use back::bytecode::RLIB_BYTECODE_EXTENSION;
72 pub use metadata::LlvmMetadataLoader;
73 pub use llvm_util::{init, target_features, print_version, print_passes, print};
76 use std::path::PathBuf;
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};
89 use rustc_mir::monomorphize;
97 pub(crate) mod linker;
100 pub(crate) mod symbol_export;
101 pub(crate) mod symbol_names;
109 mod assert_module_sources;
142 mod symbol_names_test;
149 pub struct LlvmTransCrate(());
151 impl LlvmTransCrate {
152 pub fn new() -> Self {
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;
162 fn metadata_loader() -> Box<MetadataLoader> {
163 box metadata::LlvmMetadataLoader
166 fn provide(providers: &mut ty::maps::Providers) {
167 back::symbol_names::provide(providers);
168 back::symbol_export::provide(providers);
169 base::provide(providers);
172 fn provide_extern(providers: &mut ty::maps::Providers) {
173 back::symbol_export::provide_extern(providers);
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)
184 trans: Self::OngoingCrateTranslation,
187 ) -> Self::TranslatedCrate {
188 trans.join(sess, dep_graph)
191 fn link_binary(sess: &Session, trans: &Self::TranslatedCrate, outputs: &OutputFilenames) {
192 back::link::link_binary(sess, trans, outputs, &trans.crate_name.as_str());
195 fn dump_incremental_data(trans: &Self::TranslatedCrate) {
196 back::write::dump_incremental_data(trans);
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.
208 pub source: ModuleSource,
209 pub kind: ModuleKind,
212 #[derive(Copy, Clone, Debug, PartialEq)]
213 pub enum ModuleKind {
219 impl ModuleTranslation {
220 pub fn llvm(&self) -> Option<&ModuleLlvm> {
222 ModuleSource::Translated(ref llvm) => Some(llvm),
223 ModuleSource::Preexisting(_) => None,
227 pub fn into_compiled_module(self,
230 emit_bc_compressed: bool,
231 outputs: &OutputFilenames) -> CompiledModule {
232 let pre_existing = match self.source {
233 ModuleSource::Preexisting(_) => true,
234 ModuleSource::Translated(_) => false,
236 let object = if emit_obj {
237 Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
241 let bytecode = if emit_bc {
242 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
246 let bytecode_compressed = if emit_bc_compressed {
247 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
248 .with_extension(RLIB_BYTECODE_EXTENSION))
254 llmod_id: self.llmod_id,
255 name: self.name.clone(),
266 pub struct CompiledModule {
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>,
276 pub enum ModuleSource {
277 /// Copy the `.o` files or whatever from the incr. comp. directory.
278 Preexisting(WorkProduct),
280 /// Rebuild from this LLVM module.
281 Translated(ModuleLlvm),
285 pub struct ModuleLlvm {
286 llcx: llvm::ContextRef,
287 pub llmod: llvm::ModuleRef,
288 tm: llvm::TargetMachineRef,
291 unsafe impl Send for ModuleLlvm { }
292 unsafe impl Sync for ModuleLlvm { }
294 impl Drop for ModuleLlvm {
297 llvm::LLVMDisposeModule(self.llmod);
298 llvm::LLVMContextDispose(self.llcx);
299 llvm::LLVMRustDisposeTargetMachine(self.tm);
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,
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)>,
332 #[cfg(not(stage0))] // remove after the next snapshot
333 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }