Skip to content

Commit

Permalink
Add a pass which converts struct expressions with Type_Header to head…
Browse files Browse the repository at this point in the history
…er expressions.
  • Loading branch information
fruffy committed Nov 15, 2023
1 parent 6845c48 commit 6f91dae
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions backends/p4tools/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(

compiler/compiler_target.cpp
compiler/convert_hs_index.cpp
compiler/convert_struct_expr.cpp
compiler/convert_varbits.cpp
compiler/midend.cpp
compiler/reachability.cpp
Expand Down
24 changes: 24 additions & 0 deletions backends/p4tools/common/compiler/convert_struct_expr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "backends/p4tools/common/compiler/convert_struct_expr.h"

#include "ir/irutils.h"

namespace P4Tools {

const IR::Node *ConvertStructExpr::postorder(IR::StructExpression *expr) {
auto structType = expr->type;
if (structType->is<IR::Type_Name>()) {
structType = typeMap->getTypeType(structType, true);
}
if (structType->is<IR::Type_Header>()) {
auto structExpr = expr->to<IR::StructExpression>();
return new IR::HeaderExpression(structExpr->srcInfo, expr->type, expr->type,
structExpr->components, IR::getBoolLiteral(true));
}
return expr;
}

ConvertStructExpr::ConvertStructExpr(const P4::TypeMap *typeMap) : typeMap(typeMap) {
setName("ConvertStructExpr");
visitDagOnce = false;
}
} // namespace P4Tools
23 changes: 23 additions & 0 deletions backends/p4tools/common/compiler/convert_struct_expr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef BACKENDS_P4TOOLS_COMMON_COMPILER_CONVERT_STRUCT_EXPR_H_
#define BACKENDS_P4TOOLS_COMMON_COMPILER_CONVERT_STRUCT_EXPR_H_

#include "frontends/p4/typeMap.h"
#include "ir/ir.h"
#include "ir/node.h"
#include "ir/visitor.h"

namespace P4Tools {

class ConvertStructExpr : public Transform {
private:
const P4::TypeMap *typeMap;

public:
explicit ConvertStructExpr(const P4::TypeMap *typeMap);

const IR::Node *postorder(IR::StructExpression *expr) override;
};

} // namespace P4Tools

#endif /* BACKENDS_P4TOOLS_COMMON_COMPILER_CONVERT_STRUCT_EXPR_H_ */
4 changes: 3 additions & 1 deletion backends/p4tools/common/compiler/midend.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "backends/p4tools/common/compiler/midend.h"

#include "backends/p4tools/common/compiler/convert_struct_expr.h"
#include "backends/p4tools/common/compiler/convert_varbits.h"
#include "frontends/common/constantFolding.h"
#include "frontends/common/options.h"
Expand Down Expand Up @@ -30,7 +31,6 @@
#include "midend/orderArguments.h"
#include "midend/parserUnroll.h"
#include "midend/removeLeftSlices.h"
#include "midend/removeMiss.h"
#include "midend/removeSelectBooleans.h"
#include "midend/replaceSelectRange.h"
#include "midend/simplifyBitwise.h"
Expand Down Expand Up @@ -162,6 +162,8 @@ void MidEnd::addDefaultPasses() {
new P4::HSIndexSimplifier(&refMap, &typeMap),
// Convert Type_Varbits into a type that contains information about the assigned width.
new ConvertVarbits(),
// Convert any StructExpressions with Type_Header into a HeaderExpression.
new ConvertStructExpr(&typeMap),
// Cast all boolean table keys with a bit<1>.
new P4::CastBooleanTableKeys(),
});
Expand Down

0 comments on commit 6f91dae

Please sign in to comment.