P4C
The P4 Compiler
Loading...
Searching...
No Matches
parserControlFlow.h
1/*
2 * Copyright 2016 VMware, Inc.
3 * SPDX-FileCopyrightText: 2016 VMware, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_PARSERCONTROLFLOW_H_
9#define FRONTENDS_P4_PARSERCONTROLFLOW_H_
10
11#include "frontends/p4/moveDeclarations.h"
12#include "frontends/p4/simplify.h"
13#include "frontends/p4/uniqueNames.h"
14#include "ir/ir.h"
15
16namespace P4 {
17
68
69class DoRemoveParserControlFlow : public Transform {
71
72 public:
73 DoRemoveParserControlFlow() { setName("DoRemoveParserControlFlow"); }
74 const IR::Node *postorder(IR::ParserState *state) override;
75 Visitor::profile_t init_apply(const IR::Node *node) override;
76};
77
80class RemoveParserControlFlow : public PassRepeated {
81 public:
82 explicit RemoveParserControlFlow(TypeMap *typeMap) : PassRepeated({}) {
83 passes.emplace_back(new DoRemoveParserControlFlow());
84 passes.emplace_back(new SimplifyControlFlow(typeMap, false));
85 setName("RemoveParserControlFlow");
86 }
87};
88
90class IfInParser : public Inspector {
91 bool *found;
92
93 public:
94 explicit IfInParser(bool *found) : found(found) {
95 CHECK_NULL(found);
96 setName("IfInParser");
97 }
98 void postorder(const IR::IfStatement *) override {
99 if (isInContext<IR::P4Parser>()) *found = true;
100 }
101};
102
105class RemoveParserIfs : public PassManager {
106 bool found = false;
107
108 public:
109 explicit RemoveParserIfs(TypeMap *typeMap) {
110 passes.push_back(new IfInParser(&found));
111 passes.push_back(
112 new PassIf([this] { return found; },
113 { // only do this if we found an 'if' in a parser
114 new UniqueNames(), // Give each local declaration a unique internal name
115 new MoveDeclarations(true), // Move all local declarations to the beginning
116 new RemoveParserControlFlow(typeMap)}));
117 }
118};
119
120} // namespace P4
121
122#endif /* FRONTENDS_P4_PARSERCONTROLFLOW_H_ */
Converts if statements in parsers into transitions.
Definition parserControlFlow.h:69
Definition node.h:53
Detect whether the program contains an 'if' statement in a parser.
Definition parserControlFlow.h:90
Definition visitor.h:418
Definition referenceMap.h:36
Definition moveDeclarations.h:25
Definition ir/pass_manager.h:173
Definition parserControlFlow.h:80
Definition simplify.h:72
Definition visitor.h:442
Definition typeMap.h:32
Definition uniqueNames.h:61
Definition visitor.h:78
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13