P4C
The P4 Compiler
Loading...
Searching...
No Matches
parserControlFlow.h
1/*
2Copyright 2016 VMware, 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_PARSERCONTROLFLOW_H_
18#define FRONTENDS_P4_PARSERCONTROLFLOW_H_
19
20#include "frontends/p4/moveDeclarations.h"
21#include "frontends/p4/simplify.h"
22#include "frontends/p4/uniqueNames.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
80
81 public:
82 DoRemoveParserControlFlow() { setName("DoRemoveParserControlFlow"); }
83 const IR::Node *postorder(IR::ParserState *state) override;
84 Visitor::profile_t init_apply(const IR::Node *node) override;
85};
86
90 public:
91 explicit RemoveParserControlFlow(TypeMap *typeMap) : PassRepeated({}) {
92 passes.emplace_back(new DoRemoveParserControlFlow());
93 passes.emplace_back(new SimplifyControlFlow(typeMap));
94 setName("RemoveParserControlFlow");
95 }
96};
97
99class IfInParser : public Inspector {
100 bool *found;
101
102 public:
103 explicit IfInParser(bool *found) : found(found) {
104 CHECK_NULL(found);
105 setName("IfInParser");
106 }
107 void postorder(const IR::IfStatement *) override {
108 if (findContext<IR::P4Parser>()) *found = true;
109 }
110};
111
115 bool found = false;
116
117 public:
118 explicit RemoveParserIfs(TypeMap *typeMap) {
119 passes.push_back(new IfInParser(&found));
120 passes.push_back(
121 new PassIf([this] { return found; },
122 { // only do this if we found an 'if' in a parser
123 new UniqueNames(), // Give each local declaration a unique internal name
124 new MoveDeclarations(true), // Move all local declarations to the beginning
125 new RemoveParserControlFlow(typeMap)}));
126 }
127};
128
129} // namespace P4
130
131#endif /* FRONTENDS_P4_PARSERCONTROLFLOW_H_ */
Converts if statements in parsers into transitions.
Definition parserControlFlow.h:78
Definition node.h:95
Detect whether the program contains an 'if' statement in a parser.
Definition parserControlFlow.h:99
Definition visitor.h:400
Definition referenceMap.h:36
Definition moveDeclarations.h:34
Definition ir/pass_manager.h:172
Definition ir/pass_manager.h:40
Definition ir/pass_manager.h:145
Definition parserControlFlow.h:89
Definition parserControlFlow.h:114
Definition simplify.h:70
Definition visitor.h:424
Definition typeMap.h:41
Definition uniqueNames.h:70
Definition visitor.h:78
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24