Skip to content

feat: FB_INIT user code initialization #1458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 28, 2025
Merged

feat: FB_INIT user code initialization #1458

merged 13 commits into from
Apr 28, 2025

Conversation

mhasel
Copy link
Member

@mhasel mhasel commented Apr 9, 2025

This pull request introduces enhancements to the initialization logic for FUNCTION_BLOCKs and project-wide initialization. It also includes updates to unit tests, code generation and documentation to support these changes. Below is a summary of the most important changes grouped by theme:

Enhancements to FUNCTION_BLOCK Initialization

  • Added detailed documentation on implementing FB_INIT methods for FUNCTION_BLOCKs, including examples for both IEC61131-3 and external C implementations. This includes naming conventions, initialization patterns, and interoperability considerations. (book/src/libraries/api_lib_guide.md - [1] book/src/pous.md - [2]
  • Added documentation on project-wide initialization and its behavior and usage, including architecture-specific considerations. (book/src/using_rusty.md - [3])

Updates to Code Generation

  • Implemented the generation of user-defined initialization functions (e.g., __user_init_<FunctionBlockName>) for FUNCTION_BLOCK instances, ensuring proper initialization of nested structures and base classes in object-oriented scenarios.
  • Updated the project initializer function (__init___<projectname>) to invoke the corresponding user-defined initialization functions.

Test and Snapshot Updates

  • Added or updated snapshots to reflect the new initialization logic, including __user_init_<FunctionBlockName> function definitions and their integration into the project-wide initializer.
  • Adjusted unit tests to validate the correctness of the new initialization logic and ensure compatibility with inheritance and external implementations.

Code Refinements

@mhasel mhasel changed the title Fbinit feat: FB_INIT user code initialization Apr 9, 2025
Copy link

codecov bot commented Apr 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.13%. Comparing base (e731987) to head (c755d85).
Report is 63 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1458      +/-   ##
==========================================
+ Coverage   93.67%   94.13%   +0.46%     
==========================================
  Files         166      173       +7     
  Lines       50097    56801    +6704     
==========================================
+ Hits        46928    53470    +6542     
- Misses       3169     3331     +162     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mhasel mhasel force-pushed the fbinit branch 2 times, most recently from a6ea592 to 7096598 Compare April 22, 2025 08:48
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the initialization logic for FUNCTION_BLOCKs by introducing user-defined and project-wide initialization functions, updating tests and code generation, and expanding the documentation. Key changes include:

  • Updates to test expectations and assertions for initialization function bodies.
  • New logic in the lowering phase to generate and integrate user-defined initialization calls.
  • Documentation updates in multiple markdown files to explain FUNCTION_BLOCK initialization and interoperability considerations.

Reviewed Changes

Copilot reviewed 27 out of 34 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/resolver/tests/resolve_and_lower_init_functions.rs Updated test assertions to expect an additional call statement for user-defined initialization.
src/lowering/initializers.rs Introduced create_user_init_units and integrated user-defined init calls with implicit initializer assignments.
src/lowering.rs Added candidate collection for user initialization methods and integrated global user initialization calls.
src/codegen/tests/* Added definitions of _user_init* functions across various test cases for inheritance and debugging.
compiler/plc_project/src/project.rs Updated get_init_symbol_name to support dashes in project names.
Documentation (book/src/using_rusty.md, pous.md, api_lib_guide.md) Expanded documentation for project-wide and FUNCTION_BLOCK initialization, including interoperability with C implementations.
Files not reviewed (7)
  • compiler/plc_driver/src/tests/snapshots/plc_driver__tests__multi_files__multiple_files_in_different_locations_with_debug_info.snap: Language not supported
  • compiler/plc_driver/src/tests/snapshots/plc_driver__tests__multi_files__multiple_files_with_debug_info.snap: Language not supported
  • compiler/plc_driver/src/tests/snapshots/plc_driver__tests__multi_files__multiple_source_files_generated.snap: Language not supported
  • src/codegen/tests/debug_tests/snapshots/rusty__codegen__tests__debug_tests__expression_debugging__external_impl_is_not_added_as_external_subroutine.snap: Language not supported
  • src/codegen/tests/snapshots/rusty__codegen__tests__code_gen_tests__external_program_global_var_is_external.snap: Language not supported
  • src/codegen/tests/snapshots/rusty__codegen__tests__debug_tests__global_var_nested_struct_added_to_debug_info.snap: Language not supported
  • src/codegen/tests/snapshots/rusty__codegen__tests__debug_tests__global_var_struct_added_to_debug_info.snap: Language not supported
Comments suppressed due to low confidence (1)

src/lowering/initializers.rs:283

  • [nitpick] Consider aligning the naming of this initialization call with the user-defined initialization pattern by using get_user_init_fn_name (e.g. yielding "_user_init") or add a clarifying comment to distinguish this call from those generated for user-defined init functions.
let op = create_member_reference("fb_init", id_provider.clone(), Some(base));

@mhasel mhasel marked this pull request as ready for review April 22, 2025 09:06
@mhasel mhasel requested review from ghaith and volsa April 22, 2025 09:06
{
// add the struct to potential `STRUCT_INIT` candidates
if let Some(name) = user_type.data_type.get_name() {
self.user_inits.insert(name.to_string(), false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghaith If we want to support user-defined struct-init functions for FFI purposes, their addition should be trivial if we use a naming-scheme here. We can then just query the index if such a function is present and then add it to the hashmap to be generated/called.

@@ -33,6 +35,10 @@ impl InitVisitor {
init_symbol_name: &'static str,
) -> Vec<CompilationUnit> {
let mut visitor = Self::new(index, unresolvables, id_provider);
// before visiting, we need to collect all candidates for user-defined init functions
units.iter().for_each(|unit| {
visitor.collect_user_init_candidates(unit);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done outside of/before the regular visitor since the visitor might not pick up candidates in different compilation unit (the order of visiting would matter).

@@ -0,0 +1,7 @@
FUNCTION main
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a similar test in codegen?
I would like to see the case where we have a function using a function block, and that functionblock has a user_init be tested.
I expect the function, while initializing the function block call to (implicitly) call fb_init. I'm fine with adding this as a followup if we know the case works, but it would be good to express this in codegen tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a test for this in #1471

Copy link
Collaborator

@ghaith ghaith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expecting a test in the follow up PR, but otherwise looks good.

@mhasel mhasel merged commit f2f442e into master Apr 28, 2025
23 checks passed
@mhasel mhasel deleted the fbinit branch April 28, 2025 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants