rustc_codegen_ssa/traits/
backend.rs

1use std::any::Any;
2use std::hash::Hash;
3
4use rustc_ast::expand::allocator::AllocatorKind;
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};
22
23pub trait BackendTypes {
24    type Value: CodegenObject;
25    type Metadata: CodegenObject;
26    type Function: CodegenObject;
27
28    type BasicBlock: Copy;
29    type Type: CodegenObject;
30    type Funclet;
31
32    // FIXME(eddyb) find a common convention for all of the debuginfo-related
33    // names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
34    type DIScope: Copy + Hash + PartialEq + Eq;
35    type DILocation: Copy;
36    type DIVariable: Copy;
37}
38
39pub trait CodegenBackend {
40    /// Locale resources for diagnostic messages - a string the content of the Fluent resource.
41    /// Called before `init` so that all other functions are able to emit translatable diagnostics.
42    fn locale_resource(&self) -> &'static str;
43
44    fn init(&self, _sess: &Session) {}
45
46    fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
47
48    /// Returns the features that should be set in `cfg(target_features)`.
49    /// RUSTC_SPECIFIC_FEATURES should be skipped here, those are handled outside codegen.
50    fn target_features_cfg(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> {
51        vec![]
52    }
53
54    fn print_passes(&self) {}
55
56    fn print_version(&self) {}
57
58    /// The metadata loader used to load rlib and dylib metadata.
59    ///
60    /// Alternative codegen backends may want to use different rlib or dylib formats than the
61    /// default native static archives and dynamic libraries.
62    fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
63        Box::new(crate::back::metadata::DefaultMetadataLoader)
64    }
65
66    fn provide(&self, _providers: &mut Providers) {}
67
68    fn codegen_crate<'tcx>(
69        &self,
70        tcx: TyCtxt<'tcx>,
71        metadata: EncodedMetadata,
72        need_metadata_module: bool,
73    ) -> Box<dyn Any>;
74
75    /// This is called on the returned `Box<dyn Any>` from [`codegen_crate`](Self::codegen_crate)
76    ///
77    /// # Panics
78    ///
79    /// Panics when the passed `Box<dyn Any>` was not returned by [`codegen_crate`](Self::codegen_crate).
80    fn join_codegen(
81        &self,
82        ongoing_codegen: Box<dyn Any>,
83        sess: &Session,
84        outputs: &OutputFilenames,
85    ) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>);
86
87    /// This is called on the returned [`CodegenResults`] from [`join_codegen`](Self::join_codegen).
88    fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) {
89        link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, outputs);
90    }
91
92    /// Returns `true` if this backend can be safely called from multiple threads.
93    ///
94    /// Defaults to `true`.
95    fn supports_parallel(&self) -> bool {
96        true
97    }
98}
99
100pub trait ExtraBackendMethods:
101    CodegenBackend + WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
102{
103    fn codegen_allocator<'tcx>(
104        &self,
105        tcx: TyCtxt<'tcx>,
106        module_name: &str,
107        kind: AllocatorKind,
108        alloc_error_handler_kind: AllocatorKind,
109    ) -> Self::Module;
110
111    /// This generates the codegen unit and returns it along with
112    /// a `u64` giving an estimate of the unit's processing cost.
113    fn compile_codegen_unit(
114        &self,
115        tcx: TyCtxt<'_>,
116        cgu_name: Symbol,
117    ) -> (ModuleCodegen<Self::Module>, u64);
118
119    fn target_machine_factory(
120        &self,
121        sess: &Session,
122        opt_level: config::OptLevel,
123        target_features: &[String],
124    ) -> TargetMachineFactoryFn<Self>;
125
126    fn spawn_named_thread<F, T>(
127        _time_trace: bool,
128        name: String,
129        f: F,
130    ) -> std::io::Result<std::thread::JoinHandle<T>>
131    where
132        F: FnOnce() -> T,
133        F: Send + 'static,
134        T: Send + 'static,
135    {
136        std::thread::Builder::new().name(name).spawn(f)
137    }
138}