Skip to content

Commit

Permalink
Add definition for compound assignment operators
Browse files Browse the repository at this point in the history
This patch adds definition for compound-assignment operators to the P4
language specification. These operators provide a shorter syntax for
assigning the result of an arithmetic or bitwise operator. The proposed
definition of compound assignment operators is similar the
C99 specification.

https://en.cppreference.com/w/c/language/operator_assignment

Signed-off-by: Radostin Stoyanov <radostin.stoyanov@eng.ox.ac.uk>
  • Loading branch information
rst0git committed Sep 9, 2022
1 parent 503bd98 commit 440d1c9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions p4-16/spec/P4-16-spec.mdk
Original file line number Diff line number Diff line change
Expand Up @@ -5228,6 +5228,36 @@ types (e.g. `structs`) are copied recursively, and all components
of `header`s are copied, including "validity" bits. Assignment is
not defined for `extern` values.

### Compound assignment { #sec-compound-assignment }

Compound-assignment operators provide a shorter syntax for assigning
the result of an arithmetic or bitwise operator. The compound assignment
operator expressions have the form `LHS op RHS`, where `op` is one of
`+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `|=`, `^=`, `<<=`, `>>=`, and `LHS`,
`RHS` are expressions with arithmetic types. The following table shows
all P4 assignment operators and their equivalent.

|-------------|----------------------------------|-----------|---------------|
| Operator | Operator name | Example | Equivalent of |
+-------------+:--------------------------------:+:---------:+:-------------:+
| `=` | basic assignment | `a = b` | NA |
| `+=` | addition assignment | `a += b` | `a = a + b` |
| `-=` | subtraction assignment | `a -= b` | `a = a - b` |
| `*=` | multiplication assignment | `a *= b` | `a = a * b` |
| `/=` | division assignment | `a /= b` | `a = a / b` |
| `%=` | modulo assignment | `a %= b` | `a = a % b` |
| `&=` | bitwise AND assignment | `a &= b` | `a = a & b` |
| `|=` | bitwise OR assignment | `a |= b` | `a = a | b` |
| `^=` | bitwise XOR assignment | `a ^= b` | `a = a ^ b` |
| `<<=` | bitwise left shift assignment | `a <<= b` | `a = a << b` |
| `>>=` | bitwise right shift assignment | `a >>= b` | `a = a >> b` |
|-------------|----------------------------------|-----------|---------------|

The behavior of every builtin compound-assignment expression with the form `E1 op= E2`,
where `E1` is a modifiable lvalue expression and `E2` is an rvalue expression,
is exactly the same as the behavior of the expression `E1 = E1 op E2`.


## Empty statement { #sec-empty-stmt }

The empty statement, written `;` is a no-op.
Expand Down

0 comments on commit 440d1c9

Please sign in to comment.