P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
specializeGenericFunctions.h
1/*
2Copyright 2020 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_SPECIALIZEGENERICFUNCTIONS_H_
18#define FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
31 const IR::Function *original;
33 const IR::Function *specialized;
35 const IR::MethodCallExpression *invocation;
37 const IR::Node *insertBefore;
38
39 FunctionSpecialization(cstring name, const IR::MethodCallExpression *invocation,
40 const IR::Function *function, const IR::Node *insert)
41 : name(name),
42 original(function),
43 specialized(nullptr),
45 insertBefore(insert) {
46 CHECK_NULL(invocation);
47 CHECK_NULL(invocation);
48 CHECK_NULL(insertBefore);
49 }
50};
51
53 ReferenceMap *refMap;
54 TypeMap *typeMap;
56 // Keep track of the values in the above map which are already
57 // inserted in the program.
58 std::set<FunctionSpecialization *> inserted;
59
60 void add(const IR::MethodCallExpression *mce, const IR::Function *func,
61 const IR::Node *insert) {
62 cstring name = refMap->newName(func->name);
63 FunctionSpecialization *fs = new FunctionSpecialization(name, mce, func, insert);
64 map.emplace(mce, fs);
65 }
66 FunctionSpecialization *get(const IR::MethodCallExpression *mce) const {
67 return ::get(map, mce);
68 }
69 IR::Vector<IR::Node> *getInsertions(const IR::Node *insertionPoint) {
70 IR::Vector<IR::Node> *result = nullptr;
71 for (auto s : map) {
72 if (inserted.find(s.second) != inserted.end()) continue;
73 if (s.second->insertBefore == insertionPoint) {
74 if (result == nullptr) result = new IR::Vector<IR::Node>();
75 LOG2("Will insert " << dbp(s.second->specialized) << " before "
76 << dbp(insertionPoint));
77 result->push_back(s.second->specialized);
78 inserted.emplace(s.second);
79 }
80 }
81 return result;
82 }
83};
84
88class FindFunctionSpecializations : public Inspector {
90
91 public:
92 explicit FindFunctionSpecializations(FunctionSpecializationMap *specMap) : specMap(specMap) {
93 CHECK_NULL(specMap);
94 setName("FindFunctionSpecializations");
95 }
96
97 bool preorder(const IR::MethodCallExpression *call) override;
98};
99
119class SpecializeFunctions : public Transform {
121
122 public:
123 explicit SpecializeFunctions(FunctionSpecializationMap *specMap) : specMap(specMap) {
124 CHECK_NULL(specMap);
125 setName("SpecializeFunctions");
126 }
127 const IR::Node *postorder(IR::Function *function) override;
128 const IR::Node *postorder(IR::MethodCallExpression *) override;
129 const IR::Node *insert(const IR::Node *before);
130 const IR::Node *preorder(IR::P4Control *control) override { return insert(control); }
131 const IR::Node *preorder(IR::P4Parser *parser) override { return insert(parser); }
132 const IR::Node *preorder(IR::Function *function) override { return insert(function); }
133};
134
135class SpecializeGenericFunctions : public PassManager {
137
138 public:
140 passes.emplace_back(new TypeChecking(refMap, typeMap));
141 passes.emplace_back(new FindFunctionSpecializations(&specMap));
142 passes.emplace_back(new SpecializeFunctions(&specMap));
143 specMap.refMap = refMap;
144 specMap.typeMap = typeMap;
145 setName("SpecializeGenericFunctions");
146 }
147};
148
149} // namespace P4
150
151#endif /* FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_ */
Definition externInstance.h:33
Definition specializeGenericFunctions.h:88
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
cstring newName(cstring base) override
Generate a name from base that fresh for the program.
Definition referenceMap.cpp:96
Specializes each generic function by substituting type parameters.
Definition specializeGenericFunctions.h:119
Definition specializeGenericFunctions.h:135
Definition typeChecker.h:60
Definition typeMap.h:42
Definition cstring.h:72
Definition ordered_map.h:30
Definition applyOptionsPragmas.cpp:24
Describes how a generic function is specialized.
Definition specializeGenericFunctions.h:27
const IR::Function * specialized
Result of specialization.
Definition specializeGenericFunctions.h:33
const IR::Node * insertBefore
Point in IR tree where to insert the function.
Definition specializeGenericFunctions.h:37
const IR::Function * original
Function that is being specialized.
Definition specializeGenericFunctions.h:31
const IR::MethodCallExpression * invocation
Invocation which causes this specialization.
Definition specializeGenericFunctions.h:35
cstring name
Name to use for specialized function.
Definition specializeGenericFunctions.h:29
Definition specializeGenericFunctions.h:52