diff --git a/compiler/plc_driver/src/cli.rs b/compiler/plc_driver/src/cli.rs index ca89e87434..74d029a59e 100644 --- a/compiler/plc_driver/src/cli.rs +++ b/compiler/plc_driver/src/cli.rs @@ -5,7 +5,8 @@ use encoding_rs::Encoding; use plc_diagnostics::diagnostics::{diagnostics_registry::DiagnosticsConfiguration, Diagnostic}; use std::{env, ffi::OsStr, num::ParseIntError, path::PathBuf}; -use plc::{output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat, Target, Threads}; +use plc::output::FormatOption; +use plc::{ConfigFormat, DebugLevel, ErrorFormat, Target, Threads, DEFAULT_GOT_LAYOUT_FILE}; pub type ParameterError = clap::Error; @@ -116,9 +117,11 @@ pub struct CompileParameters { Save information about the generated custom GOT layout to the given file. Format is detected by extension. Supported formats : json, toml", - parse(try_from_str = validate_config) + default_value = DEFAULT_GOT_LAYOUT_FILE, + parse(try_from_str = validate_config), + requires = "online-change" ) ] - pub got_layout_file: Option, + pub got_layout_file: String, #[clap( name = "optimization", @@ -405,8 +408,9 @@ impl CompileParameters { self.hardware_config.as_deref().and_then(get_config_format) } - pub fn got_layout_format(&self) -> Option { - self.got_layout_file.as_deref().and_then(get_config_format) + pub fn got_layout_format(&self) -> ConfigFormat { + // It is safe to unwrap here, since the provided argument to `--got-online-change` has been checked with `validate_config` + get_config_format(&self.got_layout_file).unwrap() } /// Returns the location where the build artifacts should be stored / output diff --git a/compiler/plc_driver/src/lib.rs b/compiler/plc_driver/src/lib.rs index 67460c6052..77b5d1ce68 100644 --- a/compiler/plc_driver/src/lib.rs +++ b/compiler/plc_driver/src/lib.rs @@ -20,7 +20,7 @@ use cli::{CompileParameters, ParameterError, SubCommands}; use pipelines::AnnotatedProject; use plc::{ codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat, - OnlineChange, OptimizationLevel, Target, Threads, + OnlineChange, OptimizationLevel, Target, Threads, DEFAULT_GOT_LAYOUT_FILE, }; use plc_diagnostics::{diagnostician::Diagnostician, diagnostics::Diagnostic}; @@ -50,8 +50,8 @@ pub struct CompileOptions { /// The name of the resulting compiled file pub output: String, pub output_format: FormatOption, - pub got_layout_file: Option, - pub got_layout_format: Option, + pub got_layout_file: String, + pub got_layout_format: ConfigFormat, pub optimization: OptimizationLevel, pub error_format: ErrorFormat, pub debug_level: DebugLevel, @@ -66,8 +66,8 @@ impl Default for CompileOptions { build_location: None, output: String::new(), output_format: Default::default(), - got_layout_file: None, - got_layout_format: None, + got_layout_file: String::from(DEFAULT_GOT_LAYOUT_FILE), + got_layout_format: ConfigFormat::JSON, optimization: OptimizationLevel::None, error_format: ErrorFormat::None, debug_level: DebugLevel::None, diff --git a/compiler/plc_driver/src/pipelines.rs b/compiler/plc_driver/src/pipelines.rs index c314d7794f..e62fd4e176 100644 --- a/compiler/plc_driver/src/pipelines.rs +++ b/compiler/plc_driver/src/pipelines.rs @@ -274,7 +274,7 @@ impl AnnotatedProject { context, compile_options.root.as_deref(), &unit.file_name, - compile_options.got_layout_file.clone().zip(compile_options.got_layout_format), + (compile_options.got_layout_file.clone(), compile_options.got_layout_format), compile_options.optimization, compile_options.debug_level, compile_options.online_change, diff --git a/src/codegen.rs b/src/codegen.rs index d9de0a9a6e..e4564c79a3 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -73,7 +73,7 @@ pub struct CodeGen<'ink> { /// Whether we are generating a hot-reloadable binary or not pub online_change: OnlineChange, - pub got_layout_file: Option<(String, ConfigFormat)>, + pub got_layout_file: (String, ConfigFormat), pub module_location: String, } @@ -92,7 +92,7 @@ impl<'ink> CodeGen<'ink> { context: &'ink CodegenContext, root: Option<&Path>, module_location: &str, - got_layout_file: Option<(String, ConfigFormat)>, + got_layout_file: (String, ConfigFormat), optimization_level: OptimizationLevel, debug_level: DebugLevel, online_change: OnlineChange, @@ -137,6 +137,7 @@ impl<'ink> CodeGen<'ink> { &index, &mut self.debug, self.got_layout_file.clone(), + self.online_change, ); //Generate global variables diff --git a/src/codegen/generators/variable_generator.rs b/src/codegen/generators/variable_generator.rs index 9bd3928c7a..519e3ab01e 100644 --- a/src/codegen/generators/variable_generator.rs +++ b/src/codegen/generators/variable_generator.rs @@ -5,7 +5,7 @@ use crate::{ codegen::{debug::Debug, llvm_index::LlvmTypedIndex, llvm_typesystem::cast_if_needed}, index::{get_initializer_name, Index, PouIndexEntry, VariableIndexEntry}, resolver::{AnnotationMap, AstAnnotations, Dependency}, - ConfigFormat, + ConfigFormat, OnlineChange, }; use indexmap::IndexSet; use inkwell::{module::Module, types::BasicTypeEnum, values::GlobalValue}; @@ -65,7 +65,8 @@ pub struct VariableGenerator<'ctx, 'b> { annotations: &'b AstAnnotations, types_index: &'b LlvmTypedIndex<'ctx>, debug: &'b mut DebugBuilderEnum<'ctx>, - got_layout_file: Option<(String, ConfigFormat)>, + got_layout_file: (String, ConfigFormat), + online_change: OnlineChange, } impl<'ctx, 'b> VariableGenerator<'ctx, 'b> { @@ -76,9 +77,19 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> { annotations: &'b AstAnnotations, types_index: &'b LlvmTypedIndex<'ctx>, debug: &'b mut DebugBuilderEnum<'ctx>, - got_layout_file: Option<(String, ConfigFormat)>, + got_layout_file: (String, ConfigFormat), + online_change: OnlineChange, ) -> Self { - VariableGenerator { module, llvm, global_index, annotations, types_index, debug, got_layout_file } + VariableGenerator { + module, + llvm, + global_index, + annotations, + types_index, + debug, + got_layout_file, + online_change, + } } pub fn generate_global_variables( @@ -140,7 +151,9 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> { ); } - if let Some((location, format)) = &self.got_layout_file { + if self.online_change == OnlineChange::Enabled { + let (location, format) = &self.got_layout_file; + let got_entries = read_got_layout(location.as_str(), *format)?; let mut new_globals = Vec::new(); let mut new_got_entries = HashMap::new(); diff --git a/src/lib.rs b/src/lib.rs index 715c1639b1..468eb0baea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,6 +143,8 @@ impl FromStr for ConfigFormat { } } +pub const DEFAULT_GOT_LAYOUT_FILE: &str = "online_change_got.json"; + #[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum, Serialize, Deserialize, Default)] pub enum ErrorFormat { #[default]