rusty icon indicating copy to clipboard operation
rusty copied to clipboard

feat: init functions for address-initialization

Open mhasel opened this issue 1 year ago • 0 comments

This is the first in a series of PRs to enable support for complex initializers (e.g. initializing pointers with hardware addresses/addresses not known before allocation).

This is achieved by lowering these initializers into init-functions for each POU (FUNCTION_BLOCK foo => __init_foo). Global variables which need initialization are then collected along with init functions for global instances and wrapped in a single __init function, meant to be called by a runtime before starting execution of the main task or, alternatively, called before main. For a more detailed description see tests in src/tests/adr/initializer_functions_adr.rs.

Implemented with this PR:

  • generate __init_<pou> and __init functions for programs and function blocks
  • add support for REFERENCE TO and x AT y in addition to regular REF_TO/POINTER_TO initializers
  • initialize global variables in __init body (except for struct-types)
  • change ADR builtin to behave like REF when assigning the result to a pointer

Since this PR is already quite large, the following features/fixes will be committed separately:

  • support for non-POU STRUCT types
  • pointer references to local members
  • stack allocated variable support (VAR_TEMP, functions)
  • extend init functionality to generate initializers and subsequent __init_<pou> calls for each POU instead of relying on the const-evaluator
  • complex initializers (deeply nested aggregate types, e.g. ARRAY OF STRUCT, ...)
  • compiler flag to call __init before main (use without a dedicated runtime)
  • add section to book
  • additional validation safe-guards

Currently the order in which POUs are declared matters under specific circumstances: when nesting POUs with implicit initializers this can lead to missed instances and will be fixed in a later commit.

In order to support alias pointers (x AT y) their implementation has changed. They are no longer annotated with a StatementAnnotation::ReplacementAst and therefore some of the previously written tests (especially in src/resolver/tests/lowering.rs) might be obsolete now.

mhasel avatar Jul 09 '24 08:07 mhasel