P4C
The P4 Compiler
Loading...
Searching...
No Matches
hsIndexSimplify.h
1#ifndef MIDEND_HSINDEXSIMPLIFY_H_
2#define MIDEND_HSINDEXSIMPLIFY_H_
3
4#include <memory>
5
6#include "frontends/p4/typeChecking/typeChecker.h"
7#include "frontends/p4/typeMap.h"
8#include "ir/ir.h"
9
10namespace P4 {
11
12using GeneratedVariablesMap = std::map<cstring, const IR::PathExpression *>;
13
17class HSIndexFinder : public Inspector {
18 friend class HSIndexTransform;
19 friend class HSIndexContretizer;
21 std::shared_ptr<NameGenerator> nameGen;
22 TypeMap *typeMap;
23 const IR::ArrayIndex *arrayIndex = nullptr;
24 const IR::PathExpression *newVariable = nullptr;
25 GeneratedVariablesMap *generatedVariables;
26 std::set<cstring> storedMember;
27 std::list<IR::Member *> dependedMembers;
28
29 public:
31 std::shared_ptr<NameGenerator> nameGen, TypeMap *typeMap,
32 GeneratedVariablesMap *generatedVariables)
33 : locals(locals),
34 nameGen(nameGen),
35 typeMap(typeMap),
36 generatedVariables(generatedVariables) {}
37 void postorder(const IR::ArrayIndex *curArrayIndex) override;
38
39 protected:
40 void addNewVariable();
41};
42
45 friend class HSIndexContretizer;
46 size_t index;
47 HSIndexFinder &hsIndexFinder;
48
49 public:
50 HSIndexTransform(HSIndexFinder &finder, size_t index) : index(index), hsIndexFinder(finder) {}
51 const IR::Node *postorder(IR::ArrayIndex *curArrayIndex) override;
52};
53
70 TypeMap *typeMap;
71 std::shared_ptr<MinimalNameGenerator> nameGen;
73 GeneratedVariablesMap *generatedVariables;
74
75 public:
76 explicit HSIndexContretizer(TypeMap *typeMap,
77 std::shared_ptr<MinimalNameGenerator> nameGen = nullptr,
78 IR::IndexedVector<IR::Declaration> *locals = nullptr,
79 GeneratedVariablesMap *generatedVariables = nullptr)
80 : typeMap(typeMap),
81 nameGen(nameGen),
82 locals(locals),
83 generatedVariables(generatedVariables) {
84 if (generatedVariables == nullptr) generatedVariables = new GeneratedVariablesMap();
85 }
86 Visitor::profile_t init_apply(const IR::Node *node) override {
87 auto rv = Transform::init_apply(node);
88
89 if (!nameGen) {
90 nameGen = std::make_shared<MinimalNameGenerator>();
91 node->apply(*nameGen);
92 }
93
94 return rv;
95 }
96
97 IR::Node *preorder(IR::IfStatement *ifStatement) override;
98 IR::Node *preorder(IR::AssignmentStatement *assignmentStatement) override;
99 IR::Node *preorder(IR::BlockStatement *blockStatement) override;
100 IR::Node *preorder(IR::MethodCallStatement *methodCallStatement) override;
101 IR::Node *preorder(IR::P4Control *control) override;
102 IR::Node *preorder(IR::P4Parser *parser) override;
103 IR::Node *preorder(IR::SwitchStatement *switchStatement) override;
104
105 protected:
106 IR::Node *eliminateArrayIndexes(HSIndexFinder &aiFinder, IR::Statement *statement,
107 const IR::Expression *expr);
108};
109
111 public:
112 explicit HSIndexSimplifier(TypeMap *typeMap) {
113 // remove block statements
114 passes.push_back(new TypeChecking(nullptr, typeMap, true));
115 passes.push_back(new HSIndexContretizer(typeMap));
116 setName("HSIndexSimplifier");
117 }
118};
119
120} // namespace P4
121
122#endif /* MIDEND_HSINDEXSIMPLIFY_H_ */
Definition hsIndexSimplify.h:69
Definition hsIndexSimplify.h:17
Definition hsIndexSimplify.h:110
This class substitutes index of a header stack in all occurences of found header stack.
Definition hsIndexSimplify.h:44
Definition node.h:52
Definition node.h:94
Definition visitor.h:400
Definition ir/pass_manager.h:40
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition visitor.h:78
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24