Skip to content

Commit

Permalink
rusty: Add --online-change CLI flag
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Jul 30, 2024
1 parent 89b1822 commit fe43485
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 9 deletions.
6 changes: 6 additions & 0 deletions compiler/plc_driver/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ pub struct CompileParameters {
#[clap(name = "check", long, help = "Check only, do not generate any output", global = true)]
pub check_only: bool,

#[clap(
long,
help = "Emit a binary with specific compilation information, suitable for online changes when ran under a conforming runtime"
)]
pub online_change: bool,

#[clap(subcommand)]
pub commands: Option<SubCommands>,
}
Expand Down
11 changes: 9 additions & 2 deletions compiler/plc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use std::{
use cli::{CompileParameters, ParameterError, SubCommands};
use pipelines::AnnotatedProject;
use plc::{
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel,
ErrorFormat, OptimizationLevel, Target, Threads,
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat,
OnlineChange, OptimizationLevel, Target, Threads,
};

use plc_diagnostics::{diagnostician::Diagnostician, diagnostics::Diagnostic};
Expand Down Expand Up @@ -56,6 +56,7 @@ pub struct CompileOptions {
pub error_format: ErrorFormat,
pub debug_level: DebugLevel,
pub single_module: bool,
pub online_change: OnlineChange,
}

impl Default for CompileOptions {
Expand All @@ -71,6 +72,7 @@ impl Default for CompileOptions {
error_format: ErrorFormat::None,
debug_level: DebugLevel::None,
single_module: false,
online_change: OnlineChange::Disabled,
}
}
}
Expand Down Expand Up @@ -182,6 +184,11 @@ pub fn get_compilation_context<T: AsRef<str> + AsRef<OsStr> + Debug>(
error_format: compile_parameters.error_format,
debug_level: compile_parameters.debug_level(),
single_module: compile_parameters.single_module,
online_change: if compile_parameters.online_change {
OnlineChange::Enabled
} else {
OnlineChange::Disabled
},
};

let libraries =
Expand Down
1 change: 1 addition & 0 deletions compiler/plc_driver/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
compile_options.got_layout_file.clone().zip(compile_options.got_layout_format),
compile_options.optimization,
compile_options.debug_level,
compile_options.online_change,
);
//Create a types codegen, this contains all the type declarations
//Associate the index type with LLVM types
Expand Down
17 changes: 14 additions & 3 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use self::{
use crate::{
output::FormatOption,
resolver::{AstAnnotations, Dependency, StringLiterals},
ConfigFormat, DebugLevel, OptimizationLevel, Target,
ConfigFormat, DebugLevel, OnlineChange, OptimizationLevel, Target,
};

use super::index::*;
Expand Down Expand Up @@ -75,6 +75,8 @@ pub struct CodeGen<'ink> {
pub module: Module<'ink>,
/// the debugging module creates debug information at appropriate locations
pub debug: DebugBuilderEnum<'ink>,
/// Whether we are generating a hot-reloadable binary or not
pub online_change: OnlineChange,

pub got_layout_file: Option<(String, ConfigFormat)>,

Expand All @@ -98,11 +100,18 @@ impl<'ink> CodeGen<'ink> {
got_layout_file: Option<(String, ConfigFormat)>,
optimization_level: OptimizationLevel,
debug_level: DebugLevel,
online_change: OnlineChange,
) -> CodeGen<'ink> {
let module = context.create_module(module_location);
module.set_source_file_name(module_location);
let debug = debug::DebugBuilderEnum::new(context, &module, root, optimization_level, debug_level);
CodeGen { module, debug, got_layout_file, module_location: module_location.to_string() }
CodeGen {
module,
debug,
got_layout_file,
module_location: module_location.to_string(),
online_change,
}
}

