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::{CompiledModules, CrateInfo, ModuleCodegen, TargetConfig};
22
23pub trait BackendTypes {
24 type Function: CodegenObject;
25 type BasicBlock: Copy;
26 type Funclet;
27
28 type Value: CodegenObject + PartialEq;
29 type Type: CodegenObject + PartialEq;
30 type FunctionSignature: CodegenObject + PartialEq;
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 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[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 thin_lto_supported(&self) -> bool {
85 true
86 }
87
88 fn has_zstd(&self) -> bool {
93 false
94 }
95
96 fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
101 Box::new(crate::back::metadata::DefaultMetadataLoader)
102 }
103
104 fn provide(&self, _providers: &mut Providers) {}
105
106 fn target_cpu(&self, sess: &Session) -> String;
107
108 fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>, crate_info: &CrateInfo) -> Box<dyn Any>;
109
110 fn join_codegen(
116 &self,
117 ongoing_codegen: Box<dyn Any>,
118 sess: &Session,
119 outputs: &OutputFilenames,
120 ) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>);
121
122 fn link(
124 &self,
125 sess: &Session,
126 compiled_modules: CompiledModules,
127 crate_info: CrateInfo,
128 metadata: EncodedMetadata,
129 outputs: &OutputFilenames,
130 ) {
131 link_binary(
132 sess,
133 &ArArchiveBuilderBuilder,
134 compiled_modules,
135 crate_info,
136 metadata,
137 outputs,
138 self.name(),
139 );
140 }
141}
142
143pub trait ExtraBackendMethods:
144 WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
145{
146 fn codegen_allocator<'tcx>(
147 &self,
148 tcx: TyCtxt<'tcx>,
149 module_name: &str,
150 methods: &[AllocatorMethod],
151 ) -> Self::Module;
152
153 fn compile_codegen_unit(
156 &self,
157 tcx: TyCtxt<'_>,
158 cgu_name: Symbol,
159 ) -> (ModuleCodegen<Self::Module>, u64);
160
161 fn target_machine_factory(
162 &self,
163 sess: &Session,
164 opt_level: config::OptLevel,
165 target_features: &[String],
166 ) -> TargetMachineFactoryFn<Self>;
167
168 fn spawn_named_thread<F, T>(
169 _time_trace: bool,
170 name: String,
171 f: F,
172 ) -> std::io::Result<std::thread::JoinHandle<T>>
173 where
174 F: FnOnce() -> T,
175 F: Send + 'static,
176 T: Send + 'static,
177 {
178 std::thread::Builder::new().name(name).spawn(f)
179 }
180
181 fn supports_parallel(&self) -> bool {
185 true
186 }
187}