rustc_codegen_ssa/traits/
backend.rs1use std::any::Any;
2use std::hash::Hash;
3
4use rustc_ast::expand::allocator::AllocatorMethod;
5use rustc_data_structures::sync::{DynSend, DynSync};
6use rustc_metadata::EncodedMetadata;
7use rustc_metadata::creader::MetadataLoaderDyn;
8use rustc_middle::dep_graph::WorkProductMap;
9use rustc_middle::ty::TyCtxt;
10use rustc_middle::util::Providers;
11use rustc_session::config::{OutputFilenames, PrintRequest};
12use rustc_session::{CodegenBackendInit, EarlySession, IncrCompSession, Session};
13use rustc_span::Symbol;
14use rustc_structures::CrateType;
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(&mut self, _sess: &EarlySession) -> CodegenBackendInit {
41 Default::default()
42 }
43
44 fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
45
46 fn target_config(&self, _sess: &EarlySession) -> TargetConfig {
49 TargetConfig {
50 internal_target_features: Default::default(),
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 has_zstd(&self) -> bool {
81 false
82 }
83
84 fn has_mnemonic(&self, _sess: &Session, _mnemonic: &str) -> bool {
89 false
90 }
91
92 fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
97 Box::new(crate::back::metadata::DefaultMetadataLoader)
98 }
99
100 fn provide(&self, _providers: &mut Providers) {}
103
104 fn target_cpu(&self, sess: &Session) -> String;
105
106 fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any>;
107
108 fn join_codegen(
114 &self,
115 ongoing_codegen: Box<dyn Any>,
116 sess: &Session,
117 incr_comp_session: Option<&IncrCompSession>,
118 outputs: &OutputFilenames,
119 crate_info: &CrateInfo,
120 ) -> (CompiledModules, WorkProductMap);
121
122 fn print_pass_timings(&self) {}
123
124 fn print_statistics(&self) {}
125
126 fn print_statistics_json(&self) -> String {
127 String::new()
128 }
129
130 fn link(
132 &self,
133 sess: &Session,
134 compiled_modules: CompiledModules,
135 crate_info: CrateInfo,
136 metadata: EncodedMetadata,
137 outputs: &OutputFilenames,
138 ) {
139 link_binary(
140 sess,
141 &ArArchiveBuilderBuilder,
142 compiled_modules,
143 crate_info,
144 metadata,
145 outputs,
146 self.name(),
147 );
148 }
149}
150
151pub trait ExtraBackendMethods: Send + Sync + DynSend + DynSync {
152 type Module;
153
154 fn codegen_allocator<'tcx>(
155 &self,
156 tcx: TyCtxt<'tcx>,
157 module_name: &str,
158 methods: &[AllocatorMethod],
159 ) -> Self::Module;
160
161 fn compile_codegen_unit(
164 &self,
165 tcx: TyCtxt<'_>,
166 cgu_name: Symbol,
167 bitcode_needed: bool,
168 ) -> (ModuleCodegen<Self::Module>, u64);
169}