Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce REF= and REFERENCE_TO #1251

Merged
merged 28 commits into from
Jul 10, 2024
Merged

feat: Introduce REF= and REFERENCE_TO #1251

merged 28 commits into from
Jul 10, 2024

Conversation

volsa
Copy link
Member

@volsa volsa commented Jun 24, 2024

This PR introduces two new keywords, namely REF= and REFERENCE TO:

  • REF= is essentially syntactic sugar for an assignment where the right-hand side is wrapped in a REF() function call. Therefore foo := REF(bar) and foo REF= bar are equivalent.
  • REFERENCE TO is identical to REF_TO with the exception of being auto-deref by default. A variable foo declared as REFERENCE TO will therefore auto-deref on assignments, i.e. foo := 5 is equivalent to foo^ := 5.

More information on CodeSys' REF= and REFERENCE TO documentation pages.

@volsa volsa changed the title feat: Introduce REFERENCE_TO feat: Introduce REFERENCE_TO Jun 24, 2024
@volsa volsa mentioned this pull request Jun 25, 2024
@volsa volsa changed the title feat: Introduce REFERENCE_TO feat: Introduce REF= and REFERENCE_TO Jun 25, 2024
@volsa volsa force-pushed the volsa/referenceto branch from cb093ca to 8d0ef38 Compare June 28, 2024 11:29
@volsa volsa force-pushed the volsa/referenceto branch 2 times, most recently from 1ac7bfd to 7772a3b Compare July 1, 2024 08:39
@volsa volsa force-pushed the volsa/referenceto branch from 7772a3b to a991a8c Compare July 1, 2024 08:52
@volsa volsa requested review from riederm and ghaith and removed request for riederm July 1, 2024 09:46
@volsa volsa requested review from riederm and mhasel July 1, 2024 09:46
@volsa volsa marked this pull request as ready for review July 1, 2024 15:19
Copy link
Member

@mhasel mhasel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, but I have some open questions/requests for additional test coverage.

src/codegen/generators/statement_generator.rs Outdated Show resolved Hide resolved
src/resolver/generics.rs Show resolved Hide resolved
src/validation/statement.rs Show resolved Hide resolved
src/validation/tests/assignment_validation_tests.rs Outdated Show resolved Hide resolved
src/validation/tests/assignment_validation_tests.rs Outdated Show resolved Hide resolved
src/validation/tests/assignment_validation_tests.rs Outdated Show resolved Hide resolved
src/codegen/generators/statement_generator.rs Outdated Show resolved Hide resolved
tests/lit/single/pointer/ref_assignment_operator.st Outdated Show resolved Hide resolved
src/codegen/tests/statement_codegen_test.rs Show resolved Hide resolved
@volsa volsa force-pushed the volsa/referenceto branch 2 times, most recently from b60bc18 to c5680bd Compare July 4, 2024 10:06
@volsa volsa force-pushed the volsa/referenceto branch from c5680bd to 3f74eac Compare July 4, 2024 10:08
@volsa
Copy link
Member Author

volsa commented Jul 4, 2024

I think I addressed all your comments @mhasel , should be ready for a second round 🤞

* `foo : ARRAY[...] OF REFERENCE TO (* ... *)`
* `foo : REF_TO REFERENCE TO (* ... *)`

Furthermore `REFERENCE_TO` variables must not be initialized in their declaration, e.g. `foo : REFERENCE TO DINT := bar`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi, this is not entirely correct but will be addressed in the aliasing PR later on

mhasel
mhasel previously approved these changes Jul 8, 2024
foo REF= bar;

// CHECK-NOT: ^5$
printf('%d$N', foo); // We expect some random value here because foo isn't pointing to any variable yet
Copy link
Member

@mhasel mhasel Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are auto-dereferencing an uninitialized pointer here - this is undefined behaviour. Should we initialize these pointers with null like we are doing for VAR_INPUT {ref}? Ideally we would check for use before assignment but that feature feels a bit far off still.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, generally in favor of NULL pointers here but I'd probably also address this in another PR. To have a second opinion here though cc @ghaith

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think null makes sense here so that we don't have random values. I'm in favour of not having such tests tbh. We can't test undefined behaviour (by design). Also what happens if the random value is 5? I am more in favour of having a test that verifies the pointer value of foo is null. Not sure how to do this with the auto deref rules.. maybe it's enough to have an IR check that verifies the initial values of REFERENCE TO and Pointer fields in general.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I've created an issue and deleted the test here. I'll address it in another PR since this one bloats up way too much if you'd ask me.

@volsa volsa merged commit 79abdb6 into master Jul 10, 2024
19 checks passed
@volsa volsa deleted the volsa/referenceto branch July 10, 2024 13:33
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.

3 participants