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/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "frontends/p4/typeMap.h"
24#include "ir/ir.h"
25
26namespace P4 {
27
34 std::map<const IR::P4Action *, const IR::MethodCallExpression *> invocations;
35 std::set<const IR::P4Action *> all; // for these actions remove all parameters
36 std::set<const IR::MethodCallExpression *> calls;
38 std::map<const IR::MethodCallExpression *, unsigned> defaultActions;
39
40 public:
41 void bind(const IR::P4Action *action, const IR::MethodCallExpression *invocation,
42 bool allParams) {
43 CHECK_NULL(action);
44 BUG_CHECK(invocations.find(action) == invocations.end(), "%1%: action called twice",
45 action);
46 invocations.emplace(action, invocation);
47 if (allParams) all.emplace(action);
48 calls.emplace(invocation);
49 }
50 void bindDefaultAction(const IR::P4Action *action,
51 const IR::MethodCallExpression *defaultInvocation) {
52 // We must have a binding for this action already.
53 auto actionCallInvocation = ::P4::get(invocations, action);
54 CHECK_NULL(actionCallInvocation);
55 // We must remove all arguments which are bound in the action list.
56 unsigned boundArgs = actionCallInvocation->arguments->size();
57 defaultActions.emplace(defaultInvocation, boundArgs);
58 }
59 const IR::MethodCallExpression *get(const IR::P4Action *action) const {
60 return ::P4::get(invocations, action);
61 }
62 bool removeAllParameters(const IR::P4Action *action) const {
63 return all.find(action) != all.end();
64 }
65 bool isCall(const IR::MethodCallExpression *expression) const {
66 return calls.find(expression) != calls.end();
67 }
68 unsigned argsToRemove(const IR::MethodCallExpression *defaultCall) const {
69 if (defaultActions.find(defaultCall) == defaultActions.end()) return 0;
70 return ::P4::get(defaultActions, defaultCall);
71 }
72};
73
75 TypeMap *typeMap;
76 ActionInvocation *invocations;
77
78 public:
79 FindActionParameters(TypeMap *typeMap, ActionInvocation *invocations)
80 : typeMap(typeMap), invocations(invocations) {
81 CHECK_NULL(invocations);
82 CHECK_NULL(typeMap);
83 setName("FindActionParameters");
84 }
85
86 void postorder(const IR::ActionListElement *element) override;
87 void postorder(const IR::MethodCallExpression *expression) override;
88};
89
117 ActionInvocation *invocations;
118
119 public:
120 explicit DoRemoveActionParameters(ActionInvocation *invocations) : invocations(invocations) {
121 CHECK_NULL(invocations);
122 setName("DoRemoveActionParameters");
123 }
124
125 const IR::Node *postorder(IR::P4Action *table) override;
126 const IR::Node *postorder(IR::ActionListElement *element) override;
127 const IR::Node *postorder(IR::MethodCallExpression *expression) override;
128 ActionInvocation *getInvocations() { return invocations; }
129};
130
132 public:
133 explicit RemoveActionParameters(TypeMap *typeMap, TypeChecking *typeChecking = nullptr);
134};
135
136} // namespace P4
137
138#endif /* FRONTENDS_P4_REMOVEPARAMETERS_H_ */
Definition removeParameters.h:33
Definition removeParameters.h:116
Definition removeParameters.h:74
Definition node.h:95
Definition visitor.h:400
Definition pass_manager.h:40
Definition removeParameters.h:131
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:33
Definition visitor.h:424
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