]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/lib.rs
818c96f9e91b66f3c43533eaa8b2645524bd108a
[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 //! # Note
12 //!
13 //! This API is completely unstable and subject to change.
14
15 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
16       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
17       html_root_url = "https://doc.rust-lang.org/nightly/")]
18
19 #![feature(box_patterns)]
20 #![feature(box_syntax)]
21 #![feature(custom_attribute)]
22 #![feature(libc)]
23 #![feature(rustc_diagnostic_macros)]
24 #![feature(in_band_lifetimes)]
25 #![feature(slice_sort_by_cached_key)]
26 #![feature(nll)]
27 #![allow(unused_attributes)]
28 #![allow(dead_code)]
29 #![feature(quote)]
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 libc;
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::middle::cstore::{LibSource, CrateSource, NativeLibrary};
52
53 // NB: 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 interfaces;
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
67 pub struct ModuleCodegen<M> {
68     /// The name of the module. When the crate may be saved between
69     /// compilations, incremental compilation requires that name be
70     /// unique amongst **all** crates.  Therefore, it should contain
71     /// something unique to this crate (e.g., a module path) as well
72     /// as the crate name and disambiguator.
73     /// We currently generate these names via CodegenUnit::build_cgu_name().
74     pub name: String,
75     pub module_llvm: M,
76     pub kind: ModuleKind,
77 }
78
79 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
80
81 impl<M> ModuleCodegen<M> {
82     pub fn into_compiled_module(self,
83                             emit_obj: bool,
84                             emit_bc: bool,
85                             emit_bc_compressed: bool,
86                             outputs: &OutputFilenames) -> CompiledModule {
87         let object = if emit_obj {
88             Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
89         } else {
90             None
91         };
92         let bytecode = if emit_bc {
93             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
94         } else {
95             None
96         };
97         let bytecode_compressed = if emit_bc_compressed {
98             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
99                     .with_extension(RLIB_BYTECODE_EXTENSION))
100         } else {
101             None
102         };
103
104         CompiledModule {
105             name: self.name.clone(),
106             kind: self.kind,
107             object,
108             bytecode,
109             bytecode_compressed,
110         }
111     }
112 }
113
114 #[derive(Debug)]
115 pub struct CompiledModule {
116     pub name: String,
117     pub kind: ModuleKind,
118     pub object: Option<PathBuf>,
119     pub bytecode: Option<PathBuf>,
120     pub bytecode_compressed: Option<PathBuf>,
121 }
122
123 pub struct CachedModuleCodegen {
124     pub name: String,
125     pub source: WorkProduct,
126 }
127
128 #[derive(Copy, Clone, Debug, PartialEq)]
129 pub enum ModuleKind {
130     Regular,
131     Metadata,
132     Allocator,
133 }
134
135 bitflags! {
136     pub struct MemFlags: u8 {
137         const VOLATILE = 1 << 0;
138         const NONTEMPORAL = 1 << 1;
139         const UNALIGNED = 1 << 2;
140     }
141 }
142
143 /// Misc info we load from metadata to persist beyond the tcx
144 pub struct CrateInfo {
145     pub panic_runtime: Option<CrateNum>,
146     pub compiler_builtins: Option<CrateNum>,
147     pub profiler_runtime: Option<CrateNum>,
148     pub sanitizer_runtime: Option<CrateNum>,
149     pub is_no_builtins: FxHashSet<CrateNum>,
150     pub native_libraries: FxHashMap<CrateNum, Lrc<Vec<NativeLibrary>>>,
151     pub crate_name: FxHashMap<CrateNum, String>,
152     pub used_libraries: Lrc<Vec<NativeLibrary>>,
153     pub link_args: Lrc<Vec<String>>,
154     pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
155     pub used_crates_static: Vec<(CrateNum, LibSource)>,
156     pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
157     pub wasm_imports: FxHashMap<String, String>,
158     pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
159     pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
160 }
161
162 __build_diagnostic_array! { librustc_codegen_ssa, DIAGNOSTICS }