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 "ir/ir.h"
8#include "ir/solver.h"
9#include "ir/visitor.h"
10
11namespace P4::P4Tools {
12
15
18class Model {
19 private:
21 SymbolicMapping symbolicMap;
22
23 // A visitor that iterates over an input expression and visits all the variables in the input
24 // expression. These variables correspond to variables that were used for constraints in model
25 // generated by the SMT solver. A variable needs to be
28 class SubstVisitor : public Transform {
29 const Model &self;
30
34 bool doComplete;
35
36 public:
37 const IR::Literal *preorder(IR::StateVariable *var) override;
38 const IR::Literal *preorder(IR::SymbolicVariable *var) override;
39 const IR::Literal *preorder(IR::TaintExpression *var) override;
40
41 explicit SubstVisitor(const Model &model, bool doComplete);
42 };
43
44 public:
45 // Maps an expression to its value in the model.
46 using ExpressionMap = std::map<const IR::Expression *, const IR::Literal *>;
47
49 explicit Model(SymbolicMapping symbolicMap) : symbolicMap(std::move(symbolicMap)) {}
50
51 Model(const Model &) = default;
52 Model(Model &&) = default;
53 Model &operator=(const Model &) = default;
54 Model &operator=(Model &&) = default;
55 ~Model() = default;
56
62 const IR::Literal *evaluate(const IR::Expression *expr, bool doComplete,
63 ExpressionMap *resolvedExpressions = nullptr) const;
64
65 // Evaluates a P4 StructExpression in the context of this model. Recursively calls into
66 // @evaluate and substitutes all members of this list with a Value type.
67 const IR::StructExpression *evaluateStructExpr(
68 const IR::StructExpression *structExpr, bool doComplete,
69 ExpressionMap *resolvedExpressions = nullptr) const;
70
71 // Evaluates a P4 BaseListExpression in the context of this model. Recursively calls into
72 // @evaluate and substitutes all members of this list with a Value type.
73 const IR::BaseListExpression *evaluateListExpr(
74 const IR::BaseListExpression *listExpr, bool doComplete,
75 ExpressionMap *resolvedExpressions = nullptr) const;
76
80 [[nodiscard]] const IR::Expression *get(const IR::SymbolicVariable *var, bool checked) const;
81
85 void set(const IR::SymbolicVariable *var, const IR::Expression *val);
86
88 void mergeMap(const SymbolicMapping &sourceMap);
89
91 [[nodiscard]] const SymbolicMapping &getSymbolicMap() const;
92};
93
94} // namespace P4::P4Tools
95
96#endif /* BACKENDS_P4TOOLS_COMMON_LIB_MODEL_H_ */
Definition backends/p4tools/common/lib/model.h:18
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:49
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
STL namespace.