ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
node.hpp
Go to the documentation of this file.
1 #ifndef __AST_NODE_HPP_DEFINED__
2 #define __AST_NODE_HPP_DEFINED__
3 #include <iostream>
4 #include <memory>
5 #include <set>
6 #include <string>
7 #include <unordered_map>
8 #include <vector>
9 
10 #include <assert.h>
11 #include <ilasynth/common.hpp>
12 #include <ilasynth/type.hpp>
13 #include <z3++.h>
14 
15 #include <boost/multiprecision/cpp_int.hpp>
16 #include <boost/python.hpp>
17 #include <boost/shared_ptr.hpp>
18 
19 namespace ilasynth {
20 class Abstraction;
21 class NodeRef;
22 class Node;
23 class NodeVisitorI;
25 
26 typedef boost::shared_ptr<Node> nptr_t;
27 typedef std::vector<nptr_t> nptr_vec_t;
28 typedef std::vector<const Node*> nodevec_t;
29 typedef std::set<const Node*> nodeset_t;
30 
31 struct npair_t {
37  const npair_t* parent;
38 
39  // construct arch state.
40  npair_t(const nptr_t& v, const nptr_t& n)
41  : var(v), init(v), ipred(NULL), next(n), parent(NULL) {}
42 
43  npair_t(const nptr_t& v, const nptr_t& n, const nptr_t& i, const npair_t* p)
44  : var(v), init(i), ipred(NULL), next(n), parent(p) {}
45 
46  npair_t(const npair_t* p)
47  : var(p->var), init(p->init), ipred(p->ipred), next(NULL),
48  parent(p->parent ? p->parent : p) {}
49 
50  // destructor.
51  ~npair_t() {}
52 };
53 typedef std::map<std::string, npair_t> nmap_t;
54 
55 class Node {
56  // ----------------- PRIVATE MEMBERS ----------------- //
57  static int totalObjCnt;
58 
59 private:
60  // ----------------- PRIVATE METHODS ----------------- //
61  void _initName();
62 
63 protected:
64  // -------------------- DATA MEMBERS ----------------- //
65 protected:
66  // name for this node.
67  std::string name;
68  // reference name for this node (for better reference)
69  std::string refName;
70  // unique id.
71  const int id;
72  // hash value flag.
73  mutable bool hash_inited;
74  // hash value.
75  mutable size_t hash_value;
76 
77  // helper function for getSupport
78  static void _getSupportVarsHelper(nodeset_t& supp, const Node* n);
79 
80 public: // TODO : for now leave it as public
81  // type of this node.
83 
84 public:
85  // ------------ CONSTRUCTORS/DESTRUCTORS ------------ //
86  // default constructor.
87  Node();
88  // constructor.
89  Node(NodeType t);
90  // destructor.
91  virtual ~Node();
92 
93  // ---------------------- UTILITIES ------------------ //
94  std::string& getName() { return name; }
95 
96  const std::string& getName() const { return name; }
97 
98  const std::string& getRefName() const { return refName; }
99 
100  void setRefName(const std::string& refN) { refName = refN; }
101 
102  int getId() { return id; }
103  NodeType getType() { return type; }
104 
105  // ---------------------- METHODS ------------------- //
106  // polymorphic clone method: do we need this?
107  virtual Node* clone() const;
108  // polymorphic equality method.
109  virtual bool equal(const Node* that) const;
110  // output to a stream.
111  virtual std::ostream& write(std::ostream& out) const;
112  // return the value associated with this object (or None)
113  // if it doesn't exist.
114  virtual py::object getValue() const;
115  // Is this node a constant? This function
116  // should return true only if the node is a constant,
117  // but may return false if the node is a constant.
118  //
119  // The follow truth table should make it clearer.
120  //
121  // we're allowed to lie about a constant being a variable
122  // but we can't even claim that a variable is a constant
123  // or say something is a constant when it isn't.
124  //
125  // Node Return Value
126  // CONST true allowed
127  // VAR false REQUIRED
128  // CONST false allowed
129  // VAR true NOT allowed
130  virtual bool isConstant() const;
131 
132  // ----------------- TEMPLATED VISITOR -----------------//
133  // Visit each child node in a depth-first order and
134  // apply the function object F on it.
135  template <class F> void depthFirstVisit(F& func) const {
136  unsigned n = nArgs();
137  for (unsigned i = 0; i != n; i++) {
138  const nptr_t& arg_i = this->arg(i);
139  arg_i->depthFirstVisit<F>(func);
140  }
141  func(this);
142  }
143 
144  // Get the variables in this node's support.
145  void getSupportVars(nodeset_t& sup);
146 
147  // -------------------- VISITOR ----------------------//
148  void visit(NodeVisitorI& vi);
149 
150  // number of operands.
151  virtual unsigned nArgs() const;
152 
153  // operand i.
154  virtual nptr_t arg(unsigned i) const;
155 
156  // helper function which walks through child nodes in AST
157  // and returns true if any "synthesis" construct are present.
158  bool hasSynthesisConstructs() const;
159 
160  // --------------- STATIC HELPERS --------------------//
161  static nptr_t ite(const nptr_t& cond, const nptr_t& t, const nptr_t& f);
162 
163  friend class NodeRef;
164  friend std::size_t hash_value(const Node& nref);
165  friend class FuncReduction;
166 };
167 
168 std::ostream& operator<<(std::ostream& out, const Node& that);
169 
170 // are two nodes equal?
171 bool nodeEqual(const Node* left, const Node* right);
172 // returns the hash value of this node.
173 size_t nodeHash(const Node* n);
174 
175 typedef std::unordered_map<const Node*, nptr_t, decltype(&nodeHash),
176  decltype(&nodeEqual)>
178 
179 // ---------------------------------------------------------------------- //
180 struct NodeVisitorI {
181  virtual void preVisit(const Node*);
182  virtual void postVisit(const Node*);
183 };
184 } // namespace ilasynth
185 
186 #endif
virtual bool isConstant() const
NodeType getType()
Definition: node.hpp:103
virtual void preVisit(const Node *)
std::string & getName()
Definition: node.hpp:94
size_t hash_value
Definition: node.hpp:75
nptr_t init
Definition: node.hpp:33
void depthFirstVisit(F &func) const
Definition: node.hpp:135
npair_t(const nptr_t &v, const nptr_t &n, const nptr_t &i, const npair_t *p)
Definition: node.hpp:43
~npair_t()
Definition: node.hpp:51
virtual void postVisit(const Node *)
std::string refName
Definition: node.hpp:69
bool hash_inited
Definition: node.hpp:73
Definition: type.hpp:11
void getSupportVars(nodeset_t &sup)
npair_t(const npair_t *p)
Definition: node.hpp:46
std::vector< nptr_t > nptr_vec_t
Definition: node.hpp:27
std::string name
Definition: node.hpp:67
std::map< std::string, npair_t > nmap_t
Definition: node.hpp:53
virtual std::ostream & write(std::ostream &out) const
virtual bool equal(const Node *that) const
npair_t(const nptr_t &v, const nptr_t &n)
Definition: node.hpp:40
Definition: funcReduct.hpp:13
virtual py::object getValue() const
nptr_t next
Definition: node.hpp:36
static void _getSupportVarsHelper(nodeset_t &supp, const Node *n)
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
bool nodeEqual(const Node *left, const Node *right)
nptr_t ipred
Definition: node.hpp:34
static int totalObjCnt
Definition: node.hpp:57
static nptr_t ite(const nptr_t &cond, const nptr_t &t, const nptr_t &f)
size_t nodeHash(const Node *n)
int getId()
Definition: node.hpp:102
Definition: abstraction.hpp:21
nptr_vec_t next_vec
Definition: node.hpp:35
bool hasSynthesisConstructs() const
Definition: node.hpp:55
void setRefName(const std::string &refN)
Definition: node.hpp:100
void visit(NodeVisitorI &vi)
const std::string & getRefName() const
Definition: node.hpp:98
virtual unsigned nArgs() const
Definition: node.hpp:180
const std::string & getName() const
Definition: node.hpp:96
std::set< const Node * > nodeset_t
Definition: node.hpp:29
std::vector< const Node * > nodevec_t
Definition: node.hpp:28
Definition: ast.hpp:29
virtual Node * clone() const
const int id
Definition: node.hpp:71
virtual ~Node()
nptr_t var
Definition: node.hpp:32
NodeType type
Definition: node.hpp:82
virtual nptr_t arg(unsigned i) const
std::ostream & operator<<(std::ostream &out, const Node &that)
const npair_t * parent
Definition: node.hpp:37
Definition: node.hpp:31
std::unordered_map< const Node *, nptr_t, decltype(&nodeHash), decltype(&nodeEqual)> rwmap_t
Definition: node.hpp:177