@@ -3162,6 +3162,8 @@ side-effects. P4 expressions are evaluated as follows:
31623162 the second operand is only evaluated if necessary.
31633163- The conditional operator `e1 ? e2 : e3` evaluates `e1`, and then
31643164 either evaluates `e2` or `e3`.
3165+ - Compound assignment expressions are evaluated as described in Section
3166+ [#sec-compound-assignment].
31653167- All other expressions are evaluated left-to-right as they appear in
31663168 the source program.
31673169- Method and function calls are evaluated as described in Section
@@ -5228,6 +5230,42 @@ types (e.g. `structs`) are copied recursively, and all components
52285230of `header`s are copied, including "validity" bits. Assignment is
52295231not defined for `extern` values.
52305232
5233+ ### Compound assignment { #sec-compound-assignment }
5234+
5235+ Compound assignment operators provide a shorter syntax for assigning
5236+ the result of an arithmetic, bitwise or logical operator. The compound
5237+ assignment operator expressions have the form `LHS op RHS`, where `op`
5238+ is one of `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `|=`, `&&=`, `||=`, `^=`,
5239+ `<<=`, `>>=`, `|+|=`, `|-|=`, and `LHS`, `RHS` are expressions with
5240+ arithmetic types. The following table shows all P4 assignment operators and
5241+ their equivalent.
5242+
5243+ |----------|-----------------------------------|------------|---------------|
5244+ | Operator | Operator name | Example | Equivalent of |
5245+ +----------+:---------------------------------:+:----------:+:-------------:+
5246+ | `=` | basic assignment | `a = b` | NA |
5247+ | `+=` | addition assignment | `a += b` | `a = a + b` |
5248+ | `-=` | subtraction assignment | `a -= b` | `a = a - b` |
5249+ | `*=` | multiplication assignment | `a *= b` | `a = a * b` |
5250+ | `/=` | division assignment | `a /= b` | `a = a / b` |
5251+ | `%=` | modulo assignment | `a %= b` | `a = a % b` |
5252+ | `&=` | bitwise AND assignment | `a &= b` | `a = a & b` |
5253+ | `|=` | bitwise OR assignment | `a |= b` | `a = a | b` |
5254+ | `&&=` | logical AND assignment | `a &&= b` | `a = a && b` |
5255+ | `||=` | logical OR assignment | `a ||= b` | `a = a || b` |
5256+ | `^=` | bitwise XOR assignment | `a ^= b` | `a = a ^ b` |
5257+ | `<<=` | bitwise left shift assignment | `a <<= b` | `a = a << b` |
5258+ | `>>=` | bitwise right shift assignment | `a >>= b` | `a = a >> b` |
5259+ | `|+|=` | saturating addition assignment | `a |+|= b` | `a = a |+| b` |
5260+ | `|-|=` | saturating subtraction assignment | `a |-|= b` | `a = a |-| b` |
5261+ |----------|-----------------------------------|----------- |---------------|
5262+
5263+ The behavior of every builtin compound assignment expression with the form `E1 op= E2`,
5264+ where `E1` is a modifiable l-value expression and `E2` is an r-value expression,
5265+ is exactly the same as the behavior of the expression `E1 = E1 op E2`, except that
5266+ `E1` is evaluated only once. Unlike normal assignments, compound assignments evaluate
5267+ the left-hand side before evaluating the right-hand side.
5268+
52315269## Empty statement { #sec-empty-stmt }
52325270
52335271The empty statement, written `;` is a no-op.
0 commit comments