Skip to content

Commit ab51e24

Browse files
committed
rusty: Add --online-change CLI flag
1 parent 450566c commit ab51e24

File tree

7 files changed

+43
-10
lines changed

7 files changed

+43
-10
lines changed

compiler/plc_driver/src/cli.rs

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

196+
#[clap(
197+
long,
198+
help = "Emit a binary with specific compilation information, suitable for online changes when ran under a conforming runtime"
199+
)]
200+
pub online_change: bool,
201+
196202
#[clap(subcommand)]
197203
pub commands: Option<SubCommands>,
198204
}

compiler/plc_driver/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use cli::{CompileParameters, ParameterError, SubCommands};
2020
use pipelines::AnnotatedProject;
2121
use plc::{
2222
codegen::CodegenContext, linker::LinkerType, output::FormatOption, DebugLevel, ErrorFormat,
23-
OptimizationLevel, Target, Threads,
23+
OptimizationLevel, Target, Threads, OnlineChange,
2424
};
2525

2626
use plc_diagnostics::{diagnostician::Diagnostician, diagnostics::Diagnostic};
@@ -54,6 +54,7 @@ pub struct CompileOptions {
5454
pub error_format: ErrorFormat,
5555
pub debug_level: DebugLevel,
5656
pub single_module: bool,
57+
pub online_change: OnlineChange,
5758
}
5859

5960
impl Default for CompileOptions {
@@ -67,6 +68,7 @@ impl Default for CompileOptions {
6768
error_format: ErrorFormat::None,
6869
debug_level: DebugLevel::None,
6970
single_module: false,
71+
online_change: OnlineChange::Disabled,
7072
}
7173
}
7274
}
@@ -176,6 +178,11 @@ pub fn get_compilation_context<T: AsRef<str> + AsRef<OsStr> + Debug>(
176178
error_format: compile_parameters.error_format,
177179
debug_level: compile_parameters.debug_level(),
178180
single_module: compile_parameters.single_module,
181+
online_change: if compile_parameters.online_change {
182+
OnlineChange::Enabled
183+
} else {
184+
OnlineChange::Disabled
185+
},
179186
};
180187

181188
let libraries =

compiler/plc_driver/src/pipelines.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
276276
&unit.file_name,
277277
compile_options.optimization,
278278
compile_options.debug_level,
279+
compile_options.online_change,
279280
);
280281
//Create a types codegen, this contains all the type declarations
281282
//Associate the index type with LLVM types

src/codegen.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ use self::{
1313
llvm::{GlobalValueExt, Llvm},
1414
pou_generator::{self, PouGenerator},
1515
variable_generator::VariableGenerator,
16-
},
17-
llvm_index::LlvmTypedIndex,
16+
},
17+
llvm_index::LlvmTypedIndex,
1818
};
1919
use crate::{
2020
output::FormatOption,
2121
resolver::{AstAnnotations, Dependency, StringLiterals},
22-
DebugLevel, OptimizationLevel, Target,
22+
DebugLevel, OptimizationLevel, Target, OnlineChange
2323
};
2424

2525
use super::index::*;
@@ -70,6 +70,8 @@ pub struct CodeGen<'ink> {
7070
pub module: Module<'ink>,
7171
/// the debugging module creates debug information at appropriate locations
7272
pub debug: DebugBuilderEnum<'ink>,
73+
/// Whether we are generating a hot-reloadable binary or not
74+
pub online_change: OnlineChange,
7375

7476
pub module_location: String,
7577
}
@@ -90,11 +92,12 @@ impl<'ink> CodeGen<'ink> {
9092
module_location: &str,
9193
optimization_level: OptimizationLevel,
9294
debug_level: DebugLevel,
95+
online_change: OnlineChange,
9396
) -> CodeGen<'ink> {
9497
let module = context.create_module(module_location);
9598
module.set_source_file_name(module_location);
9699
let debug = debug::DebugBuilderEnum::new(context, &module, root, optimization_level, debug_level);
97-
CodeGen { module, debug, module_location: module_location.to_string() }
100+
CodeGen { module, debug, module_location: module_location.to_string(), online_change }
98101
}
99102

100103
pub fn generate_llvm_index(
@@ -135,6 +138,7 @@ impl<'ink> CodeGen<'ink> {
135138
annotations,
136139
&index,
137140
&mut self.debug,
141+
self.online_change,
138142
)?;
139143
let llvm = Llvm::new(context, context.create_builder());
140144
index.merge(llvm_impl_index);
@@ -197,7 +201,8 @@ impl<'ink> CodeGen<'ink> {
197201
) -> Result<GeneratedModule<'ink>, Diagnostic> {
198202
//generate all pous
199203
let llvm = Llvm::new(context, context.create_builder());
200-
let pou_generator = PouGenerator::new(llvm, global_index, annotations, llvm_index);
204+
let pou_generator =
205+
PouGenerator::new(llvm, global_index, annotations, llvm_index, self.online_change);
201206

202207
//Generate the POU stubs in the first go to make sure they can be referenced.
203208
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| {
@@ -149,8 +152,9 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
149152
index: &'cg Index,
150153
annotations: &'cg AstAnnotations,
151154
llvm_index: &'cg LlvmTypedIndex<'ink>,
155+
online_change: OnlineChange,
152156
) -> PouGenerator<'ink, 'cg> {
153-
PouGenerator { llvm, index, annotations, llvm_index }
157+
PouGenerator { llvm, index, annotations, llvm_index, online_change }
154158
}
155159

156160
fn mangle_function(&self, implementation: &ImplementationIndexEntry) -> String {
@@ -281,8 +285,10 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
281285

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

284-
let section_name = self.mangle_function(implementation);
285-
curr_f.set_section(Some(&section_name));
288+
if self.online_change == OnlineChange::Enabled {
289+
let section_name = self.mangle_function(implementation);
290+
curr_f.set_section(Some(&section_name));
291+
}
286292

287293
let pou_name = implementation.get_call_name();
288294
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
@@ -163,6 +163,7 @@ pub mod tests {
163163
"main",
164164
crate::OptimizationLevel::None,
165165
debug_level,
166+
crate::OnlineChange::Disabled,
166167
);
167168
let annotations = AstAnnotations::new(annotations, id_provider.next_id());
168169
let llvm_index = code_generator
@@ -234,6 +235,7 @@ pub mod tests {
234235
&unit.file_name,
235236
crate::OptimizationLevel::None,
236237
debug_level,
238+
crate::OnlineChange::Disabled,
237239
);
238240
let llvm_index = code_generator.generate_llvm_index(
239241
context,

0 commit comments

Comments
 (0)