rustc_codegen_ssa/traits/
backend.rs
1use std::any::Any;
2use std::hash::Hash;
3
4use rustc_ast::expand::allocator::AllocatorKind;
5use rustc_data_structures::fx::FxIndexMap;
6use rustc_data_structures::sync::{DynSend, DynSync};
7use rustc_metadata::EncodedMetadata;
8use rustc_metadata::creader::MetadataLoaderDyn;
9use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
10use rustc_middle::ty::TyCtxt;
11use rustc_middle::util::Providers;
12use rustc_session::Session;
13use rustc_session::config::{self, OutputFilenames, PrintRequest};
14use rustc_span::Symbol;
15
16use super::CodegenObject;
17use super::write::WriteBackendMethods;
18use crate::back::archive::ArArchiveBuilderBuilder;
19use crate::back::link::link_binary;
20use crate::back::write::TargetMachineFactoryFn;
21use crate::{CodegenResults, ModuleCodegen};
22
23pub trait BackendTypes {
24 type Value: CodegenObject;
25 type Metadata: CodegenObject;
26 type Function: CodegenObject;
27
28 type BasicBlock: Copy;
29 type Type: CodegenObject;
30 type Funclet;
31
32 type DIScope: Copy + Hash + PartialEq + Eq;
35 type DILocation: Copy;
36 type DIVariable: Copy;
37}
38
39pub trait CodegenBackend {
40 fn locale_resource(&self) -> &'static str;
43
44 fn init(&self, _sess: &Session) {}
45
46 fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
47
48 fn target_features_cfg(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> {
51 vec![]
52 }
53
54 fn print_passes(&self) {}
55
56 fn print_version(&self) {}
57
58 fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
63 Box::new(crate::back::metadata::DefaultMetadataLoader)
64 }
65
66 fn provide(&self, _providers: &mut Providers) {}
67
68 fn codegen_crate<'tcx>(
69 &self,
70 tcx: TyCtxt<'tcx>,
71 metadata: EncodedMetadata,
72 need_metadata_module: bool,
73 ) -> Box<dyn Any>;
74
75 fn join_codegen(
81 &self,
82 ongoing_codegen: Box<dyn Any>,
83 sess: &Session,
84 outputs: &OutputFilenames,
85 ) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>);
86
87 fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) {
89 link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, outputs);
90 }
91
92 fn supports_parallel(&self) -> bool {
96 true
97 }
98}
99
100pub trait ExtraBackendMethods:
101 CodegenBackend + WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
102{
103 fn codegen_allocator<'tcx>(
104 &self,
105 tcx: TyCtxt<'tcx>,
106 module_name: &str,
107 kind: AllocatorKind,
108 alloc_error_handler_kind: AllocatorKind,
109 ) -> Self::Module;
110
111 fn compile_codegen_unit(
114 &self,
115 tcx: TyCtxt<'_>,
116 cgu_name: Symbol,
117 ) -> (ModuleCodegen<Self::Module>, u64);
118
119 fn target_machine_factory(
120 &self,
121 sess: &Session,
122 opt_level: config::OptLevel,
123 target_features: &[String],
124 ) -> TargetMachineFactoryFn<Self>;
125
126 fn spawn_named_thread<F, T>(
127 _time_trace: bool,
128 name: String,
129 f: F,
130 ) -> std::io::Result<std::thread::JoinHandle<T>>
131 where
132 F: FnOnce() -> T,
133 F: Send + 'static,
134 T: Send + 'static,
135 {
136 std::thread::Builder::new().name(name).spawn(f)
137 }
138}