Add bounded for statements#1382
Conversation
Signed-off-by: Sehyuk Ahn <shyukahn@gmail.com>
Signed-off-by: Sehyuk Ahn <shyukahn@gmail.com>
Signed-off-by: Sehyuk Ahn <shyukahn@gmail.com>
…time known value Signed-off-by: Sehyuk Ahn <shyukahn@gmail.com>
7b8ec74 to
a7b0029
Compare
|
I'd suggest also immediately adding the other two, easily boundable (as far as I can tell) types of for loops to the (Obviously) Bounded subset: where you would probably want to restrict the forCollectionExpr to be a local compile time known value. Though I suppose that could be another PR as well. |
Signed-off-by: Sehyuk Ahn <shyukahn@gmail.com>
|
Those 2 for loops have been added to the bounded subset. This is the full syntax for bounded Regarding the restriction of |
Currently, For example, both of these possible expansions of |
Signed-off-by: Sehyuk Ahn <shyukahn@gmail.com>
|
I added some changes to this PR.
@jafingerhut and @ChrisDodd, could you please review the changes? |
| will only be evaluated once, before the first iteration of the loop. All side effects | ||
| in the list or range expression will occur before the first iteration of the loop body. | ||
|
|
||
| Instantiations are not allowed within `for` statements. |
There was a problem hiding this comment.
I'm not sure if this is necessary, or is opening another can of worms -- generally, instantiations happen at compile time/config time, so are not allowed in constructs that run at runtime (apply blocks, actions, or functions). But loops are (currently) only allowed in those places (so run at runtime). We don't have a good description of this elsewhere, however.
There was a problem hiding this comment.
Yes, it indeed opens another can of worms. It is generally true that loops are allowed in apply blocks, actions, or functions that are (conceptually) irrelevant to instantiation.
The tricky case is directApplication within statement:
statement
: assignmentOrMethodCallStatement
| directApplication (* this! *)
| conditionalStatement
| emptyStatement
| blockStatement
| returnStatement
| exitStatement
| switchStatement
;
Direct application is a syntactic sugar for instantiation + apply invocation. And if we have such within a loop (which is syntactically allowed as per the grammar):
for (bit<8> i = 0; i < 8; i ++) {
subcontrol.apply();
}There is no way to statically give unique control-plane names to each of the instances made within the loop.
But also seems like a more general discussion should be made on a separate issue, regarding directApplication, since they may also be nested within conditionalStatements.
if (... some non-trivial condition ...) subcontrol.apply();
else subcontrol.apply();
jafingerhut
left a comment
There was a problem hiding this comment.
LGTM. Thanks for generalizing the original proposal to allow more general update statements!
This PR adds a new subsection 12.8.1. Bounded for statement under 12.8. For statement.
Bounded
forstatements form a subset of generalforstatements, and are ensured to be bounded statically by adding the following syntactic constraints:i < nwhereiis the loop variable andnis a local compile time known value.i += 1whereiis the loop variable.For example, this is a bounded for statement, which is a common use of
forstatements:On the other hand, these
forstatements are not considered as boundedforstatements, though they may be actually bounded:For now, this version states a very simple subset of bounded
forstatements. This PR is meant to be a starting point of the discussion on adding boundedforstatements to the specification.Additionally, one restriction is added, which also corresponds to general
forstatements:forstatements.