P4C
The P4 Compiler
Loading...
Searching...
No Matches
removeParameters.h
1/*
2 * Copyright 2016 VMware, Inc.
3 * SPDX-FileCopyrightText: 2016 VMware, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_REMOVEPARAMETERS_H_
9#define FRONTENDS_P4_REMOVEPARAMETERS_H_
10
11#include "frontends/common/resolveReferences/resolveReferences.h"
12#include "frontends/p4/typeChecking/typeChecker.h"
13#include "frontends/p4/typeMap.h"
14#include "ir/ir.h"
15
16namespace P4 {
17
24 std::map<const IR::P4Action *, const IR::MethodCallExpression *> invocations;
25 std::set<const IR::P4Action *> all; // for these actions remove all parameters
26 std::set<const IR::MethodCallExpression *> calls;
28 std::map<const IR::MethodCallExpression *, unsigned> defaultActions;
29
30 public:
31 void bind(const IR::P4Action *action, const IR::MethodCallExpression *invocation,
32 bool allParams) {
33 CHECK_NULL(action);
34 BUG_CHECK(invocations.find(action) == invocations.end(), "%1%: action called twice",
35 action);
36 invocations.emplace(action, invocation);
37 if (allParams) all.emplace(action);
38 calls.emplace(invocation);
39 }
40 void bindDefaultAction(const IR::P4Action *action,
41 const IR::MethodCallExpression *defaultInvocation) {
42 // We must have a binding for this action already.
43 auto actionCallInvocation = ::P4::get(invocations, action);
44 CHECK_NULL(actionCallInvocation);
45 // We must remove all arguments which are bound in the action list.
46 unsigned boundArgs = actionCallInvocation->arguments->size();
47 defaultActions.emplace(defaultInvocation, boundArgs);
48 }
49 const IR::MethodCallExpression *get(const IR::P4Action *action) const {
50 return ::P4::get(invocations, action);
51 }
52 bool removeAllParameters(const IR::P4Action *action) const {
53 return all.find(action) != all.end();
54 }
55 bool isCall(const IR::MethodCallExpression *expression) const {
56 return calls.find(expression) != calls.end();
57 }
58 unsigned argsToRemove(const IR::MethodCallExpression *defaultCall) const {
59 if (defaultActions.find(defaultCall) == defaultActions.end()) return 0;
60 return ::P4::get(defaultActions, defaultCall);
61 }
62};
63
64class FindActionParameters : public Inspector, public ResolutionContext {
65 TypeMap *typeMap;
66 ActionInvocation *invocations;
67
68 public:
69 FindActionParameters(TypeMap *typeMap, ActionInvocation *invocations)
70 : typeMap(typeMap), invocations(invocations) {
71 CHECK_NULL(invocations);
72 CHECK_NULL(typeMap);
73 setName("FindActionParameters");
74 }
75
76 void postorder(const IR::ActionListElement *element) override;
77 void postorder(const IR::MethodCallExpression *expression) override;
78};
79
106class DoRemoveActionParameters : public Transform {
107 ActionInvocation *invocations;
108 TypeMap *typeMap;
109 MinimalNameGenerator *nameGen;
110
111 public:
112 DoRemoveActionParameters(ActionInvocation *invocations, TypeMap *typeMap,
113 MinimalNameGenerator *nameGen)
114 : invocations(invocations), typeMap(typeMap), nameGen(nameGen) {
115 CHECK_NULL(invocations);
116 CHECK_NULL(typeMap);
117 CHECK_NULL(nameGen);
118 setName("DoRemoveActionParameters");
119 }
120
121 const IR::Node *postorder(IR::P4Action *table) override;
122 const IR::Node *postorder(IR::ActionListElement *element) override;
123 const IR::Node *postorder(IR::MethodCallExpression *expression) override;
124 ActionInvocation *getInvocations() { return invocations; }
125};
126
127class RemoveActionParameters : public PassManager {
128 public:
129 explicit RemoveActionParameters(TypeMap *typeMap, TypeChecking *typeChecking = nullptr);
130};
131
132} // namespace P4
133
134#endif /* FRONTENDS_P4_REMOVEPARAMETERS_H_ */
Definition removeParameters.h:23
Definition node.h:53
Definition visitor.h:418
Definition referenceMap.h:36
Definition visitor.h:442
Definition typeChecker.h:55
Definition typeMap.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13