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 std::vector<IR::Vector<IR::Declaration> *> toMove;
40 void push() { toMove.push_back(new IR::Vector<IR::Declaration>()); }
41 void pop() {
42 BUG_CHECK(!toMove.empty(), "Empty move stack");
43 toMove.pop_back();
44 }
45 IR::Vector<IR::Declaration> *getMoves() const {
46 BUG_CHECK(!toMove.empty(), "Empty move stack");
47 return toMove.back();
48 }
49 void addMove(const IR::Declaration *decl) { getMoves()->push_back(decl); }
50
51 public:
52 explicit MoveDeclarations(bool parsersOnly = false) : parsersOnly(parsersOnly) {
53 setName("MoveDeclarations");
54 visitDagOnce = false;
55 }
56 void end_apply(const IR::Node *) override { BUG_CHECK(toMove.empty(), "Non empty move stack"); }
57 const IR::Node *preorder(IR::P4Action *action) override {
58 if (parsersOnly) {
59 prune();
60 return action;
61 }
62 if (findContext<IR::P4Control>() == nullptr)
63 // If we are not in a control, move to the beginning of the action.
64 // Otherwise move to the control's beginning.
65 push();
66 return action;
67 }
68 const IR::Node *preorder(IR::P4Control *control) override {
69 if (parsersOnly) {
70 prune();
71 return control;
72 }
73 push();
74 return control;
75 }
76 const IR::Node *preorder(IR::P4Parser *parser) override {
77 push();
78 return parser;
79 }
80 const IR::Node *preorder(IR::Function *function) override {
81 if (parsersOnly) {
82 prune();
83 return function;
84 }
85 push();
86 return function;
87 }
88 const IR::Node *postorder(IR::P4Action *action) override;
89 const IR::Node *postorder(IR::P4Control *control) override;
90 const IR::Node *postorder(IR::P4Parser *parser) override;
91 const IR::Node *postorder(IR::Function *function) override;
92 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
93 const IR::Node *postorder(IR::Declaration_Constant *decl) override;
94};
95
103 MinimalNameGenerator nameGen;
104 IR::IndexedVector<IR::StatOrDecl> toMove; // This contains just IR::AssignmentStatement
105 const IR::ParserState *oldStart; // nullptr if we do not want to rename the start state
106 cstring newStartName; // name allocated to the new start state
107 bool loopsBackToStart; // true if start is reachable from any other state
108
109 public:
110 MoveInitializers() : newStartName("") { setName("MoveInitializers"); }
111 profile_t init_apply(const IR::Node *node) override;
112 const IR::Node *preorder(IR::P4Parser *parser) override;
113 const IR::Node *preorder(IR::Declaration_Variable *decl) override;
114 const IR::Node *postorder(IR::ParserState *state) override;
115 const IR::Node *postorder(IR::Path *path) override;
116 const IR::Node *postorder(IR::P4Parser *parser) override;
117 const IR::Node *postorder(IR::P4Control *control) override;
118};
119
120} // namespace P4
121
122#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:102
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