p4-spec
p4-spec copied to clipboard
Support assignment and arithmetic operations (+=)
Personnel
- [x] Owner: @rst0git
- [x] Supporters: @mbudiu-vmw
Design
- [x] Document:
Implementation
- [ ]
p4-spec
: - [ ]
p4c
:
Process
- [ ] LDWG discussed:
- [ ] LDWG approved:
- [ ] Merged into
p4-spec
: - [ ] Merged into
p4c
:
=======================================
I am trying to remember if we have a case where a lvalue expression can have a side effect which will need to be handled correctly in the specifications and the compilers. That is my only concern here. An example where a side effect could happen I suspect is in header stack indexing:
hdr_stack[func(h)].field += 4;
But that could be resolved by doing:
bit tmp1 = func(h);
hdr_stack[tmp1].field = hdr_stack[tmp1].field + 4;
Candidates for expressions to do similarly to +=:
-=
&=
|=
^=
*=
/=
<<=
>>=
C specifies that a += b
is equivalent to a = a + b
except that a
is evaluated only once. C99 says, for example:
A compound assignment of the form E1 op = E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once.
It would make sense to me to specify compound assignment in P4 in a similar way.
@mbudiu-vmw, @apinski-cavium and @blp thank you for your input! I have opened the following pull request to specify compound assignment operators in the P4_16 spec: https://github.com/p4lang/p4-spec/pull/1144
In the interest of tidying up the set of active issues on the P4 specification repository, I'm marking this as "stalled" and closing it. Of course, we can always re-open it in the future if there is interest in resurrecting it.