P4C
The P4 Compiler
Loading...
Searching...
No Matches
actionSynthesis.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef MIDEND_ACTIONSYNTHESIS_H_
18#define MIDEND_ACTIONSYNTHESIS_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
31 public:
32 virtual ~ActionSynthesisPolicy() {}
37 virtual bool convert(const Visitor::Context *ctxt, const IR::P4Control *control) = 0;
38
49 virtual bool can_combine(const Visitor::Context *, const IR::BlockStatement *,
50 const IR::StatOrDecl *) {
51 return true;
52 }
53};
54
77 ReferenceMap *refMap;
78 TypeMap *typeMap;
79 std::vector<const IR::P4Table *> tables; // inserted tables
80
81 public:
83 : refMap(refMap), typeMap(typeMap) {
84 CHECK_NULL(refMap);
85 CHECK_NULL(typeMap);
86 setName("DoMoveActionsToTables");
87 }
88 const IR::Node *preorder(IR::P4Parser *parser) override {
89 prune();
90 return parser;
91 }
92 const IR::Node *postorder(IR::P4Control *control) override;
93 const IR::Node *preorder(IR::P4Control *control) override {
94 tables.clear();
95 return control;
96 }
97 const IR::Node *preorder(IR::P4Action *action) override {
98 prune();
99 return action;
100 }
101 const IR::Node *postorder(IR::MethodCallStatement *statement) override;
102};
103
122 ReferenceMap *refMap;
123 TypeMap *typeMap;
124 std::vector<const IR::P4Action *> actions; // inserted actions
125 bool changes = false;
126 ActionSynthesisPolicy *policy;
127
128 public:
129 // If true the statement must be moved to an action
130 bool mustMove(const IR::MethodCallStatement *statement);
131 bool mustMove(const IR::AssignmentStatement *statement);
132
134 : refMap(refMap), typeMap(typeMap), policy(policy) {
135 CHECK_NULL(refMap);
136 CHECK_NULL(typeMap);
137 setName("DoSynthesizeActions");
138 }
139 const IR::Node *preorder(IR::P4Parser *parser) override {
140 prune();
141 return parser;
142 }
143 const IR::Node *postorder(IR::P4Control *control) override;
144 const IR::Node *preorder(IR::P4Control *control) override;
145 const IR::Node *preorder(IR::P4Action *action) override {
146 prune();
147 return action;
148 } // skip actions
149 // We do not handle return: this pass should be called after it has been removed
150 const IR::Node *preorder(IR::BlockStatement *statement) override;
151 const IR::Node *preorder(IR::AssignmentStatement *statement) override;
152 const IR::Node *preorder(IR::MethodCallStatement *statement) override;
153 const IR::Node *preorder(IR::ExitStatement *statement) override;
154 const IR::Node *preorder(IR::Function *function) override {
155 prune();
156 return function;
157 }
158
159 protected:
160 const IR::Statement *createAction(const IR::Statement *body);
161};
162
164 public:
165 SynthesizeActions(ReferenceMap *refMap, TypeMap *typeMap,
166 ActionSynthesisPolicy *policy = nullptr,
167 TypeChecking *typeChecking = nullptr) {
168 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap);
169 passes.push_back(typeChecking);
170 passes.push_back(new DoSynthesizeActions(refMap, typeMap, policy));
171 setName("SynthesizeActions");
172 }
173};
174
176 public:
177 MoveActionsToTables(ReferenceMap *refMap, TypeMap *typeMap,
178 TypeChecking *typeChecking = nullptr) {
179 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap);
180 passes.push_back(typeChecking);
181 passes.push_back(new DoMoveActionsToTables(refMap, typeMap));
182 setName("MoveActionsToTables");
183 }
184};
185
186} // namespace P4
187
188#endif /* MIDEND_ACTIONSYNTHESIS_H_ */
Definition actionSynthesis.h:30
virtual bool can_combine(const Visitor::Context *, const IR::BlockStatement *, const IR::StatOrDecl *)
Definition actionSynthesis.h:49
virtual bool convert(const Visitor::Context *ctxt, const IR::P4Control *control)=0
Definition actionSynthesis.h:76
Definition actionSynthesis.h:121
Definition node.h:95
Definition actionSynthesis.h:175
Definition pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition actionSynthesis.h:163
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition visitor.h:47