Skip to content

Commit

Permalink
cli: Make --got-layout-file override the default GOT layout file loca…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
CohenArthur committed Jul 30, 2024
1 parent db1140f commit daa7fad
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
14 changes: 9 additions & 5 deletions compiler/plc_driver/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String>,
pub got_layout_file: String,

#[clap(
name = "optimization",
Expand Down Expand Up @@ -405,8 +408,9 @@ impl CompileParameters {
self.hardware_config.as_deref().and_then(get_config_format)
}

pub fn got_layout_format(&self) -> Option<ConfigFormat> {
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
Expand Down
10 changes: 5 additions & 5 deletions compiler/plc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<String>,
pub got_layout_format: Option<ConfigFormat>,
pub got_layout_file: String,
pub got_layout_format: ConfigFormat,
pub optimization: OptimizationLevel,
pub error_format: ErrorFormat,
pub debug_level: DebugLevel,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_driver/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
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,
Expand Down
5 changes: 3 additions & 2 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -137,6 +137,7 @@ impl<'ink> CodeGen<'ink> {
&index,
&mut self.debug,
self.got_layout_file.clone(),
self.online_change,
);

//Generate global variables
Expand Down
23 changes: 18 additions & 5 deletions src/codegen/generators/variable_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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> {
Expand All @@ -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(
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit daa7fad

Please sign in to comment.