]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/lib.rs
Rollup merge of #58251 - h-michael:librustc_traits-2018, r=Centril
[rust.git] / src / librustc_codegen_ssa / lib.rs
1 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
2
3 #![feature(box_patterns)]
4 #![feature(box_syntax)]
5 #![feature(custom_attribute)]
6 #![feature(libc)]
7 #![feature(rustc_diagnostic_macros)]
8 #![feature(in_band_lifetimes)]
9 #![feature(slice_sort_by_cached_key)]
10 #![feature(nll)]
11 #![allow(unused_attributes)]
12 #![allow(dead_code)]
13
14 #![recursion_limit="256"]
15
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.
19
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;
36 extern crate cc;
37 extern crate libc;
38 extern crate jobserver;
39 extern crate memmap;
40 extern crate num_cpus;
41
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;
52
53 // N.B., this module needs to be declared first so diagnostics are
54 // registered before they are used.
55 mod diagnostics;
56
57 pub mod common;
58 pub mod traits;
59 pub mod mir;
60 pub mod debuginfo;
61 pub mod base;
62 pub mod callee;
63 pub mod glue;
64 pub mod meth;
65 pub mod mono_item;
66 pub mod back;
67
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().
75     pub name: String,
76     pub module_llvm: M,
77     pub kind: ModuleKind,
78 }
79
80 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
81
82 impl<M> ModuleCodegen<M> {
83     pub fn into_compiled_module(self,
84                             emit_obj: bool,
85                             emit_bc: bool,
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)))
90         } else {
91             None
92         };
93         let bytecode = if emit_bc {
94             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
95         } else {
96             None
97         };
98         let bytecode_compressed = if emit_bc_compressed {
99             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
100                     .with_extension(RLIB_BYTECODE_EXTENSION))
101         } else {
102             None
103         };
104
105         CompiledModule {
106             name: self.name.clone(),
107             kind: self.kind,
108             object,
109             bytecode,
110             bytecode_compressed,
111         }
112     }
113 }
114
115 #[derive(Debug)]
116 pub struct CompiledModule {
117     pub name: String,
118     pub kind: ModuleKind,
119     pub object: Option<PathBuf>,
120     pub bytecode: Option<PathBuf>,
121     pub bytecode_compressed: Option<PathBuf>,
122 }
123
124 pub struct CachedModuleCodegen {
125     pub name: String,
126     pub source: WorkProduct,
127 }
128
129 #[derive(Copy, Clone, Debug, PartialEq)]
130 pub enum ModuleKind {
131     Regular,
132     Metadata,
133     Allocator,
134 }
135
136 bitflags! {
137     pub struct MemFlags: u8 {
138         const VOLATILE = 1 << 0;
139         const NONTEMPORAL = 1 << 1;
140         const UNALIGNED = 1 << 2;
141     }
142 }
143
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>>,
161 }
162
163
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,
169     pub crate_hash: Svh,
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,
174 }
175
176 __build_diagnostic_array! { librustc_codegen_ssa, DIAGNOSTICS }