P4C
The P4 Compiler
Loading...
Searching...
No Matches
backends/p4tools/common/lib/model.h
1#ifndef BACKENDS_P4TOOLS_COMMON_LIB_MODEL_H_
2#define BACKENDS_P4TOOLS_COMMON_LIB_MODEL_H_
3
4#include <map>
5#include <utility>
6
7#include "absl/container/btree_map.h"
8#include "ir/ir.h"
9#include "ir/solver.h"
10#include "ir/visitor.h"
11
12namespace P4::P4Tools {
13
15using SymbolicMapType = absl::btree_map<IR::StateVariable, const IR::Expression *>;
16
19class Model {
20 private:
22 SymbolicMapping symbolicMap;
23
24 // A visitor that iterates over an input expression and visits all the variables in the input
25 // expression. These variables correspond to variables that were used for constraints in model
26 // generated by the SMT solver. A variable needs to be
29 class SubstVisitor : public Transform {
30 const Model &self;
31
35 bool doComplete;
36
37 public:
38 const IR::Literal *preorder(IR::StateVariable *var) override;
39 const IR::Literal *preorder(IR::SymbolicVariable *var) override;
40 const IR::Literal *preorder(IR::TaintExpression *var) override;
41
42 explicit SubstVisitor(const Model &model, bool doComplete);
43 };
44
45 public:
46 // Maps an expression to its value in the model.
47 using ExpressionMap = std::map<const IR::Expression *, const IR::Literal *>;
48
50 explicit Model(SymbolicMapping symbolicMap) : symbolicMap(std::move(symbolicMap)) {}
51
52 Model(const Model &) = default;
53 Model(Model &&) = default;
54 Model &operator=(const Model &) = default;
55 Model &operator=(Model &&) = default;
56 ~Model() = default;
57
63 const IR::Literal *evaluate(const IR::Expression *expr, bool doComplete,
64 ExpressionMap *resolvedExpressions = nullptr) const;
65
66 // Evaluates a P4 StructExpression in the context of this model. Recursively calls into
67 // @evaluate and substitutes all members of this list with a Value type.
68 const IR::StructExpression *evaluateStructExpr(
69 const IR::StructExpression *structExpr, bool doComplete,
70 ExpressionMap *resolvedExpressions = nullptr) const;
71
72 // Evaluates a P4 BaseListExpression in the context of this model. Recursively calls into
73 // @evaluate and substitutes all members of this list with a Value type.
74 const IR::BaseListExpression *evaluateListExpr(
75 const IR::BaseListExpression *listExpr, bool doComplete,
76 ExpressionMap *resolvedExpressions = nullptr) const;
77
81 [[nodiscard]] const IR::Expression *get(const IR::SymbolicVariable *var, bool checked) const;
82
86 void set(const IR::SymbolicVariable *var, const IR::Expression *val);
87
89 void mergeMap(const SymbolicMapping &sourceMap);
90
92 [[nodiscard]] const SymbolicMapping &getSymbolicMap() const;
93};
94
95} // namespace P4::P4Tools
96
97#endif /* BACKENDS_P4TOOLS_COMMON_LIB_MODEL_H_ */
Definition backends/p4tools/common/lib/model.h:19
const SymbolicMapping & getSymbolicMap() const
Definition model.cpp:106
Model(SymbolicMapping symbolicMap)
A model is initialized with a symbolic map. Usually, these are derived from the solver.
Definition backends/p4tools/common/lib/model.h:50
void mergeMap(const SymbolicMapping &sourceMap)
Merge a sumbolic map into this model, do NOT override existing values.
Definition model.cpp:108
const IR::Expression * get(const IR::SymbolicVariable *var, bool checked) const
Definition model.cpp:93
const IR::Literal * evaluate(const IR::Expression *expr, bool doComplete, ExpressionMap *resolvedExpressions=nullptr) const
Definition model.cpp:81
void set(const IR::SymbolicVariable *var, const IR::Expression *val)
Definition model.cpp:102
Definition visitor.h:424
Definition common/compiler/compiler_result.cpp:3
absl::btree_map< IR::StateVariable, const IR::Expression * > SymbolicMapType
Symbolic maps map a state variable to a IR::Expression.
Definition backends/p4tools/common/lib/model.h:15
absl::btree_map< const IR::SymbolicVariable *, const IR::Expression *, IR::SymbolicVariableLess > SymbolicMapping
This type maps symbolic variables to their value assigned by the solver.
Definition solver.h:20
STL namespace.