P4C
The P4 Compiler
Loading...
Searching...
No Matches
moveDeclarations.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 FRONTENDS_P4_MOVEDECLARATIONS_H_
18#define FRONTENDS_P4_MOVEDECLARATIONS_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
35 bool parsersOnly;
36
39 // FIXME: Do we really need IR::Vector here?
40 std::vector<IR::Vector<IR::Declaration>> toMove;
41 void push() { toMove.emplace_back(); }
42 void pop() {
43 BUG_CHECK(!toMove.empty(), "Empty move stack");
44 toMove.pop_back();
45 }
46 const IR::Vector<IR::Declaration> &getMoves() const {
47 BUG_CHECK(!toMove.empty(), "Empty move stack");
48 return toMove.back();
49 }
50 IR::Vector<IR::Declaration> &getMoves() {
51 BUG_CHECK(!toMove.empty(), "Empty move stack");
52 return toMove.back();
53 }
54 void addMove(const IR::Declaration *decl) { getMoves().push_back(decl); }
55
56 public:
57 explicit MoveDeclarations(bool parsersOnly = false) : parsersOnly(parsersOnly) {
58 setName("MoveDeclarations");
59 visitDagOnce = false;
60 }
61 void end_apply(const IR::Node *) override { BUG_CHECK(toMove.empty(), "Non empty move stack"); }
62 const IR::Node *preorder(IR::P4Action *action) override {
63 if (parsersOnly) {
64 prune();
65 return action;
66 }
67 if (findContext<IR::P4Control>() == nullptr)
68 // If we are not in a control, move to the beginning of the action.
69 // Otherwise move to the control's beginning.
70 push();
71 return action;
72 }
73 const IR::Node *preorder(IR::P4Control *control) override {
74 if (parsersOnly) {
75 prune();
76 return control;
77 }
78 push();
79 return control;
80 }
81 const IR::Node *preorder(IR::P4Parser *parser) override {
82 push();
83 return parser;
84 }
85 const IR::Node *preorder(IR::Function *function) override {
86 if (parsersOnly) {
87 prune();
88 return function;
89 }
90 push();
91 return function;
92 }
93 const IR::Node *postorder(IR::P4Action *action) override;
94 const IR::Node *postorder(IR::P4Control *control) override;
95 const IR::Node *postorder(IR::P4Parser *parser) override;
96 const IR::Node *postorder(IR::Function *function) override;
97 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
98 const IR::Node *postorder(IR::Declaration_Constant *decl) override;
99};
100
108 MinimalNameGenerator nameGen;
109 IR::IndexedVector<IR::StatOrDecl> toMove; // This contains just IR::AssignmentStatement
110 const IR::ParserState *oldStart; // nullptr if we do not want to rename the start state
111 cstring newStartName; // name allocated to the new start state
112 bool loopsBackToStart; // true if start is reachable from any other state
113
114 public:
115 MoveInitializers() : newStartName("") { setName("MoveInitializers"); }
116 profile_t init_apply(const IR::Node *node) override;
117 const IR::Node *preorder(IR::P4Parser *parser) override;
118 const IR::Node *preorder(IR::Declaration_Variable *decl) override;
119 const IR::Node *postorder(IR::ParserState *state) override;
120 const IR::Node *postorder(IR::Path *path) override;
121 const IR::Node *postorder(IR::P4Parser *parser) override;
122 const IR::Node *postorder(IR::P4Control *control) override;
123};
124
125} // namespace P4
126
127#endif /* FRONTENDS_P4_MOVEDECLARATIONS_H_ */
Definition node.h:52
Definition node.h:94
Definition vector.h:59
Definition referenceMap.h:36
Definition moveDeclarations.h:34
Definition moveDeclarations.h:107
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition visitor.h:424
Definition visitor.h:78
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24