rustc_codegen_ssa/traits/
backend.rs1use std::any::Any;
2use std::hash::Hash;
3
4use rustc_ast::expand::allocator::AllocatorMethod;
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, CrateType, 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, TargetConfig};
22
23pub trait BackendTypes {
24 type Value: CodegenObject + PartialEq;
25 type Metadata: CodegenObject;
26 type Function: CodegenObject;
27
28 type BasicBlock: Copy;
29 type Type: CodegenObject + PartialEq;
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 name(&self) -> &'static str;
41
42 fn init(&self, _sess: &Session) {}
43
44 fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
45
46 fn target_config(&self, _sess: &Session) -> TargetConfig {
49 TargetConfig {
50 target_features: ::alloc::vec::Vec::new()vec![],
51 unstable_target_features: ::alloc::vec::Vec::new()vec![],
52 has_reliable_f16: true,
55 has_reliable_f16_math: true,
56 has_reliable_f128: true,
57 has_reliable_f128_math: true,
58 }
59 }
60
61 fn supported_crate_types(&self, _sess: &Session) -> Vec<CrateType> {
62 <[_]>::into_vec(::alloc::boxed::box_new([CrateType::Executable,
CrateType::Dylib, CrateType::Rlib, CrateType::StaticLib,
CrateType::Cdylib, CrateType::ProcMacro, CrateType::Sdylib]))vec![
63 CrateType::Executable,
64 CrateType::Dylib,
65 CrateType::Rlib,
66 CrateType::StaticLib,
67 CrateType::Cdylib,
68 CrateType::ProcMacro,
69 CrateType::Sdylib,
70 ]
71 }
72
73 fn print_passes(&self) {}
74
75 fn print_version(&self) {}
76
77 fn replaced_intrinsics(&self) -> Vec<Symbol> {
80 ::alloc::vec::Vec::new()vec![]
81 }
82
83 fn has_zstd(&self) -> bool {
88 false
89 }
90
91 fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
96 Box::new(crate::back::metadata::DefaultMetadataLoader)
97 }
98
99 fn provide(&self, _providers: &mut Providers) {}
100
101 fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any>;
102
103 fn join_codegen(
109 &self,
110 ongoing_codegen: Box<dyn Any>,
111 sess: &Session,
112 outputs: &OutputFilenames,
113 ) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>);
114
115 fn link(
117 &self,
118 sess: &Session,
119 codegen_results: CodegenResults,
120 metadata: EncodedMetadata,
121 outputs: &OutputFilenames,
122 ) {
123 link_binary(
124 sess,
125 &ArArchiveBuilderBuilder,
126 codegen_results,
127 metadata,
128 outputs,
129 self.name(),
130 );
131 }
132}
133
134pub trait ExtraBackendMethods:
135 CodegenBackend + WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
136{
137 fn codegen_allocator<'tcx>(
138 &self,
139 tcx: TyCtxt<'tcx>,
140 module_name: &str,
141 methods: &[AllocatorMethod],
142 ) -> Self::Module;
143
144 fn compile_codegen_unit(
147 &self,
148 tcx: TyCtxt<'_>,
149 cgu_name: Symbol,
150 ) -> (ModuleCodegen<Self::Module>, u64);
151
152 fn target_machine_factory(
153 &self,
154 sess: &Session,
155 opt_level: config::OptLevel,
156 target_features: &[String],
157 ) -> TargetMachineFactoryFn<Self>;
158
159 fn spawn_named_thread<F, T>(
160 _time_trace: bool,
161 name: String,
162 f: F,
163 ) -> std::io::Result<std::thread::JoinHandle<T>>
164 where
165 F: FnOnce() -> T,
166 F: Send + 'static,
167 T: Send + 'static,
168 {
169 std::thread::Builder::new().name(name).spawn(f)
170 }
171
172 fn supports_parallel(&self) -> bool {
176 true
177 }
178}