You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
P4's slicing syntax is derived from Verilog, using expressions like:
foo[7:0] -- extract the bottom 8 bits of foo
bar[31:24] -- extract 8 bits bar (3rd byte from lsb)
As with Verilog, the indexes are required to be constants, so as to not incur excessive cost in hardware. This is also very important for typechecking as the size of a bitslice is always a constant and can be readily determined.
Recent versions of Verilog have added a syntax allowing limited variable slices with fixed size. A slice of the form bitvec [ expr +: const ] is equivalent to bitvec [ expr + const - 1 : expr ] but allows expr to be any expression (not just a constant). The resulting slice can still be typechecked easily (since const is required to be constant, the size of the result is constant).
This could be considered as syntactic sugar -- the expression bv [ e +: k ] is equivalent to (bv >> e)[k-1:0] in semantics, and could be converted to that. However, on some targets it may actually be easier to deal with the +: form rather than having a separate shift and slice.
I am closing a nearly identical issue #1302 in favor of this one, but copying one statement that is probably implied by the above, but wanted to make sure it wasn't lost:
bitvec[ expr +: const ] should hopefully be possible for the compiler to determine is type bit<const> as long as const is a local compile-time known value, even if expr is an expression that varies at run time. I understand if the spec and/or implementation requires expr to be a compile-time known value, too, for various reasons.
bitvec[ expr : const ] should hopefully be possible for the compiler to determine is type bit<const> as long as const is a local compile-time known value, even if expr is an expression that varies at run time. I understand if the spec and/or implementation requires expr to be a compile-time known value, too, for various reasons.
I think you mean bitvec[ expr +: const ] here? That is pretty much the whole point of this new syntax
P4's slicing syntax is derived from Verilog, using expressions like:
foo[7:0]
-- extract the bottom 8 bits of foobar[31:24]
-- extract 8 bits bar (3rd byte from lsb)As with Verilog, the indexes are required to be constants, so as to not incur excessive cost in hardware. This is also very important for typechecking as the size of a bitslice is always a constant and can be readily determined.
Recent versions of Verilog have added a syntax allowing limited variable slices with fixed size. A slice of the form
bitvec [ expr +: const ]
is equivalent tobitvec [ expr + const - 1 : expr ]
but allowsexpr
to be any expression (not just a constant). The resulting slice can still be typechecked easily (sinceconst
is required to be constant, the size of the result is constant).This could be considered as syntactic sugar -- the expression
bv [ e +: k ]
is equivalent to(bv >> e)[k-1:0]
in semantics, and could be converted to that. However, on some targets it may actually be easier to deal with the+:
form rather than having a separate shift and slice.Personnel
Design
Implementation
p4-spec
: add +: slices #1316p4c
: Support for [lsb+:width] slices p4c#4917Process
p4-spec
:p4c
:=======================================
The text was updated successfully, but these errors were encountered: