1 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
3 #![feature(box_patterns)]
4 #![feature(box_syntax)]
5 #![feature(custom_attribute)]
7 #![feature(rustc_diagnostic_macros)]
8 #![feature(in_band_lifetimes)]
9 #![feature(slice_sort_by_cached_key)]
11 #![allow(unused_attributes)]
14 #![recursion_limit="256"]
16 //! This crate contains codegen code that is used by all codegen backends (LLVM and others).
17 //! The backend-agnostic functions of this crate use functions defined in various traits that
18 //! have to be implemented by each backends.
20 #[macro_use] extern crate bitflags;
21 #[macro_use] extern crate log;
22 extern crate rustc_apfloat;
23 #[macro_use] extern crate rustc;
24 extern crate rustc_target;
25 extern crate rustc_mir;
26 #[macro_use] extern crate syntax;
27 extern crate syntax_pos;
28 extern crate rustc_incremental;
29 extern crate rustc_codegen_utils;
30 extern crate rustc_data_structures;
31 extern crate rustc_allocator;
32 extern crate rustc_fs_util;
33 extern crate serialize;
34 extern crate rustc_errors;
35 extern crate rustc_demangle;
38 extern crate jobserver;
40 extern crate num_cpus;
42 use std::path::PathBuf;
43 use rustc::dep_graph::WorkProduct;
44 use rustc::session::config::{OutputFilenames, OutputType};
45 use rustc::middle::lang_items::LangItem;
46 use rustc::hir::def_id::CrateNum;
47 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
48 use rustc_data_structures::sync::Lrc;
49 use rustc_data_structures::svh::Svh;
50 use rustc::middle::cstore::{LibSource, CrateSource, NativeLibrary};
51 use syntax_pos::symbol::Symbol;
53 // N.B., this module needs to be declared first so diagnostics are
54 // registered before they are used.
68 pub struct ModuleCodegen<M> {
69 /// The name of the module. When the crate may be saved between
70 /// compilations, incremental compilation requires that name be
71 /// unique amongst **all** crates. Therefore, it should contain
72 /// something unique to this crate (e.g., a module path) as well
73 /// as the crate name and disambiguator.
74 /// We currently generate these names via CodegenUnit::build_cgu_name().
80 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
82 impl<M> ModuleCodegen<M> {
83 pub fn into_compiled_module(self,
86 emit_bc_compressed: bool,
87 outputs: &OutputFilenames) -> CompiledModule {
88 let object = if emit_obj {
89 Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
93 let bytecode = if emit_bc {
94 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
98 let bytecode_compressed = if emit_bc_compressed {
99 Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
100 .with_extension(RLIB_BYTECODE_EXTENSION))
106 name: self.name.clone(),
116 pub struct CompiledModule {
118 pub kind: ModuleKind,
119 pub object: Option<PathBuf>,
120 pub bytecode: Option<PathBuf>,
121 pub bytecode_compressed: Option<PathBuf>,
124 pub struct CachedModuleCodegen {
126 pub source: WorkProduct,
129 #[derive(Copy, Clone, Debug, PartialEq)]
130 pub enum ModuleKind {
137 pub struct MemFlags: u8 {
138 const VOLATILE = 1 << 0;
139 const NONTEMPORAL = 1 << 1;
140 const UNALIGNED = 1 << 2;
144 /// Misc info we load from metadata to persist beyond the tcx
145 pub struct CrateInfo {
146 pub panic_runtime: Option<CrateNum>,
147 pub compiler_builtins: Option<CrateNum>,
148 pub profiler_runtime: Option<CrateNum>,
149 pub sanitizer_runtime: Option<CrateNum>,
150 pub is_no_builtins: FxHashSet<CrateNum>,
151 pub native_libraries: FxHashMap<CrateNum, Lrc<Vec<NativeLibrary>>>,
152 pub crate_name: FxHashMap<CrateNum, String>,
153 pub used_libraries: Lrc<Vec<NativeLibrary>>,
154 pub link_args: Lrc<Vec<String>>,
155 pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
156 pub used_crates_static: Vec<(CrateNum, LibSource)>,
157 pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
158 pub wasm_imports: FxHashMap<String, String>,
159 pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
160 pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
164 pub struct CodegenResults {
165 pub crate_name: Symbol,
166 pub modules: Vec<CompiledModule>,
167 pub allocator_module: Option<CompiledModule>,
168 pub metadata_module: CompiledModule,
170 pub metadata: rustc::middle::cstore::EncodedMetadata,
171 pub windows_subsystem: Option<String>,
172 pub linker_info: back::linker::LinkerInfo,
173 pub crate_info: CrateInfo,
176 __build_diagnostic_array! { librustc_codegen_ssa, DIAGNOSTICS }