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_f16b: 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 has_zstd(&self) -> bool {
82 false
83 }
84
85 fn has_mnemonic(&self, _sess: &Session, _mnemonic: &str) -> bool {
90 false
91 }
92
93 fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
98 Box::new(crate::back::metadata::DefaultMetadataLoader)
99 }
100
101 fn provide(&self, _providers: &mut Providers) {}
104
105 fn target_cpu(&self, sess: &Session) -> String;
106
107 fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any>;
108
109 fn join_codegen(
115 &self,
116 ongoing_codegen: Box<dyn Any>,
117 sess: &Session,
118 incr_comp_session: Option<&IncrCompSession>,
119 outputs: &OutputFilenames,
120 crate_info: &CrateInfo,
121 ) -> (CompiledModules, WorkProductMap);
122
123 fn link(
125 &self,
126 sess: &Session,
127 compiled_modules: CompiledModules,
128 crate_info: CrateInfo,
129 metadata: EncodedMetadata,
130 outputs: &OutputFilenames,
131 ) {
132 link_binary(
133 sess,
134 &ArArchiveBuilderBuilder,
135 compiled_modules,
136 crate_info,
137 metadata,
138 outputs,
139 self.name(),
140 );
141 }
142}
143
144pub trait ExtraBackendMethods: Send + Sync + DynSend + DynSync {
145 type Module;
146
147 fn codegen_allocator<'tcx>(
148 &self,
149 tcx: TyCtxt<'tcx>,
150 module_name: &str,
151 methods: &[AllocatorMethod],
152 ) -> Self::Module;
153
154 fn compile_codegen_unit(
157 &self,
158 tcx: TyCtxt<'_>,
159 cgu_name: Symbol,
160 bitcode_needed: bool,
161 ) -> (ModuleCodegen<Self::Module>, u64);
162}