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

Reserved identifiers #929

Open
5 of 9 tasks
mihaibudiu opened this issue Mar 29, 2021 · 5 comments
Open
5 of 9 tasks

Reserved identifiers #929

mihaibudiu opened this issue Mar 29, 2021 · 5 comments

Comments

@mihaibudiu
Copy link
Contributor

mihaibudiu commented Mar 29, 2021

Personnel

  • Owner: @mbudiu-vmw
  • Supporters: @fruffy

Design

  • Document: See below

Implementation

Process

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

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

This discussion p4lang/p4c#2706 made it clear that the compiler needs to handle some identifiers specially.
For example, __v1model_version is used to store the version of the v1model.p4 used, and the compiler should not delete this constant declaration even if it is dead, since the back-end depends on it to generate code, and the toP4 pass uses it to generate the proper macro before including v1model.p4. The PR cited assumes that all global identifiers starting with __ are reserved, but the spec does not codify this anywhere.

@vgurevich
Copy link
Contributor

I might be wrong, but I think that it is better to have these version constants to be #define-s and not P4 constants.

The rationale is that they really need to be examined at the compile-time and typically #ifdef is a more powerful mechanism, compared with the regular if() (or even a new, upcoming switch()) statement, simply because it can be used in any context whatsoever, while the P4 statements can only be used in a few, very limited places.

Obviously, if we change these version constants to become CPP defines, the problem at hand will go away on its own :)

@mihaibudiu
Copy link
Contributor Author

The main issue with macros is that they are not represented in the compiler IR. They only exist in the preprocessor, and after substitution they completely disappear. So having a const is one way to preserve these values throughout the compilation process.

@jnfoster
Copy link
Contributor

I'm confused... as a CPP define, the compiler has even less visibility. The version becomes just another integer literal. Isn't that a step in the wrong direction?

@jnfoster
Copy link
Contributor

Looks like @mbudiu-vmw beat me to it.

@vgurevich
Copy link
Contributor

The point is that in practice, these constants are used to modify the code before it is fed into the compiler, meaning that we want to see in the IR only whatever is left and not to see in the IR anything that we actually want to remove.

Also, for example, imagine the situation where you are using an extern, defined in one version of the architecture and not defined in another one (or, perhaps, the definitions are not quite the same). You cannot use an if() to choose between these instantiations, but it is very easily done using #ifdef.

This is a general issue that goes beyond just the version constants. If you want, we can always have a compromise where you define both, but that is not going to help with the original problem :)

I have tons of programs that have many different variations/profiles. Sometimes if() can be used and I do use it, but very often it can't be and then I have to use the preprocessor.

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

3 participants