forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveParameters.h
More file actions
143 lines (126 loc) · 5.05 KB
/
removeParameters.h
File metadata and controls
143 lines (126 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2016 VMware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef FRONTENDS_P4_REMOVEPARAMETERS_H_
#define FRONTENDS_P4_REMOVEPARAMETERS_H_
#include "frontends/common/resolveReferences/resolveReferences.h"
#include "frontends/p4/typeChecking/typeChecker.h"
#include "frontends/p4/typeMap.h"
#include "ir/ir.h"
namespace P4 {
/**
* For each action that is invoked keep the list of arguments that
* it's called with. There must be only one call of each action;
* this is done by LocalizeActions.
*/
class ActionInvocation {
std::map<const IR::P4Action *, const IR::MethodCallExpression *> invocations;
std::set<const IR::P4Action *> all; // for these actions remove all parameters
std::set<const IR::MethodCallExpression *> calls;
/// how many arguments to remove from each default action
std::map<const IR::MethodCallExpression *, unsigned> defaultActions;
public:
void bind(const IR::P4Action *action, const IR::MethodCallExpression *invocation,
bool allParams) {
CHECK_NULL(action);
BUG_CHECK(invocations.find(action) == invocations.end(), "%1%: action called twice",
action);
invocations.emplace(action, invocation);
if (allParams) all.emplace(action);
calls.emplace(invocation);
}
void bindDefaultAction(const IR::P4Action *action,
const IR::MethodCallExpression *defaultInvocation) {
// We must have a binding for this action already.
auto actionCallInvocation = ::P4::get(invocations, action);
CHECK_NULL(actionCallInvocation);
// We must remove all arguments which are bound in the action list.
unsigned boundArgs = actionCallInvocation->arguments->size();
defaultActions.emplace(defaultInvocation, boundArgs);
}
const IR::MethodCallExpression *get(const IR::P4Action *action) const {
return ::P4::get(invocations, action);
}
bool removeAllParameters(const IR::P4Action *action) const {
return all.find(action) != all.end();
}
bool isCall(const IR::MethodCallExpression *expression) const {
return calls.find(expression) != calls.end();
}
unsigned argsToRemove(const IR::MethodCallExpression *defaultCall) const {
if (defaultActions.find(defaultCall) == defaultActions.end()) return 0;
return ::P4::get(defaultActions, defaultCall);
}
};
class FindActionParameters : public Inspector, public ResolutionContext {
TypeMap *typeMap;
ActionInvocation *invocations;
public:
FindActionParameters(TypeMap *typeMap, ActionInvocation *invocations)
: typeMap(typeMap), invocations(invocations) {
CHECK_NULL(invocations);
CHECK_NULL(typeMap);
setName("FindActionParameters");
}
void postorder(const IR::ActionListElement *element) override;
void postorder(const IR::MethodCallExpression *expression) override;
};
/**
* Removes parameters of an action which are in/inout/out.
*
* \code{.cpp}
* control c(inout bit<32> x) {
* action a(in bit<32> arg) { x = arg; }
* table t() { actions = { a(10); } }
* apply { ... } }
* \endcode
*
* is converted to
*
* \code{.cpp}
* control c(inout bit<32> x) {
* bit<32> arg;
* action a() { arg = 10; x = arg; }
* table t() { actions = { a; } }
* apply { ... } }
* \endcode
*
* @pre This pass requires each action to have a single caller.
* It must run after the LocalizeActions pass, which
* in turn must be run after actions inlining.
* It also run after UniqueParameters pass.
* @post in/inout/out parameters of an action are removed.
*/
class DoRemoveActionParameters : public Transform {
ActionInvocation *invocations;
TypeMap *typeMap;
MinimalNameGenerator *nameGen;
public:
DoRemoveActionParameters(ActionInvocation *invocations, TypeMap *typeMap,
MinimalNameGenerator *nameGen)
: invocations(invocations), typeMap(typeMap), nameGen(nameGen) {
CHECK_NULL(invocations);
CHECK_NULL(typeMap);
CHECK_NULL(nameGen);
setName("DoRemoveActionParameters");
}
const IR::Node *postorder(IR::P4Action *table) override;
const IR::Node *postorder(IR::ActionListElement *element) override;
const IR::Node *postorder(IR::MethodCallExpression *expression) override;
ActionInvocation *getInvocations() { return invocations; }
};
class RemoveActionParameters : public PassManager {
public:
explicit RemoveActionParameters(TypeMap *typeMap, TypeChecking *typeChecking = nullptr);
};
} // namespace P4
#endif /* FRONTENDS_P4_REMOVEPARAMETERS_H_ */