Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding Verilog style +: (and possibly -:) slice operator(s) #1304

Open
5 of 9 tasks
ChrisDodd opened this issue Sep 5, 2024 · 2 comments
Open
5 of 9 tasks

Comments

@ChrisDodd
Copy link
Contributor

ChrisDodd commented Sep 5, 2024

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.

Personnel

Design

  • Document: Description above, plus text in PR for spec, should be sufficient.

Implementation

Process

  • LDWG discussed:
  • LDWG approved:
  • Merged into p4-spec:
  • Merged into p4c:

=======================================

@jafingerhut
Copy link
Collaborator

jafingerhut commented Sep 5, 2024

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.

@ChrisDodd
Copy link
Contributor Author

  • 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants