Skip to content

Codegen: FB invocation does not generate input assignments #192

Description

@Jan-Bulthuis

Problem

When an instance of a function block is stored in a VAR_GLOBAL, invoking it seems to have the incorrect codegen. As a result, the final C++ file does not compile.

Impact

  • STruCpp states that compilation is succesfull
  • The resulting C++ file can not be compiled

Reproduction

Consider this minimal example:

FUNCTION_BLOCK FB_AND
    VAR_INPUT
        I1 : BOOL;
        I2 : BOOL;
    END_VAR
    VAR_OUTPUT
        Q1 : BOOL;
    END_VAR

    Q1 := I1 AND I2;
END_FUNCTION_BLOCK

VAR_GLOBAL
    FB_AND_INST : FB_AND;
END_VAR

FUNCTION FUN : BOOL
    FB_AND_INST(I1 := TRUE, I2 := FALSE);
    FUN := FB_AND_INST.Q1;
END_FUNCTION

STruCpp converts this to the following C++:

// Generated by STruC++ - IEC 61131-3 Structured Text to C++ Compiler
// Do not edit this file manually.

#include "code.hpp"

namespace strucpp {

}  // namespace strucpp
// Generated by STruC++ - IEC 61131-3 Structured Text to C++ Compiler
// Do not edit this file manually.

#include "code.hpp"

namespace strucpp {

FB_AND::FB_AND() {
    // Initialize variables
}

void FB_AND::operator()() {
    Q1 = (I1) & (I2);
}

}  // namespace strucpp
// Generated by STruC++ - IEC 61131-3 Structured Text to C++ Compiler
// Do not edit this file manually.

#include "code.hpp"

namespace strucpp {

IEC_BOOL FUN() {
    IEC_BOOL FUN_result;
    FB_AND_INST(true, false);
    FUN_result = FB_AND_INST.Q1;
    return FUN_result;
}

}  // namespace strucpp

The line FB_AND_INST(true, false) does not compile. Based on tests/backend/codegen-fb.test.ts, specifically test FB invocation codegen I would expect the code to look like

FB_AND_INST.I1 = true;
FB_AND_INST.I2 = false;
FB_AND_INST();

Code Locations

No response

Fix

No response

Acceptance

The explicit assignment statements are generated and the generated code compiles :)

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 working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions