128 lines
3.9 KiB
Rust
Raw Normal View History

use super::write::WriteBackendMethods;
2018-09-27 15:31:20 +02:00
use super::CodegenObject;
use crate::ModuleCodegen;
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_data_structures::fx::FxHashMap;
2020-03-31 21:18:30 +02:00
use rustc_errors::ErrorReported;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
2020-03-29 16:41:09 +02:00
use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
2020-03-29 16:41:09 +02:00
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_session::{
config::{self, OutputFilenames, PrintRequest},
Session,
};
use rustc_span::symbol::Symbol;
use rustc_target::abi::LayoutOf;
use rustc_target::spec::Target;
pub use rustc_data_structures::sync::MetadataRef;
use std::any::Any;
2019-12-22 17:42:04 -05:00
use std::sync::Arc;
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
type BasicBlock: Copy;
2018-09-14 17:48:57 +02:00
type Type: CodegenObject;
type Funclet;
// FIXME(eddyb) find a common convention for all of the debuginfo-related
// names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
type DIScope: Copy;
type DIVariable: Copy;
}
2018-09-13 14:58:19 +02:00
pub trait Backend<'tcx>:
2020-03-04 14:50:21 +00:00
Sized + BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
2018-09-13 14:58:19 +02:00
{
}
impl<'tcx, T> Backend<'tcx> for T where
2020-03-04 14:50:21 +00:00
Self: BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
2018-11-24 16:15:26 +01:00
{
}
2018-09-25 17:52:03 +02: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.
/// 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
}
fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
fn provide(&self, _providers: &mut Providers);
fn provide_extern(&self, _providers: &mut Providers);
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,
) -> Result<(Box<dyn Any>, FxHashMap<WorkProductId, WorkProduct>), ErrorReported>;
/// 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,
codegen_results: Box<dyn Any>,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported>;
}
pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
2019-06-14 00:48:52 +03:00
fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
fn write_compressed_metadata<'tcx>(
2018-09-25 17:52:03 +02:00
&self,
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
metadata: &EncodedMetadata,
llvm_module: &mut Self::Module,
);
2019-06-14 00:48:52 +03:00
fn codegen_allocator<'tcx>(
&self,
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
mods: &mut Self::Module,
kind: AllocatorKind,
has_alloc_error_handler: bool,
);
/// This generates the codegen unit and returns it along with
/// a `u64` giving an estimate of the unit's processing cost.
fn compile_codegen_unit(
&self,
tcx: TyCtxt<'_>,
cgu_name: Symbol,
) -> (ModuleCodegen<Self::Module>, u64);
fn target_machine_factory(
&self,
sess: &Session,
opt_level: config::OptLevel,
) -> Arc<dyn Fn() -> Result<Self::TargetMachine, String> + Send + Sync>;
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;
}