17#ifndef FRONTENDS_P4_SIDEEFFECTS_H_
18#define FRONTENDS_P4_SIDEEFFECTS_H_
22#include "frontends/common/resolveReferences/referenceMap.h"
23#include "frontends/common/resolveReferences/resolveReferences.h"
24#include "frontends/p4/methodInstance.h"
25#include "frontends/p4/typeChecking/typeChecker.h"
35 const IR::P4Table *table;
36 const IR::MethodCallExpression *call;
39 setName(
"HasTableApply");
42 void postorder(
const IR::MethodCallExpression *expression)
override {
44 if (!mi->isApply())
return;
46 if (!am->object->is<IR::P4Table>())
return;
47 BUG_CHECK(table ==
nullptr,
"%1% and %2%: multiple table applications in one expression",
49 table = am->object->to<IR::P4Table>();
51 LOG3(
"Invoked table is " << dbp(table));
74 void postorder(
const IR::MethodCallExpression *mce)
override {
75 if (refMap ==
nullptr || typeMap ==
nullptr) {
88 if (bim->name.name != IR::Type_Header::isValid) {
94 void postorder(
const IR::ConstructorCallExpression *cce)
override {
102 setName(
"SideEffects");
106 static bool check(
const IR::Expression *expression,
const Visitor *calledBy,
110 se.setCalledBy(calledBy);
111 expression->apply(se, ctxt);
122 return !ec->method->hasAnnotation(IR::Annotation::noSideEffectsAnnotation);
124 return !ef->function->hasAnnotation(IR::Annotation::noSideEffectsAnnotation);
125 if (
const auto *bim = mi->to<
BuiltInMethod>())
return bim->name != IR::Type_Header::isValid;
166 std::set<const IR::Expression *> *added;
172 std::set<const IR::Expression *> temporaries;
174 cstring createTemporary(
const IR::Type *type);
176 const IR::Expression *expression);
177 bool mayAlias(
const IR::Expression *left,
const IR::Expression *right,
179 bool containsHeaderType(
const IR::Type *type);
183 : typeMap(typeMap), added(added) {
185 setName(
"DoSimplifyExpressions");
188 const IR::Node *postorder(IR::Expression *expression)
override;
189 const IR::Node *preorder(IR::StructExpression *expression)
override;
190 const IR::Node *preorder(IR::ListExpression *expression)
override;
191 const IR::Node *preorder(IR::Literal *expression)
override;
192 const IR::Node *preorder(IR::ArrayIndex *expression)
override;
193 const IR::Node *preorder(IR::Member *expression)
override;
194 const IR::Node *preorder(IR::SelectExpression *expression)
override;
195 const IR::Node *preorder(IR::Operation_Unary *expression)
override;
196 const IR::Node *preorder(IR::Operation_Binary *expression)
override;
197 const IR::Node *shortCircuit(IR::Operation_Binary *expression);
198 const IR::Node *preorder(IR::Mux *expression)
override;
199 const IR::Node *preorder(IR::LAnd *expression)
override;
200 const IR::Node *preorder(IR::LOr *expression)
override;
201 const IR::Node *preorder(IR::MethodCallExpression *mce)
override;
203 const IR::Node *preorder(IR::ConstructorCallExpression *cce)
override {
207 const IR::Node *preorder(IR::Property *prop)
override {
211 const IR::Node *preorder(IR::Annotation *anno)
override {
216 const IR::Node *postorder(IR::P4Parser *parser)
override;
217 const IR::Node *postorder(IR::Function *function)
override;
218 const IR::Node *postorder(IR::P4Control *control)
override;
219 const IR::Node *postorder(IR::P4Action *action)
override;
220 const IR::Node *postorder(IR::ParserState *state)
override;
221 const IR::Node *postorder(IR::AssignmentStatement *statement)
override;
222 const IR::Node *postorder(IR::MethodCallStatement *statement)
override;
223 const IR::Node *postorder(IR::ReturnStatement *statement)
override;
224 const IR::Node *preorder(IR::SwitchStatement *statement)
override;
225 const IR::Node *preorder(IR::IfStatement *statement)
override;
226 const IR::Node *preorder(IR::ForStatement *statement)
override;
227 const IR::Node *preorder(IR::ForInStatement *statement)
override;
230 void end_apply(
const IR::Node *)
override;
235 std::vector<const IR::Declaration_Variable *> declarations;
236 std::vector<const IR::AssignmentStatement *> statements;
281 std::map<const IR::P4Table *, TableInsertions *> toInsert;
282 std::set<const IR::P4Table *> *invokedInKey;
286 : typeMap(typeMap), invokedInKey(invokedInKey) {
288 CHECK_NULL(invokedInKey);
289 setName(
"KeySideEffect");
294 virtual const IR::Node *doStatement(
const IR::Statement *statement,
295 const IR::Expression *expression,
298 const IR::Node *preorder(IR::Key *key)
override;
302 const IR::Node *postorder(IR::MethodCallStatement *statement)
override {
303 return doStatement(statement, statement->methodCall, getContext());
305 const IR::Node *postorder(IR::IfStatement *statement)
override {
306 return doStatement(statement, statement->condition, getContext());
308 const IR::Node *postorder(IR::SwitchStatement *statement)
override {
309 return doStatement(statement, statement->expression, getContext());
311 const IR::Node *postorder(IR::AssignmentStatement *statement)
override {
312 return doStatement(statement, statement->right, getContext());
314 const IR::Node *postorder(IR::KeyElement *element)
override;
315 const IR::Node *postorder(IR::P4Table *table)
override;
316 const IR::Node *preorder(IR::P4Table *table)
override;
326 std::set<const IR::P4Table *> *invokedInKey;
330 : typeMap(typeMap), invokedInKey(invokedInKey) {
331 CHECK_NULL(invokedInKey);
332 setName(
"TableInKeys");
335 invokedInKey->clear();
336 return Inspector::init_apply(node);
338 void postorder(
const IR::MethodCallExpression *mce)
override {
339 if (!findContext<IR::Key>())
return;
341 hta.setCalledBy(
this);
342 (void)mce->apply(hta, getContext());
343 if (hta.table !=
nullptr) {
344 LOG2(
"Table " << hta.table <<
" invoked in key of another table");
345 invokedInKey->emplace(hta.table);
360 void postorder(
const IR::MethodCallExpression *expression)
override {
361 if (findContext<IR::ActionList>()) {
363 hta.setCalledBy(
this);
364 (void)expression->apply(hta, getContext());
365 if (hta.table !=
nullptr) {
366 ::P4::error(ErrorType::ERR_UNSUPPORTED,
"%1%: table invocation in action argument",
378 std::set<const IR::P4Table *> invokedInKey;
380 std::set<const IR::Expression *> added;
385 if (!typeChecking) typeChecking =
new TypeChecking(
nullptr, typeMap);
386 if (!skipSideEffectOrdering) {
389 passes.push_back(typeChecking);
391 passes.push_back(
new TablesInKeys(typeMap, &invokedInKey));
394 setName(
"SideEffectOrdering");
Definition methodInstance.h:129
Definition methodInstance.h:260
Definition referenceMap.h:57
Convert expressions so that each expression contains at most one side effect.
Definition sideEffects.h:162
Definition methodInstance.h:149
Definition methodInstance.h:232
Checks to see whether an IR node includes a table.apply() sub-expression.
Definition sideEffects.h:31
Definition sideEffects.h:277
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 referenceMap.h:36
Definition ir/pass_manager.h:145
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition sideEffects.h:373
Determines whether an expression may have method or constructor invocations.
Definition sideEffects.h:62
unsigned sideEffectCount
Number of side effects in this expression.
Definition sideEffects.h:72
SideEffects(DeclarationLookup *refMap, TypeMap *typeMap)
Definition sideEffects.h:101
const IR::Node * nodeWithSideEffect
Last visited side-effecting node. Null if no node has side effects.
Definition sideEffects.h:69
static bool hasSideEffect(const IR::MethodCallExpression *mce, DeclarationLookup *refMap, TypeMap *typeMap)
Definition sideEffects.h:115
static bool check(const IR::Expression *expression, const Visitor *calledBy, DeclarationLookup *refMap, TypeMap *typeMap, const Visitor::Context *ctxt=nullptr)
Definition sideEffects.h:106
Definition sideEffects.h:355
Definition sideEffects.h:324
Definition typeChecker.h:55
Definition source_file.h:131
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
Definition sideEffects.h:233