Skip to content

Commit 2f185cb

Browse files
committed
add +: slices
Signed-off-by: Chris Dodd <[email protected]>
1 parent 7f4dbd4 commit 2f185cb

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

p4-16/spec/P4-16-spec.adoc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,10 +3615,20 @@ Bit-strings also support the following operations:
36153615
`+0 <= L <= H < W+` are checked statically (where `W` is
36163616
the length of the source bit-string). Note that both endpoints of
36173617
the extraction are inclusive. The bounds are required to be
3618-
local compile-time known values so that the width of the result can be computed at compile time. Slices are also l-values, which means that P4 supports assigning to a slice: `e[H:L] = x`.
3618+
local compile-time known values so that the width of the result can be computed
3619+
at compile time. Slices are also l-values, which means that P4 supports
3620+
assigning to a slice: `e[H:L] = x`.
36193621
The effect of this statement is to set bits `H` through `L` (inclusive of
36203622
both) of `e` to the bit-pattern represented by `x`, and leaves all other bits
3621-
of `e` unchanged. A slice of an unsigned integer is an unsigned integer.
3623+
of `e` unchanged. A slice is always an unsigned integer.
3624+
A slice may also be specified as `[L+:W]` where `L` is the lowest bit of the slice
3625+
and `W` is the width of the slice. In this case, only `W` must be a non-negative
3626+
local compile-time known numeric value. `L` must also be a numeric value and
3627+
some architectures may require it to be compile-time known. It must also be
3628+
non-negative and in-range for the type. Some architectures may allow variable indexing,
3629+
in which case an out of range value will be equivalent to a shift of that amount.
3630+
An index that is not compile-time known must be a `bit<W>` type in order to ensure that
3631+
it is non-negative.
36223632
* Concatenation of bit-strings and/or fixed-width signed integers, denoted by `++`.
36233633
The two operands must be either `bit<W>` or `int<W>`, and they can be of
36243634
different signedness and width. The result has the same signedness as the
@@ -3695,21 +3705,8 @@ The `int<W>` datatype also support the following operations:
36953705
** all result bits are zero when shifting a non-negative value right
36963706
** all result bits are one when shifting a negative value right
36973707
* Extraction of a set of contiguous bits, also known as a slice,
3698-
denoted by `[H:L]`, where `H` and `L` must be expressions that evaluate to
3699-
non-negative, local compile-time known values, and `H >= L` must be true.
3700-
The types of `H` and `L` (which do not need to be identical)
3701-
must be numeric (<<sec-numeric-values>>).
3702-
The result is an unsigned bit-string of width `H - L + 1`, including the bits
3703-
numbered from `L` (which becomes the least significant bit of the result)
3704-
to `H` (the most significant bit of the result) from the source operand.
3705-
The conditions `+0 <= L <= H < W+` are checked statically (where `W` is
3706-
the length of the source bit-string). Note that both endpoints of
3707-
the extraction are inclusive. The bounds are required to be values
3708-
that are known at compile time so that the width of the result can be
3709-
computed at compile time. Slices are also l-values, which means that P4
3710-
supports assigning to a slice: `e[H:L] = x`.
3711-
The effect of this statement is to set bits `H` through `L` of `e` to the
3712-
bit-pattern represented by `x`, and leaves all other bits of `e` unchanged.
3708+
denoted by `[H:L]` or `[L+:W]`, with the same semantics as slices of
3709+
bit strings.
37133710
A slice of a signed integer is treated as an unsigned integer.
37143711
* Concatenation of bit-strings and/or fixed-width signed integers, denoted by `++`.
37153712
The two operands must be either `bit<W>` or `int<W>`, and they can be of different signedness and width.

p4-16/spec/grammar.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ lvalue
10971097
| lvalue "." member
10981098
| lvalue "[" expression "]"
10991099
| lvalue "[" expression ":" expression "]"
1100+
| lvalue "[" expression "+" ":" expression "]"
11001101
| "(" lvalue ")"
11011102
;
11021103
// end::lvalue[]
@@ -1130,6 +1131,7 @@ expression
11301131
| prefixedNonTypeName
11311132
| expression "[" expression "]"
11321133
| expression "[" expression ":" expression "]"
1134+
| expression "[" expression "+" ":" expression "]"
11331135
| "{" expressionList optTrailingComma "}"
11341136
| "{#}"
11351137
| "{" kvList optTrailingComma "}"
@@ -1180,6 +1182,7 @@ nonBraceExpression
11801182
| prefixedNonTypeName
11811183
| nonBraceExpression "[" expression "]"
11821184
| nonBraceExpression "[" expression ":" expression "]"
1185+
| nonBraceExpression "[" expression "+" ":" expression "]"
11831186
| "(" expression ")"
11841187
| "!" expression %prec PREFIX
11851188
| "~" expression %prec PREFIX

0 commit comments

Comments
 (0)