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/constantFolding.h"
21#include "frontends/common/resolveReferences/referenceMap.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
27class FrontEndPolicy; // full definition not needed here
28
34 const IR::IContainer *specialized;
40 IR::IndexedVector<IR::Declaration> *declarations;
42 const IR::Node *invocation;
44 const IR::Node *insertBefore;
45
46 SpecializationInfo(const IR::Node *invocation, const IR::IContainer *cont,
47 const IR::Node *insertion)
48 : specialized(cont),
49 typeArguments(nullptr),
50 constructorArguments(new IR::Vector<IR::Argument>()),
51 declarations(new IR::IndexedVector<IR::Declaration>()),
53 insertBefore(insertion) {
54 CHECK_NULL(cont);
55 CHECK_NULL(invocation);
56 CHECK_NULL(insertion);
57 }
58 const IR::Type_Declaration *synthesize(ReferenceMap *refMap) const;
59};
60
65 const IR::Argument *convertArgument(const IR::Argument *arg, SpecializationInfo *info,
66 const IR::Parameter *param);
67
68 public:
69 TypeMap *typeMap = nullptr;
70 ReferenceMap *refMap = nullptr;
77 void addSpecialization(const IR::ConstructorCallExpression *invocation,
78 const IR::IContainer *container, const IR::Node *insertion);
85 void addSpecialization(const IR::Declaration_Instance *invocation,
86 const IR::IContainer *container, const IR::Node *insertion);
87 IR::Vector<IR::Node> *getSpecializations(const IR::Node *insertion) const;
88 cstring getName(const IR::Node *insertion) const {
89 auto s = ::get(specializations, insertion);
90 if (s == nullptr) return nullptr;
91 return s->name;
92 }
93 void clear() { specializations.clear(); }
94};
95
99class FindSpecializations : public Inspector {
100 SpecializationMap *specMap;
101
102 public:
103 explicit FindSpecializations(SpecializationMap *specMap) : specMap(specMap) {
104 CHECK_NULL(specMap);
105 setName("FindSpecializations");
106 }
107
108 const IR::Node *findInsertionPoint() const;
109 bool isSimpleConstant(const IR::Expression *expression) const;
110 Visitor::profile_t init_apply(const IR::Node *node) override {
111 specMap->clear();
112 return Inspector::init_apply(node);
113 }
117 bool noParameters(const IR::IContainer *container);
118
119 bool preorder(const IR::P4Parser *parser) override { return noParameters(parser); }
120 bool preorder(const IR::P4Control *control) override { return noParameters(control); }
121 void postorder(const IR::ConstructorCallExpression *expression) override;
122 void postorder(const IR::Declaration_Instance *decl) override;
123
124 bool preorder(const IR::Parameter *) override {
125 // This prevents specialization of the default values of
126 // parameters; we don't care to specialize these, we only need
127 // to specialize them if they appear in a call.
128 return false;
129 }
130};
131
167class Specialize : public Transform {
168 SpecializationMap *specMap;
169 const IR::Node *instantiate(const IR::Node *node);
170
171 public:
172 explicit Specialize(SpecializationMap *specMap) : specMap(specMap) {
173 CHECK_NULL(specMap);
174 setName("Specialize");
175 }
176 const IR::Node *postorder(IR::P4Parser *parser) override { return instantiate(parser); }
177 // skip packages
178 const IR::Node *preorder(IR::Type_Package *package) override {
179 prune();
180 return package;
181 }
182 const IR::Node *postorder(IR::P4Control *control) override { return instantiate(control); }
183 const IR::Node *postorder(IR::ConstructorCallExpression *expression) override;
184 const IR::Node *postorder(IR::Declaration_Instance *) override;
185};
186
211class SpecializeAll : public PassRepeated {
212 SpecializationMap specMap;
213
214 public:
215 SpecializeAll(ReferenceMap *refMap, TypeMap *typeMap, FrontEndPolicy *policy);
216};
217
218} // namespace P4
219
220#endif /* FRONTENDS_P4_SPECIALIZE_H_ */
Definition externInstance.h:33
Definition specialize.h:99
bool noParameters(const IR::IContainer *container)
Definition specialize.cpp:189
Definition frontend.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
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)
Definition specialize.cpp:79
Specializes each Parser and Control by substituting type arguments and constructor parameters.
Definition specialize.h:211
Specializes each Parser and Control with constant constructor arguments by substituting type argument...
Definition specialize.h:167
Definition typeMap.h:42
Definition cstring.h:72
Definition ordered_map.h:30
Definition applyOptionsPragmas.cpp:24
Describes how a parser or control is specialized.
Definition specialize.h:30
const IR::IContainer * specialized
Actual parser or control that is being specialized.
Definition specialize.h:34
cstring name
Name to use for specialized object.
Definition specialize.h:32
const IR::Node * insertBefore
Where in the program should the specialization be inserted.
Definition specialize.h:44
IR::IndexedVector< IR::Declaration > * declarations
Declarations to insert in the list of locals.
Definition specialize.h:40
const IR::Node * invocation
Invocation which causes this specialization.
Definition specialize.h:42
const IR::Vector< IR::Type > * typeArguments
Values to substitute for type arguments.
Definition specialize.h:36
IR::Vector< IR::Argument > * constructorArguments
Values to substitute for constructor arguments.
Definition specialize.h:38