P4C
The P4 Compiler
Loading...
Searching...
No Matches
moveDeclarations.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_MOVEDECLARATIONS_H_
9#define FRONTENDS_P4_MOVEDECLARATIONS_H_
10
11#include "frontends/common/resolveReferences/referenceMap.h"
12#include "frontends/common/resolveReferences/resolveReferences.h"
13#include "ir/ir.h"
14
15namespace P4 {
16
25class MoveDeclarations : public Transform {
26 bool parsersOnly;
27
30 // FIXME: Do we really need IR::Vector here?
31 std::vector<IR::Vector<IR::Declaration>> toMove;
32 void push() { toMove.emplace_back(); }
33 void pop() {
34 BUG_CHECK(!toMove.empty(), "Empty move stack");
35 toMove.pop_back();
36 }
37 const IR::Vector<IR::Declaration> &getMoves() const {
38 BUG_CHECK(!toMove.empty(), "Empty move stack");
39 return toMove.back();
40 }
41 IR::Vector<IR::Declaration> &getMoves() {
42 BUG_CHECK(!toMove.empty(), "Empty move stack");
43 return toMove.back();
44 }
45 void addMove(const IR::Declaration *decl) { getMoves().push_back(decl); }
46
47 public:
48 explicit MoveDeclarations(bool parsersOnly = false) : parsersOnly(parsersOnly) {
49 setName("MoveDeclarations");
50 visitDagOnce = false;
51 }
52 void end_apply(const IR::Node *) override { BUG_CHECK(toMove.empty(), "Non empty move stack"); }
53 const IR::Node *preorder(IR::P4Action *action) override {
54 if (parsersOnly) {
55 prune();
56 return action;
57 }
58 if (!isInContext<IR::P4Control>())
59 // If we are not in a control, move to the beginning of the action.
60 // Otherwise move to the control's beginning.
61 push();
62 return action;
63 }
64 const IR::Node *preorder(IR::P4Control *control) override {
65 if (parsersOnly) {
66 prune();
67 return control;
68 }
69 push();
70 return control;
71 }
72 const IR::Node *preorder(IR::P4Parser *parser) override {
73 push();
74 return parser;
75 }
76 const IR::Node *preorder(IR::Function *function) override {
77 if (parsersOnly) {
78 prune();
79 return function;
80 }
81 push();
82 return function;
83 }
84 const IR::Node *postorder(IR::P4Action *action) override;
85 const IR::Node *postorder(IR::P4Control *control) override;
86 const IR::Node *postorder(IR::P4Parser *parser) override;
87 const IR::Node *postorder(IR::Function *function) override;
88 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
89 const IR::Node *postorder(IR::Declaration_Constant *decl) override;
90};
91
98class MoveInitializers : public Transform, public ResolutionContext {
100 IR::IndexedVector<IR::StatOrDecl> toMove; // This contains just IR::AssignmentStatement
101 const IR::ParserState *oldStart; // nullptr if we do not want to rename the start state
102 cstring newStartName; // name allocated to the new start state
103 bool loopsBackToStart; // true if start is reachable from any other state
104
105 public:
106 MoveInitializers() : newStartName("") { setName("MoveInitializers"); }
107 profile_t init_apply(const IR::Node *node) override;
108 const IR::Node *preorder(IR::P4Parser *parser) override;
109 const IR::Node *preorder(IR::Declaration_Variable *decl) override;
110 const IR::Node *postorder(IR::ParserState *state) override;
111 const IR::Node *postorder(IR::Path *path) override;
112 const IR::Node *postorder(IR::P4Parser *parser) override;
113 const IR::Node *postorder(IR::P4Control *control) override;
114};
115
116} // namespace P4
117
118#endif /* FRONTENDS_P4_MOVEDECLARATIONS_H_ */
Definition indexed_vector.h:31
Definition node.h:53
Definition ir/vector.h:59
Definition referenceMap.h:36
Definition visitor.h:442
Definition visitor.h:78
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13