P4C
The P4 Compiler
Loading...
Searching...
No Matches
checkExternInvocationCommon.h
1/*
2 * Copyright 2021 Intel Corp.
3 * SPDX-FileCopyrightText: 2021 Intel Corp.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef MIDEND_CHECKEXTERNINVOCATIONCOMMON_H_
9#define MIDEND_CHECKEXTERNINVOCATIONCOMMON_H_
10
11#include "frontends/common/resolveReferences/resolveReferences.h"
12#include "frontends/p4/methodInstance.h"
13#include "ir/ir.h"
14#include "ir/visitor.h"
15#include "lib/bitvec.h"
16
17namespace P4 {
18
19class TypeMap;
20
32class CheckExternInvocationCommon : public Inspector, public ResolutionContext {
33 protected:
34 TypeMap *typeMap;
35 std::map<cstring /* extType */, bitvec> pipeConstraints;
36
45 virtual void initPipeConstraints() = 0;
46
54 virtual cstring getBlockName(int bit) = 0;
55
65 virtual void checkExtern(const ExternMethod *extMethod, const IR::MethodCallExpression *expr) {
66 (void)extMethod;
67 (void)expr;
68 }
69
79 virtual void checkExtern(const ExternFunction *extFunction,
80 const IR::MethodCallExpression *expr) {
81 (void)extFunction;
82 (void)expr;
83 }
84
92 int bit = vec.ffs(0);
93 BUG_CHECK(vec.ffs(bit), "Trying to extract multiple block encodings");
94 return getBlockName(bit);
95 }
96
106 void setPipeConstraints(cstring extType, bitvec vec) {
107 if (!pipeConstraints.count(extType)) {
108 pipeConstraints.emplace(extType, vec);
109 } else {
110 auto &cons = pipeConstraints.at(extType);
111 cons |= vec;
112 }
113 }
114
128 void checkPipeConstraints(cstring extType, bitvec bv, const IR::MethodCallExpression *expr,
129 cstring extName, cstring pipe) {
130 BUG_CHECK(pipeConstraints.count(extType), "pipe constraints not defined for %1%", extType);
131 auto constraint = pipeConstraints.at(extType) & bv;
132 if (!bv.empty() && constraint.empty()) {
133 if (extName != "")
134 ::P4::error(ErrorType::ERR_UNSUPPORTED, "%s %s %s cannot be used in the %s %s",
135 expr->srcInfo, extType, extName, pipe, extractBlock(bv));
136 else
137 ::P4::error(ErrorType::ERR_UNSUPPORTED, "%s %s cannot be used in the %s %s",
138 expr->srcInfo, extType, pipe, extractBlock(bv));
139 }
140 }
141
142 explicit CheckExternInvocationCommon(TypeMap *typeMap) : typeMap(typeMap) {}
143
144 public:
145 bool preorder(const IR::MethodCallExpression *expr) override {
146 auto mi = MethodInstance::resolve(expr, this, typeMap);
147 if (auto extMethod = mi->to<ExternMethod>()) {
148 checkExtern(extMethod, expr);
149 } else if (auto extFunction = mi->to<ExternFunction>()) {
150 checkExtern(extFunction, expr);
151 }
152 return false;
153 }
154};
155
156} // namespace P4
157
158#endif /* MIDEND_CHECKEXTERNINVOCATIONCOMMON_H_ */
Base class which can be used to prepare classes for checking constraints for invocations of externs (...
Definition checkExternInvocationCommon.h:32
void setPipeConstraints(cstring extType, bitvec vec)
Set the pipe (parser/control block) constraints.
Definition checkExternInvocationCommon.h:106
cstring extractBlock(bitvec vec)
Get the name of the block which is represented by bit set in the bitvec.
Definition checkExternInvocationCommon.h:91
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:65
virtual void checkExtern(const ExternFunction *extFunction, const IR::MethodCallExpression *expr)
Method for checking constraints of extern functions given by parameters.
Definition checkExternInvocationCommon.h:79
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:128
Definition methodInstance.h:194
Definition methodInstance.h:168
Definition visitor.h:418
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
Definition typeMap.h:32
Definition bitvec.h:120
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58