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#include <vector>
7
8#include <boost/container/flat_map.hpp>
9
10#include "ir/ir.h"
11#include "ir/solver.h"
12#include "ir/visitor.h"
13
14namespace P4Tools {
15
17using SymbolicMapType = boost::container::flat_map<IR::StateVariable, const IR::Expression *>;
18
21class Model {
22 private:
24 SymbolicMapping symbolicMap;
25
26 // A visitor that iterates over an input expression and visits all the variables in the input
27 // expression. These variables correspond to variables that were used for constraints in model
28 // generated by the SMT solver. A variable needs to be
31 class SubstVisitor : public Transform {
32 const Model &self;
33
37 bool doComplete;
38
39 public:
40 const IR::Literal *preorder(IR::StateVariable *var) override;
41 const IR::Literal *preorder(IR::SymbolicVariable *var) override;
42 const IR::Literal *preorder(IR::TaintExpression *var) override;
43
44 explicit SubstVisitor(const Model &model, bool doComplete);
45 };
46
47 public:
48 // Maps an expression to its value in the model.
49 using ExpressionMap = std::map<const IR::Expression *, const IR::Literal *>;
50
52 explicit Model(SymbolicMapping symbolicMap) : symbolicMap(std::move(symbolicMap)) {}
53
54 Model(const Model &) = default;
55 Model(Model &&) = default;
56 Model &operator=(const Model &) = default;
57 Model &operator=(Model &&) = default;
58 ~Model() = default;
59
65 const IR::Literal *evaluate(const IR::Expression *expr, bool doComplete,
66 ExpressionMap *resolvedExpressions = nullptr) const;
67
68 // Evaluates a P4 StructExpression in the context of this model. Recursively calls into
69 // @evaluate and substitutes all members of this list with a Value type.
70 const IR::StructExpression *evaluateStructExpr(
71 const IR::StructExpression *structExpr, bool doComplete,
72 ExpressionMap *resolvedExpressions = nullptr) const;
73
74 // Evaluates a P4 BaseListExpression in the context of this model. Recursively calls into
75 // @evaluate and substitutes all members of this list with a Value type.
76 const IR::BaseListExpression *evaluateListExpr(
77 const IR::BaseListExpression *listExpr, bool doComplete,
78 ExpressionMap *resolvedExpressions = nullptr) const;
79
83 [[nodiscard]] const IR::Expression *get(const IR::SymbolicVariable *var, bool checked) const;
84
88 void set(const IR::SymbolicVariable *var, const IR::Expression *val);
89
91 void mergeMap(const SymbolicMapping &sourceMap);
92
94 [[nodiscard]] const SymbolicMapping &getSymbolicMap() const;
95};
96
97} // namespace P4Tools
98
99#endif /* BACKENDS_P4TOOLS_COMMON_LIB_MODEL_H_ */
Definition backends/p4tools/common/lib/model.h:21
const IR::Expression * get(const IR::SymbolicVariable *var, bool checked) const
Definition model.cpp:95
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:52
void set(const IR::SymbolicVariable *var, const IR::Expression *val)
Definition model.cpp:104
const IR::Literal * evaluate(const IR::Expression *expr, bool doComplete, ExpressionMap *resolvedExpressions=nullptr) const
Definition model.cpp:83
const SymbolicMapping & getSymbolicMap() const
Definition model.cpp:108
void mergeMap(const SymbolicMapping &sourceMap)
Merge a sumbolic map into this model, do NOT override existing values.
Definition model.cpp:110
Definition visitor.h:420
Definition common/compiler/compiler_result.cpp:3
boost::container::flat_map< IR::StateVariable, const IR::Expression * > SymbolicMapType
Symbolic maps map a state variable to a IR::Expression.
Definition backends/p4tools/common/lib/model.h:17
STL namespace.