P4C
The P4 Compiler
Loading...
Searching...
No Matches
specialize.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_SPECIALIZE_H_
18#define FRONTENDS_P4_SPECIALIZE_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/typeMap.h"
23#include "ir/ir.h"
24#include "ir/pass_manager.h"
25
26namespace P4 {
27
28class FrontEndPolicy; // full definition not needed here
29
35 const IR::IContainer *specialized;
46
47 SpecializationInfo(const IR::Node *invocation, const IR::IContainer *cont,
48 const IR::Node *insertion)
49 : specialized(cont),
50 typeArguments(nullptr),
51 constructorArguments(new IR::Vector<IR::Argument>()),
53 insertBefore(insertion) {
54 CHECK_NULL(cont);
55 CHECK_NULL(invocation);
56 CHECK_NULL(insertion);
57 }
58 const IR::Type_Declaration *synthesize(const Visitor::Context *ctxt);
59};
60
65
66 public:
67 TypeMap *typeMap = nullptr;
74 void addSpecialization(const IR::ConstructorCallExpression *invocation,
75 const IR::IContainer *container, const IR::Node *insertion,
76 DeclarationLookup *declLookup, NameGenerator *nameGen);
83 void addSpecialization(const IR::Declaration_Instance *invocation,
84 const IR::IContainer *container, const IR::Node *insertion,
85 DeclarationLookup *declLookup, NameGenerator *nameGen);
86 IR::Vector<IR::Node> *getSpecializations(const IR::Node *insertion,
87 const Visitor::Context *ctxt) const;
88 cstring getName(const IR::Node *insertion) const {
89 auto s = ::P4::get(specializations, insertion);
90 if (s == nullptr) return nullptr;
91 return s->name;
92 }
93 void clear() { specializations.clear(); }
94};
95
100 SpecializationMap *specMap;
101 MinimalNameGenerator nameGen;
102
103 public:
104 explicit FindSpecializations(SpecializationMap *specMap) : specMap(specMap) {
105 CHECK_NULL(specMap);
106 setName("FindSpecializations");
107 }
108
109 const IR::Node *findInsertionPoint() const;
110 bool isSimpleConstant(const IR::Expression *expression) const;
111 Visitor::profile_t init_apply(const IR::Node *node) override {
112 auto rv = Inspector::init_apply(node);
113
114 specMap->clear();
115 node->apply(nameGen);
116
117 return rv;
118 }
122 bool noParameters(const IR::IContainer *container);
123
124 bool preorder(const IR::P4Parser *parser) override { return noParameters(parser); }
125 bool preorder(const IR::P4Control *control) override { return noParameters(control); }
126 void postorder(const IR::ConstructorCallExpression *expression) override;
127 void postorder(const IR::Declaration_Instance *decl) override;
128
129 bool preorder(const IR::Parameter *) override {
130 // This prevents specialization of the default values of
131 // parameters; we don't care to specialize these, we only need
132 // to specialize them if they appear in a call.
133 return false;
134 }
135};
136
172class Specialize : public Transform {
173 SpecializationMap *specMap;
174 const IR::Node *instantiate(const IR::Node *node, const Visitor::Context *ctxt);
175
176 public:
177 explicit Specialize(SpecializationMap *specMap) : specMap(specMap) {
178 CHECK_NULL(specMap);
179 setName("Specialize");
180 }
181 const IR::Node *postorder(IR::P4Parser *parser) override {
182 return instantiate(parser, getContext());
183 }
184 // skip packages
185 const IR::Node *preorder(IR::Type_Package *package) override {
186 prune();
187 return package;
188 }
189 const IR::Node *postorder(IR::P4Control *control) override {
190 return instantiate(control, getContext());
191 }
192 const IR::Node *postorder(IR::ConstructorCallExpression *expression) override;
193 const IR::Node *postorder(IR::Declaration_Instance *) override;
194};
195
221 SpecializationMap specMap;
222
223 public:
224 SpecializeAll(TypeMap *typeMap, FrontEndPolicy *policy);
225};
226
227} // namespace P4
228
229#endif /* FRONTENDS_P4_SPECIALIZE_H_ */
Definition referenceMap.h:57
Definition specialize.h:99
bool noParameters(const IR::IContainer *container)
Definition specialize.cpp:196
Definition frontends/p4/frontend.h:32
Definition node.h:52
Definition node.h:94
Definition vector.h:59
Definition visitor.h:400
Definition referenceMap.h:36
Definition referenceMap.h:29
Definition ir/pass_manager.h:145
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Maintains a map from invocation to a SpecializationInfo object.
Definition specialize.h:62
void addSpecialization(const IR::ConstructorCallExpression *invocation, const IR::IContainer *container, const IR::Node *insertion, DeclarationLookup *declLookup, NameGenerator *nameGen)
Definition specialize.cpp:83
Specializes each Parser and Control by substituting type arguments and constructor parameters.
Definition specialize.h:220
Specializes each Parser and Control with constant constructor arguments by substituting type argument...
Definition specialize.h:172
Definition visitor.h:424
Definition typeMap.h:41
Definition visitor.h:78
Definition cstring.h:85
Definition ordered_map.h:32
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Describes how a parser or control is specialized.
Definition specialize.h:31
const IR::IContainer * specialized
Actual parser or control that is being specialized.
Definition specialize.h:35
cstring name
Name to use for specialized object.
Definition specialize.h:33
const IR::Node * insertBefore
Where in the program should the specialization be inserted.
Definition specialize.h:45
const IR::Node * invocation
Invocation which causes this specialization.
Definition specialize.h:43
IR::IndexedVector< IR::Declaration > declarations
Declarations to insert in the list of locals.
Definition specialize.h:41
const IR::Vector< IR::Type > * typeArguments
Values to substitute for type arguments.
Definition specialize.h:37
IR::Vector< IR::Argument > * constructorArguments
Values to substitute for constructor arguments.
Definition specialize.h:39
Definition visitor.h:47