1 // Copyright 2017 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 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
12 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
13 html_root_url = "https://doc.rust-lang.org/nightly/")]
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
17 #![feature(custom_attribute)]
19 #![feature(rustc_diagnostic_macros)]
20 #![feature(in_band_lifetimes)]
21 #![feature(slice_sort_by_cached_key)]
23 #![allow(unused_attributes)]
27 //! This crate contains codegen code that is used by all codegen backends (LLVM and others).
28 //! The backend-agnostic functions of this crate use functions defined in various traits that
29 //! have to be implemented by each backends.
31 #[macro_use] extern crate bitflags;
32 #[macro_use] extern crate log;
33 extern crate rustc_apfloat;
34 #[macro_use] extern crate rustc;
35 extern crate rustc_target;
36 extern crate rustc_mir;
37 #[macro_use] extern crate syntax;
38 extern crate syntax_pos;
39 extern crate rustc_incremental;
40 extern crate rustc_codegen_utils;
41 extern crate rustc_data_structures;
42 extern crate rustc_allocator;
43 extern crate rustc_fs_util;
44 extern crate serialize;
45 extern crate rustc_errors;
46 extern crate rustc_demangle;
49 extern crate jobserver;
51 extern crate num_cpus;
53 use std::path::PathBuf;
54 use rustc::dep_graph::WorkProduct;
55 use rustc::session::config::{OutputFilenames, OutputType};
56 use rustc::middle::lang_items::LangItem;
57 use rustc::hir::def_id::CrateNum;
58 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
59 use rustc_data_structures::sync::Lrc;
60 use rustc_data_structures::svh::Svh;
61 use rustc::middle::cstore::{LibSource, CrateSource, NativeLibrary};
62 use syntax_pos::symbol::Symbol;
64 // N.B., this module needs to be declared first so diagnostics are
65 // registered before they are used.
79 pub struct ModuleCodegen<M> {
80 /// The name of the module. When the crate may be saved between
81 /// compilations, incremental compilation requires that name be
82 /// unique amongst **all** crates. Therefore, it should contain
83 /// something unique to this crate (e.g., a module path) as well
84 /// as the crate name and disambiguator.
85 /// We currently generate these names via CodegenUnit::build_cgu_name().
91 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
93 impl<M> ModuleCodegen<M> {
94 pub fn into_compiled_module(self,
97 emit_bc_compressed: bool,
98 outputs: &OutputFilenames) -> CompiledModule {
99 let object = if emit_obj {
100 Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
104 let bytecode = if emit_bc {
105 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
109 let bytecode_compressed = if emit_bc_compressed {
110 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
111 .with_extension(RLIB_BYTECODE_EXTENSION))
117 name: self.name.clone(),
127 pub struct CompiledModule {
129 pub kind: ModuleKind,
130 pub object: Option<PathBuf>,
131 pub bytecode: Option<PathBuf>,
132 pub bytecode_compressed: Option<PathBuf>,
135 pub struct CachedModuleCodegen {
137 pub source: WorkProduct,
140 #[derive(Copy, Clone, Debug, PartialEq)]
141 pub enum ModuleKind {
148 pub struct MemFlags: u8 {
149 const VOLATILE = 1 << 0;
150 const NONTEMPORAL = 1 << 1;
151 const UNALIGNED = 1 << 2;
155 /// Misc info we load from metadata to persist beyond the tcx
156 pub struct CrateInfo {
157 pub panic_runtime: Option<CrateNum>,
158 pub compiler_builtins: Option<CrateNum>,
159 pub profiler_runtime: Option<CrateNum>,
160 pub sanitizer_runtime: Option<CrateNum>,
161 pub is_no_builtins: FxHashSet<CrateNum>,
162 pub native_libraries: FxHashMap<CrateNum, Lrc<Vec<NativeLibrary>>>,
163 pub crate_name: FxHashMap<CrateNum, String>,
164 pub used_libraries: Lrc<Vec<NativeLibrary>>,
165 pub link_args: Lrc<Vec<String>>,
166 pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
167 pub used_crates_static: Vec<(CrateNum, LibSource)>,
168 pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
169 pub wasm_imports: FxHashMap<String, String>,
170 pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
171 pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
175 pub struct CodegenResults {
176 pub crate_name: Symbol,
177 pub modules: Vec<CompiledModule>,
178 pub allocator_module: Option<CompiledModule>,
179 pub metadata_module: CompiledModule,
181 pub metadata: rustc::middle::cstore::EncodedMetadata,
182 pub windows_subsystem: Option<String>,
183 pub linker_info: back::linker::LinkerInfo,
184 pub crate_info: CrateInfo,
187 __build_diagnostic_array! { librustc_codegen_ssa, DIAGNOSTICS }