Skip to content

Commit 9d57e9f

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 a4fdd6d commit 9d57e9f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

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

52805314
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)