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