ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
synthesizer.hpp
Go to the documentation of this file.
1 #ifndef __SYNTHESIZER_HPP_DEFINED__
2 #define __SYNTHESIZER_HPP_DEFINED__
3 
4 #include <boost/python.hpp>
5 #include <boost/shared_ptr.hpp>
6 #include <boost/variant.hpp>
7 #include <map>
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <z3++.h>
12 
13 #include <ilasynth/abstraction.hpp>
14 #include <ilasynth/ast.hpp>
15 #include <ilasynth/common.hpp>
16 #include <ilasynth/smt.hpp>
17 #include <ilasynth/synrewriter.hpp>
18 
19 namespace ilasynth {
20 
21 // ---------------------------------------------------------------------- //
22 struct SupportVars {
23  bool canFixUp;
25 
26  // set of visited nodes.
27  std::set<const Node*> visited;
28 
29  // set of booleans.
30  std::set<const BoolVar*> bools;
31  // set of bitvectors.
32  std::set<const BitvectorVar*> bitvecs;
33 
34  // set of memories.
35  struct mem_info_t {
36  const MemVar* mem;
39 
40  bool operator==(const mem_info_t& that) const {
41  return mem->equal(that.mem) && addr->equal(that.addr) &&
42  rddata->equal(that.rddata);
43  }
44  };
45  std::vector<mem_info_t> rdexprs;
46 
47  // visitor function.
48  void dfs(const Node* n);
49  // reset.
50  void clear();
51  // make the rdexprs unique.
52  void uniquifyRdExprs();
53 
54  // check if this expression depends on the support.
55  bool depCheck(z3::context& c, z3::solver& S, const nptr_t& ex);
56 
57  SupportVars() : canFixUp(true), enumRdExprVars(false) {}
58 };
59 
60 // ---------------------------------------------------------------------- //
61 struct DistInput {
62  std::map<std::string, MemValues> mems;
63  std::map<std::string, std::string> bitvecs;
64  std::map<std::string, bool> bools;
65  std::vector<std::string> rdaddrs;
66 
67  DistInput(Abstraction& a, Z3ExprAdapter& c, z3::model& m, SupportVars& sv);
68  void fixUp(SupportVars& s, Z3ExprAdapter& c, z3::model& m);
69  void toPython(py::dict& d);
70 
71  bool getBoolValue(const std::string& n) const;
72  std::string getBitvecStr(const std::string& n) const;
73  const MemValues& getMemValues(const std::string& n) const;
74 };
75 std::ostream& operator<<(std::ostream& out, const DistInput& di);
76 
77 // ---------------------------------------------------------------------- //
78 struct SimOutput {
79  boost::variant<MemValues, std::string, bool> out;
80  // default constructor.
81  SimOutput();
82  // constructor with a particular output.
83  SimOutput(const NodeType& nt, const py::object& r);
84  // copy constructor.
85  SimOutput(const SimOutput& that);
86  // assignment operator.
87  SimOutput& operator=(const SimOutput& that);
88  // equality check.
89  bool operator==(const SimOutput& that) const;
90  // initialize with a particular output.
91  void initOutput(const NodeType& nt, const py::object& r);
92 };
93 std::ostream& operator<<(std::ostream& out, const SimOutput& simout);
94 
95 typedef boost::shared_ptr<SimOutput> simout_ptr_t;
96 
97 // ---------------------------------------------------------------------- //
98 struct DITreeNode;
99 typedef boost::shared_ptr<DITreeNode> dtree_ptr_t;
100 
101 struct DITreeNode {
102  typedef std::pair<simout_ptr_t, dtree_ptr_t> outpair_t;
103  typedef std::vector<outpair_t> outpair_vec_t;
104 
105  std::unique_ptr<DistInput> inputs;
108 
109  DITreeNode(Abstraction& a, Z3ExprAdapter& c, z3::model& m, SupportVars& sv);
110  DITreeNode();
111  ~DITreeNode();
112 };
113 
114 class Synthesizer;
115 struct DITree {
118 
119  // pointer to synthesizer.
121  // head of the tree.
123  // in replay mode - this is the pointer to the next dist input node.
125  // in insert mode, this is the location where the next node will be
126  // inserted.
128 
129  DistInput* getDistInput(const z3::expr& y);
130  void setOutput(const simout_ptr_t& out);
131  nptr_t getExpr(const z3::expr& y, const nptr_t& ex, int i,
132  const nptr_t& de_expr);
133 
134  DITree(Synthesizer& s);
135  void reset(bool reuseModels);
136  void rewind();
137 };
138 
139 // ---------------------------------------------------------------------- //
140 struct SimoutAdapter : public boost::static_visitor<z3::expr> {
142  const Node* node;
143  z3::expr r_expr;
144  z3::expr r_cnst;
145 
146  // constructor.
148 
149  // visitors.
150  z3::expr operator()(bool res) const;
151  z3::expr operator()(const std::string& res) const;
152  z3::expr operator()(const MemValues& mv) const;
153 };
154 
155 // ---------------------------------------------------------------------- //
156 class Synthesizer {
157 protected:
158  static const char* suffix1;
159  static const char* suffix2;
160 
162  const std::vector<nmap_t*> maps;
164 
166  z3::context c;
167  z3::solver S, Sp;
169 
171 
172  void _addExpr(const nptr_t& expr, Z3ExprAdapter& c1, Z3ExprAdapter& c2);
173 
174  std::unique_ptr<DistInput> _getDistInput(z3::expr y);
175 
176  nptr_t _synthesize(const std::string& name, const nptr_t& de_expr,
177  const nptr_t& expr, const z3::expr& y, PyObject* pyfun);
178 
179  nptr_t _synthesizeEx(const std::string& name, const nptr_t& de_expr,
180  const nptr_t& expr, const z3::expr& y, PyObject* pyfun);
181 
182  z3::expr _createSynMiter(const nptr_t& ex);
183 
184  void _initSynthesis();
185  nptr_t _synthesizeOp(const std::string& name, const nptr_t& var,
186  nptr_vec_t& next_vec, const nptr_t& next,
187  PyObject* pyfun);
188 
189  nptr_t _getCombinedExpr(const nptr_t& var, const nptr_vec_t& next_vec);
190 
191  bool _eq(const nptr_t& n1, const nptr_t& n2);
192 
193  // visitor for the assumptions.
194  struct init_assump_t : public assump_visitor_i {
198 
200  : syn(syn_), c1(c1_), c2(c2_) {}
201 
202  virtual void useAssump(const nptr_t& a);
203  };
204  friend struct init_assump_t;
205 
206  void _addConstRegAssumps();
207  void _npair_check();
208 
209 public:
210  // constructor.
211  Synthesizer(Abstraction& abs, const std::vector<nmap_t*>& maps);
212  // destructor.
213  ~Synthesizer();
214 
215  // synthesize all.
216  void synthesizeAll(PyObject* pyfun);
217 
218  // synthesize this reg.
219  void synthesizeReg(nmap_t::iterator pos, PyObject* pyfun);
220 
221  friend class DITree;
222 };
223 // ---------------------------------------------------------------------- //
224 } // namespace ilasynth
225 
226 #endif
Z3ExprAdapter & c1
Definition: synthesizer.hpp:196
const MemVar * mem
Definition: synthesizer.hpp:36
void synthesizeReg(nmap_t::iterator pos, PyObject *pyfun)
nptr_t _synthesize(const std::string &name, const nptr_t &de_expr, const nptr_t &expr, const z3::expr &y, PyObject *pyfun)
std::set< const BoolVar * > bools
Definition: synthesizer.hpp:30
void synthesizeAll(PyObject *pyfun)
Definition: abstraction.hpp:27
virtual bool equal(const Node *that) const
Definition: synthesizer.hpp:116
std::map< std::string, bool > bools
Definition: synthesizer.hpp:64
SupportVars decodeSupport
Definition: synthesizer.hpp:163
Definition: synthesizer.hpp:35
outpair_vec_t outputs
Definition: synthesizer.hpp:106
void toPython(py::dict &d)
nptr_t getExpr(const z3::expr &y, const nptr_t &ex, int i, const nptr_t &de_expr)
const Node * node
Definition: synthesizer.hpp:142
boost::variant< MemValues, std::string, bool > out
Definition: synthesizer.hpp:79
Definition: synthesizer.hpp:22
virtual void useAssump(const nptr_t &a)
Definition: type.hpp:11
std::set< const Node * > visited
Definition: synthesizer.hpp:27
std::map< std::string, MemValues > mems
Definition: synthesizer.hpp:62
Definition: smt.hpp:145
Z3ExprAdapter c1
Definition: synthesizer.hpp:168
std::unique_ptr< DistInput > _getDistInput(z3::expr y)
SimoutAdapter(Z3ExprRewritingAdapter &a, const Node *n)
static const char * suffix1
Definition: synthesizer.hpp:158
std::vector< nptr_t > nptr_vec_t
Definition: node.hpp:27
Definition: synthesizer.hpp:61
Definition: synthesizer.hpp:115
Synthesizer & syn
Definition: synthesizer.hpp:120
mode_t
Definition: synthesizer.hpp:116
z3::expr _createSynMiter(const nptr_t &ex)
Definition: abstraction.hpp:31
virtual bool equal(const Node *that) const
bool operator==(const SimOutput &that) const
Z3ExprAdapter & c2
Definition: synthesizer.hpp:197
void fixUp(SupportVars &s, Z3ExprAdapter &c, z3::model &m)
z3::expr r_cnst
Definition: synthesizer.hpp:144
z3::context c
Definition: synthesizer.hpp:166
z3::solver S
Definition: synthesizer.hpp:167
bool operator==(const mem_info_t &that) const
Definition: synthesizer.hpp:40
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
bool reuseModels
Definition: synthesizer.hpp:117
SupportVars()
Definition: synthesizer.hpp:57
bool canFixUp
Definition: synthesizer.hpp:23
std::vector< mem_info_t > rdexprs
Definition: synthesizer.hpp:45
Definition: synthesizer.hpp:156
std::unique_ptr< DistInput > inputs
Definition: synthesizer.hpp:105
void reset(bool reuseModels)
Z3ExprRewritingAdapter & adapter
Definition: synthesizer.hpp:141
static const char * suffix2
Definition: synthesizer.hpp:159
dtree_ptr_t replay_ptr
Definition: synthesizer.hpp:124
nptr_t _synthesizeEx(const std::string &name, const nptr_t &de_expr, const nptr_t &expr, const z3::expr &y, PyObject *pyfun)
Definition: memvalues.hpp:20
Definition: smt.hpp:14
const MemValues & getMemValues(const std::string &n) const
boost::shared_ptr< SimOutput > simout_ptr_t
Definition: synthesizer.hpp:95
void _addExpr(const nptr_t &expr, Z3ExprAdapter &c1, Z3ExprAdapter &c2)
z3::solver Sp
Definition: synthesizer.hpp:167
Definition: abstraction.hpp:21
Definition: bitvec.hpp:26
Definition: node.hpp:55
dtree_ptr_t head
Definition: synthesizer.hpp:122
void dfs(const Node *n)
Definition: synthesizer.hpp:194
Definition: mem.hpp:30
nptr_t _synthesizeOp(const std::string &name, const nptr_t &var, nptr_vec_t &next_vec, const nptr_t &next, PyObject *pyfun)
Definition: synthesizer.hpp:116
std::string getBitvecStr(const std::string &n) const
bool _eq(const nptr_t &n1, const nptr_t &n2)
std::map< std::string, std::string > bitvecs
Definition: synthesizer.hpp:63
Z3ExprAdapter c2
Definition: synthesizer.hpp:168
std::vector< std::string > rdaddrs
Definition: synthesizer.hpp:65
dtree_ptr_t * insert_ptr
Definition: synthesizer.hpp:127
init_assump_t(Synthesizer &syn_, Z3ExprAdapter &c1_, Z3ExprAdapter &c2_)
Definition: synthesizer.hpp:199
Synthesizer & syn
Definition: synthesizer.hpp:195
int MAX_SYN_ITER
Definition: synthesizer.hpp:165
SimOutput & operator=(const SimOutput &that)
const BitvectorExpr * rddata
Definition: synthesizer.hpp:38
void setOutput(const simout_ptr_t &out)
std::set< const BitvectorVar * > bitvecs
Definition: synthesizer.hpp:32
DITree(Synthesizer &s)
enum ilasynth::DITree::mode_t mode
std::pair< simout_ptr_t, dtree_ptr_t > outpair_t
Definition: synthesizer.hpp:102
Abstraction & abs
Definition: synthesizer.hpp:161
bool getBoolValue(const std::string &n) const
Definition: synthesizer.hpp:140
Synthesizer(Abstraction &abs, const std::vector< nmap_t *> &maps)
nptr_t _getCombinedExpr(const nptr_t &var, const nptr_vec_t &next_vec)
std::vector< outpair_t > outpair_vec_t
Definition: synthesizer.hpp:103
z3::expr operator()(bool res) const
nptr_t result
Definition: synthesizer.hpp:107
Definition: synthesizer.hpp:101
Definition: synthesizer.hpp:78
DistInput(Abstraction &a, Z3ExprAdapter &c, z3::model &m, SupportVars &sv)
bool depCheck(z3::context &c, z3::solver &S, const nptr_t &ex)
bool enumRdExprVars
Definition: synthesizer.hpp:24
DITree ditree
Definition: synthesizer.hpp:170
z3::expr r_expr
Definition: synthesizer.hpp:143
boost::shared_ptr< DITreeNode > dtree_ptr_t
Definition: synthesizer.hpp:98
std::ostream & operator<<(std::ostream &out, const Node &that)
const std::vector< nmap_t * > maps
Definition: synthesizer.hpp:162
DistInput * getDistInput(const z3::expr &y)
const BitvectorExpr * addr
Definition: synthesizer.hpp:37
void initOutput(const NodeType &nt, const py::object &r)