P4C
The P4 Compiler
Loading...
Searching...
No Matches
evaluator.h
1/*
2Copyright 2013-present Barefoot Networks, 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 EVALUATOR_EVALUATOR_H_
18#define EVALUATOR_EVALUATOR_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/typeMap.h"
23#include "ir/ir.h"
24#include "ir/pass_manager.h"
25
26namespace P4 {
27
28class IHasBlock {
29 public:
30 virtual IR::ToplevelBlock *getToplevelBlock() const = 0;
31};
32
33class Evaluator final : public Inspector, public IHasBlock {
34 const ReferenceMap *refMap;
35 const TypeMap *typeMap;
36 std::vector<IR::Block *> blockStack;
37 IR::ToplevelBlock *toplevelBlock;
38
39 protected:
40 void pushBlock(IR::Block *block);
41 void popBlock(IR::Block *block);
42
43 public:
44 Evaluator(const ReferenceMap *refMap, const TypeMap *typeMap)
45 : refMap(refMap), typeMap(typeMap), toplevelBlock(nullptr) {
46 CHECK_NULL(refMap);
47 CHECK_NULL(typeMap);
48 setName("Evaluator");
49 visitDagOnce = false;
50 }
51 IR::ToplevelBlock *getToplevelBlock() const override { return toplevelBlock; }
52
53 IR::Block *currentBlock() const;
56 void setValue(const IR::Node *node, const IR::CompileTimeValue *constant);
59 bool hasValue(const IR::Node *node) const;
60 const IR::CompileTimeValue *getValue(const IR::Node *node) const;
61
64 std::vector<const IR::CompileTimeValue *> *evaluateArguments(
65 const IR::ParameterList *parameters, const IR::Vector<IR::Argument> *arguments,
66 IR::Block *context);
67
68 profile_t init_apply(const IR::Node *node) override;
69
70 // The traversal order is controlled very explicitly
71 bool preorder(const IR::P4Program *program) override;
72 bool preorder(const IR::Declaration_Constant *decl) override;
73 bool preorder(const IR::P4Table *table) override;
74 bool preorder(const IR::Declaration_Instance *inst) override;
75 bool preorder(const IR::ConstructorCallExpression *inst) override;
76 bool preorder(const IR::MethodCallExpression *expr) override;
77 bool preorder(const IR::PathExpression *expression) override;
78 bool preorder(const IR::Property *prop) override;
79 bool preorder(const IR::Member *expression) override;
80 bool preorder(const IR::ListExpression *expression) override;
81 bool preorder(const IR::P4ListExpression *expression) override;
82 bool preorder(const IR::StructExpression *expression) override;
83 bool preorder(const IR::Constant *expression) override {
84 setValue(expression, expression);
85 return false;
86 }
87 bool preorder(const IR::StringLiteral *expression) override {
88 setValue(expression, expression);
89 return false;
90 }
91 bool preorder(const IR::BoolLiteral *expression) override {
92 setValue(expression, expression);
93 return false;
94 }
95 bool preorder(const IR::ListCompileTimeValue *expression) override {
96 setValue(expression, expression);
97 return false;
98 }
99 bool preorder(const IR::StructCompileTimeValue *expression) override {
100 setValue(expression, expression);
101 return false;
102 }
103 bool preorder(const IR::Declaration_ID *expression) override {
104 setValue(expression, expression);
105 return false;
106 }
107
108 const IR::Block *processConstructor(const IR::Node *node, const IR::Type *type,
109 const IR::Type *instanceType,
110 const IR::Vector<IR::Argument> *arguments);
111};
112
115class EvaluatorPass final : public PassManager, public IHasBlock {
116 P4::Evaluator *evaluator;
117 std::unique_ptr<ReferenceMap> selfRefMap;
118
119 public:
120 IR::ToplevelBlock *getToplevelBlock() const override { return evaluator->getToplevelBlock(); }
121 EvaluatorPass(ReferenceMap *refMap, TypeMap *typeMap);
122 explicit EvaluatorPass(TypeMap *typeMap) : EvaluatorPass(nullptr, typeMap) {}
123};
124
125} // namespace P4
126
127#endif /* EVALUATOR_EVALUATOR_H_ */
Definition evaluator.h:33
void setValue(const IR::Node *node, const IR::CompileTimeValue *constant)
Definition evaluator.cpp:54
std::vector< const IR::CompileTimeValue * > * evaluateArguments(const IR::ParameterList *parameters, const IR::Vector< IR::Argument > *arguments, IR::Block *context)
Definition evaluator.cpp:111
bool hasValue(const IR::Node *node) const
Definition evaluator.cpp:61
Definition evaluator.h:115
Definition evaluator.h:28
Definition node.h:95
Definition vector.h:59
Definition visitor.h:400
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition visitor.h:78
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24