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::{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::{CompiledModules, CrateInfo, ModuleCodegen, TargetConfig};
21
22pub trait BackendTypes {
23 type Function: CodegenObject;
24 type BasicBlock: Copy;
25 type Funclet;
26
27 type Value: CodegenObject + PartialEq;
28 type Type: CodegenObject + PartialEq;
29 type FunctionSignature: CodegenObject + PartialEq;
30
31 type DIScope: Copy + Hash + PartialEq + Eq;
34 type DILocation: Copy;
35 type DIVariable: Copy;
36}
37
38pub trait CodegenBackend {
39 fn name(&self) -> &'static str;
40
41 fn init(&self, _sess: &Session) {}
42
43 fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
44
45 fn target_config(&self, _sess: &Session) -> TargetConfig {
48 TargetConfig {
49 target_features: ::alloc::vec::Vec::new()vec![],
50 unstable_target_features: ::alloc::vec::Vec::new()vec![],
51 has_reliable_f16: true,
54 has_reliable_f16_math: true,
55 has_reliable_f128: true,
56 has_reliable_f128_math: true,
57 }
58 }
59
60 fn supported_crate_types(&self, _sess: &Session) -> Vec<CrateType> {
61 ::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![
62 CrateType::Executable,
63 CrateType::Dylib,
64 CrateType::Rlib,
65 CrateType::StaticLib,
66 CrateType::Cdylib,
67 CrateType::ProcMacro,
68 CrateType::Sdylib,
69 ]
70 }
71
72 fn print_passes(&self) {}
73
74 fn print_version(&self) {}
75
76 fn replaced_intrinsics(&self) -> Vec<Symbol> {
79 ::alloc::vec::Vec::new()vec![]
80 }
81
82 fn fallback_intrinsics(&self) -> Vec<Symbol> {
85 ::alloc::vec::Vec::new()vec![]
86 }
87
88 fn thin_lto_supported(&self) -> bool {
90 true
91 }
92
93 fn has_zstd(&self) -> bool {
98 false
99 }
100
101 fn has_mnemonic(&self, _sess: &Session, _mnemonic: &str) -> bool {
106 false
107 }
108
109 fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
114 Box::new(crate::back::metadata::DefaultMetadataLoader)
115 }
116
117 fn provide(&self, _providers: &mut Providers) {}
118
119 fn target_cpu(&self, sess: &Session) -> String;
120
121 fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any>;
122
123 fn join_codegen(
129 &self,
130 ongoing_codegen: Box<dyn Any>,
131 sess: &Session,
132 outputs: &OutputFilenames,
133 crate_info: &CrateInfo,
134 ) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>);
135
136 fn print_pass_timings(&self) {}
137
138 fn print_statistics(&self) {}
139
140 fn print_statistics_json(&self) -> String {
141 String::new()
142 }
143
144 fn link(
146 &self,
147 sess: &Session,
148 compiled_modules: CompiledModules,
149 crate_info: CrateInfo,
150 metadata: EncodedMetadata,
151 outputs: &OutputFilenames,
152 ) {
153 link_binary(
154 sess,
155 &ArArchiveBuilderBuilder,
156 compiled_modules,
157 crate_info,
158 metadata,
159 outputs,
160 self.name(),
161 );
162 }
163}
164
165pub trait ExtraBackendMethods:
166 WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
167{
168 fn codegen_allocator<'tcx>(
169 &self,
170 tcx: TyCtxt<'tcx>,
171 module_name: &str,
172 methods: &[AllocatorMethod],
173 ) -> Self::Module;
174
175 fn compile_codegen_unit(
178 &self,
179 tcx: TyCtxt<'_>,
180 cgu_name: Symbol,
181 ) -> (ModuleCodegen<Self::Module>, u64);
182
183 fn supports_parallel(&self) -> bool {
187 true
188 }
189}