P4C
The P4 Compiler
Loading...
Searching...
No Matches
removeReturns.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_REMOVERETURNS_H_
18#define FRONTENDS_P4_REMOVERETURNS_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/ternaryBool.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "ir/ir.h"
24
25namespace P4 {
26using namespace literals;
27
33class HasExits : public Inspector {
34 public:
35 bool hasExits;
36 bool hasReturns;
37 HasExits() : hasExits(false), hasReturns(false) { setName("HasExits"); }
38
39 void postorder(const IR::ExitStatement *) override { hasExits = true; }
40 void postorder(const IR::ReturnStatement *) override { hasReturns = true; }
41};
42
69 /* This pass does not use (inherit from) ControlFlowVisitor, even though it is doing
70 * control flow analysis, as it turns out to be more efficient to do it directly here
71 * by overloading the branching constructs (if/switch/loops) and not cloning the visitor,
72 * because we *only* care about statements (not expressions) locally in a block (and
73 * not across calls), and we only have one bit of data (hasJumped flag). */
74
77 bool hasJumped = false;
78
81 bool movedToIfBranch = false;
82
83 bool preorder(IR::BlockStatement *) override;
84 bool moveFromParentTo(const IR::Statement *&child);
85 bool preorder(IR::IfStatement *) override;
86 bool preorder(IR::SwitchStatement *) override;
87 void postorder(IR::LoopStatement *) override;
88 bool branch() {
89 hasJumped = true;
90 // no need to visit children
91 return false;
92 }
93 bool preorder(IR::BreakStatement *) override { return branch(); }
94 bool preorder(IR::ContinueStatement *) override { return branch(); }
95 bool preorder(IR::ExitStatement *) override { return branch(); }
96 bool preorder(IR::ReturnStatement *) override { return branch(); }
97 // Only visit statements, skip all expressions
98 bool preorder(IR::Expression *) override { return false; }
99
100 public:
102};
103
122 protected:
123 MinimalNameGenerator nameGen;
124 IR::ID returnVar; // one for each context
125 IR::ID returnedValue; // only for functions that return expressions
126 cstring variableName;
127 cstring retValName;
128
129 std::vector<TernaryBool> stack;
130 void push() { stack.push_back(TernaryBool::No); }
131 void pop() { stack.pop_back(); }
132 void set(TernaryBool r) {
133 BUG_CHECK(!stack.empty(), "Empty stack");
134 stack.back() = r;
135 }
136 TernaryBool hasReturned() {
137 BUG_CHECK(!stack.empty(), "Empty stack");
138 return stack.back();
139 }
140
141 public:
142 explicit DoRemoveReturns(cstring varName = "hasReturned"_cs, cstring retValName = "retval"_cs)
143 : variableName(varName), retValName(retValName) {
144 visitDagOnce = false;
145 setName("DoRemoveReturns");
146 }
147
148 const IR::Node *preorder(IR::Function *function) override;
149 const IR::Node *preorder(IR::BlockStatement *statement) override;
150 const IR::Node *preorder(IR::ReturnStatement *statement) override;
151 const IR::Node *preorder(IR::ExitStatement *statement) override;
152 const IR::Node *preorder(IR::IfStatement *statement) override;
153 const IR::Node *preorder(IR::SwitchStatement *statement) override;
154
155 const IR::Node *preorder(IR::P4Action *action) override;
156 const IR::Node *preorder(IR::P4Control *control) override;
157 const IR::Node *preorder(IR::P4Parser *parser) override {
158 prune();
159 return parser;
160 }
161
162 const IR::Node *postorder(IR::LoopStatement *loop) override;
163 profile_t init_apply(const IR::Node *node) override;
164};
165
167 public:
169};
170
171} // namespace P4
172
173#endif /* FRONTENDS_P4_REMOVERETURNS_H_ */
Definition removeReturns.h:121
Definition removeReturns.h:33
Definition node.h:95
Definition visitor.h:400
Definition referenceMap.h:36
Definition visitor.h:372
Definition removeReturns.h:68
Definition ir/pass_manager.h:40
Definition removeReturns.h:166
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
Definition id.h:28