Skip to content

Commit 9af9ac5

Browse files
committed
Add definition for compound assignment operators
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 <[email protected]>
1 parent 503bd98 commit 9af9ac5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

p4-16/spec/P4-16-spec.mdk

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
52285230
of `header`s are copied, including "validity" bits. Assignment is
52295231
not 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

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

p4-16/spec/grammar.mdk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,20 @@ annotationToken
726726
| "?"
727727
| "."
728728
| "="
729+
| "+="
730+
| "-="
731+
| "*="
732+
| "/="
733+
| "%="
734+
| "&="
735+
| "|="
736+
| "&&="
737+
| "||="
738+
| "^="
739+
| "<<="
740+
| ">>="
741+
| "|+|="
742+
| "|-|="
729743
| ";"
730744
| "@"
731745
| UNKNOWN_TOKEN

0 commit comments

Comments
 (0)