]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_ssa/src/traits/backend.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / compiler / rustc_codegen_ssa / src / traits / backend.rs
CommitLineData
a1dfa0c6
XL
1use super::write::WriteBackendMethods;
2use super::CodegenObject;
fc512014 3use crate::back::write::TargetMachineFactoryFn;
29967ef6 4use crate::{CodegenResults, ModuleCodegen};
60c5eb7d 5
74b04a01 6use rustc_ast::expand::allocator::AllocatorKind;
29967ef6 7use rustc_data_structures::fx::FxHashMap;
ba9703b0 8use rustc_errors::ErrorReported;
29967ef6 9use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
ba9703b0
XL
10use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
11use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
12use rustc_middle::ty::query::Providers;
13use rustc_middle::ty::{Ty, TyCtxt};
14use rustc_session::{
15 config::{self, OutputFilenames, PrintRequest},
16 Session,
17};
dfeec247 18use rustc_span::symbol::Symbol;
ba9703b0 19use rustc_target::abi::LayoutOf;
1b1a35ee 20use rustc_target::spec::Target;
a1dfa0c6 21
ba9703b0
XL
22pub use rustc_data_structures::sync::MetadataRef;
23
24use std::any::Any;
60c5eb7d 25
a1dfa0c6
XL
26pub trait BackendTypes {
27 type Value: CodegenObject;
e74abb32
XL
28 type Function: CodegenObject;
29
a1dfa0c6
XL
30 type BasicBlock: Copy;
31 type Type: CodegenObject;
32 type Funclet;
33
74b04a01
XL
34 // FIXME(eddyb) find a common convention for all of the debuginfo-related
35 // names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
a1dfa0c6 36 type DIScope: Copy;
29967ef6 37 type DILocation: Copy;
74b04a01 38 type DIVariable: Copy;
a1dfa0c6
XL
39}
40
41pub trait Backend<'tcx>:
ba9703b0 42 Sized + BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
a1dfa0c6
XL
43{
44}
45
46impl<'tcx, T> Backend<'tcx> for T where
ba9703b0 47 Self: BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
a1dfa0c6
XL
48{
49}
50
ba9703b0
XL
51pub trait CodegenBackend {
52 fn init(&self, _sess: &Session) {}
53 fn print(&self, _req: PrintRequest, _sess: &Session) {}
54 fn target_features(&self, _sess: &Session) -> Vec<Symbol> {
55 vec![]
56 }
57 fn print_passes(&self) {}
58 fn print_version(&self) {}
59
1b1a35ee
XL
60 /// If this plugin provides additional builtin targets, provide the one enabled by the options here.
61 /// Be careful: this is called *before* init() is called.
62 fn target_override(&self, _opts: &config::Options) -> Option<Target> {
63 None
64 }
65
ba9703b0 66 fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
f035d41b
XL
67 fn provide(&self, _providers: &mut Providers);
68 fn provide_extern(&self, _providers: &mut Providers);
ba9703b0
XL
69 fn codegen_crate<'tcx>(
70 &self,
71 tcx: TyCtxt<'tcx>,
72 metadata: EncodedMetadata,
73 need_metadata_module: bool,
74 ) -> Box<dyn Any>;
75
76 /// This is called on the returned `Box<dyn Any>` from `codegen_backend`
77 ///
78 /// # Panics
79 ///
80 /// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
81 fn join_codegen(
82 &self,
83 ongoing_codegen: Box<dyn Any>,
84 sess: &Session,
29967ef6 85 ) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported>;
ba9703b0
XL
86
87 /// This is called on the returned `Box<dyn Any>` from `join_codegen`
88 ///
89 /// # Panics
90 ///
91 /// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
92 fn link(
93 &self,
94 sess: &Session,
29967ef6 95 codegen_results: CodegenResults,
ba9703b0
XL
96 outputs: &OutputFilenames,
97 ) -> Result<(), ErrorReported>;
98}
99
dfeec247 100pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
dc9dc135
XL
101 fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
102 fn write_compressed_metadata<'tcx>(
a1dfa0c6 103 &self,
dc9dc135 104 tcx: TyCtxt<'tcx>,
48663c56
XL
105 metadata: &EncodedMetadata,
106 llvm_module: &mut Self::Module,
107 );
dc9dc135 108 fn codegen_allocator<'tcx>(
9fa01778 109 &self,
dc9dc135 110 tcx: TyCtxt<'tcx>,
9fa01778 111 mods: &mut Self::Module,
dc9dc135 112 kind: AllocatorKind,
29967ef6 113 has_alloc_error_handler: bool,
9fa01778 114 );
dfeec247
XL
115 /// This generates the codegen unit and returns it along with
116 /// a `u64` giving an estimate of the unit's processing cost.
e74abb32
XL
117 fn compile_codegen_unit(
118 &self,
119 tcx: TyCtxt<'_>,
120 cgu_name: Symbol,
dfeec247 121 ) -> (ModuleCodegen<Self::Module>, u64);
a1dfa0c6
XL
122 fn target_machine_factory(
123 &self,
124 sess: &Session,
9fa01778 125 opt_level: config::OptLevel,
fc512014 126 ) -> TargetMachineFactoryFn<Self>;
a1dfa0c6 127 fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;
29967ef6 128 fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>;
a1dfa0c6 129}