-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
codegen_backend.rs
209 lines (185 loc) · 6.99 KB
/
codegen_backend.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! The Rust compiler.
//!
//! # Note
//!
//! This API is completely unstable and subject to change.
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
html_root_url = "https://doc.rust-lang.org/nightly/")]
#![deny(warnings)]
#![feature(box_syntax)]
use std::any::Any;
use std::io::Write;
use std::fs;
use std::path::Path;
use std::sync::{mpsc, Arc};
use rustc_data_structures::owning_ref::OwningRef;
use flate2::Compression;
use flate2::write::DeflateEncoder;
use syntax::symbol::Symbol;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::session::{Session, CompileIncomplete};
use rustc::session::config::{CrateType, OutputFilenames, PrintRequest};
use rustc::ty::TyCtxt;
use rustc::ty::query::Providers;
use rustc::middle::cstore::EncodedMetadata;
use rustc::middle::cstore::MetadataLoader;
use rustc::dep_graph::DepGraph;
use rustc_target::spec::Target;
use rustc_mir::monomorphize::collector;
use link::out_filename;
pub use rustc_data_structures::sync::MetadataRef;
pub trait CodegenBackend {
fn init(&self, _sess: &Session) {}
fn print(&self, _req: PrintRequest, _sess: &Session) {}
fn target_features(&self, _sess: &Session) -> Vec<Symbol> { vec![] }
fn print_passes(&self) {}
fn print_version(&self) {}
fn diagnostics(&self) -> &[(&'static str, &'static str)] { &[] }
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync>;
fn provide(&self, _providers: &mut Providers);
fn provide_extern(&self, _providers: &mut Providers);
fn codegen_crate<'a, 'tcx>(
&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
rx: mpsc::Receiver<Box<dyn Any + Send>>
) -> Box<dyn Any>;
/// This is called on the returned `Box<dyn Any>` from `codegen_backend`
///
/// # Panics
///
/// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
fn join_codegen_and_link(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
dep_graph: &DepGraph,
outputs: &OutputFilenames,
) -> Result<(), CompileIncomplete>;
}
pub struct NoLlvmMetadataLoader;
impl MetadataLoader for NoLlvmMetadataLoader {
fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> {
let buf = fs::read(filename).map_err(|e| format!("metadata file open err: {:?}", e))?;
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf);
Ok(rustc_erase_owner!(buf.map_owner_box()))
}
fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String> {
self.get_rlib_metadata(target, filename)
}
}
pub struct MetadataOnlyCodegenBackend(());
pub struct OngoingCodegen {
metadata: EncodedMetadata,
metadata_version: Vec<u8>,
crate_name: Symbol,
}
impl MetadataOnlyCodegenBackend {
pub fn boxed() -> Box<dyn CodegenBackend> {
box MetadataOnlyCodegenBackend(())
}
}
impl CodegenBackend for MetadataOnlyCodegenBackend {
fn init(&self, sess: &Session) {
for cty in sess.opts.crate_types.iter() {
match *cty {
CrateType::Rlib | CrateType::Dylib | CrateType::Executable => {},
_ => {
sess.diagnostic().warn(
&format!("LLVM unsupported, so output type {} is not supported", cty)
);
},
}
}
}
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
box NoLlvmMetadataLoader
}
fn provide(&self, providers: &mut Providers) {
::symbol_names::provide(providers);
providers.target_features_whitelist = |_tcx, _cnum| {
Default::default() // Just a dummy
};
providers.is_reachable_non_generic = |_tcx, _defid| true;
providers.exported_symbols = |_tcx, _crate| Arc::new(Vec::new());
}
fn provide_extern(&self, providers: &mut Providers) {
providers.is_reachable_non_generic = |_tcx, _defid| true;
}
fn codegen_crate<'a, 'tcx>(
&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
_rx: mpsc::Receiver<Box<dyn Any + Send>>
) -> Box<dyn Any> {
use rustc_mir::monomorphize::item::MonoItem;
::check_for_rustc_errors_attr(tcx);
::symbol_names_test::report_symbol_names(tcx);
::rustc_incremental::assert_dep_graph(tcx);
::rustc_incremental::assert_module_sources::assert_module_sources(tcx);
::rustc_mir::monomorphize::assert_symbols_are_distinct(tcx,
collector::collect_crate_mono_items(
tcx,
collector::MonoItemCollectionMode::Eager
).0.iter()
);
// FIXME: Fix this
// ::rustc::middle::dependency_format::calculate(tcx);
let _ = tcx.link_args(LOCAL_CRATE);
let _ = tcx.native_libraries(LOCAL_CRATE);
for mono_item in
collector::collect_crate_mono_items(
tcx,
collector::MonoItemCollectionMode::Eager
).0 {
if let MonoItem::Fn(inst) = mono_item {
let def_id = inst.def_id();
if def_id.is_local() {
let _ = inst.def.is_inline(tcx);
let _ = tcx.codegen_fn_attrs(def_id);
}
}
}
tcx.sess.abort_if_errors();
let metadata = tcx.encode_metadata();
box OngoingCodegen {
metadata,
metadata_version: tcx.metadata_encoding_version().to_vec(),
crate_name: tcx.crate_name(LOCAL_CRATE),
}
}
fn join_codegen_and_link(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
_dep_graph: &DepGraph,
outputs: &OutputFilenames,
) -> Result<(), CompileIncomplete> {
let ongoing_codegen = ongoing_codegen.downcast::<OngoingCodegen>()
.expect("Expected MetadataOnlyCodegenBackend's OngoingCodegen, found Box<dyn Any>");
for &crate_type in sess.opts.crate_types.iter() {
if crate_type != CrateType::Rlib &&
crate_type != CrateType::Dylib {
continue;
}
let output_name =
out_filename(sess, crate_type, &outputs, &ongoing_codegen.crate_name.as_str());
let mut compressed = ongoing_codegen.metadata_version.clone();
let metadata = if crate_type == CrateType::Dylib {
DeflateEncoder::new(&mut compressed, Compression::fast())
.write_all(&ongoing_codegen.metadata.raw_data)
.unwrap();
&compressed
} else {
&ongoing_codegen.metadata.raw_data
};
fs::write(&output_name, metadata).unwrap();
}
sess.abort_if_errors();
if !sess.opts.crate_types.contains(&CrateType::Rlib)
&& !sess.opts.crate_types.contains(&CrateType::Dylib)
{
sess.fatal("Executables are not supported by the metadata-only backend.");
}
Ok(())
}
}