p4-spec icon indicating copy to clipboard operation
p4-spec copied to clipboard

Support assignment and arithmetic operations (+=)

Open mihaibudiu opened this issue 2 years ago • 3 comments

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:

=======================================

mihaibudiu avatar Sep 07 '22 17:09 mihaibudiu

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 +=:

-=
&=
|=
^=
*=
/=
<<=
>>=

apinski-cavium avatar Sep 07 '22 17:09 apinski-cavium

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.

blp avatar Sep 07 '22 17:09 blp

@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

rst0git avatar Sep 09 '22 13:09 rst0git

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.

jnfoster avatar Nov 11 '23 13:11 jnfoster