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
79class DoRemoveParserControlFlow : public Transform {
80 ReferenceMap *refMap;
81
82 public:
83 explicit DoRemoveParserControlFlow(ReferenceMap *refMap) : refMap(refMap) {
84 CHECK_NULL(refMap);
85 setName("DoRemoveParserControlFlow");
86 }
87 const IR::Node *postorder(IR::ParserState *state) override;
88 Visitor::profile_t init_apply(const IR::Node *node) override;
89};
90
93class RemoveParserControlFlow : public PassRepeated {
94 public:
95 RemoveParserControlFlow(ReferenceMap *refMap, TypeMap *typeMap) : PassRepeated({}) {
96 passes.emplace_back(new DoRemoveParserControlFlow(refMap));
97 passes.emplace_back(new SimplifyControlFlow(refMap, typeMap));
98 setName("RemoveParserControlFlow");
99 }
100};
101
103class IfInParser : public Inspector {
104 bool *found;
105
106 public:
107 explicit IfInParser(bool *found) : found(found) {
108 CHECK_NULL(found);
109 setName("IfInParser");
110 }
111 void postorder(const IR::IfStatement *) override {
112 if (findContext<IR::P4Parser>()) *found = true;
113 }
114};
115
118class RemoveParserIfs : public PassManager {
119 bool found = false;
120
121 public:
122 RemoveParserIfs(ReferenceMap *refMap, TypeMap *typeMap) {
123 passes.push_back(new IfInParser(&found));
124 passes.push_back(new PassIf(
125 [this] { return found; },
126 {// only do this if we found an 'if' in a parser
127 new ResolveReferences(refMap),
128 new UniqueNames(refMap), // Give each local declaration a unique internal name
129 new MoveDeclarations(true), // Move all local declarations to the beginning
130 new ResolveReferences(refMap), new RemoveParserControlFlow(refMap, typeMap)}));
131 }
132};
133
134} // namespace P4
135
136#endif /* FRONTENDS_P4_PARSERCONTROLFLOW_H_ */
Converts if statements in parsers into transitions.
Definition parserControlFlow.h:79
Detect whether the program contains an 'if' statement in a parser.
Definition parserControlFlow.h:103
Definition moveDeclarations.h:33
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition parserControlFlow.h:93
Definition parserControlFlow.h:118
Definition resolveReferences.h:119
Definition simplify.h:73
Definition typeMap.h:42
Definition uniqueNames.h:52
Definition applyOptionsPragmas.cpp:24