ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
smt.hpp
Go to the documentation of this file.
1 #ifndef __SMT_HPP_DEFINED__
2 #define __SMT_HPP_DEFINED__
3 
4 #include <boost/shared_ptr.hpp>
5 #include <ilasynth/ast.hpp>
6 #include <ilasynth/ast/hash.hpp>
7 #include <ilasynth/exception.hpp>
8 #include <ilasynth/util.hpp>
9 #include <unordered_map>
10 #include <z3++.h>
11 
12 namespace ilasynth {
13 // A function object that converts nodes into Z3 expressions.
15 public:
16  // Define types.
17  typedef std::unordered_map<const Node*, z3::expr, decltype(&nodeHash),
18  decltype(&nodeEqual)>
20  // simplify flag.
21  bool simplify;
22 
23 protected:
24  // These are used during memoization.
27 
28  // The underlying Z3 context.
29  z3::context& c;
30  // The "suffix" (used only for synthesis operators).
31  std::string suffix;
32 
34  std::string name_suffix;
35 
36  // Utility function extracts the Z3 expression corresponding
37  // to the i-th argument of the Node n.
38  z3::expr _getArg(const expr_map_t& m, const Node* n, int i);
39  // This extracts expressions from exprmap.
40  z3::expr getArgExpr(const Node* n, int i) { return _getArg(exprmap, n, i); }
41  // This extracts expressions from cnstmap.
42  z3::expr getArgCnst(const Node* n, int i) { return _getArg(cnstmap, n, i); }
43 
44  // These functions populate exprmap and cnstmap.
45  void _populateExprMap(const Node* n);
46  void _populateCnstMap(const Node* n);
47 
48  // Convert a boolean variable into a Z3 expression.
49  virtual z3::expr getBoolVarExpr(const BoolVar* bv);
50  // Convert a boolean op into a Z3 expression.
51  virtual z3::expr getBoolOpExpr(const BoolOp* op);
52  // for booleans
53  virtual z3::expr getChoiceExpr(const BoolChoice* op);
54 
55  // Convert a bitvector variable into a Z3 expression.
56  virtual z3::expr getBitvectorVarExpr(const BitvectorVar* bvv);
57  // Convert a bitvector op into a Z3 expression.
58  virtual z3::expr getBvOpExpr(const BitvectorOp* op);
59  // Convert a choice op into a Z3 expression. (bitvectors)
60  virtual z3::expr getChoiceExpr(const BitvectorChoice* op);
61  // Convert a in-range expression to Z3.
62  virtual z3::expr getBVInRangeExpr(const BVInRange* op);
63  // Constraints for the in-range operator.
64  virtual z3::expr getBVInRangeCnst(const BVInRange* op);
65 
66  // Convert a memory var into a Z3 expression.
67  virtual z3::expr getMemVarExpr(const MemVar* mv);
68  // Convert a mem write expression into Z3.
69  virtual z3::expr getMemOpExpr(const MemOp* mw);
70  // for memories.
71  virtual z3::expr getChoiceExpr(const MemChoice* op);
72 
73  // Convert a function variable into a Z3 expression.
74  virtual z3::expr getFuncVarExpr(const FuncVar* fv);
75 
76 public:
77  // Constructors.
78  Z3ExprAdapter(z3::context& c, const std::string& suffix);
79  Z3ExprAdapter(z3::context& c, const char* suffix);
80  // Destructor.
81  virtual ~Z3ExprAdapter();
82 
83  // This is used by depthFirstVisit.
84  virtual void operator()(const Node* n);
85 
86  // This will call node->depthFirstVisit.
87  z3::expr getExpr(const Node* n);
88  // As will this.
89  z3::expr getCnst(const Node* n);
90 
91  // return the context.
92  z3::context& ctx() const { return c; }
93 
94  // clear the memo.
95  void clear() {
96  exprmap.clear();
97  cnstmap.clear();
98  }
99 
100  // Extract a string representation of the numeric value
101  // of node r in the model m.
102  std::string extractNumeralString(z3::model& m, const Node* r);
103  // Extract the integer value of node r in model m.
104  int getNumeralInt(z3::model& m, const Node* r);
105  // Extract the arbitrary precision int value of node r in model m.
106  mp_int_t getNumeralCppInt(z3::model& m, const Node* r);
107  // Extract the boolean value of node r in model m.
108  bool getBoolValue(z3::model& m, const Node* r);
109  // Return the value of the ith choice boolean
110  template <typename T>
111  bool getChoiceBool(z3::model& m, const ChoiceExpr<T>* op, int i) {
112  using namespace z3;
113  std::string name = op->getChoiceVarName(i) + suffix;
114  expr ci = c.bool_const(name.c_str());
115  z3::expr mi = m.eval(ci, true);
116  Z3_lbool bi = Z3_get_bool_value(c, mi);
117  ILA_ASSERT(bi != Z3_L_UNDEF, "Unable to extract bool from model.");
118  return bi == Z3_L_TRUE;
119  }
120 
121  void setNameSuffix(const std::string& ns) { name_suffix = ns; }
122  const std::string& getNameSuffix() const { return name_suffix; }
123 
124 private:
125  // Helper to convert choices into z3 expressions.
126  template <typename T> z3::expr _getChoiceExpr(const ChoiceExpr<T>* op) {
127  using namespace z3;
128 
129  expr vi_ = getArgExpr(op, 0);
130  unsigned nargs = op->nArgs();
131  for (unsigned i = 1; i != nargs; i++) {
132  std::string name = op->getChoiceVarName(i - 1) + suffix;
133  expr ci = c.bool_const(name.c_str());
134  expr vi = getArgExpr(op, i);
135  expr vi_next = ite(ci, vi, vi_);
136  vi_ = vi_next;
137  }
138  return vi_;
139  }
140 };
141 
142 class DistInput;
143 
144 // The function object we use during synthesis to rewrite expressions.
146 protected:
147  // The distinguishing input this is based on.
149  // Convert a boolean variable into a Z3 expression.
150  virtual z3::expr getBoolVarExpr(const BoolVar* bv);
151  // Convert a bitvector variable into a Z3 expression.
152  virtual z3::expr getBitvectorVarExpr(const BitvectorVar* bvv);
153  // Convert a mem var into Z3.
154  virtual z3::expr getMemVarExpr(const MemVar* mv);
155 
156 public:
157  // Constructors.
158  Z3ExprRewritingAdapter(z3::context& c, const DistInput* di,
159  const std::string& suffix);
160  Z3ExprRewritingAdapter(z3::context& c, const DistInput* di,
161  const char* suffix);
162  // Destructor.
164  // wrapper over getExpr and getCnst
165  z3::expr getIOCnst(const Node* n, const py::object& result);
166 };
167 
169 public:
170  Z3FixedpointAdapter(z3::context& c, const std::string& suffix)
171  : Z3ExprAdapter(c, suffix) {}
172 
174 };
175 } // namespace ilasynth
176 
177 #endif
mp_int_t getNumeralCppInt(z3::model &m, const Node *r)
Definition: func.hpp:33
Definition: bitvec.hpp:83
bool getChoiceBool(z3::model &m, const ChoiceExpr< T > *op, int i)
Definition: smt.hpp:111
z3::expr getArgCnst(const Node *n, int i)
Definition: smt.hpp:42
z3::expr getArgExpr(const Node *n, int i)
Definition: smt.hpp:40
virtual z3::expr getBvOpExpr(const BitvectorOp *op)
virtual void operator()(const Node *n)
virtual z3::expr getBoolOpExpr(const BoolOp *op)
Z3ExprAdapter(z3::context &c, const std::string &suffix)
virtual z3::expr getBVInRangeCnst(const BVInRange *op)
std::string extractNumeralString(z3::model &m, const Node *r)
virtual z3::expr getBitvectorVarExpr(const BitvectorVar *bvv)
Definition: smt.hpp:145
Definition: synthesizer.hpp:61
z3::context & ctx() const
Definition: smt.hpp:92
boost::multiprecision::cpp_int mp_int_t
Definition: common.hpp:9
z3::expr getCnst(const Node *n)
#define ILA_ASSERT(b, msg)
Definition: util.hpp:9
Definition: choice.hpp:47
Definition: bool.hpp:78
virtual z3::expr getFuncVarExpr(const FuncVar *fv)
Definition: smt.hpp:168
bool simplify
Definition: smt.hpp:21
std::string name_suffix
Added to every name.
Definition: smt.hpp:34
bool nodeEqual(const Node *left, const Node *right)
void clear()
Definition: smt.hpp:95
virtual z3::expr getMemOpExpr(const MemOp *mw)
z3::expr _getChoiceExpr(const ChoiceExpr< T > *op)
Definition: smt.hpp:126
~Z3FixedpointAdapter()
Definition: smt.hpp:173
virtual z3::expr getBoolVarExpr(const BoolVar *bv)
size_t nodeHash(const Node *n)
expr_map_t exprmap
Definition: smt.hpp:25
Definition: smt.hpp:14
Definition: abstraction.hpp:21
const std::string & getNameSuffix() const
Definition: smt.hpp:122
z3::expr getExpr(const Node *n)
Definition: node.hpp:55
Definition: bitvec.hpp:38
void setNameSuffix(const std::string &ns)
Definition: smt.hpp:121
std::string suffix
Definition: smt.hpp:31
Definition: mem.hpp:30
virtual unsigned nArgs() const
Definition: choice.hpp:86
Definition: bvinrange.hpp:13
virtual z3::expr getMemVarExpr(const MemVar *mv)
virtual z3::expr getBVInRangeExpr(const BVInRange *op)
expr_map_t cnstmap
Definition: smt.hpp:26
Z3FixedpointAdapter(z3::context &c, const std::string &suffix)
Definition: smt.hpp:170
Definition: bool.hpp:35
std::unordered_map< const Node *, z3::expr, decltype(&nodeHash), decltype(&nodeEqual)> expr_map_t
Definition: smt.hpp:19
bool getBoolValue(z3::model &m, const Node *r)
virtual z3::expr getChoiceExpr(const BoolChoice *op)
int getNumeralInt(z3::model &m, const Node *r)
z3::context & c
Definition: smt.hpp:29
Definition: mem.hpp:67
void _populateExprMap(const Node *n)
const DistInput * distInp
Definition: smt.hpp:148
z3::expr _getArg(const expr_map_t &m, const Node *n, int i)
const char * getChoiceVarName(unsigned i) const
Definition: choice.hpp:98
void _populateCnstMap(const Node *n)