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 #![allow(unused_attributes)]
26 #![feature(i128_type)]
28 #![feature(inclusive_range)]
29 #![feature(inclusive_range_syntax)]
32 #![feature(rustc_diagnostic_macros)]
33 #![feature(slice_patterns)]
34 #![feature(conservative_impl_trait)]
36 use rustc::dep_graph::WorkProduct;
37 use syntax_pos::symbol::Symbol;
40 extern crate bitflags;
43 #[macro_use] extern crate rustc;
44 extern crate jobserver;
45 extern crate num_cpus;
46 extern crate rustc_mir;
47 extern crate rustc_allocator;
48 extern crate rustc_apfloat;
49 extern crate rustc_back;
50 extern crate rustc_binaryen;
51 extern crate rustc_const_math;
52 extern crate rustc_data_structures;
53 extern crate rustc_demangle;
54 extern crate rustc_incremental;
55 extern crate rustc_llvm as llvm;
56 extern crate rustc_platform_intrinsics as intrinsics;
57 extern crate rustc_trans_utils;
59 #[macro_use] extern crate log;
60 #[macro_use] extern crate syntax;
61 extern crate syntax_pos;
62 extern crate rustc_errors as errors;
63 extern crate serialize;
65 extern crate cc; // Used to locate MSVC
68 pub use base::trans_crate;
69 use back::bytecode::RLIB_BYTECODE_EXTENSION;
71 pub use metadata::LlvmMetadataLoader;
72 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
75 use std::path::PathBuf;
79 use rustc::dep_graph::DepGraph;
80 use rustc::hir::def_id::CrateNum;
81 use rustc::middle::cstore::MetadataLoader;
82 use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
83 use rustc::session::Session;
84 use rustc::session::config::{OutputFilenames, OutputType};
85 use rustc::ty::{self, TyCtxt};
86 use rustc::util::nodemap::{FxHashSet, FxHashMap};
88 use rustc_mir::monomorphize;
96 pub(crate) mod linker;
99 pub(crate) mod symbol_export;
100 pub(crate) mod symbol_names;
108 mod assert_module_sources;
141 mod symbol_names_test;
148 pub struct LlvmTransCrate(());
150 impl LlvmTransCrate {
151 pub fn new() -> Self {
156 impl rustc_trans_utils::trans_crate::TransCrate for LlvmTransCrate {
157 type MetadataLoader = metadata::LlvmMetadataLoader;
158 type OngoingCrateTranslation = back::write::OngoingCrateTranslation;
159 type TranslatedCrate = CrateTranslation;
161 fn metadata_loader() -> Box<MetadataLoader> {
162 box metadata::LlvmMetadataLoader
165 fn provide(providers: &mut ty::maps::Providers) {
166 back::symbol_names::provide(providers);
167 back::symbol_export::provide(providers);
168 base::provide(providers);
171 fn provide_extern(providers: &mut ty::maps::Providers) {
172 back::symbol_export::provide_extern(providers);
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)
183 trans: Self::OngoingCrateTranslation,
186 ) -> Self::TranslatedCrate {
187 trans.join(sess, dep_graph)
190 fn link_binary(sess: &Session, trans: &Self::TranslatedCrate, outputs: &OutputFilenames) {
191 back::link::link_binary(sess, trans, outputs, &trans.crate_name.as_str());
194 fn dump_incremental_data(trans: &Self::TranslatedCrate) {
195 back::write::dump_incremental_data(trans);
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.
207 pub source: ModuleSource,
208 pub kind: ModuleKind,
211 #[derive(Copy, Clone, Debug, PartialEq)]
212 pub enum ModuleKind {
218 impl ModuleTranslation {
219 pub fn llvm(&self) -> Option<&ModuleLlvm> {
221 ModuleSource::Translated(ref llvm) => Some(llvm),
222 ModuleSource::Preexisting(_) => None,
226 pub fn into_compiled_module(self,
229 emit_bc_compressed: bool,
230 outputs: &OutputFilenames) -> CompiledModule {
231 let pre_existing = match self.source {
232 ModuleSource::Preexisting(_) => true,
233 ModuleSource::Translated(_) => false,
235 let object = if emit_obj {
236 Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
240 let bytecode = if emit_bc {
241 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
245 let bytecode_compressed = if emit_bc_compressed {
246 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
247 .with_extension(RLIB_BYTECODE_EXTENSION))
253 llmod_id: self.llmod_id,
254 name: self.name.clone(),
265 pub struct CompiledModule {
267 pub llmod_id: String,
268 pub kind: ModuleKind,
269 pub pre_existing: bool,
270 pub object: Option<PathBuf>,
271 pub bytecode: Option<PathBuf>,
272 pub bytecode_compressed: Option<PathBuf>,
275 pub enum ModuleSource {
276 /// Copy the `.o` files or whatever from the incr. comp. directory.
277 Preexisting(WorkProduct),
279 /// Rebuild from this LLVM module.
280 Translated(ModuleLlvm),
284 pub struct ModuleLlvm {
285 llcx: llvm::ContextRef,
286 pub llmod: llvm::ModuleRef,
287 tm: llvm::TargetMachineRef,
290 unsafe impl Send for ModuleLlvm { }
291 unsafe impl Sync for ModuleLlvm { }
293 impl Drop for ModuleLlvm {
296 llvm::LLVMDisposeModule(self.llmod);
297 llvm::LLVMContextDispose(self.llcx);
298 llvm::LLVMRustDisposeTargetMachine(self.tm);
303 pub struct CrateTranslation {
304 pub crate_name: Symbol,
305 pub modules: Vec<CompiledModule>,
306 allocator_module: Option<CompiledModule>,
307 metadata_module: CompiledModule,
308 pub link: rustc::middle::cstore::LinkMeta,
309 pub metadata: rustc::middle::cstore::EncodedMetadata,
310 windows_subsystem: Option<String>,
311 linker_info: back::linker::LinkerInfo,
312 crate_info: CrateInfo,
315 // Misc info we load from metadata to persist beyond the tcx
316 pub struct CrateInfo {
317 panic_runtime: Option<CrateNum>,
318 compiler_builtins: Option<CrateNum>,
319 profiler_runtime: Option<CrateNum>,
320 sanitizer_runtime: Option<CrateNum>,
321 is_no_builtins: FxHashSet<CrateNum>,
322 native_libraries: FxHashMap<CrateNum, Rc<Vec<NativeLibrary>>>,
323 crate_name: FxHashMap<CrateNum, String>,
324 used_libraries: Rc<Vec<NativeLibrary>>,
325 link_args: Rc<Vec<String>>,
326 used_crate_source: FxHashMap<CrateNum, Rc<CrateSource>>,
327 used_crates_static: Vec<(CrateNum, LibSource)>,
328 used_crates_dynamic: Vec<(CrateNum, LibSource)>,
331 #[cfg(not(stage0))] // remove after the next snapshot
332 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }