P4C
The P4 Compiler
Loading...
Searching...
No Matches
removeParameters.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_REMOVEPARAMETERS_H_
18#define FRONTENDS_P4_REMOVEPARAMETERS_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "frontends/p4/typeMap.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
33 std::map<const IR::P4Action *, const IR::MethodCallExpression *> invocations;
34 std::set<const IR::P4Action *> all; // for these actions remove all parameters
35 std::set<const IR::MethodCallExpression *> calls;
37 std::map<const IR::MethodCallExpression *, unsigned> defaultActions;
38
39 public:
40 void bind(const IR::P4Action *action, const IR::MethodCallExpression *invocation,
41 bool allParams) {
42 CHECK_NULL(action);
43 BUG_CHECK(invocations.find(action) == invocations.end(), "%1%: action called twice",
44 action);
45 invocations.emplace(action, invocation);
46 if (allParams) all.emplace(action);
47 calls.emplace(invocation);
48 }
49 void bindDefaultAction(const IR::P4Action *action,
50 const IR::MethodCallExpression *defaultInvocation) {
51 // We must have a binding for this action already.
52 auto actionCallInvocation = ::P4::get(invocations, action);
53 CHECK_NULL(actionCallInvocation);
54 // We must remove all arguments which are bound in the action list.
55 unsigned boundArgs = actionCallInvocation->arguments->size();
56 defaultActions.emplace(defaultInvocation, boundArgs);
57 }
58 const IR::MethodCallExpression *get(const IR::P4Action *action) const {
59 return ::P4::get(invocations, action);
60 }
61 bool removeAllParameters(const IR::P4Action *action) const {
62 return all.find(action) != all.end();
63 }
64 bool isCall(const IR::MethodCallExpression *expression) const {
65 return calls.find(expression) != calls.end();
66 }
67 unsigned argsToRemove(const IR::MethodCallExpression *defaultCall) const {
68 if (defaultActions.find(defaultCall) == defaultActions.end()) return 0;
69 return ::P4::get(defaultActions, defaultCall);
70 }
71};
72
73class FindActionParameters : public Inspector, public ResolutionContext {
74 TypeMap *typeMap;
75 ActionInvocation *invocations;
76
77 public:
78 FindActionParameters(TypeMap *typeMap, ActionInvocation *invocations)
79 : typeMap(typeMap), invocations(invocations) {
80 CHECK_NULL(invocations);
81 CHECK_NULL(typeMap);
82 setName("FindActionParameters");
83 }
84
85 void postorder(const IR::ActionListElement *element) override;
86 void postorder(const IR::MethodCallExpression *expression) override;
87};
88
115class DoRemoveActionParameters : public Transform {
116 ActionInvocation *invocations;
117 TypeMap *typeMap;
118 MinimalNameGenerator *nameGen;
119
120 public:
121 DoRemoveActionParameters(ActionInvocation *invocations, TypeMap *typeMap,
122 MinimalNameGenerator *nameGen)
123 : invocations(invocations), typeMap(typeMap), nameGen(nameGen) {
124 CHECK_NULL(invocations);
125 CHECK_NULL(typeMap);
126 CHECK_NULL(nameGen);
127 setName("DoRemoveActionParameters");
128 }
129
130 const IR::Node *postorder(IR::P4Action *table) override;
131 const IR::Node *postorder(IR::ActionListElement *element) override;
132 const IR::Node *postorder(IR::MethodCallExpression *expression) override;
133 ActionInvocation *getInvocations() { return invocations; }
134};
135
136class RemoveActionParameters : public PassManager {
137 public:
138 explicit RemoveActionParameters(TypeMap *typeMap, TypeChecking *typeChecking = nullptr);
139};
140
141} // namespace P4
142
143#endif /* FRONTENDS_P4_REMOVEPARAMETERS_H_ */
Definition removeParameters.h:32
Definition node.h:53
Definition visitor.h:418
Definition referenceMap.h:36
Definition visitor.h:442
Definition typeChecker.h:55
Definition typeMap.h:41
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24