|
| 1 | +//! The initializer lowering module is responsible for additing initialization logic |
| 2 | +//! to PLC AST nodes. This includes generating default values for variables, handling |
| 3 | +//! constant expressions, and ensuring that all necessary initializations are present |
| 4 | +//! before code generation. The module traverses the AST and modifies nodes as needed |
| 5 | +//! to include initialization code, making sure that the resulting AST is ready for |
| 6 | +//! further compilation stages. |
| 7 | +//! Initialization logic is as follows: |
| 8 | +//! - Every struct(and POU) has an optional implicit initializer for constant fields. |
| 9 | +//! - The name for this initializer is `<StructName>_init` |
| 10 | +//! - The field is always private to the module |
| 11 | +//! - Every struct(and POU) has a constructor for fields with pointer assignments or non-constant values |
| 12 | +//! - The name for this constructor is `<StructName>_ctor` |
| 13 | +//! - The constructor is always public and implicitly uses the implicit initializer |
| 14 | +//! - Variables of the struct are initialized using the implicit initializer if their value is |
| 15 | +//! derived at compile time, otherwise the constructor is used. |
| 16 | +//! - Global variables are initialized in a global constructor function called `__global_ctor` |
| 17 | +//! - This function is called per module inside the static initialization code |
| 18 | +//! - The function is private to the module |
| 19 | +//! - Stateless POUs (functions and methods) are initialized during their call. |
| 20 | +//! - Variables of a stateless POU of a struct type are initialized using the constructor call. |
| 21 | +//! - External POUs and struct constructors are marked as `extern` and have no body. |
| 22 | +//! - External variables are not re-initialized in the global constructor, they are assumed to be |
| 23 | +//! initialized externally. |
| 24 | +//! - Bulit-in types and variables are not re-initialized in the global |
| 25 | +//! constructor. |
| 26 | +
|
| 27 | +use plc::index::{FxIndexMap, Index}; |
| 28 | +use plc_ast::{ast::AstNode, visitor::AstVisitor}; |
| 29 | + |
| 30 | +enum Body { |
| 31 | + Internal(Vec<AstNode>), |
| 32 | + External, |
| 33 | + None, |
| 34 | +} |
| 35 | + |
| 36 | +pub struct Initializer<'idx> { |
| 37 | + index: &'idx Index, |
| 38 | + implicit_initializers: FxIndexMap<String, Body>, |
| 39 | + constructors: FxIndexMap<String, Body>, |
| 40 | + global_constructor: Vec<AstNode>, |
| 41 | +} |
| 42 | + |
| 43 | +//TODO: might need to be a mutable ast visitor |
| 44 | +impl AstVisitor for Initializer<'_> { |
| 45 | + fn visit_compilation_unit(&mut self, unit: &plc_ast::ast::CompilationUnit) { |
| 46 | + // Read all structs and POU structs, collect their implicit initializers if available |
| 47 | + unit.pous.iter().for_each(|pou| { |
| 48 | + // find the pou index entry |
| 49 | + if let Some(pie) = self.index.find_pou_type(&pou.name) { |
| 50 | + pie.initial_value |
| 51 | + } |
| 52 | + }); |
| 53 | + // Add a call to the constructor to memcpy the imlicit initializer |
| 54 | + // For each of the call statement or reference in the pou initializer, add an assignment to |
| 55 | + // the constructor |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl Initializer<'_> { |
| 60 | + pub fn new(index: &Index) -> Initializer<'_> { |
| 61 | + Initializer { |
| 62 | + index, |
| 63 | + implicit_initializers: FxIndexMap::default(), |
| 64 | + constructors: FxIndexMap::default(), |
| 65 | + global_constructor: Vec::new(), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +mod tests { |
| 71 | + #[test] |
| 72 | + fn struct_gets_imlicit_initializer_and_constructor() {} |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn struct_gets_constructor() {} |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn nested_structs_get_initializers_and_constructors() {} |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn variable_with_pointer_initializer_is_added_to_constructor() {} |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn enum_default_values_in_struct() {} |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn nested_struct_with_different_default_values() {} |
| 88 | +} |
0 commit comments