Skip to content

Expected semantics when inverting a ForStatement #546

@TooMuchDakka

Description

@TooMuchDakka

At the moment a ForStatement can perform iterations as defined below:

// $i gets the values 0, 1, 2 assigned
for $i = 0 to 3 step 1 do
  ...
rof

// $i gets the values 3, 2, 1 assigned
for $i = 3 to 0 step 1 do
  ...
rof

In both cases the iteration range is equal to the interval [start, end). Additionally, we assume that SyReC inherits the statement inversion semantics from its predecessor Janus which defines the inversion of a ForStatement by not only inverting the statements in the loop body but also by switching the start and end value of the ForStatement.

However, if we do perform said switch which includes the simple swap of start end end then the resulting "inverted" ForStatement does not match the actual inverse one and could also lead to subtle index-out-of-range errors as shown in #545. The two ForStatements from above would at the moment be inverted as:

// $i gets the values 3, 2, 1 assigned, in the non-inverted loop $i would have the values 0, 1, 2
for $i = 3 to 0 step 1 do
   ...
rof

// $i gets the values 0, 1, 2 assigned, in the non-inverted loop $i would have the values 3, 2, 1
for $i = 0 to 3 step 1 do
  ...
rof

We can see that in both inverted loops the iteration range ([end, start)) now starts at the value excluded in the non-inverted loop and excludes the start value that is included in the iteration range of the non-inverted loop. The actual inverse of both loops would defined the iteration range as (end, start] with a loop in which the start value is smaller than the end being inverted as:

// To be inverted loop
for $i = 0 to 3 step 1 do
   ...
rof

for $i = 3 to 0 step 1 do
  // All occurrences of $i would have to be replaced with ($i - 1). This should be fine since
  // $i is a compile time constant which in turn would also allow one to evaluate ($i - 1) at compile time.
  ...
rof

A loop in which start is larger than end could be inverted similarily with two equivalent definitions being shown in the example below:

// To be inverted loop
for $i = 3 to 0 step 1 do
   ...
rof

// Variant one
for $i = 0 to 3 step 1 do
  // Replace all occurrences of $i with ($i + 1)
  ...
rof

// Variant two
for $i = (0 + 1) to (3 + 1) step 1 do
  ...
rof

@burgholzer Should we update the inversion semantics of a ForStatement as defined above or is the current behaviour in which a simple swap of the start and end value occurs the expected one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions