]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/lib.rs
Various minor/cosmetic improvements to code
[rust.git] / src / librustc_codegen_ssa / lib.rs
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.
4 //
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.
10
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/")]
14
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
17 #![feature(custom_attribute)]
18 #![feature(libc)]
19 #![feature(rustc_diagnostic_macros)]
20 #![feature(in_band_lifetimes)]
21 #![feature(slice_sort_by_cached_key)]
22 #![feature(nll)]
23 #![allow(unused_attributes)]
24 #![allow(dead_code)]
25 #![feature(quote)]
26
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.
30
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;
47 extern crate cc;
48 extern crate libc;
49 extern crate jobserver;
50 extern crate memmap;
51 extern crate num_cpus;
52
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;
63
64 // N.B., this module needs to be declared first so diagnostics are
65 // registered before they are used.
66 mod diagnostics;
67
68 pub mod common;
69 pub mod traits;
70 pub mod mir;
71 pub mod debuginfo;
72 pub mod base;
73 pub mod callee;
74 pub mod glue;
75 pub mod meth;
76 pub mod mono_item;
77 pub mod back;
78
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().
86     pub name: String,
87     pub module_llvm: M,
88     pub kind: ModuleKind,
89 }
90
91 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
92
93 impl<M> ModuleCodegen<M> {
94     pub fn into_compiled_module(self,
95                             emit_obj: bool,
96                             emit_bc: bool,
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)))
101         } else {
102             None
103         };
104         let bytecode = if emit_bc {
105             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
106         } else {
107             None
108         };
109         let bytecode_compressed = if emit_bc_compressed {
110             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
111                     .with_extension(RLIB_BYTECODE_EXTENSION))
112         } else {
113             None
114         };
115
116         CompiledModule {
117             name: self.name.clone(),
118             kind: self.kind,
119             object,
120             bytecode,
121             bytecode_compressed,
122         }
123     }
124 }
125
126 #[derive(Debug)]
127 pub struct CompiledModule {
128     pub name: String,
129     pub kind: ModuleKind,
130     pub object: Option<PathBuf>,
131     pub bytecode: Option<PathBuf>,
132     pub bytecode_compressed: Option<PathBuf>,
133 }
134
135 pub struct CachedModuleCodegen {
136     pub name: String,
137     pub source: WorkProduct,
138 }
139
140 #[derive(Copy, Clone, Debug, PartialEq)]
141 pub enum ModuleKind {
142     Regular,
143     Metadata,
144     Allocator,
145 }
146
147 bitflags! {
148     pub struct MemFlags: u8 {
149         const VOLATILE = 1 << 0;
150         const NONTEMPORAL = 1 << 1;
151         const UNALIGNED = 1 << 2;
152     }
153 }
154
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>>,
172 }
173
174
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,
180     pub crate_hash: Svh,
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,
185 }
186
187 __build_diagnostic_array! { librustc_codegen_ssa, DIAGNOSTICS }