pub fn generate_llvm_index(
Expand Down Expand Up @@ -224,6 +233,7 @@ impl<'ink> CodeGen<'ink> {
annotations,
&index,
&mut self.debug,
self.online_change,
)?;
let llvm = Llvm::new(context, context.create_builder());
index.merge(llvm_impl_index);
Expand Down Expand Up @@ -286,7 +296,8 @@ impl<'ink> CodeGen<'ink> {
) -> Result<GeneratedModule<'ink>, Diagnostic> {
//generate all pous
let llvm = Llvm::new(context, context.create_builder());
let pou_generator = PouGenerator::new(llvm, global_index, annotations, &llvm_index);
let pou_generator =
PouGenerator::new(llvm, global_index, annotations, &llvm_index, self.online_change);

//Generate the POU stubs in the first go to make sure they can be referenced.
for implementation in &unit.implementations {
Expand Down
14 changes: 10 additions & 4 deletions src/codegen/generators/pou_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
index::{self, ImplementationType},
resolver::{AstAnnotations, Dependency},
typesystem::{DataType, DataTypeInformation, VarArgs, DINT_TYPE},
OnlineChange,
};

/// The pou_generator contains functions to generate the code for POUs (PROGRAM, FUNCTION, FUNCTION_BLOCK)
Expand Down Expand Up @@ -49,6 +50,7 @@ pub struct PouGenerator<'ink, 'cg> {
index: &'cg Index,
annotations: &'cg AstAnnotations,
llvm_index: &'cg LlvmTypedIndex<'ink>,
online_change: OnlineChange,
}

/// Creates opaque implementations for all callable items in the index
Expand All @@ -61,9 +63,10 @@ pub fn generate_implementation_stubs<'ink>(
annotations: &AstAnnotations,
types_index: &LlvmTypedIndex<'ink>,
debug: &mut DebugBuilderEnum<'ink>,
online_change: OnlineChange,
) -> Result<LlvmTypedIndex<'ink>, Diagnostic> {
let mut llvm_index = LlvmTypedIndex::default();
let pou_generator = PouGenerator::new(llvm, index, annotations, types_index);
let pou_generator = PouGenerator::new(llvm, index, annotations, types_index, online_change);
let implementations = dependencies
.into_iter()
.filter_map(|it| {
Expand Down Expand Up @@ -150,8 +153,9 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
index: &'cg Index,
annotations: &'cg AstAnnotations,
llvm_index: &'cg LlvmTypedIndex<'ink>,
online_change: OnlineChange,
) -> PouGenerator<'ink, 'cg> {
PouGenerator { llvm, index, annotations, llvm_index }
PouGenerator { llvm, index, annotations, llvm_index, online_change }
}

fn mangle_function(&self, implementation: &ImplementationIndexEntry) -> Result<String, Diagnostic> {
Expand Down Expand Up @@ -286,8 +290,10 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {

let curr_f = module.add_function(implementation.get_call_name(), function_declaration, None);

let section_name = self.mangle_function(implementation)?;
curr_f.set_section(Some(&section_name));
if self.online_change == OnlineChange::Enabled {
let section_name = self.mangle_function(implementation)?;
curr_f.set_section(Some(&section_name));
}

let pou_name = implementation.get_call_name();
if let Some(pou) = self.index.find_pou(pou_name) {
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ pub enum DebugLevel {
Full(usize),
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum OnlineChange {
Enabled,
Disabled,
}

impl From<OptimizationLevel> for inkwell::OptimizationLevel {
fn from(val: OptimizationLevel) -> Self {
match val {
Expand Down
2 changes: 2 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub mod tests {
None,
crate::OptimizationLevel::None,
debug_level,
crate::OnlineChange::Disabled,
);
let annotations = AstAnnotations::new(annotations, id_provider.next_id());

Expand Down Expand Up @@ -239,6 +240,7 @@ pub mod tests {
None,
crate::OptimizationLevel::None,
debug_level,
crate::OnlineChange::Disabled,
);
let got_layout = Mutex::new(None);

Expand Down

0 comments on commit fe43485

Please sign in to comment.