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/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/ternaryBool.h"
23#include "frontends/p4/typeChecking/typeChecker.h"
24#include "ir/ir.h"
25
26namespace P4 {
27using namespace literals;
28
34class HasExits : public Inspector {
35 public:
36 bool hasExits;
37 bool hasReturns;
38 HasExits() : hasExits(false), hasReturns(false) { setName("HasExits"); }
39
40 void postorder(const IR::ExitStatement *) override { hasExits = true; }
41 void postorder(const IR::ReturnStatement *) override { hasReturns = true; }
42};
43
61class DoRemoveReturns : public Transform {
62 protected:
64 IR::ID returnVar; // one for each context
65 IR::ID returnedValue; // only for functions that return expressions
66 cstring variableName;
67 cstring retValName;
68
69 std::vector<TernaryBool> stack;
70 void push() { stack.push_back(TernaryBool::No); }
71 void pop() { stack.pop_back(); }
72 void set(TernaryBool r) {
73 BUG_CHECK(!stack.empty(), "Empty stack");
74 stack.back() = r;
75 }
76 TernaryBool hasReturned() {
77 BUG_CHECK(!stack.empty(), "Empty stack");
78 return stack.back();
79 }
80
81 public:
82 explicit DoRemoveReturns(cstring varName = "hasReturned"_cs, cstring retValName = "retval"_cs)
83 : variableName(varName), retValName(retValName) {
84 visitDagOnce = false;
85 setName("DoRemoveReturns");
86 }
87
88 const IR::Node *preorder(IR::Function *function) override;
89 const IR::Node *preorder(IR::BlockStatement *statement) override;
90 const IR::Node *preorder(IR::ReturnStatement *statement) override;
91 const IR::Node *preorder(IR::ExitStatement *statement) override;
92 const IR::Node *preorder(IR::IfStatement *statement) override;
93 const IR::Node *preorder(IR::SwitchStatement *statement) override;
94
95 const IR::Node *preorder(IR::P4Action *action) override;
96 const IR::Node *preorder(IR::P4Control *control) override;
97 const IR::Node *preorder(IR::P4Parser *parser) override {
98 prune();
99 return parser;
100 }
101
102 const IR::Node *postorder(IR::LoopStatement *loop) override;
103 profile_t init_apply(const IR::Node *node) override;
104};
105
106class RemoveReturns : public PassManager {
107 public:
108 RemoveReturns() {
109 passes.push_back(new DoRemoveReturns());
110 setName("RemoveReturns");
111 }
112};
113
114} // namespace P4
115
116#endif /* FRONTENDS_P4_REMOVERETURNS_H_ */
Definition removeReturns.h:61
Definition node.h:95
Definition visitor.h:400
Definition referenceMap.h:36
Definition visitor.h:424
Definition visitor.h:78
Definition cstring.h:85
Definition cstring.h:80
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition id.h:28