2018-10-23 17:01:35 +02:00
|
|
|
use super::write::WriteBackendMethods;
|
2018-09-27 15:31:20 +02:00
|
|
|
use super::CodegenObject;
|
2020-09-23 16:57:50 +01:00
|
|
|
use crate::back::write::TargetMachineFactoryFn;
|
2020-10-10 16:18:36 +02:00
|
|
|
use crate::{CodegenResults, ModuleCodegen};
|
2019-05-17 02:20:14 +01:00
|
|
|
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::expand::allocator::AllocatorKind;
|
2020-10-10 15:14:58 +02:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2021-09-24 18:15:36 +02:00
|
|
|
use rustc_metadata::EncodedMetadata;
|
2020-10-10 15:14:58 +02:00
|
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
2021-09-02 00:09:34 +03:00
|
|
|
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf, TyAndLayout};
|
2021-05-30 17:24:54 +02:00
|
|
|
use rustc_middle::ty::query::{ExternProviders, Providers};
|
2021-09-02 00:09:34 +03:00
|
|
|
use rustc_middle::ty::{Ty, TyCtxt};
|
2020-03-12 18:07:58 -05:00
|
|
|
use rustc_session::{
|
|
|
|
config::{self, OutputFilenames, PrintRequest},
|
2020-11-14 03:02:03 +01:00
|
|
|
cstore::MetadataLoaderDyn,
|
2020-03-12 18:07:58 -05:00
|
|
|
Session,
|
|
|
|
};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::Symbol;
|
2021-09-02 00:09:34 +03:00
|
|
|
use rustc_target::abi::call::FnAbi;
|
2020-09-17 12:01:12 +02:00
|
|
|
use rustc_target::spec::Target;
|
2018-08-30 15:41:59 +02:00
|
|
|
|
2020-03-12 18:07:58 -05:00
|
|
|
pub use rustc_data_structures::sync::MetadataRef;
|
|
|
|
|
|
|
|
use std::any::Any;
|
2019-05-17 02:20:14 +01:00
|
|
|
|
2018-09-13 14:58:19 +02:00
|
|
|
pub trait BackendTypes {
|
2018-09-14 17:48:57 +02:00
|
|
|
type Value: CodegenObject;
|
2019-10-13 11:28:19 +02:00
|
|
|
type Function: CodegenObject;
|
2019-08-27 11:45:03 +02:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
type BasicBlock: Copy;
|
2018-09-14 17:48:57 +02:00
|
|
|
type Type: CodegenObject;
|
2018-11-13 12:51:42 +02:00
|
|
|
type Funclet;
|
2018-09-20 15:47:22 +02:00
|
|
|
|
2020-01-26 18:50:13 +02:00
|
|
|
// FIXME(eddyb) find a common convention for all of the debuginfo-related
|
|
|
|
// names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
|
2018-09-20 15:47:22 +02:00
|
|
|
type DIScope: Copy;
|
2020-02-10 22:52:30 +02:00
|
|
|
type DILocation: Copy;
|
2020-01-26 18:50:13 +02:00
|
|
|
type DIVariable: Copy;
|
2018-08-28 11:40:34 +02:00
|
|
|
}
|
2018-09-13 14:58:19 +02:00
|
|
|
|
|
|
|
pub trait Backend<'tcx>:
|
2021-09-02 00:09:34 +03:00
|
|
|
Sized
|
|
|
|
+ BackendTypes
|
|
|
|
+ HasTyCtxt<'tcx>
|
|
|
|
+ LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
|
|
|
|
+ FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>>
|
2018-09-13 14:58:19 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, T> Backend<'tcx> for T where
|
2021-09-02 00:09:34 +03:00
|
|
|
Self: BackendTypes
|
|
|
|
+ HasTyCtxt<'tcx>
|
|
|
|
+ LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
|
|
|
|
+ FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>>
|
2018-11-24 16:15:26 +01:00
|
|
|
{
|
|
|
|
}
|
2018-09-25 17:52:03 +02:00
|
|
|
|
2020-03-12 18:07:58 -05:00
|
|
|
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) {}
|
|
|
|
|
2020-09-17 12:14:18 +02:00
|
|
|
/// If this plugin provides additional builtin targets, provide the one enabled by the options here.
|
2020-09-17 12:01:12 +02:00
|
|
|
/// Be careful: this is called *before* init() is called.
|
2020-09-17 12:14:18 +02:00
|
|
|
fn target_override(&self, _opts: &config::Options) -> Option<Target> {
|
|
|
|
None
|
|
|
|
}
|
2020-09-17 12:01:12 +02:00
|
|
|
|
2021-05-29 15:00:18 +02:00
|
|
|
/// The metadata loader used to load rlib and dylib metadata.
|
|
|
|
///
|
|
|
|
/// Alternative codegen backends may want to use different rlib or dylib formats than the
|
|
|
|
/// default native static archives and dynamic libraries.
|
|
|
|
fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
|
|
|
|
Box::new(crate::back::metadata::DefaultMetadataLoader)
|
|
|
|
}
|
|
|
|
|
2021-05-29 14:51:27 +02:00
|
|
|
fn provide(&self, _providers: &mut Providers) {}
|
2021-05-30 17:24:54 +02:00
|
|
|
fn provide_extern(&self, _providers: &mut ExternProviders) {}
|
2020-03-12 18:07:58 -05:00
|
|
|
fn codegen_crate<'tcx>(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
metadata: EncodedMetadata,
|
|
|
|
need_metadata_module: bool,
|
|
|
|
) -> 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(
|
|
|
|
&self,
|
|
|
|
ongoing_codegen: Box<dyn Any>,
|
|
|
|
sess: &Session,
|
2021-12-13 00:00:00 +00:00
|
|
|
outputs: &OutputFilenames,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed>;
|
2020-03-12 18:07:58 -05:00
|
|
|
|
|
|
|
/// This is called on the returned `Box<dyn Any>` from `join_codegen`
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
|
|
|
|
fn link(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
2020-10-10 16:18:36 +02:00
|
|
|
codegen_results: CodegenResults,
|
2020-03-12 18:07:58 -05:00
|
|
|
outputs: &OutputFilenames,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<(), ErrorGuaranteed>;
|
2020-03-12 18:07:58 -05:00
|
|
|
}
|
|
|
|
|
2020-01-05 02:10:23 +01:00
|
|
|
pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
|
2019-06-14 00:48:52 +03:00
|
|
|
fn codegen_allocator<'tcx>(
|
2019-02-25 08:52:46 +01:00
|
|
|
&self,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-08-31 11:16:10 -07:00
|
|
|
module_name: &str,
|
2019-06-12 00:11:55 +03:00
|
|
|
kind: AllocatorKind,
|
2020-09-07 10:45:20 +02:00
|
|
|
has_alloc_error_handler: bool,
|
2022-04-30 21:20:08 +02:00
|
|
|
) -> Self::Module;
|
2020-01-08 06:05:26 +01:00
|
|
|
/// This generates the codegen unit and returns it along with
|
|
|
|
/// a `u64` giving an estimate of the unit's processing cost.
|
2019-09-25 13:14:43 -04:00
|
|
|
fn compile_codegen_unit(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'_>,
|
2019-10-21 17:14:03 +11:00
|
|
|
cgu_name: Symbol,
|
2020-01-05 02:10:23 +01:00
|
|
|
) -> (ModuleCodegen<Self::Module>, u64);
|
2018-10-23 17:01:35 +02:00
|
|
|
fn target_machine_factory(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
2018-10-27 15:29:06 +03:00
|
|
|
opt_level: config::OptLevel,
|
2021-09-24 18:02:02 +03:00
|
|
|
target_features: &[String],
|
2020-09-23 16:57:50 +01:00
|
|
|
) -> TargetMachineFactoryFn<Self>;
|
2018-10-23 17:01:35 +02:00
|
|
|
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;
|
2020-09-17 17:39:26 +08:00
|
|
|
fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>;
|
2021-11-05 00:00:00 +00:00
|
|
|
|
|
|
|
fn spawn_thread<F, T>(_time_trace: bool, f: F) -> std::thread::JoinHandle<T>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
F: Send + 'static,
|
|
|
|
T: Send + 'static,
|
|
|
|
{
|
|
|
|
std::thread::spawn(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_named_thread<F, T>(
|
|
|
|
_time_trace: bool,
|
|
|
|
name: String,
|
|
|
|
f: F,
|
|
|
|
) -> std::io::Result<std::thread::JoinHandle<T>>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
F: Send + 'static,
|
|
|
|
T: Send + 'static,
|
|
|
|
{
|
|
|
|
std::thread::Builder::new().name(name).spawn(f)
|
|
|
|
}
|
2018-09-26 17:00:01 +02:00
|
|
|
}
|