2018-01-04 17:15:40 +00:00
|
|
|
#![feature(rustc_private)]
|
2021-05-29 16:14:30 +00:00
|
|
|
#![deny(warnings)]
|
2018-01-04 17:15:40 +00:00
|
|
|
|
2020-03-12 23:07:58 +00:00
|
|
|
extern crate rustc_codegen_ssa;
|
2019-03-02 12:46:10 +00:00
|
|
|
extern crate rustc_data_structures;
|
2019-07-19 20:06:31 +00:00
|
|
|
extern crate rustc_driver;
|
2021-09-24 19:06:11 +00:00
|
|
|
extern crate rustc_errors;
|
2020-03-12 23:07:58 +00:00
|
|
|
extern crate rustc_hir;
|
2021-09-24 19:06:11 +00:00
|
|
|
extern crate rustc_metadata;
|
|
|
|
extern crate rustc_middle;
|
2020-03-11 11:49:08 +00:00
|
|
|
extern crate rustc_session;
|
2020-01-01 23:01:07 +00:00
|
|
|
extern crate rustc_span;
|
2020-03-12 23:07:58 +00:00
|
|
|
extern crate rustc_symbol_mangling;
|
|
|
|
extern crate rustc_target;
|
2018-01-04 17:15:40 +00:00
|
|
|
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
2020-10-10 15:59:16 +00:00
|
|
|
use rustc_codegen_ssa::{CodegenResults, CrateInfo};
|
2023-04-07 21:26:30 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
2022-01-23 18:34:26 +00:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2021-09-24 19:06:11 +00:00
|
|
|
use rustc_metadata::EncodedMetadata;
|
2020-10-10 15:59:16 +00:00
|
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
2020-03-29 16:08:01 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::config::OutputFilenames;
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_session::Session;
|
|
|
|
use std::any::Any;
|
2018-01-04 17:15:40 +00:00
|
|
|
|
2019-03-02 12:46:10 +00:00
|
|
|
struct TheBackend;
|
2018-01-04 17:15:40 +00:00
|
|
|
|
2018-05-08 13:10:16 +00:00
|
|
|
impl CodegenBackend for TheBackend {
|
2022-10-17 13:11:26 +00:00
|
|
|
fn locale_resource(&self) -> &'static str { "" }
|
|
|
|
|
2018-05-08 13:10:16 +00:00
|
|
|
fn codegen_crate<'a, 'tcx>(
|
2018-01-04 17:15:40 +00:00
|
|
|
&self,
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-10-10 15:59:16 +00:00
|
|
|
metadata: EncodedMetadata,
|
2019-04-26 07:22:36 +00:00
|
|
|
_need_metadata_module: bool,
|
2019-09-21 23:32:51 +00:00
|
|
|
) -> Box<dyn Any> {
|
2020-10-10 15:59:16 +00:00
|
|
|
Box::new(CodegenResults {
|
|
|
|
modules: vec![],
|
|
|
|
allocator_module: None,
|
|
|
|
metadata_module: None,
|
|
|
|
metadata,
|
2021-07-06 17:40:21 +00:00
|
|
|
crate_info: CrateInfo::new(tcx, "fake_target_cpu".to_string()),
|
2020-10-10 15:59:16 +00:00
|
|
|
})
|
2018-01-04 17:15:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 13:16:14 +00:00
|
|
|
fn join_codegen(
|
2018-01-04 17:15:40 +00:00
|
|
|
&self,
|
2019-09-21 23:32:51 +00:00
|
|
|
ongoing_codegen: Box<dyn Any>,
|
2020-01-28 13:16:14 +00:00
|
|
|
_sess: &Session,
|
2021-12-13 00:00:00 +00:00
|
|
|
_outputs: &OutputFilenames,
|
2023-04-07 21:26:30 +00:00
|
|
|
) -> Result<(CodegenResults, FxIndexMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
|
2020-10-10 15:59:16 +00:00
|
|
|
let codegen_results = ongoing_codegen
|
|
|
|
.downcast::<CodegenResults>()
|
|
|
|
.expect("in join_codegen: ongoing_codegen is not a CodegenResults");
|
2023-04-07 21:26:30 +00:00
|
|
|
Ok((*codegen_results, FxIndexMap::default()))
|
2020-01-28 13:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn link(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
2020-10-10 15:59:16 +00:00
|
|
|
codegen_results: CodegenResults,
|
2018-01-04 17:15:40 +00:00
|
|
|
outputs: &OutputFilenames,
|
2022-01-23 18:34:26 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2023-02-26 20:27:27 +00:00
|
|
|
use rustc_session::{config::{CrateType, OutFileName}, output::out_filename};
|
2018-01-04 17:15:40 +00:00
|
|
|
use std::io::Write;
|
2021-05-29 16:14:30 +00:00
|
|
|
let crate_name = codegen_results.crate_info.local_crate_name;
|
2018-01-04 17:15:40 +00:00
|
|
|
for &crate_type in sess.opts.crate_types.iter() {
|
2018-07-26 17:13:11 +00:00
|
|
|
if crate_type != CrateType::Rlib {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 03:26:58 +00:00
|
|
|
sess.fatal(format!("Crate type is {:?}", crate_type));
|
2018-01-04 17:15:40 +00:00
|
|
|
}
|
2022-12-06 12:46:10 +00:00
|
|
|
let output_name = out_filename(sess, crate_type, &outputs, crate_name);
|
2023-02-26 20:27:27 +00:00
|
|
|
match output_name {
|
|
|
|
OutFileName::Real(ref path) => {
|
|
|
|
let mut out_file = ::std::fs::File::create(path).unwrap();
|
|
|
|
write!(out_file, "This has been \"compiled\" successfully.").unwrap();
|
|
|
|
}
|
|
|
|
OutFileName::Stdout => {
|
|
|
|
let mut stdout = std::io::stdout();
|
|
|
|
write!(stdout, "This has been \"compiled\" successfully.").unwrap();
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 17:15:40 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 13:10:16 +00:00
|
|
|
/// This is the entrypoint for a hot plugged rustc_codegen_llvm
|
2018-01-04 17:15:40 +00:00
|
|
|
#[no_mangle]
|
2019-09-21 23:32:51 +00:00
|
|
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
2019-03-02 12:46:10 +00:00
|
|
|
Box::new(TheBackend)
|
2018-01-04 17:15:40 +00:00
|
|
|
}
|