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