p4-spec
p4-spec copied to clipboard
Clarify whether P4-16 allows `switch` statements in actions
Current behavior of the p4c
compiler is:
- Report the following error message for backends that do run the
EliminateSwitch
midend pass:
error: SwitchStatement: switch statements not supported in actions on this target
- Supports
switch
statements in actions for backends that do not run theEliminateSwitch
midend pass.- Note that this is not tested in the
p4c
repository because thep4test
backend does run theEliminateSwitch
midend pass.
- Note that this is not tested in the
However, the P4-16 spec seems to indicate that switch
statements are not supported in actions at all.
For example, returns are not supported in parsers, and switch statements are only supported in control blocks.
The switch statement can only be used within control blocks.
Should the P4-16 spec be modified to allow switch
statements in actions? Or should the above error message be moved to a frontend pass that is by default run for all backends?
Below is an example program, which backends that do not run EliminateSwitch
do accept, and backends that do run EliminateSwitch
do not accept.
extern void __e(in bit<28> arg);
struct meta_t {
bit<8> f1;
bit<8> f2;
}
action foo(inout meta_t meta) {
switch (meta.f1 + meta.f2) {
0 : { __e(0); }
2 : { }
3 :
5 : { __e(5); }
7 :
default : { __e(99); }
}
}
control C(inout meta_t meta) {
table t {
actions = { foo(meta); }
default_action = foo(meta);
}
apply {
t.apply();
}
}
control proto(inout meta_t meta);
package top(proto p);
top(C()) main;