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, 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::{CodegenResults, ModuleCodegen, TargetConfig};
22
23pub trait BackendTypes {
24 type Value: CodegenObject + PartialEq;
25 type Metadata: CodegenObject;
26 type Function: CodegenObject;
27
28 type BasicBlock: Copy;
29 type Type: CodegenObject + PartialEq;
30 type Funclet;
31
32 type DIScope: Copy + Hash + PartialEq + Eq;
35 type DILocation: Copy;
36 type DIVariable: Copy;
37}
38
39pub trait CodegenBackend {
40 fn locale_resource(&self) -> &'static str;
43
44 fn name(&self) -> &'static str;
45
46 fn init(&self, _sess: &Session) {}
47
48 fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
49
50 fn target_config(&self, _sess: &Session) -> TargetConfig {
53 TargetConfig {
54 target_features: vec![],
55 unstable_target_features: vec![],
56 has_reliable_f16: true,
59 has_reliable_f16_math: true,
60 has_reliable_f128: true,
61 has_reliable_f128_math: true,
62 }
63 }
64
65 fn print_passes(&self) {}
66
67 fn print_version(&self) {}
68
69 fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
74 Box::new(crate::back::metadata::DefaultMetadataLoader)
75 }
76
77 fn provide(&self, _providers: &mut Providers) {}
78
79 fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any>;
80
81 fn join_codegen(
87 &self,
88 ongoing_codegen: Box<dyn Any>,
89 sess: &Session,
90 outputs: &OutputFilenames,
91 ) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>);
92
93 fn link(
95 &self,
96 sess: &Session,
97 codegen_results: CodegenResults,
98 metadata: EncodedMetadata,
99 outputs: &OutputFilenames,
100 ) {
101 link_binary(
102 sess,
103 &ArArchiveBuilderBuilder,
104 codegen_results,
105 metadata,
106 outputs,
107 self.name(),
108 );
109 }
110}
111
112pub trait ExtraBackendMethods:
113 CodegenBackend + WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
114{
115 fn codegen_allocator<'tcx>(
116 &self,
117 tcx: TyCtxt<'tcx>,
118 module_name: &str,
119 methods: &[AllocatorMethod],
120 ) -> Self::Module;
121
122 fn compile_codegen_unit(
125 &self,
126 tcx: TyCtxt<'_>,
127 cgu_name: Symbol,
128 ) -> (ModuleCodegen<Self::Module>, u64);
129
130 fn target_machine_factory(
131 &self,
132 sess: &Session,
133 opt_level: config::OptLevel,
134 target_features: &[String],
135 ) -> TargetMachineFactoryFn<Self>;
136
137 fn spawn_named_thread<F, T>(
138 _time_trace: bool,
139 name: String,
140 f: F,
141 ) -> std::io::Result<std::thread::JoinHandle<T>>
142 where
143 F: FnOnce() -> T,
144 F: Send + 'static,
145 T: Send + 'static,
146 {
147 std::thread::Builder::new().name(name).spawn(f)
148 }
149
150 fn supports_parallel(&self) -> bool {
154 true
155 }
156}