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/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/moveDeclarations.h"
22#include "frontends/p4/simplify.h"
23#include "frontends/p4/uniqueNames.h"
24#include "ir/ir.h"
25
26namespace P4 {
27
81
82 public:
83 DoRemoveParserControlFlow() { setName("DoRemoveParserControlFlow"); }
84 const IR::Node *postorder(IR::ParserState *state) override;
85 Visitor::profile_t init_apply(const IR::Node *node) override;
86};
87
91 public:
92 explicit RemoveParserControlFlow(TypeMap *typeMap) : PassRepeated({}) {
93 passes.emplace_back(new DoRemoveParserControlFlow());
94 passes.emplace_back(new SimplifyControlFlow(typeMap));
95 setName("RemoveParserControlFlow");
96 }
97};
98
100class IfInParser : public Inspector {
101 bool *found;
102
103 public:
104 explicit IfInParser(bool *found) : found(found) {
105 CHECK_NULL(found);
106 setName("IfInParser");
107 }
108 void postorder(const IR::IfStatement *) override {
109 if (findContext<IR::P4Parser>()) *found = true;
110 }
111};
112
116 bool found = false;
117
118 public:
119 explicit RemoveParserIfs(TypeMap *typeMap) {
120 passes.push_back(new IfInParser(&found));
121 passes.push_back(
122 new PassIf([this] { return found; },
123 { // only do this if we found an 'if' in a parser
124 new UniqueNames(), // Give each local declaration a unique internal name
125 new MoveDeclarations(true), // Move all local declarations to the beginning
126 new RemoveParserControlFlow(typeMap)}));
127 }
128};
129
130} // namespace P4
131
132#endif /* FRONTENDS_P4_PARSERCONTROLFLOW_H_ */
Converts if statements in parsers into transitions.
Definition parserControlFlow.h:79
Definition node.h:95
Detect whether the program contains an 'if' statement in a parser.
Definition parserControlFlow.h:100
Definition visitor.h:400
Definition referenceMap.h:36
Definition moveDeclarations.h:34
Definition pass_manager.h:172
Definition pass_manager.h:40
Definition pass_manager.h:145
Definition parserControlFlow.h:90
Definition parserControlFlow.h:115
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