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>()),
52 declarations(new IR::IndexedVector<IR::Declaration>()),
54 insertBefore(insertion) {
55 CHECK_NULL(cont);
56 CHECK_NULL(invocation);
57 CHECK_NULL(insertion);
58 }
59 const IR::Type_Declaration *synthesize(const Visitor::Context *ctxt) const;
60};
61
66
67 public:
68 TypeMap *typeMap = nullptr;
75 void addSpecialization(const IR::ConstructorCallExpression *invocation,
76 const IR::IContainer *container, const IR::Node *insertion,
77 DeclarationLookup *declLookup, NameGenerator *nameGen);
84 void addSpecialization(const IR::Declaration_Instance *invocation,
85 const IR::IContainer *container, const IR::Node *insertion,
86 DeclarationLookup *declLookup, NameGenerator *nameGen);
87 IR::Vector<IR::Node> *getSpecializations(const IR::Node *insertion,
88 const Visitor::Context *ctxt) const;
89 cstring getName(const IR::Node *insertion) const {
90 auto s = ::P4::get(specializations, insertion);
91 if (s == nullptr) return nullptr;
92 return s->name;
93 }
94 void clear() { specializations.clear(); }
95};
96
101 SpecializationMap *specMap;
102 MinimalNameGenerator nameGen;
103
104 public:
105 explicit FindSpecializations(SpecializationMap *specMap) : specMap(specMap) {
106 CHECK_NULL(specMap);
107 setName("FindSpecializations");
108 }
109
110 const IR::Node *findInsertionPoint() const;
111 bool isSimpleConstant(const IR::Expression *expression) const;
112 Visitor::profile_t init_apply(const IR::Node *node) override {
113 auto rv = Inspector::init_apply(node);
114
115 specMap->clear();
116 node->apply(nameGen);
117
118 return rv;
119 }
123 bool noParameters(const IR::IContainer *container);
124
125 bool preorder(const IR::P4Parser *parser) override { return noParameters(parser); }
126 bool preorder(const IR::P4Control *control) override { return noParameters(control); }
127 void postorder(const IR::ConstructorCallExpression *expression) override;
128 void postorder(const IR::Declaration_Instance *decl) override;
129
130 bool preorder(const IR::Parameter *) override {
131 // This prevents specialization of the default values of
132 // parameters; we don't care to specialize these, we only need
133 // to specialize them if they appear in a call.
134 return false;
135 }
136};
137
173class Specialize : public Transform {
174 SpecializationMap *specMap;
175 const IR::Node *instantiate(const IR::Node *node, const Visitor::Context *ctxt);
176
177 public:
178 explicit Specialize(SpecializationMap *specMap) : specMap(specMap) {
179 CHECK_NULL(specMap);
180 setName("Specialize");
181 }
182 const IR::Node *postorder(IR::P4Parser *parser) override {
183 return instantiate(parser, getContext());
184 }
185 // skip packages
186 const IR::Node *preorder(IR::Type_Package *package) override {
187 prune();
188 return package;
189 }
190 const IR::Node *postorder(IR::P4Control *control) override {
191 return instantiate(control, getContext());
192 }
193 const IR::Node *postorder(IR::ConstructorCallExpression *expression) override;
194 const IR::Node *postorder(IR::Declaration_Instance *) override;
195};
196
222 SpecializationMap specMap;
223
224 public:
225 SpecializeAll(TypeMap *typeMap, FrontEndPolicy *policy);
226};
227
228} // namespace P4
229
230#endif /* FRONTENDS_P4_SPECIALIZE_H_ */
Definition referenceMap.h:57
Definition specialize.h:100
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:63
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:221
Specializes each Parser and Control with constant constructor arguments by substituting type argument...
Definition specialize.h:173
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
IR::IndexedVector< IR::Declaration > * declarations
Declarations to insert in the list of locals.
Definition specialize.h:41
const IR::Node * invocation
Invocation which causes this specialization.
Definition specialize.h:43
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