From f59d45805f425cca16dafa3087cdcf6912006bf8 Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 12 Sep 2023 14:41:34 +0800 Subject: [PATCH] standalone: Add command line flags for target properties For testing codegen for different platforms on the host system. --- nac3standalone/src/main.rs | 40 ++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index d3337c808..f1a216002 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -49,6 +49,18 @@ struct CommandLineArgs { /// Whether to emit LLVM IR at the end of every module. #[arg(long, default_value_t = false)] emit_llvm: bool, + + /// The target triple to compile for. + #[arg(long)] + triple: Option, + + /// The target CPU to compile for. + #[arg(long)] + mcpu: Option, + + /// Additional target features to enable/disable, specified using the `+`/`-` prefixes. + #[arg(long)] + target_features: Option, } fn handle_typevar_definition( @@ -177,7 +189,24 @@ fn handle_assignment_pattern( fn main() { let cli = CommandLineArgs::parse(); - let CommandLineArgs { file_name, threads, opt_level, emit_llvm } = cli; + let CommandLineArgs { + file_name, + threads, + opt_level, + emit_llvm, + triple, + mcpu, + target_features, + } = cli; + + Target::initialize_all(&InitializationConfig::default()); + + let host_target_machine = CodeGenTargetMachineOptions::from_host(); + let triple = triple.unwrap_or(host_target_machine.triple.clone()); + let mcpu = mcpu + .map(|arg| if arg == "native" { host_target_machine.cpu.clone() } else { arg }) + .unwrap_or_default(); + let target_features = target_features.unwrap_or_default(); let opt_level = match opt_level { 0 => OptimizationLevel::None, 1 => OptimizationLevel::Less, @@ -186,8 +215,6 @@ fn main() { _ => OptimizationLevel::Aggressive, }; - Target::initialize_all(&InitializationConfig::default()); - let program = match fs::read_to_string(file_name.clone()) { Ok(program) => program, Err(err) => { @@ -272,7 +299,12 @@ fn main() { let llvm_options = CodeGenLLVMOptions { opt_level, - target: CodeGenTargetMachineOptions::from_host_triple(), + target: CodeGenTargetMachineOptions { + triple, + cpu: mcpu, + features: target_features, + ..host_target_machine + }, emit_llvm, };