]>
Commit | Line | Data |
---|---|---|
a1dfa0c6 XL |
1 | use super::write::WriteBackendMethods; |
2 | use super::CodegenObject; | |
fc512014 | 3 | use crate::back::write::TargetMachineFactoryFn; |
29967ef6 | 4 | use crate::{CodegenResults, ModuleCodegen}; |
60c5eb7d | 5 | |
74b04a01 | 6 | use rustc_ast::expand::allocator::AllocatorKind; |
29967ef6 | 7 | use rustc_data_structures::fx::FxHashMap; |
ba9703b0 | 8 | use rustc_errors::ErrorReported; |
c295e0f8 | 9 | use rustc_metadata::EncodedMetadata; |
29967ef6 | 10 | use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; |
c295e0f8 | 11 | use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf, TyAndLayout}; |
3c0e092e | 12 | use rustc_middle::ty::query::{ExternProviders, Providers}; |
ba9703b0 XL |
13 | use rustc_middle::ty::{Ty, TyCtxt}; |
14 | use rustc_session::{ | |
15 | config::{self, OutputFilenames, PrintRequest}, | |
c295e0f8 | 16 | cstore::MetadataLoaderDyn, |
ba9703b0 XL |
17 | Session, |
18 | }; | |
dfeec247 | 19 | use rustc_span::symbol::Symbol; |
c295e0f8 | 20 | use rustc_target::abi::call::FnAbi; |
1b1a35ee | 21 | use rustc_target::spec::Target; |
a1dfa0c6 | 22 | |
ba9703b0 XL |
23 | pub use rustc_data_structures::sync::MetadataRef; |
24 | ||
25 | use std::any::Any; | |
60c5eb7d | 26 | |
a1dfa0c6 XL |
27 | pub trait BackendTypes { |
28 | type Value: CodegenObject; | |
e74abb32 XL |
29 | type Function: CodegenObject; |
30 | ||
a1dfa0c6 XL |
31 | type BasicBlock: Copy; |
32 | type Type: CodegenObject; | |
33 | type Funclet; | |
34 | ||
74b04a01 XL |
35 | // FIXME(eddyb) find a common convention for all of the debuginfo-related |
36 | // names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.). | |
a1dfa0c6 | 37 | type DIScope: Copy; |
29967ef6 | 38 | type DILocation: Copy; |
74b04a01 | 39 | type DIVariable: Copy; |
a1dfa0c6 XL |
40 | } |
41 | ||
42 | pub trait Backend<'tcx>: | |
94222f64 XL |
43 | Sized |
44 | + BackendTypes | |
45 | + HasTyCtxt<'tcx> | |
c295e0f8 XL |
46 | + LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>> |
47 | + FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>> | |
a1dfa0c6 XL |
48 | { |
49 | } | |
50 | ||
51 | impl<'tcx, T> Backend<'tcx> for T where | |
94222f64 XL |
52 | Self: BackendTypes |
53 | + HasTyCtxt<'tcx> | |
c295e0f8 XL |
54 | + LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>> |
55 | + FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>> | |
a1dfa0c6 XL |
56 | { |
57 | } | |
58 | ||
ba9703b0 XL |
59 | pub trait CodegenBackend { |
60 | fn init(&self, _sess: &Session) {} | |
61 | fn print(&self, _req: PrintRequest, _sess: &Session) {} | |
62 | fn target_features(&self, _sess: &Session) -> Vec<Symbol> { | |
63 | vec![] | |
64 | } | |
65 | fn print_passes(&self) {} | |
66 | fn print_version(&self) {} | |
67 | ||
1b1a35ee XL |
68 | /// If this plugin provides additional builtin targets, provide the one enabled by the options here. |
69 | /// Be careful: this is called *before* init() is called. | |
70 | fn target_override(&self, _opts: &config::Options) -> Option<Target> { | |
71 | None | |
72 | } | |
73 | ||
17df50a5 XL |
74 | /// The metadata loader used to load rlib and dylib metadata. |
75 | /// | |
76 | /// Alternative codegen backends may want to use different rlib or dylib formats than the | |
77 | /// default native static archives and dynamic libraries. | |
78 | fn metadata_loader(&self) -> Box<MetadataLoaderDyn> { | |
79 | Box::new(crate::back::metadata::DefaultMetadataLoader) | |
80 | } | |
81 | ||
82 | fn provide(&self, _providers: &mut Providers) {} | |
3c0e092e | 83 | fn provide_extern(&self, _providers: &mut ExternProviders) {} |
ba9703b0 XL |
84 | fn codegen_crate<'tcx>( |
85 | &self, | |
86 | tcx: TyCtxt<'tcx>, | |
87 | metadata: EncodedMetadata, | |
88 | need_metadata_module: bool, | |
89 | ) -> Box<dyn Any>; | |
90 | ||
91 | /// This is called on the returned `Box<dyn Any>` from `codegen_backend` | |
92 | /// | |
93 | /// # Panics | |
94 | /// | |
95 | /// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`. | |
96 | fn join_codegen( | |
97 | &self, | |
98 | ongoing_codegen: Box<dyn Any>, | |
99 | sess: &Session, | |
a2a8927a | 100 | outputs: &OutputFilenames, |
29967ef6 | 101 | ) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported>; |
ba9703b0 XL |
102 | |
103 | /// This is called on the returned `Box<dyn Any>` from `join_codegen` | |
104 | /// | |
105 | /// # Panics | |
106 | /// | |
107 | /// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`. | |
108 | fn link( | |
109 | &self, | |
110 | sess: &Session, | |
29967ef6 | 111 | codegen_results: CodegenResults, |
ba9703b0 XL |
112 | outputs: &OutputFilenames, |
113 | ) -> Result<(), ErrorReported>; | |
114 | } | |
115 | ||
dfeec247 | 116 | pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync { |
dc9dc135 | 117 | fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module; |
dc9dc135 | 118 | fn codegen_allocator<'tcx>( |
9fa01778 | 119 | &self, |
dc9dc135 | 120 | tcx: TyCtxt<'tcx>, |
c295e0f8 XL |
121 | module_llvm: &mut Self::Module, |
122 | module_name: &str, | |
dc9dc135 | 123 | kind: AllocatorKind, |
29967ef6 | 124 | has_alloc_error_handler: bool, |
9fa01778 | 125 | ); |
dfeec247 XL |
126 | /// This generates the codegen unit and returns it along with |
127 | /// a `u64` giving an estimate of the unit's processing cost. | |
e74abb32 XL |
128 | fn compile_codegen_unit( |
129 | &self, | |
130 | tcx: TyCtxt<'_>, | |
131 | cgu_name: Symbol, | |
dfeec247 | 132 | ) -> (ModuleCodegen<Self::Module>, u64); |
a1dfa0c6 XL |
133 | fn target_machine_factory( |
134 | &self, | |
135 | sess: &Session, | |
9fa01778 | 136 | opt_level: config::OptLevel, |
fc512014 | 137 | ) -> TargetMachineFactoryFn<Self>; |
a1dfa0c6 | 138 | fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str; |
29967ef6 | 139 | fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>; |
3c0e092e XL |
140 | |
141 | fn spawn_thread<F, T>(_time_trace: bool, f: F) -> std::thread::JoinHandle<T> | |
142 | where | |
143 | F: FnOnce() -> T, | |
144 | F: Send + 'static, | |
145 | T: Send + 'static, | |
146 | { | |
147 | std::thread::spawn(f) | |
148 | } | |
149 | ||
150 | fn spawn_named_thread<F, T>( | |
151 | _time_trace: bool, | |
152 | name: String, | |
153 | f: F, | |
154 | ) -> std::io::Result<std::thread::JoinHandle<T>> | |
155 | where | |
156 | F: FnOnce() -> T, | |
157 | F: Send + 'static, | |
158 | T: Send + 'static, | |
159 | { | |
160 | std::thread::Builder::new().name(name).spawn(f) | |
161 | } | |
a1dfa0c6 | 162 | } |