55 associated_type_bounds,
66 never_type,
77 try_blocks,
8- hash_drain_filter
8+ hash_drain_filter,
9+ str_split_once
910) ]
1011#![ warn( rust_2018_idioms) ]
1112#![ warn( unused_lifetimes) ]
@@ -34,6 +35,7 @@ extern crate rustc_target;
3435extern crate rustc_driver;
3536
3637use std:: any:: Any ;
38+ use std:: str:: FromStr ;
3739
3840use rustc_codegen_ssa:: traits:: CodegenBackend ;
3941use rustc_codegen_ssa:: CodegenResults ;
@@ -172,12 +174,53 @@ impl<'tcx, M: Module> CodegenCx<'tcx, M> {
172174}
173175
174176#[ derive( Copy , Clone , Debug ) ]
177+ pub enum CodegenMode {
178+ Aot ,
179+ Jit ,
180+ }
181+
182+ impl Default for CodegenMode {
183+ fn default ( ) -> Self {
184+ CodegenMode :: Aot
185+ }
186+ }
187+
188+ impl FromStr for CodegenMode {
189+ type Err = String ;
190+
191+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
192+ match s {
193+ "aot" => Ok ( CodegenMode :: Aot ) ,
194+ "jit" => Ok ( CodegenMode :: Jit ) ,
195+ _ => Err ( format ! ( "Unknown codegen mode `{}`" , s) ) ,
196+ }
197+ }
198+ }
199+
200+ #[ derive( Copy , Clone , Debug , Default ) ]
175201pub struct BackendConfig {
176- pub use_jit : bool ,
202+ pub codegen_mode : CodegenMode ,
203+ }
204+
205+ impl BackendConfig {
206+ fn from_opts ( opts : & [ String ] ) -> Result < Self , String > {
207+ let mut config = BackendConfig :: default ( ) ;
208+ for opt in opts {
209+ if let Some ( ( name, value) ) = opt. split_once ( '=' ) {
210+ match name {
211+ "mode" => config. codegen_mode = value. parse ( ) ?,
212+ _ => return Err ( format ! ( "Unknown option `{}`" , name) ) ,
213+ }
214+ } else {
215+ return Err ( format ! ( "Invalid option `{}`" , opt) ) ;
216+ }
217+ }
218+ Ok ( config)
219+ }
177220}
178221
179222pub struct CraneliftCodegenBackend {
180- pub config : BackendConfig ,
223+ pub config : Option < BackendConfig > ,
181224}
182225
183226impl CodegenBackend for CraneliftCodegenBackend {
@@ -204,7 +247,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
204247 metadata : EncodedMetadata ,
205248 need_metadata_module : bool ,
206249 ) -> Box < dyn Any > {
207- let res = driver:: codegen_crate ( tcx, metadata, need_metadata_module, self . config ) ;
250+ let config = if let Some ( config) = self . config {
251+ config
252+ } else {
253+ BackendConfig :: from_opts ( & tcx. sess . opts . cg . llvm_args )
254+ . unwrap_or_else ( |err| tcx. sess . fatal ( & err) )
255+ } ;
256+ let res = driver:: codegen_crate ( tcx, metadata, need_metadata_module, config) ;
208257
209258 rustc_symbol_mangling:: test:: report_symbol_names ( tcx) ;
210259
@@ -305,7 +354,5 @@ fn build_isa(sess: &Session) -> Box<dyn isa::TargetIsa + 'static> {
305354/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
306355#[ no_mangle]
307356pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
308- Box :: new ( CraneliftCodegenBackend {
309- config : BackendConfig { use_jit : false } ,
310- } )
357+ Box :: new ( CraneliftCodegenBackend { config : None } )
311358}
0 commit comments