1 use super::write::WriteBackendMethods;
2 use super::CodegenObject;
3 use crate::ModuleCodegen;
5 use rustc_ast::expand::allocator::AllocatorKind;
6 use rustc_errors::ErrorReported;
7 use rustc_middle::dep_graph::DepGraph;
8 use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
9 use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
10 use rustc_middle::ty::query::Providers;
11 use rustc_middle::ty::{Ty, TyCtxt};
13 config::{self, OutputFilenames, PrintRequest},
16 use rustc_span::symbol::Symbol;
17 use rustc_target::abi::LayoutOf;
19 pub use rustc_data_structures::sync::MetadataRef;
24 pub trait BackendTypes {
25 type Value: CodegenObject;
26 type Function: CodegenObject;
28 type BasicBlock: Copy;
29 type Type: CodegenObject;
32 // FIXME(eddyb) find a common convention for all of the debuginfo-related
33 // names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
35 type DIVariable: Copy;
38 pub trait Backend<'tcx>:
39 Sized + BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
43 impl<'tcx, T> Backend<'tcx> for T where
44 Self: BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
48 pub trait CodegenBackend {
49 fn init(&self, _sess: &Session) {}
50 fn print(&self, _req: PrintRequest, _sess: &Session) {}
51 fn target_features(&self, _sess: &Session) -> Vec<Symbol> {
54 fn print_passes(&self) {}
55 fn print_version(&self) {}
57 fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
58 fn provide(&self, _providers: &mut Providers);
59 fn provide_extern(&self, _providers: &mut Providers);
60 fn codegen_crate<'tcx>(
63 metadata: EncodedMetadata,
64 need_metadata_module: bool,
67 /// This is called on the returned `Box<dyn Any>` from `codegen_backend`
71 /// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
74 ongoing_codegen: Box<dyn Any>,
77 ) -> Result<Box<dyn Any>, ErrorReported>;
79 /// This is called on the returned `Box<dyn Any>` from `join_codegen`
83 /// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
87 codegen_results: Box<dyn Any>,
88 outputs: &OutputFilenames,
89 ) -> Result<(), ErrorReported>;
92 pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
93 fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
94 fn write_compressed_metadata<'tcx>(
97 metadata: &EncodedMetadata,
98 llvm_module: &mut Self::Module,
100 fn codegen_allocator<'tcx>(
103 mods: &mut Self::Module,
106 /// This generates the codegen unit and returns it along with
107 /// a `u64` giving an estimate of the unit's processing cost.
108 fn compile_codegen_unit(
112 ) -> (ModuleCodegen<Self::Module>, u64);
113 fn target_machine_factory(
116 opt_level: config::OptLevel,
117 ) -> Arc<dyn Fn() -> Result<Self::TargetMachine, String> + Send + Sync>;
118 fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;