P4C
The P4 Compiler
Loading...
Searching...
No Matches
checkExternInvocationCommon.h
1/*
2Copyright 2021 Intel Corp.
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 MIDEND_CHECKEXTERNINVOCATIONCOMMON_H_
18#define MIDEND_CHECKEXTERNINVOCATIONCOMMON_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/methodInstance.h"
22#include "ir/ir.h"
23#include "ir/visitor.h"
24#include "lib/bitvec.h"
25
26namespace P4 {
27
28class TypeMap;
29
42 protected:
43 TypeMap *typeMap;
44 std::map<cstring /* extType */, bitvec> pipeConstraints;
45
54 virtual void initPipeConstraints() = 0;
55
63 virtual cstring getBlockName(int bit) = 0;
64
74 virtual void checkExtern(const ExternMethod *extMethod, const IR::MethodCallExpression *expr) {
75 (void)extMethod;
76 (void)expr;
77 }
78
88 virtual void checkExtern(const ExternFunction *extFunction,
89 const IR::MethodCallExpression *expr) {
90 (void)extFunction;
91 (void)expr;
92 }
93
101 int bit = vec.ffs(0);
102 BUG_CHECK(vec.ffs(bit), "Trying to extract multiple block encodings");
103 return getBlockName(bit);
104 }
105
115 void setPipeConstraints(cstring extType, bitvec vec) {
116 if (!pipeConstraints.count(extType)) {
117 pipeConstraints.emplace(extType, vec);
118 } else {
119 auto &cons = pipeConstraints.at(extType);
120 cons |= vec;
121 }
122 }
123
137 void checkPipeConstraints(cstring extType, bitvec bv, const IR::MethodCallExpression *expr,
138 cstring extName, cstring pipe) {
139 BUG_CHECK(pipeConstraints.count(extType), "pipe constraints not defined for %1%", extType);
140 auto constraint = pipeConstraints.at(extType) & bv;
141 if (!bv.empty() && constraint.empty()) {
142 if (extName != "")
143 ::P4::error(ErrorType::ERR_UNSUPPORTED, "%s %s %s cannot be used in the %s %s",
144 expr->srcInfo, extType, extName, pipe, extractBlock(bv));
145 else
146 ::P4::error(ErrorType::ERR_UNSUPPORTED, "%s %s cannot be used in the %s %s",
147 expr->srcInfo, extType, pipe, extractBlock(bv));
148 }
149 }
150
151 explicit CheckExternInvocationCommon(TypeMap *typeMap) : typeMap(typeMap) {}
152
153 public:
154 bool preorder(const IR::MethodCallExpression *expr) override {
155 auto mi = MethodInstance::resolve(expr, this, typeMap);
156 if (auto extMethod = mi->to<ExternMethod>()) {
157 checkExtern(extMethod, expr);
158 } else if (auto extFunction = mi->to<ExternFunction>()) {
159 checkExtern(extFunction, expr);
160 }
161 return false;
162 }
163};
164
165} // namespace P4
166
167#endif /* MIDEND_CHECKEXTERNINVOCATIONCOMMON_H_ */
Base class which can be used to prepare classes for checking constraints for invocations of externs (...
Definition checkExternInvocationCommon.h:41
void setPipeConstraints(cstring extType, bitvec vec)
Set the pipe (parser/control block) constraints.
Definition checkExternInvocationCommon.h:115
cstring extractBlock(bitvec vec)
Get the name of the block which is represented by bit set in the bitvec.
Definition checkExternInvocationCommon.h:100
virtual cstring getBlockName(int bit)=0
Get the name of the block which is represented in bit vector (bitvec) by bit with index given by 'bit...
virtual void initPipeConstraints()=0
Method used to initialize the constraints.
virtual void checkExtern(const ExternMethod *extMethod, const IR::MethodCallExpression *expr)
Method for checking constraints of extern method given by parameters.
Definition checkExternInvocationCommon.h:74
virtual void checkExtern(const ExternFunction *extFunction, const IR::MethodCallExpression *expr)
Method for checking constraints of extern functions given by parameters.
Definition checkExternInvocationCommon.h:88
void checkPipeConstraints(cstring extType, bitvec bv, const IR::MethodCallExpression *expr, cstring extName, cstring pipe)
Check if the invocation of extern object method or extern function is valid in the block where it is ...
Definition checkExternInvocationCommon.h:137
Definition methodInstance.h:194
Definition methodInstance.h:168
Definition visitor.h:400
static MethodInstance * resolve(const IR::MethodCallExpression *mce, const DeclarationLookup *refMap, TypeMap *typeMap, bool useExpressionType=false, const Visitor::Context *ctxt=nullptr, bool incomplete=false)
Definition methodInstance.cpp:27
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition typeMap.h:41
Definition bitvec.h:120
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51