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