2020-09-23 19:51:56 +00:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
2018-10-01 16:07:04 +00:00
|
|
|
#![feature(box_patterns)]
|
2019-03-14 14:12:56 +00:00
|
|
|
#![feature(try_blocks)]
|
2021-08-30 09:12:07 +00:00
|
|
|
#![feature(once_cell)]
|
2019-07-31 19:00:35 +00:00
|
|
|
#![feature(associated_type_bounds)]
|
2022-03-22 20:21:33 +00:00
|
|
|
#![feature(strict_provenance)]
|
2022-04-26 17:01:10 +00:00
|
|
|
#![feature(int_roundings)]
|
2022-06-22 11:02:23 +00:00
|
|
|
#![feature(if_let_guard)]
|
2018-12-13 15:57:25 +00:00
|
|
|
#![recursion_limit = "256"]
|
2022-02-23 13:06:22 +00:00
|
|
|
#![allow(rustc::potential_query_instability)]
|
2018-12-13 15:57:25 +00:00
|
|
|
|
2018-10-04 13:23:10 +00:00
|
|
|
//! This crate contains codegen code that is used by all codegen backends (LLVM and others).
|
|
|
|
//! The backend-agnostic functions of this crate use functions defined in various traits that
|
|
|
|
//! have to be implemented by each backends.
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc_macros;
|
2018-10-03 11:49:57 +00:00
|
|
|
#[macro_use]
|
2020-08-05 11:35:53 +00:00
|
|
|
extern crate tracing;
|
2019-02-09 14:31:47 +00:00
|
|
|
#[macro_use]
|
2020-03-29 14:41:09 +00:00
|
|
|
extern crate rustc_middle;
|
2018-10-01 16:07:04 +00:00
|
|
|
|
2020-12-19 11:36:35 +00:00
|
|
|
use rustc_ast as ast;
|
2018-10-03 11:49:57 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::CrateNum;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::dep_graph::WorkProduct;
|
|
|
|
use rustc_middle::middle::dependency_format::Dependencies;
|
2022-04-02 21:54:51 +00:00
|
|
|
use rustc_middle::middle::exported_symbols::SymbolExportKind;
|
2021-05-30 15:24:54 +00:00
|
|
|
use rustc_middle::ty::query::{ExternProviders, Providers};
|
2022-06-14 04:52:01 +00:00
|
|
|
use rustc_serialize::opaque::{MemDecoder, MemEncoder};
|
|
|
|
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
2021-07-06 16:28:07 +00:00
|
|
|
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
|
2020-11-14 02:02:03 +00:00
|
|
|
use rustc_session::cstore::{self, CrateSource};
|
2020-12-19 11:36:35 +00:00
|
|
|
use rustc_session::utils::NativeLibKind;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
2022-04-26 01:02:43 +00:00
|
|
|
use rustc_span::DebuggerVisualizerFile;
|
2022-05-24 18:14:48 +00:00
|
|
|
use std::collections::BTreeSet;
|
2019-09-16 20:34:57 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-10-03 11:49:57 +00:00
|
|
|
|
2018-10-23 15:01:35 +00:00
|
|
|
pub mod back;
|
2018-10-03 11:49:57 +00:00
|
|
|
pub mod base;
|
2018-10-01 16:07:04 +00:00
|
|
|
pub mod common;
|
2020-06-22 06:29:08 +00:00
|
|
|
pub mod coverageinfo;
|
2018-10-03 11:49:57 +00:00
|
|
|
pub mod debuginfo;
|
|
|
|
pub mod glue;
|
|
|
|
pub mod meth;
|
|
|
|
pub mod mir;
|
|
|
|
pub mod mono_item;
|
2020-10-04 09:12:56 +00:00
|
|
|
pub mod target_features;
|
2018-10-23 15:01:35 +00:00
|
|
|
pub mod traits;
|
2018-10-01 16:07:04 +00:00
|
|
|
|
|
|
|
pub struct ModuleCodegen<M> {
|
|
|
|
/// The name of the module. When the crate may be saved between
|
|
|
|
/// compilations, incremental compilation requires that name be
|
2019-02-08 13:53:55 +00:00
|
|
|
/// unique amongst **all** crates. Therefore, it should contain
|
2018-10-01 16:07:04 +00:00
|
|
|
/// something unique to this crate (e.g., a module path) as well
|
|
|
|
/// as the crate name and disambiguator.
|
|
|
|
/// We currently generate these names via CodegenUnit::build_cgu_name().
|
|
|
|
pub name: String,
|
|
|
|
pub module_llvm: M,
|
|
|
|
pub kind: ModuleKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M> ModuleCodegen<M> {
|
|
|
|
pub fn into_compiled_module(
|
|
|
|
self,
|
|
|
|
emit_obj: bool,
|
2020-09-23 16:33:54 +00:00
|
|
|
emit_dwarf_obj: bool,
|
2018-10-01 16:07:04 +00:00
|
|
|
emit_bc: bool,
|
|
|
|
outputs: &OutputFilenames,
|
|
|
|
) -> CompiledModule {
|
2019-12-06 12:18:32 +00:00
|
|
|
let object = emit_obj.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
|
2020-09-23 16:33:54 +00:00
|
|
|
let dwarf_object = emit_dwarf_obj.then(|| outputs.temp_path_dwo(Some(&self.name)));
|
2019-12-06 12:18:32 +00:00
|
|
|
let bytecode = emit_bc.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
|
2018-10-01 16:07:04 +00:00
|
|
|
|
2020-09-23 16:33:54 +00:00
|
|
|
CompiledModule { name: self.name.clone(), kind: self.kind, object, dwarf_object, bytecode }
|
2018-10-01 16:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Debug, Encodable, Decodable)]
|
2018-10-01 16:07:04 +00:00
|
|
|
pub struct CompiledModule {
|
|
|
|
pub name: String,
|
|
|
|
pub kind: ModuleKind,
|
|
|
|
pub object: Option<PathBuf>,
|
2020-09-23 16:33:54 +00:00
|
|
|
pub dwarf_object: Option<PathBuf>,
|
2018-10-01 16:07:04 +00:00
|
|
|
pub bytecode: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CachedModuleCodegen {
|
|
|
|
pub name: String,
|
|
|
|
pub source: WorkProduct,
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Encodable, Decodable)]
|
2018-10-01 16:07:04 +00:00
|
|
|
pub enum ModuleKind {
|
|
|
|
Regular,
|
|
|
|
Metadata,
|
|
|
|
Allocator,
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:31:47 +00:00
|
|
|
bitflags::bitflags! {
|
2018-10-03 11:49:57 +00:00
|
|
|
pub struct MemFlags: u8 {
|
|
|
|
const VOLATILE = 1 << 0;
|
|
|
|
const NONTEMPORAL = 1 << 1;
|
|
|
|
const UNALIGNED = 1 << 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:36:35 +00:00
|
|
|
#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
|
|
|
|
pub struct NativeLib {
|
|
|
|
pub kind: NativeLibKind,
|
|
|
|
pub name: Option<Symbol>,
|
2022-08-24 10:10:40 +00:00
|
|
|
pub filename: Option<Symbol>,
|
2020-12-19 11:36:35 +00:00
|
|
|
pub cfg: Option<ast::MetaItem>,
|
2021-03-25 04:45:09 +00:00
|
|
|
pub verbatim: Option<bool>,
|
2021-03-08 20:42:54 +00:00
|
|
|
pub dll_imports: Vec<cstore::DllImport>,
|
2020-12-19 11:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&cstore::NativeLib> for NativeLib {
|
|
|
|
fn from(lib: &cstore::NativeLib) -> Self {
|
2021-03-08 20:42:54 +00:00
|
|
|
NativeLib {
|
|
|
|
kind: lib.kind,
|
2022-08-24 10:10:40 +00:00
|
|
|
filename: lib.filename,
|
2021-03-08 20:42:54 +00:00
|
|
|
name: lib.name,
|
|
|
|
cfg: lib.cfg.clone(),
|
|
|
|
verbatim: lib.verbatim,
|
|
|
|
dll_imports: lib.dll_imports.clone(),
|
|
|
|
}
|
2020-12-19 11:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Misc info we load from metadata to persist beyond the tcx.
|
2019-12-21 10:37:15 +00:00
|
|
|
///
|
|
|
|
/// Note: though `CrateNum` is only meaningful within the same tcx, information within `CrateInfo`
|
|
|
|
/// is self-contained. `CrateNum` can be viewed as a unique identifier within a `CrateInfo`, where
|
|
|
|
/// `used_crate_source` contains all `CrateSource` of the dependents, and maintains a mapping from
|
|
|
|
/// identifiers (`CrateNum`) to `CrateSource`. The other fields map `CrateNum` to the crate's own
|
|
|
|
/// additional properties, so that effectively we can retrieve each dependent crate's `CrateSource`
|
|
|
|
/// and the corresponding properties without referencing information outside of a `CrateInfo`.
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Debug, Encodable, Decodable)]
|
2018-10-03 14:56:24 +00:00
|
|
|
pub struct CrateInfo {
|
2021-07-06 16:28:07 +00:00
|
|
|
pub target_cpu: String,
|
|
|
|
pub exported_symbols: FxHashMap<CrateType, Vec<String>>,
|
2022-04-02 21:54:51 +00:00
|
|
|
pub linked_symbols: FxHashMap<CrateType, Vec<(String, SymbolExportKind)>>,
|
2021-05-29 15:37:38 +00:00
|
|
|
pub local_crate_name: Symbol,
|
2018-10-03 14:56:24 +00:00
|
|
|
pub compiler_builtins: Option<CrateNum>,
|
|
|
|
pub profiler_runtime: Option<CrateNum>,
|
|
|
|
pub is_no_builtins: FxHashSet<CrateNum>,
|
2020-12-19 11:36:35 +00:00
|
|
|
pub native_libraries: FxHashMap<CrateNum, Vec<NativeLib>>,
|
2022-04-05 12:52:53 +00:00
|
|
|
pub crate_name: FxHashMap<CrateNum, Symbol>,
|
2020-12-19 11:36:35 +00:00
|
|
|
pub used_libraries: Vec<NativeLib>,
|
2018-10-03 14:56:24 +00:00
|
|
|
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
|
2021-06-07 10:18:28 +00:00
|
|
|
pub used_crates: Vec<CrateNum>,
|
2019-09-16 20:34:57 +00:00
|
|
|
pub dependency_formats: Lrc<Dependencies>,
|
2021-05-29 15:08:46 +00:00
|
|
|
pub windows_subsystem: Option<String>,
|
2022-05-24 18:14:48 +00:00
|
|
|
pub natvis_debugger_visualizers: BTreeSet<DebuggerVisualizerFile>,
|
2018-10-03 11:49:57 +00:00
|
|
|
}
|
2018-10-01 16:07:04 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Encodable, Decodable)]
|
2018-10-23 15:01:35 +00:00
|
|
|
pub struct CodegenResults {
|
|
|
|
pub modules: Vec<CompiledModule>,
|
|
|
|
pub allocator_module: Option<CompiledModule>,
|
2019-04-23 23:08:25 +00:00
|
|
|
pub metadata_module: Option<CompiledModule>,
|
2021-09-24 16:15:36 +00:00
|
|
|
pub metadata: rustc_metadata::EncodedMetadata,
|
2018-10-23 15:01:35 +00:00
|
|
|
pub crate_info: CrateInfo,
|
|
|
|
}
|
2019-10-13 10:11:27 +00:00
|
|
|
|
2022-08-25 17:04:00 +00:00
|
|
|
pub enum CodegenErrors<'a> {
|
2022-08-25 16:05:23 +00:00
|
|
|
WrongFileType,
|
|
|
|
EmptyVersionNumber,
|
2022-08-25 17:04:00 +00:00
|
|
|
EncodingVersionMismatch { version_array: String, rlink_version: u32 },
|
|
|
|
RustcVersionMismatch { rustc_version: String, current_version: &'a str },
|
2022-08-25 16:05:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-10-13 10:11:27 +00:00
|
|
|
crate::back::symbol_export::provide(providers);
|
2021-01-23 10:11:51 +00:00
|
|
|
crate::base::provide(providers);
|
2020-10-09 17:35:17 +00:00
|
|
|
crate::target_features::provide(providers);
|
2019-10-13 10:11:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 15:24:54 +00:00
|
|
|
pub fn provide_extern(providers: &mut ExternProviders) {
|
2019-10-13 10:11:27 +00:00
|
|
|
crate::back::symbol_export::provide_extern(providers);
|
|
|
|
}
|
2019-10-15 11:42:27 +00:00
|
|
|
|
|
|
|
/// Checks if the given filename ends with the `.rcgu.o` extension that `rustc`
|
|
|
|
/// uses for the object files it generates.
|
|
|
|
pub fn looks_like_rust_object_file(filename: &str) -> bool {
|
|
|
|
let path = Path::new(filename);
|
|
|
|
let ext = path.extension().and_then(|s| s.to_str());
|
|
|
|
if ext != Some(OutputType::Object.extension()) {
|
|
|
|
// The file name does not end with ".o", so it can't be an object file.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the ".o" at the end
|
|
|
|
let ext2 = path.file_stem().and_then(|s| Path::new(s).extension()).and_then(|s| s.to_str());
|
|
|
|
|
|
|
|
// Check if the "inner" extension
|
|
|
|
ext2 == Some(RUST_CGU_EXT)
|
|
|
|
}
|
2022-04-02 14:50:08 +00:00
|
|
|
|
2022-04-02 15:26:39 +00:00
|
|
|
const RLINK_VERSION: u32 = 1;
|
|
|
|
const RLINK_MAGIC: &[u8] = b"rustlink";
|
|
|
|
|
2022-04-02 14:50:08 +00:00
|
|
|
const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
|
|
|
|
|
|
|
|
impl CodegenResults {
|
|
|
|
pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec<u8> {
|
2022-06-14 04:52:01 +00:00
|
|
|
let mut encoder = MemEncoder::new();
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 03:30:45 +00:00
|
|
|
encoder.emit_raw_bytes(RLINK_MAGIC);
|
2022-04-02 15:26:39 +00:00
|
|
|
// `emit_raw_bytes` is used to make sure that the version representation does not depend on
|
|
|
|
// Encoder's inner representation of `u32`.
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 03:30:45 +00:00
|
|
|
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
|
|
|
|
encoder.emit_str(RUSTC_VERSION.unwrap());
|
|
|
|
Encodable::encode(codegen_results, &mut encoder);
|
2022-06-16 06:00:25 +00:00
|
|
|
encoder.finish()
|
2022-04-02 14:50:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 17:04:00 +00:00
|
|
|
pub fn deserialize_rlink<'a>(data: Vec<u8>) -> Result<Self, CodegenErrors<'a>> {
|
2022-04-02 15:26:39 +00:00
|
|
|
// The Decodable machinery is not used here because it panics if the input data is invalid
|
|
|
|
// and because its internal representation may change.
|
2022-04-02 14:50:08 +00:00
|
|
|
if !data.starts_with(RLINK_MAGIC) {
|
2022-08-25 16:05:23 +00:00
|
|
|
return Err(CodegenErrors::WrongFileType);
|
2022-04-02 14:50:08 +00:00
|
|
|
}
|
2022-04-02 15:26:39 +00:00
|
|
|
let data = &data[RLINK_MAGIC.len()..];
|
|
|
|
if data.len() < 4 {
|
2022-08-25 16:05:23 +00:00
|
|
|
return Err(CodegenErrors::EmptyVersionNumber);
|
2022-04-02 15:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut version_array: [u8; 4] = Default::default();
|
|
|
|
version_array.copy_from_slice(&data[..4]);
|
|
|
|
if u32::from_be_bytes(version_array) != RLINK_VERSION {
|
2022-08-25 16:05:23 +00:00
|
|
|
return Err(CodegenErrors::EncodingVersionMismatch {
|
|
|
|
version_array: String::from_utf8_lossy(&version_array).to_string(),
|
2022-08-25 17:04:00 +00:00
|
|
|
rlink_version: RLINK_VERSION,
|
2022-08-24 17:15:44 +00:00
|
|
|
});
|
2022-04-02 15:26:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 04:52:01 +00:00
|
|
|
let mut decoder = MemDecoder::new(&data[4..], 0);
|
2022-04-02 14:50:08 +00:00
|
|
|
let rustc_version = decoder.read_str();
|
|
|
|
let current_version = RUSTC_VERSION.unwrap();
|
|
|
|
if rustc_version != current_version {
|
2022-08-25 16:05:23 +00:00
|
|
|
return Err(CodegenErrors::RustcVersionMismatch {
|
|
|
|
rustc_version: rustc_version.to_string(),
|
2022-08-25 17:04:00 +00:00
|
|
|
current_version,
|
2022-08-24 17:15:44 +00:00
|
|
|
});
|
2022-04-02 14:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let codegen_results = CodegenResults::decode(&mut decoder);
|
|
|
|
Ok(codegen_results)
|
|
|
|
}
|
|
|
|
}
|