diff --git a/backends/p4tools/common/CMakeLists.txt b/backends/p4tools/common/CMakeLists.txt index 27ac042bd2..ba68b56f33 100644 --- a/backends/p4tools/common/CMakeLists.txt +++ b/backends/p4tools/common/CMakeLists.txt @@ -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 diff --git a/backends/p4tools/common/compiler/convert_struct_expr.cpp b/backends/p4tools/common/compiler/convert_struct_expr.cpp new file mode 100644 index 0000000000..5196c2083f --- /dev/null +++ b/backends/p4tools/common/compiler/convert_struct_expr.cpp @@ -0,0 +1,25 @@ +#include "backends/p4tools/common/compiler/convert_struct_expr.h" + +#include "ir/irutils.h" + +namespace P4Tools { + +const IR::Node *ConvertStructExpr::postorder(IR::StructExpression *structExpr) { + auto structType = structExpr->type; + bool resolved = false; + if (structType->is()) { + structType = typeMap->getTypeType(structType, true); + resolved = true; + } + if (resolved) { + return new IR::StructExpression(structExpr->srcInfo, structType, structExpr->type, + structExpr->components); + } + return structExpr; +} + +ConvertStructExpr::ConvertStructExpr(const P4::TypeMap *typeMap) : typeMap(typeMap) { + setName("ConvertStructExpr"); + visitDagOnce = false; +} +} // namespace P4Tools diff --git a/backends/p4tools/common/compiler/convert_struct_expr.h b/backends/p4tools/common/compiler/convert_struct_expr.h new file mode 100644 index 0000000000..1224b47b8e --- /dev/null +++ b/backends/p4tools/common/compiler/convert_struct_expr.h @@ -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_ */ diff --git a/backends/p4tools/common/compiler/midend.cpp b/backends/p4tools/common/compiler/midend.cpp index c670bcd851..ba0e4be161 100644 --- a/backends/p4tools/common/compiler/midend.cpp +++ b/backends/p4tools/common/compiler/midend.cpp @@ -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" @@ -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" @@ -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(), });