P4C
The P4 Compiler
Loading...
Searching...
No Matches
eliminateTuples.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 MIDEND_ELIMINATETUPLES_H_
18#define MIDEND_ELIMINATETUPLES_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
32 std::set<const IR::Type_Struct *> inserted;
33 const IR::Type *convertType(const IR::Type *type);
34
35 public:
36 NameGenerator &ng;
37 P4::TypeMap *typeMap;
38
39 ReplacementMap(NameGenerator &ng, TypeMap *typeMap) : ng(ng), typeMap(typeMap) {
40 CHECK_NULL(typeMap);
41 }
42 const IR::Type_Struct *getReplacement(const IR::Type_BaseList *tt);
43 IR::IndexedVector<IR::Node> *getNewReplacements();
44};
45
77class DoReplaceTuples final : public Transform {
78 ReplacementMap repl;
80
81 public:
82 explicit DoReplaceTuples(TypeMap *typeMap) : repl(ng, typeMap) { setName("DoReplaceTuples"); }
83 Visitor::profile_t init_apply(const IR::Node *node) override {
84 auto rv = Transform::init_apply(node);
85 node->apply(ng);
86
87 return rv;
88 }
89 const IR::Node *skip(const IR::Node *node) {
90 prune();
91 return node;
92 }
93 const IR::Node *postorder(IR::Type_BaseList *type) override;
94 const IR::Node *insertReplacements(const IR::Node *before);
95 const IR::Node *postorder(IR::Type_Struct *type) override { return insertReplacements(type); }
96 const IR::Node *postorder(IR::ArrayIndex *expression) override;
97 const IR::Node *postorder(IR::Type_Typedef *type) override { return insertReplacements(type); }
98 const IR::Node *postorder(IR::Type_Newtype *type) override { return insertReplacements(type); }
99 const IR::Node *postorder(IR::P4Parser *parser) override { return insertReplacements(parser); }
100 const IR::Node *postorder(IR::P4Control *control) override {
101 return insertReplacements(control);
102 }
103 const IR::Node *postorder(IR::Method *method) override { return insertReplacements(method); }
104 const IR::Node *postorder(IR::Type_Extern *ext) override { return insertReplacements(ext); }
105 const IR::Node *postorder(IR::Declaration_Instance *decl) override {
106 return insertReplacements(decl);
107 }
108 const IR::Node *preorder(IR::P4ValueSet *set) override {
109 // Disable substitution of type parameters for value sets.
110 // We want to keep these as tuples.
111 return skip(set);
112 }
113 const IR::Node *preorder(IR::P4ListExpression *expression) override { return skip(expression); }
114 const IR::Node *preorder(IR::Type_P4List *list) override { return skip(list); }
115};
116
117class EliminateTuples final : public PassManager {
118 public:
119 explicit EliminateTuples(TypeMap *typeMap, TypeChecking *typeChecking = nullptr,
120 TypeInference *typeInference = nullptr) {
121 passes.push_back(typeChecking ? typeChecking : new TypeChecking(nullptr, typeMap));
122 passes.push_back(new DoReplaceTuples(typeMap));
123 passes.push_back(new ClearTypeMap(typeMap));
124 // We do a round of type-checking which may mutate the program.
125 // This will convert some ListExpressions
126 // into StructExpression where tuples were converted
127 // to structs.
128 passes.push_back(typeInference ? typeInference : new TypeInference(typeMap, false));
129 setName("EliminateTuples");
130 }
131};
132
133} // namespace P4
134
135#endif /* MIDEND_ELIMINATETUPLES_H_ */
Definition typeChecker.h:32
Definition eliminateTuples.h:77
Definition eliminateTuples.h:117
Definition node.h:52
Definition node.h:94
Definition referenceMap.h:36
Definition referenceMap.h:29
Definition ir/pass_manager.h:40
Definition eliminateTuples.h:30
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeChecker.h:483
Definition typeMap.h:41
Definition visitor.h:78
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24