Skip to content

ARRAY OF STRUCT wrongly initialized when only one element is given #929

@mhasel

Description

@mhasel
Member

Given the following example

PROGRAM mainProg
    VAR
        incorrect : ARRAY[0..1] OF STRUCT2 := [(x1 := TRUE, x2 := 128, x3 := 12, x4 := 421)]; // wrong initializer
        correct : ARRAY[0..1] OF STRUCT2 := [(x1 := TRUE, x2 := 128), (x1 := TRUE, x2 := 128)]; // correct initializer
    END_VAR
END_PROGRAM

    TYPE STRUCT2 :
        STRUCT
            x1 : BOOL;
            x2 : DINT;
            x3 : DINT;
            x4 : DINT;
        END_STRUCT
    END_TYPE

our generated IR differs significantly between the two variables. Here is what we generate, when multiple (at least 2) array members are specified:

llvm_value: "[2 x %STRUCT2] [%STRUCT2 { i8 1, i32 128, i32 0, i32 0 }, %STRUCT2 { i8 1, i32 128, i32 0, i32 0 }]",

This is what happens if we only specify 1 element:

llvm_value: "[4 x %STRUCT2] [%STRUCT2 { i8 1, i32 0, i32 0, i32 0 }, %STRUCT2 { i8 0, i32 128, i32 0, i32 0 }, %STRUCT2 { i8 0, i32 0, i32 12, i32 0 }, %STRUCT2 { i8 0, i32 0, i32 0, i32 421 }]",

Not only is the single element no longer initialized as we'd expect, the llvm array value now also magically has a length of 4 instead of the declared length of 2.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcodegen

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @volsa@mhasel

        Issue actions

          ARRAY OF STRUCT wrongly initialized when only one element is given · Issue #929 · PLC-lang/rusty