Skip to content

Conversation

volsa
Copy link
Member

@volsa volsa commented Jul 17, 2025

No description provided.

@volsa volsa mentioned this pull request Jul 17, 2025
9 tasks
@volsa volsa force-pushed the vosa/polymorphism branch 2 times, most recently from f7b7c11 to 04bc60d Compare July 29, 2025 15:44
volsa added 14 commits August 1, 2025 13:15
`POINTER TO <function>` are now indexed in the codegen
Previously when dealing with function pointer calls such as `fnPtr^()`
the resolver would only annotate the operator and any arguments. Some
codegen parts require an annotation on the whole call statement however
(which makes sense), as such this commit fixes the described issue.
Visuallized, assuming `fnPtr` points to a function returning a DINT:
```
fnPtr^();
^^^^^^^^ -> StatementAnnotation::Value { resulting_type: "DINT" }
```
@volsa volsa force-pushed the vosa/polymorphism branch from 04bc60d to e79c95e Compare August 14, 2025 14:30
volsa and others added 7 commits August 26, 2025 09:20
Function pointers can now point to the body of a function block, allowing
for code execution such as
```
FUNCTION_BLOCK A
    VAR
        localState: DINT := 5;
    END_VAR

    METHOD foo
        // ...
    END_METHOD

    printf('localState = %d$N', localState);
END_FUNCTION_BLOCK

FUNCTION main
    VAR
        instanceA: A;
        fnBodyPtr: FNPTR A := ADR(A);
    END_VAR

    fnBodyPtr^(instanceA); // prints "localState = 5"
END_FUNCTION
```
Also inject `__vtable` member variable into "root" POUs
Methods calls which must make use of the virtual table are now
desugared. For example a method call within a method such as `foo(1, 2)`
will now be desugared into `__vtable_{FB_NAME}#(THIS^.__vtable^).foo^(THIS^, 1, 2)`.
Similarly, a variable like `refInstance: POINTER TO FbA` making a method call
such as `refInstance^.foo(1, 2)` will be lowered into some similar form, except
not making use of THIS, rather of the operator name (excluding the method name), i.e.
`__vtable_FbA#(refInstance^.__vtable^).foo(refInstance^, 1, 2)`.
The `__vtable` member field is now initialized with a right hand side of
`ADR(__vtable_<POU_NAME>_instance)` which represents the global instance
variable of a virtual table that is initialized with its function pointers.
@volsa volsa force-pushed the vosa/polymorphism branch from e79c95e to 27a06a5 Compare August 26, 2025 11:22
The `__body` function pointer now points at the function blocks body
method in the virtual table struct. As a result, code execution like
the following is now possible
```
VAR
    instanceA: A;
    instanceB: B; // Child of A

    refInstanceA: POINTER TO A;
END_VAR

refInstanceA := ADR(A);
refInstanceA^(); // Calls body method of A

refInstanceA := ADR(B);
refInstanceA^(); // Calls body method of B
```
@volsa volsa force-pushed the vosa/polymorphism branch from 956a3eb to 213376f Compare August 27, 2025 09:07
@volsa volsa force-pushed the vosa/polymorphism branch from 4a5aff8 to 1bb460a Compare August 31, 2025 14:34
@volsa volsa force-pushed the vosa/polymorphism branch from 1bb460a to b3b3dec Compare September 1, 2025 08:43
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.

1 participant