@@ -3177,6 +3177,8 @@ side-effects. P4 expressions are evaluated as follows:
31773177 the second operand is only evaluated if necessary.
31783178- The conditional operator `e1 ? e2 : e3` evaluates `e1`, and then
31793179 either evaluates `e2` or `e3`.
3180+ - Compound assignment expressions are evaluated as described in Section
3181+ [#sec-compound-assignment].
31803182- All other expressions are evaluated left-to-right as they appear in
31813183 the source program.
31823184- Method and function calls are evaluated as described in Section
@@ -5275,6 +5277,38 @@ types (e.g. `structs`) are copied recursively, and all components
52755277of `header`s are copied, including "validity" bits. Assignment is
52765278not defined for `extern` values.
52775279
5280+ ### Compound assignment { #sec-compound-assignment }
5281+
5282+ Compound assignment operators provide a shorter syntax for assigning
5283+ the result of an arithmetic or bitwise operator. The compound assignment
5284+ operator expressions have the form `LHS op RHS`, where `op` is one of those
5285+ in the table below, and `LHS` and `RHS` are expressions with types appropriate
5286+ for the operator.
5287+
5288+ |----------|-----------------------------------|------------|---------------|
5289+ | Operator | Operator name | Example | Equivalent of |
5290+ +----------+:---------------------------------:+:----------:+:-------------:+
5291+ | `=` | basic assignment | `a = b` | NA |
5292+ | `+=` | addition assignment | `a += b` | `a = a + b` |
5293+ | `-=` | subtraction assignment | `a -= b` | `a = a - b` |
5294+ | `*=` | multiplication assignment | `a *= b` | `a = a * b` |
5295+ | `/=` | division assignment | `a /= b` | `a = a / b` |
5296+ | `%=` | modulo assignment | `a %= b` | `a = a % b` |
5297+ | `&=` | bitwise AND assignment | `a &= b` | `a = a & b` |
5298+ | `|=` | bitwise OR assignment | `a |= b` | `a = a | b` |
5299+ | `^=` | bitwise XOR assignment | `a ^= b` | `a = a ^ b` |
5300+ | `<<=` | bitwise left shift assignment | `a <<= b` | `a = a << b` |
5301+ | `>>=` | bitwise right shift assignment | `a >>= b` | `a = a >> b` |
5302+ | `|+|=` | saturating addition assignment | `a |+|= b` | `a = a |+| b` |
5303+ | `|-|=` | saturating subtraction assignment | `a |-|= b` | `a = a |-| b` |
5304+ |----------|-----------------------------------|----------- |---------------|
5305+
5306+ The behavior of every builtin compound assignment expression with the form `E1 op= E2`,
5307+ where `E1` is a modifiable l-value expression and `E2` is an r-value expression, is exactly
5308+ the same as the behavior of the expression `E1 = E1 op E2`, except that `E1` is evaluated
5309+ only once. Like other P4 expressions where subexpressions are evaluated left to right,
5310+ `E1` is evaluated before `E2`.
5311+
52785312## Empty statement { #sec-empty-stmt }
52795313
52805314The empty statement, written `;` is a no-op.
0 commit comments