-
Notifications
You must be signed in to change notification settings - Fork 446
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
Make the Tofino spec files independent of the generated IR. #5063
Draft
fruffy
wants to merge
3
commits into
main
Choose a base branch
from
fruffy/tofino_specs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make the json loader and parser independent from the generated ir files.
Signed-off-by: fruffy <fruffy@nyu.edu>
- Loading branch information
commit ad24e06fadee72c5054cb77feef18b8650e86d83
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2013-present Barefoot Networks, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef IR_INODE_H_ | ||
#define IR_INODE_H_ | ||
|
||
#include "lib/castable.h" | ||
#include "lib/cstring.h" | ||
#include "lib/exceptions.h" | ||
#include "lib/rtti.h" | ||
#include "lib/source_file.h" | ||
|
||
namespace P4 { | ||
class JSONGenerator; | ||
class JSONLoader; | ||
} // namespace P4 | ||
|
||
namespace P4::IR { | ||
|
||
class Node; | ||
|
||
/// SFINAE helper to check if given class has a `static_type_name` | ||
/// method. Definite node classes have them while interfaces do not | ||
template <class, class = void> | ||
struct has_static_type_name : std::false_type {}; | ||
|
||
template <class T> | ||
struct has_static_type_name<T, std::void_t<decltype(T::static_type_name())>> : std::true_type {}; | ||
|
||
template <class T> | ||
inline constexpr bool has_static_type_name_v = has_static_type_name<T>::value; | ||
|
||
// node interface | ||
class INode : public Util::IHasSourceInfo, public IHasDbPrint, public ICastable { | ||
public: | ||
virtual ~INode() {} | ||
virtual const Node *getNode() const = 0; | ||
virtual Node *getNode() = 0; | ||
virtual void toJSON(JSONGenerator &) const = 0; | ||
virtual cstring node_type_name() const = 0; | ||
virtual void validate() const {} | ||
|
||
// default checkedTo implementation for nodes: just fallback to generic ICastable method | ||
template <typename T> | ||
std::enable_if_t<!has_static_type_name_v<T>, const T *> checkedTo() const { | ||
return ICastable::checkedTo<T>(); | ||
} | ||
|
||
// alternative checkedTo implementation that produces slightly better error message | ||
// due to node_type_name() / static_type_name() being available | ||
template <typename T> | ||
std::enable_if_t<has_static_type_name_v<T>, const T *> checkedTo() const { | ||
const auto *result = to<T>(); | ||
BUG_CHECK(result, "Cast failed: %1% with type %2% is not a %3%.", this, node_type_name(), | ||
T::static_type_name()); | ||
return result; | ||
} | ||
|
||
DECLARE_TYPEINFO(INode); | ||
}; | ||
|
||
} // namespace P4::IR | ||
|
||
#endif /* IR_INODE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef IR_UNPACKER_TABLE_H_ | ||
#define IR_UNPACKER_TABLE_H_ | ||
|
||
#include "ir/inode.h" | ||
#include "lib/cstring.h" | ||
|
||
namespace P4 { | ||
class JSONLoader; | ||
|
||
using NodeFactoryFn = IR::INode *(*)(JSONLoader &); | ||
namespace IR { | ||
extern std::map<cstring, NodeFactoryFn> unpacker_table; | ||
|
||
} // namespace IR | ||
|
||
} // namespace P4 | ||
|
||
#endif /* IR_UNPACKER_TABLE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright 2013-present Barefoot Networks, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef IR_UNPARSED_CONSTANT_H_ | ||
#define IR_UNPARSED_CONSTANT_H_ | ||
|
||
#include "lib/cstring.h" | ||
|
||
namespace P4 { | ||
|
||
/** | ||
* An unparsed numeric constant. We produce these as token values during | ||
* lexing. The parser is responsible for actually interpreting the raw text as a | ||
* numeric value and transforming it into an IR::Constant using parseConstant(). | ||
* | ||
* To illustrate how a numeric constant is represented using this struct, | ||
* here is a breakdown of '16w0x12': | ||
* | ||
* ___ | ||
* / ___ | ||
* | / | ||
* | bitwidth (if @hasWidth) | 16 | ||
* | \___ | ||
* | | ||
* | ___ | ||
* | / | ||
* | separator (if @hasWidth) | w | ||
* | \___ | ||
* @text | | ||
* | ___ | ||
* | / | ||
* | ignored prefix of length @skip | 0x | ||
* | \___ | ||
* | | ||
* | ___ | ||
* | / | ||
* | numeric value in base @base | w | ||
* | \___ | ||
* \___ | ||
* | ||
* Simple numeric constants like '5' are specified by setting @hasWidth to | ||
* false and providing a @skip length of 0. | ||
*/ | ||
struct UnparsedConstant { | ||
cstring text; /// Raw P4 text which was recognized as a numeric constant. | ||
unsigned skip; /// An ignored prefix of the numeric constant (e.g. '0x'). | ||
unsigned base; /// The base in which the constant is expressed. | ||
bool hasWidth; /// If true, a bitwidth and separator are present. | ||
}; | ||
|
||
std::ostream &operator<<(std::ostream &out, const UnparsedConstant &constant); | ||
|
||
bool operator<(const UnparsedConstant &a, const UnparsedConstant &b); | ||
|
||
} // namespace P4 | ||
|
||
#endif /* IR_UNPARSED_CONSTANT_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asl I have a question on the RTTI macros. I am trying to factor out some classes to avoid pulling in the entire IR code. Unfortunately, for IDeclaration and INode macros like
DECLARE_TYPEINFO_WITH_TYPEID(INode, NodeKind::INode);
require pulling inir/ir-tree-macros.h
which is generated.Is there a way to avoid this while still being correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the typeid's should be unique. It is part of
gen-tree-macro.h
. The location is a bit weird, but it was emitted there exactly in order not to pull the whole IR header. It could be even split into separate .h file ifgen-tree-macro.h
is undesired.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, gen-tree-macro depends on the generator which is a little unpleasant. Although the code there is less complex than what is being put into ir-generated.h.
You effectively run into #5013.
Maybe there is a way to depend on gen-tree or just the rtti enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, one way to do this is to split enum into "static" and "generated" parts. Static would be for all classes not autogenerated (
INode
,IDeclaration
,Vector
), etc. and everything else will be outside the scope.