-
Notifications
You must be signed in to change notification settings - Fork 452
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
How to access a field's typedef type in p4c implementation? #2310
Comments
This experiment and my questions about it are in hopes of moving forward with this issue: p4lang/p4-spec#815 |
@jafingerhut I did spend time yesterday trying to find a C++ API with no luck. Note, the p4runtime API generation is invoked after p4c frontend processing completes. If you see p4c typeChecking frontend code, p4c has already morphed the typedef to its base type. https://github.com/p4lang/p4c/blob/master/frontends/p4/typeChecking/typeChecker.cpp#L1350 This is also why I think p4runtime code generation in @mbudiu-vmw and @antoninbas may be able to help. One thought I have is to save the typedef in a |
Sorry, I forgot to mention that |
You can do similar things starting from a PathExpression. |
First, there is one missing statement which converts
|
Thanks for the code snippets. I will try them out soon. Is there a brief explanation of what a IR::PathExpression includes, or just one or two different examples of what the compiler uses them to represent? |
The entire IR is documented in a very compact way in the ir/*,def files. Currently a PathExpression looks like this:
And Path is
The Path initially contained a vector of namespaces, which may be needed in the future, so we left the class in. |
Any identifier that shows up in an expression is represented by a PathExpression. |
Even with |
Perhaps there is a way to get from a |
Member is an expression that extracts a field from another expression. The expression a.b is represented as Member(PathExpression("a"), "b"). You can get the declaration of "a", then it's type (which should be a structLikeType), and then you can look for the field named "b" in that struct. |
I have tried the code snippets mentioned above, in particular Hemant's small modification to Mihai's sample code (the first did not compile), and it also finds a type of https://github.com/jafingerhut/p4-guide/tree/master/typedef-in-p4c-ir#experiment-2 Any other recommendations for how to get |
The types in the typeMap will never have Typedef objects in them; you have to navigate the hierarchy of declarations explicitly. You have to find the declaration of the struct, then discover it's type, which is a typename, then find the declaration of that guy, and then look at the fields of it. The rule of thumb is: never use the typeMap. |
I am sure I am misunderstanding something, but isn't that what your suggestion in this earlier comment is doing? #2310 (comment) If that is what that earlier code was intended to do, it seems to fail on the call to |
I had already said yesterday if the code snippets are tried the code will fail at getting the I worked in the typeChecker code and got what Andy needs. It remains to be seen how we write code in To avoid printing too much debugged data, I have a check for
|
More investigation of p4c frontend code is needed. Turns out, if I remove the 'if (expression->toString() == "addr0") { |
Here is a piece of code that works for key expressions that are members:
The key observation is that to obtain the declaration of a type when you know its name (e.g., |
Slide 81 in this presentation explains why the typeMap is not useful: |
Sorry for my previous comment, now deleted. I only saw the last comment, and not the one just before that, which gives some other C++ code to try. I will give it a go and let you know. Thank you! |
I tried out the updated code you provided in this comment: #2310 (comment) (thank you for that code, by the way) I based my changes on a very recent version (2020-May-02) of the p4c source code, plus only your recommended code additions, and I only added that code to a method named When I attempt to build p4c, I get many error messages about those added lines of code, given in the file attached to this comment. I am a little ashamed to admit that my ignorance of the p4c class structure, and/or perhaps the C++ language itself, have made it fairly confusing for me to see what kinds of changes would enable this code to compile. If anyone has any recommendations, I would appreciate it. |
The assumption is that this code is called in some visitor method, like pre-order. The context is maintained by the visitor.
|
the only problem is with the findContext call in this program; if you can pass a pointer to the |
Can we close this issue? |
I obviously have not aggressively pursued this line of thinking in a while, but I still would love it if someone with actual p4c compiler chops (not me, unfortunately) could figure out how to make typedef info available to control plane API generation, since those typedef names could be helpful in providing useful info to a control plane API, e.g. that the type was not only |
In principle this should be possible: we can send all typedef information and the actual type used in the declaration (assuming it has not been replaced at that point) to the control-plane. But this may make things complicated, especially with the new generic structs: something can have a type |
Things will become even more complicated if we introduce the |
See the README.md file in this directory for an experiment I tried, and how it seems to be accessing information about a field's underlying
bit<W>
type, but not itstypedef
, even though the output from all of p4c's front-end and mid-end passes 'remember' thistypedef
name, and the association of thattypedef
name with the field in question.https://github.com/jafingerhut/p4-guide/tree/master/typedef-in-p4c-ir
The text was updated successfully, but these errors were encountered: