Skip to content

Commit fe43485

Browse files
committed
rusty: Add --online-change CLI flag
1 parent 89b1822 commit fe43485

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

compiler/plc_driver/src/cli.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ pub struct CompileParameters {
213213
#[clap(name = "check", long, help = "Check only, do not generate any output", global = true)]
214214
pub check_only: bool,
215215

216+
#[clap(
217+
long,
218+
help = "Emit a binary with specific compilation information, suitable for online changes when ran under a conforming runtime"
219+
)]
220+
pub online_change: bool,
221+
216222
#[clap(subcommand)]
217223
pub commands: Option<SubCommands>,
218224
}

compiler/plc_driver/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use std::{
1919
use cli::{CompileParameters, ParameterError, SubCommands};
2020
use pipelines::AnnotatedProject;
2121
use plc::{
22-
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel,
23-
ErrorFormat, OptimizationLevel, Target, Threads,
22+
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat,
23+
OnlineChange, OptimizationLevel, Target, Threads,
2424
};
2525

2626
use plc_diagnostics::{diagnostician::Diagnostician, diagnostics::Diagnostic};
@@ -56,6 +56,7 @@ pub struct CompileOptions {
5656
pub error_format: ErrorFormat,
5757
pub debug_level: DebugLevel,
5858
pub single_module: bool,
59+
pub online_change: OnlineChange,
5960
}
6061

6162
impl Default for CompileOptions {
@@ -71,6 +72,7 @@ impl Default for CompileOptions {
7172
error_format: ErrorFormat::None,
7273
debug_level: DebugLevel::None,
7374
single_module: false,
75+
online_change: OnlineChange::Disabled,
7476
}
7577
}
7678
}
@@ -182,6 +184,11 @@ pub fn get_compilation_context<T: AsRef<str> + AsRef<OsStr> + Debug>(
182184
error_format: compile_parameters.error_format,
183185
debug_level: compile_parameters.debug_level(),
184186
single_module: compile_parameters.single_module,
187+
online_change: if compile_parameters.online_change {
188+
OnlineChange::Enabled
189+
} else {
190+
OnlineChange::Disabled
191+
},
185192
};
186193

187194
let libraries =

compiler/plc_driver/src/pipelines.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
330330
compile_options.got_layout_file.clone().zip(compile_options.got_layout_format),
331331
compile_options.optimization,
332332
compile_options.debug_level,
333+
compile_options.online_change,
333334
);
334335
//Create a types codegen, this contains all the type declarations
335336
//Associate the index type with LLVM types

src/codegen.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use self::{
2121
use crate::{
2222
output::FormatOption,
2323
resolver::{AstAnnotations, Dependency, StringLiterals},
24-
ConfigFormat, DebugLevel, OptimizationLevel, Target,
24+
ConfigFormat, DebugLevel, OnlineChange, OptimizationLevel, Target,
2525
};
2626

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

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

@@ -98,11 +100,18 @@ impl<'ink> CodeGen<'ink> {
98100
got_layout_file: Option<(String, ConfigFormat)>,
99101
optimization_level: OptimizationLevel,
100102
debug_level: DebugLevel,
103+
online_change: OnlineChange,
101104
) -> CodeGen<'ink> {
102105
let module = context.create_module(module_location);
103106
module.set_source_file_name(module_location);
104107
let debug = debug::DebugBuilderEnum::new(context, &module, root, optimization_level, debug_level);
105-
CodeGen { module, debug, got_layout_file, module_location: module_location.to_string() }
108+
CodeGen {
109+
module,
110+
debug,
111+
got_layout_file,
112+
module_location: module_location.to_string(),
113+
online_change,
114+
}
106115
}
107116

108117
pub fn generate_llvm_index(
@@ -224,6 +233,7 @@ impl<'ink> CodeGen<'ink> {
224233
annotations,
225234
&index,
226235
&mut self.debug,
236+
self.online_change,
227237
)?;
228238
let llvm = Llvm::new(context, context.create_builder());
229239
index.merge(llvm_impl_index);
@@ -286,7 +296,8 @@ impl<'ink> CodeGen<'ink> {
286296
) -> Result<GeneratedModule<'ink>, Diagnostic> {
287297
//generate all pous
288298
let llvm = Llvm::new(context, context.create_builder());
289-
let pou_generator = PouGenerator::new(llvm, global_index, annotations, &llvm_index);
299+
let pou_generator =
300+
PouGenerator::new(llvm, global_index, annotations, &llvm_index, self.online_change);
290301

291302
//Generate the POU stubs in the first go to make sure they can be referenced.
292303
for implementation in &unit.implementations {

src/codegen/generators/pou_generator.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
index::{self, ImplementationType},
1717
resolver::{AstAnnotations, Dependency},
1818
typesystem::{DataType, DataTypeInformation, VarArgs, DINT_TYPE},
19+
OnlineChange,
1920
};
2021

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

5456
/// Creates opaque implementations for all callable items in the index
@@ -61,9 +63,10 @@ pub fn generate_implementation_stubs<'ink>(
6163
annotations: &AstAnnotations,
6264
types_index: &LlvmTypedIndex<'ink>,
6365
debug: &mut DebugBuilderEnum<'ink>,
66+
online_change: OnlineChange,
6467
) -> Result<LlvmTypedIndex<'ink>, Diagnostic> {
6568
let mut llvm_index = LlvmTypedIndex::default();
66-
let pou_generator = PouGenerator::new(llvm, index, annotations, types_index);
69+
let pou_generator = PouGenerator::new(llvm, index, annotations, types_index, online_change);
6770
let implementations = dependencies
6871
.into_iter()
6972
.filter_map(|it| {
@@ -150,8 +153,9 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
150153
index: &'cg Index,
151154
annotations: &'cg AstAnnotations,
152155
llvm_index: &'cg LlvmTypedIndex<'ink>,
156+
online_change: OnlineChange,
153157
) -> PouGenerator<'ink, 'cg> {
154-
PouGenerator { llvm, index, annotations, llvm_index }
158+
PouGenerator { llvm, index, annotations, llvm_index, online_change }
155159
}
156160

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

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

289-
let section_name = self.mangle_function(implementation)?;
290-
curr_f.set_section(Some(&section_name));
293+
if self.online_change == OnlineChange::Enabled {
294+
let section_name = self.mangle_function(implementation)?;
295+
curr_f.set_section(Some(&section_name));
296+
}
291297

292298
let pou_name = implementation.get_call_name();
293299
if let Some(pou) = self.index.find_pou(pou_name) {

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ pub enum DebugLevel {
176176
Full(usize),
177177
}
178178

179+
#[derive(Debug, Copy, Clone, PartialEq)]
180+
pub enum OnlineChange {
181+
Enabled,
182+
Disabled,
183+
}
184+
179185
impl From<OptimizationLevel> for inkwell::OptimizationLevel {
180186
fn from(val: OptimizationLevel) -> Self {
181187
match val {

src/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ pub mod tests {
164164
None,
165165
crate::OptimizationLevel::None,
166166
debug_level,
167+
crate::OnlineChange::Disabled,
167168
);
168169
let annotations = AstAnnotations::new(annotations, id_provider.next_id());
169170

@@ -239,6 +240,7 @@ pub mod tests {
239240
None,
240241
crate::OptimizationLevel::None,
241242
debug_level,
243+
crate::OnlineChange::Disabled,
242244
);
243245
let got_layout = Mutex::new(None);
244246

0 commit comments

Comments
 (0)