ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
synrewriter.hpp
Go to the documentation of this file.
1 #ifndef __SYN_HPP_DEFINED__
2 #define __SYN_HPP_DEFINED__
3 
4 #include <ilasynth/ast.hpp>
5 #include <ilasynth/smt.hpp>
6 
7 namespace ilasynth {
8 // A function object that rewrites the AST according to the result
9 // of the synthesis.
10 class SynRewriter {
11 public:
12  // Define types.
13  typedef std::unordered_map<const Node*, nptr_t, decltype(&nodeHash),
14  decltype(&nodeEqual)>
16 
17 protected:
18  // the map between old and new nodes.
20  // the model we are examining.
21  z3::model& m;
22  // the expression converter we are using to evalute the model.
24  // find the rewritten args.
25  void getNewArgs(const Node* n, nptr_vec_t& args);
26 
27 public:
28  // Constructor.
29  SynRewriter(z3::model& m, Z3ExprAdapter& a);
30 
31  // Destructor.
32  ~SynRewriter();
33 
34  // This is used by depthFirstVisit.
35  void operator()(const Node* n);
36 
37  // do the rewrite.
38  nptr_t rewrite(const Node* n);
39 
40 private:
41  template <typename T> void _synChoiceExpr(const ChoiceExpr<T>* op) {
42  int i = (int)op->nArgs() - 2;
43  ILA_ASSERT(i >= 0, "Choice has too few args!");
44  for (; i >= 0; i--) {
45  bool ci = adapter.getChoiceBool<T>(m, op, i);
46  if (ci) {
47  break;
48  }
49  }
50  nptr_vec_t args;
51  getNewArgs(op, args);
52  nptr_t nptr = args[i + 1];
53  // nptr->write(std::cout << "expr: ") << std::endl;
54  exprmap.insert({(Node*)op, nptr});
55  }
56 };
57 } // namespace ilasynth
58 
59 #endif
bool getChoiceBool(z3::model &m, const ChoiceExpr< T > *op, int i)
Definition: smt.hpp:111
std::unordered_map< const Node *, nptr_t, decltype(&nodeHash), decltype(&nodeEqual)> expr_map_t
Definition: synrewriter.hpp:15
Definition: synrewriter.hpp:10
void operator()(const Node *n)
z3::model & m
Definition: synrewriter.hpp:21
std::vector< nptr_t > nptr_vec_t
Definition: node.hpp:27
#define ILA_ASSERT(b, msg)
Definition: util.hpp:9
Definition: choice.hpp:47
SynRewriter(z3::model &m, Z3ExprAdapter &a)
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
bool nodeEqual(const Node *left, const Node *right)
void getNewArgs(const Node *n, nptr_vec_t &args)
size_t nodeHash(const Node *n)
Definition: smt.hpp:14
Definition: abstraction.hpp:21
Definition: node.hpp:55
virtual unsigned nArgs() const
Definition: choice.hpp:86
void _synChoiceExpr(const ChoiceExpr< T > *op)
Definition: synrewriter.hpp:41
Z3ExprAdapter & adapter
Definition: synrewriter.hpp:23
nptr_t rewrite(const Node *n)
expr_map_t exprmap
Definition: synrewriter.hpp:19