@@ -305,6 +305,43 @@ impl ToJson for RelocModel {
305305 }
306306}
307307
308+ #[ derive( Clone , Copy , PartialEq , Hash , Debug ) ]
309+ pub enum CodeModel {
310+ Tiny ,
311+ Small ,
312+ Kernel ,
313+ Medium ,
314+ Large ,
315+ }
316+
317+ impl FromStr for CodeModel {
318+ type Err = ( ) ;
319+
320+ fn from_str ( s : & str ) -> Result < CodeModel , ( ) > {
321+ Ok ( match s {
322+ // "tiny" => CodeModel::Tiny, // Not exposed to users right now.
323+ "small" => CodeModel :: Small ,
324+ "kernel" => CodeModel :: Kernel ,
325+ "medium" => CodeModel :: Medium ,
326+ "large" => CodeModel :: Large ,
327+ _ => return Err ( ( ) ) ,
328+ } )
329+ }
330+ }
331+
332+ impl ToJson for CodeModel {
333+ fn to_json ( & self ) -> Json {
334+ match * self {
335+ CodeModel :: Tiny => "tiny" ,
336+ CodeModel :: Small => "small" ,
337+ CodeModel :: Kernel => "kernel" ,
338+ CodeModel :: Medium => "medium" ,
339+ CodeModel :: Large => "large" ,
340+ }
341+ . to_json ( )
342+ }
343+ }
344+
308345#[ derive( Clone , Copy , PartialEq , Hash , Debug ) ]
309346pub enum TlsModel {
310347 GeneralDynamic ,
@@ -699,7 +736,8 @@ pub struct TargetOptions {
699736 /// -relocation-model=$relocation_model`. Defaults to `Pic`.
700737 pub relocation_model : RelocModel ,
701738 /// Code model to use. Corresponds to `llc -code-model=$code_model`.
702- pub code_model : Option < String > ,
739+ /// Defaults to `None` which means "inherited from the base LLVM target".
740+ pub code_model : Option < CodeModel > ,
703741 /// TLS model to use. Options are "global-dynamic" (default), "local-dynamic", "initial-exec"
704742 /// and "local-exec". This is similar to the -ftls-model option in GCC/Clang.
705743 pub tls_model : TlsModel ,
@@ -1114,6 +1152,18 @@ impl Target {
11141152 Some ( Ok ( ( ) ) )
11151153 } ) ) . unwrap_or( Ok ( ( ) ) )
11161154 } ) ;
1155+ ( $key_name: ident, CodeModel ) => ( {
1156+ let name = ( stringify!( $key_name) ) . replace( "_" , "-" ) ;
1157+ obj. find( & name[ ..] ) . and_then( |o| o. as_string( ) . and_then( |s| {
1158+ match s. parse:: <CodeModel >( ) {
1159+ Ok ( code_model) => base. options. $key_name = Some ( code_model) ,
1160+ _ => return Some ( Err ( format!( "'{}' is not a valid code model. \
1161+ Run `rustc --print code-models` to \
1162+ see the list of supported values.", s) ) ) ,
1163+ }
1164+ Some ( Ok ( ( ) ) )
1165+ } ) ) . unwrap_or( Ok ( ( ) ) )
1166+ } ) ;
11171167 ( $key_name: ident, TlsModel ) => ( {
11181168 let name = ( stringify!( $key_name) ) . replace( "_" , "-" ) ;
11191169 obj. find( & name[ ..] ) . and_then( |o| o. as_string( ) . and_then( |s| {
@@ -1266,7 +1316,7 @@ impl Target {
12661316 key ! ( only_cdylib, bool ) ;
12671317 key ! ( executables, bool ) ;
12681318 key ! ( relocation_model, RelocModel ) ?;
1269- key ! ( code_model, optional ) ;
1319+ key ! ( code_model, CodeModel ) ? ;
12701320 key ! ( tls_model, TlsModel ) ?;
12711321 key ! ( disable_redzone, bool ) ;
12721322 key ! ( eliminate_frame_pointer, bool ) ;
0 commit comments