P4C
The P4 Compiler
Loading...
Searching...
No Matches
flattenLogMsg.h
1#ifndef MIDEND_FLATTENLOGMSG_H_
2#define MIDEND_FLATTENLOGMSG_H_
3
4#include "frontends/p4/typeChecking/typeChecker.h"
5#include "ir/ir.h"
6
7namespace P4 {
8
9using TypeLogMsgParams = std::pair<IR::IndexedVector<IR::NamedExpression>, std::string>;
10
11/*
12 Find types in log_msg function for replace.
13*/
15 P4::TypeMap *typeMap;
17 // Map with new log_msg methods where is each key is a original identifier of IR::Node.
19 size_t index;
20
21 public:
22 explicit FindTypesInLogMsgInvocationToReplace(P4::TypeMap *typeMap) : typeMap(typeMap) {
23 setName("FindTypesInLogMsgInvocationToReplace");
24 CHECK_NULL(typeMap);
25 }
26 bool preorder(const IR::MethodCallStatement *methodCallStatement) override;
27 void createReplacement(const IR::Type_StructLike *type);
28 const IR::MethodCallStatement *prepareLogMsgStatement(
29 const IR::MethodCallStatement *methodCallStatement);
30 const IR::Type_StructLike *getReplacement(const cstring name) const {
31 return ::P4::get(replacement, name);
32 }
33 const IR::MethodCallStatement *getReplacementMethodCall(unsigned id) const {
34 return ::P4::get(logMsgReplacement, id);
35 }
36 bool empty() const { return replacement.empty(); }
37 bool hasStructInParameter(const IR::MethodCallStatement *methodCallStatement);
38
39 protected:
40 TypeLogMsgParams unfoldStruct(const IR::Expression *expr, std::string strParam,
41 std::string curName = "");
42 const IR::Type_StructLike *generateNewStructType(const IR::Type_StructLike *structType,
44 IR::ID newName();
45};
46
65 P4::TypeMap *typeMap;
66 FindTypesInLogMsgInvocationToReplace *findTypesInLogMsgInvocationToReplace;
67
68 public:
69 explicit ReplaceLogMsg(
70 P4::TypeMap *typeMap,
71 FindTypesInLogMsgInvocationToReplace *findTypesInLogMsgInvocationToReplace)
72 : typeMap(typeMap),
73 findTypesInLogMsgInvocationToReplace(findTypesInLogMsgInvocationToReplace) {
74 CHECK_NULL(typeMap);
75 CHECK_NULL(findTypesInLogMsgInvocationToReplace);
76 setName("ReplaceLogMsg");
77 }
78 const IR::Node *preorder(IR::P4Program *program) override;
79 const IR::Node *postorder(IR::MethodCallStatement *methodCallStatement) override;
80 const IR::Node *postorder(IR::Type_Struct *typeStruct) override;
81};
82
83class FlattenLogMsg final : public PassManager {
84 public:
85 explicit FlattenLogMsg(P4::TypeMap *typeMap) {
86 auto findTypesInLogMsgInvocationToReplace =
88 passes.push_back(new TypeChecking(nullptr, typeMap));
89 passes.push_back(findTypesInLogMsgInvocationToReplace);
90 passes.push_back(new ReplaceLogMsg(typeMap, findTypesInLogMsgInvocationToReplace));
91 passes.push_back(new ClearTypeMap(typeMap));
92 setName("FlattenLogMsg");
93 }
94};
95
96} // namespace P4
97
98#endif /* MIDEND_FLATTENLOGMSG_H_ */
Definition typeChecker.h:32
Definition flattenLogMsg.h:14
Definition flattenLogMsg.h:83
Definition node.h:52
Definition node.h:94
Definition visitor.h:400
Definition visitor.h:788
Definition ir/pass_manager.h:40
Definition flattenLogMsg.h:64
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition cstring.h:85
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition id.h:28