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

Simplify jsonAssignment in ParserConverter and ActionConverter #4232

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Simplify jsonAssignment in ParserConverter and ActionConverter
The inParser parameter is not actually used. I think it was introduced
back when both converters shared the same implementation.

Signed-off-by: Antonin Bas <antonin.bas@gmail.com>
  • Loading branch information
antoninbas committed Nov 10, 2023
commit ff649dc0d207fe132948f4fb00433159f2e1eb22
13 changes: 4 additions & 9 deletions backends/bmv2/common/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ limitations under the License.

namespace BMV2 {

cstring ActionConverter::jsonAssignment(const IR::Type *type, bool inParser) {
if (!inParser && type->is<IR::Type_Varbits>()) return "assign_VL";
cstring ActionConverter::jsonAssignment(const IR::Type *type) {
if (type->is<IR::Type_Varbits>()) return "assign_VL";
if (type->is<IR::Type_HeaderUnion>()) return "assign_union";
if (type->is<IR::Type_Header>() || type->is<IR::Type_Struct>()) return "assign_header";
if (auto ts = type->to<IR::Type_Stack>()) {
Expand All @@ -31,12 +31,7 @@ cstring ActionConverter::jsonAssignment(const IR::Type *type, bool inParser) {
else
return "assign_header_stack";
}
if (inParser)
// Unfortunately set can do some things that assign cannot,
// e.g., handle lookahead on the RHS.
return "set";
else
return "assign";
return "assign";
}

void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
Expand Down Expand Up @@ -113,7 +108,7 @@ void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
l = assign->left;
r = assign->right;
auto type = ctxt->typeMap->getType(l, true);
cstring operation = jsonAssignment(type, false);
cstring operation = jsonAssignment(type);
auto primitive = mkPrimitive(operation, result);
auto parameters = mkParameters(primitive);
primitive->emplace_non_null("source_info", assign->sourceInfoJsonObj());
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/common/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ActionConverter : public Inspector {

void convertActionBody(const IR::Vector<IR::StatOrDecl> *body, Util::JsonArray *result);
void convertActionParams(const IR::ParameterList *parameters, Util::JsonArray *params);
cstring jsonAssignment(const IR::Type *type, bool inParser);
cstring jsonAssignment(const IR::Type *type);
void postorder(const IR::P4Action *action) override;

public:
Expand Down
14 changes: 5 additions & 9 deletions backends/bmv2/common/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ limitations under the License.

namespace BMV2 {

cstring ParserConverter::jsonAssignment(const IR::Type *type, bool inParser) {
if (!inParser && type->is<IR::Type_Varbits>()) return "assign_VL";
cstring ParserConverter::jsonAssignment(const IR::Type *type) {
if (type->is<IR::Type_HeaderUnion>()) return "assign_union";
if (type->is<IR::Type_Header>() || type->is<IR::Type_Struct>()) return "assign_header";
if (auto ts = type->to<IR::Type_Stack>()) {
Expand All @@ -36,12 +35,9 @@ cstring ParserConverter::jsonAssignment(const IR::Type *type, bool inParser) {
else
return "assign_header_stack";
}
if (inParser)
// Unfortunately set can do some things that assign cannot,
// e.g., handle lookahead on the RHS.
return "set";
else
return "assign";
// Unfortunately set can do some things that assign cannot, e.g., handle
// lookahead on the RHS.
return "set";
}

Util::IJson *ParserConverter::convertParserStatement(const IR::StatOrDecl *stat) {
Expand Down Expand Up @@ -107,7 +103,7 @@ Util::IJson *ParserConverter::convertParserStatement(const IR::StatOrDecl *stat)
if (stat->is<IR::AssignmentStatement>()) {
auto assign = stat->to<IR::AssignmentStatement>();
auto type = ctxt->typeMap->getType(assign->left, true);
cstring operation = jsonAssignment(type, true);
cstring operation = jsonAssignment(type);
result->emplace("op", operation);
auto l = ctxt->conv->convertLeftValue(assign->left);
bool convertBool = type->is<IR::Type_Boolean>();
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/common/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ParserConverter : public Inspector {
Util::IJson *convertSelectKey(const IR::SelectExpression *expr);
Util::IJson *convertPathExpression(const IR::PathExpression *expr);
Util::IJson *createDefaultTransition();
cstring jsonAssignment(const IR::Type *type, bool inParser);
cstring jsonAssignment(const IR::Type *type);
std::vector<Util::IJson *> convertSelectExpression(const IR::SelectExpression *expr);
void addValueSets(const IR::P4Parser *parser);

Expand Down