]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/traits/backend.rs
Rollup merge of #104112 - yancyribbens:add-copy-to-repeat-description, r=JohnTitor
[rust.git] / compiler / rustc_codegen_ssa / src / traits / backend.rs
1 use super::write::WriteBackendMethods;
2 use super::CodegenObject;
3 use crate::back::write::TargetMachineFactoryFn;
4 use crate::{CodegenResults, ModuleCodegen};
5
6 use rustc_ast::expand::allocator::AllocatorKind;
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_errors::ErrorGuaranteed;
9 use rustc_metadata::EncodedMetadata;
10 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
11 use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf, TyAndLayout};
12 use rustc_middle::ty::query::{ExternProviders, Providers};
13 use rustc_middle::ty::{Ty, TyCtxt};
14 use rustc_session::{
15     config::{self, OutputFilenames, PrintRequest},
16     cstore::MetadataLoaderDyn,
17     Session,
18 };
19 use rustc_span::symbol::Symbol;
20 use rustc_target::abi::call::FnAbi;
21 use rustc_target::spec::Target;
22
23 pub use rustc_data_structures::sync::MetadataRef;
24
25 use std::any::Any;
26
27 pub trait BackendTypes {
28     type Value: CodegenObject;
29     type Function: CodegenObject;
30
31     type BasicBlock: Copy;
32     type Type: CodegenObject;
33     type Funclet;
34
35     // FIXME(eddyb) find a common convention for all of the debuginfo-related
36     // names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
37     type DIScope: Copy;
38     type DILocation: Copy;
39     type DIVariable: Copy;
40 }
41
42 pub trait Backend<'tcx>:
43     Sized
44     + BackendTypes
45     + HasTyCtxt<'tcx>
46     + LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
47     + FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>>
48 {
49 }
50
51 impl<'tcx, T> Backend<'tcx> for T where
52     Self: BackendTypes
53         + HasTyCtxt<'tcx>
54         + LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
55         + FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>>
56 {
57 }
58
59 pub trait CodegenBackend {
60     fn init(&self, _sess: &Session) {}
61     fn print(&self, _req: PrintRequest, _sess: &Session) {}
62     fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> {
63         vec![]
64     }
65     fn print_passes(&self) {}
66     fn print_version(&self) {}
67
68     /// If this plugin provides additional builtin targets, provide the one enabled by the options here.
69     /// Be careful: this is called *before* init() is called.
70     fn target_override(&self, _opts: &config::Options) -> Option<Target> {
71         None
72     }
73
74     /// The metadata loader used to load rlib and dylib metadata.
75     ///
76     /// Alternative codegen backends may want to use different rlib or dylib formats than the
77     /// default native static archives and dynamic libraries.
78     fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
79         Box::new(crate::back::metadata::DefaultMetadataLoader)
80     }
81
82     fn provide(&self, _providers: &mut Providers) {}
83     fn provide_extern(&self, _providers: &mut ExternProviders) {}
84     fn codegen_crate<'tcx>(
85         &self,
86         tcx: TyCtxt<'tcx>,
87         metadata: EncodedMetadata,
88         need_metadata_module: bool,
89     ) -> Box<dyn Any>;
90
91     /// This is called on the returned `Box<dyn Any>` from `codegen_backend`
92     ///
93     /// # Panics
94     ///
95     /// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
96     fn join_codegen(
97         &self,
98         ongoing_codegen: Box<dyn Any>,
99         sess: &Session,
100         outputs: &OutputFilenames,
101     ) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed>;
102
103     /// This is called on the returned `Box<dyn Any>` from `join_codegen`
104     ///
105     /// # Panics
106     ///
107     /// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
108     fn link(
109         &self,
110         sess: &Session,
111         codegen_results: CodegenResults,
112         outputs: &OutputFilenames,
113     ) -> Result<(), ErrorGuaranteed>;
114 }
115
116 pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
117     fn codegen_allocator<'tcx>(
118         &self,
119         tcx: TyCtxt<'tcx>,
120         module_name: &str,
121         kind: AllocatorKind,
122         alloc_error_handler_kind: AllocatorKind,
123     ) -> Self::Module;
124     /// This generates the codegen unit and returns it along with
125     /// a `u64` giving an estimate of the unit's processing cost.
126     fn compile_codegen_unit(
127         &self,
128         tcx: TyCtxt<'_>,
129         cgu_name: Symbol,
130     ) -> (ModuleCodegen<Self::Module>, u64);
131     fn target_machine_factory(
132         &self,
133         sess: &Session,
134         opt_level: config::OptLevel,
135         target_features: &[String],
136     ) -> TargetMachineFactoryFn<Self>;
137
138     fn spawn_thread<F, T>(_time_trace: bool, f: F) -> std::thread::JoinHandle<T>
139     where
140         F: FnOnce() -> T,
141         F: Send + 'static,
142         T: Send + 'static,
143     {
144         std::thread::spawn(f)
145     }
146
147     fn spawn_named_thread<F, T>(
148         _time_trace: bool,
149         name: String,
150         f: F,
151     ) -> std::io::Result<std::thread::JoinHandle<T>>
152     where
153         F: FnOnce() -> T,
154         F: Send + 'static,
155         T: Send + 'static,
156     {
157         std::thread::Builder::new().name(name).spawn(f)
158     }
159 }