ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
bitvec.hpp
Go to the documentation of this file.
1 
2 #ifndef __AST_BITVEC_HPP_DEFINED__
3 #define __AST_BITVEC_HPP_DEFINED__
4 
5 #include <boost/lexical_cast.hpp>
6 #include <boost/multiprecision/cpp_int.hpp>
7 #include <boost/python.hpp>
8 #include <boost/shared_ptr.hpp>
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 
13 #include <assert.h>
14 #include <ilasynth/ast/node.hpp>
15 #include <ilasynth/type.hpp>
16 #include <z3++.h>
17 
18 #include <ilasynth/ast.hpp>
19 
20 namespace ilasynth {
21 class Abstraction;
22 class FuncReduction;
23 
24 // ---------------------------------------------------------------------- //
25 // Bitvector expressions are derived from this class.
26 class BitvectorExpr : public Node {
27 public:
28  // constructor.
29  BitvectorExpr(int width);
30  // constructor for ChoiceExpr.
32  // destructor.
33  virtual ~BitvectorExpr();
34 };
35 
36 // ---------------------------------------------------------------------- //
37 // Bitvector variables.
38 class BitvectorVar : public BitvectorExpr {
39 public:
40  // constructor.
41  BitvectorVar(const std::string& n, int width);
42  // destructor.
43  virtual ~BitvectorVar();
44  // clone.
45  virtual Node* clone() const;
46  // equality method.
47  virtual bool equal(const Node* that) const;
48  // stream output.
49  virtual std::ostream& write(std::ostream& out) const;
50 };
51 
52 // ---------------------------------------------------------------------- //
53 // Bitvector constants.
54 class BitvectorConst : public BitvectorExpr {
55 protected:
57 
58 public:
59  // constructor with longs.
60  BitvectorConst(const mp_int_t& v, int width);
61  // constructor with ints.
62  BitvectorConst(unsigned int v, int width);
63  // copy constructor.
64  BitvectorConst(const BitvectorConst& other);
65  // destructor.
66  virtual ~BitvectorConst();
67  // clone
68  virtual Node* clone() const;
69  // equality method.
70  virtual bool equal(const Node* that) const;
71  // get value.
72  const mp_int_t& val() const { return value; }
73  // return value.
74  virtual py::object getValue() const;
75  // stream output.
76  virtual std::ostream& write(std::ostream& out) const;
77  // get the value as a string.
78  std::string vstr() const { return boost::lexical_cast<std::string>(value); }
79 };
80 
81 // ---------------------------------------------------------------------- //
82 // Bitvector operators.
83 class BitvectorOp : public BitvectorExpr {
84 public:
85  // Number of operands.
87 
88  // What is the operation?
89  enum Op {
90  // invalid
92  // unary
100  // binary.
104  OR,
122  // ternary
123  IF,
125  } op;
126 
127  static const std::string operatorNames[];
128 
129 private:
130  // the operands themselves.
132  std::vector<int> params;
133 
134  // Don't forget to update these helper functions below.
135  static bool isUnary(Op op) { return op >= NEGATE && op <= EXTRACT; }
136  static bool isBinary(Op op) { return op >= ADD && op <= READMEMBLOCK; }
137  static bool isTernary(Op op) { return op >= IF && op <= IF; }
138  static bool isNary(Op op) { return op >= APPLY_FUNC && op <= APPLY_FUNC; }
139  // helper functions to determine result and argument types.
140  static int getUnaryResultWidth(Op op, const nptr_t& n);
141  static int getBinaryResultWidth(Op op, const nptr_t& n1, const nptr_t& n2);
142  static int getBinaryResultWidth(Op op, const nptr_t& n1, const nptr_t& n2,
143  int param);
144  static int getBinaryResultWidth(Op op, const nptr_t& n1, int param);
145  static int getNaryResultWidth(Op op, nptr_vec_t& args);
146  static int getNaryResultWidth(Op op, nptr_vec_t& args,
147  std::vector<int>& params);
148 
149  static bool checkUnaryOpWidth(Op op, const nptr_t& n, int width);
150  static int checkBinaryOpWidth(Op op, const nptr_t& n1, const nptr_t& n2,
151  int width);
152  static int checkBinaryOpWidth(Op op, const nptr_t& n1, const nptr_t& n2,
153  int param, int width);
154  static int checkBinaryOpWidth(Op op, const nptr_t& n1, int param, int width);
155  static int checkNaryOpWidth(Op op, nptr_vec_t& args, int width);
156  static int checkNaryOpWidth(Op op, nptr_vec_t& args, std::vector<int>& params,
157  int width);
158 
159 public:
160  // constructors.
161  // Unary op
162  BitvectorOp(Op op, const nptr_t& n1);
163  BitvectorOp(Op op, const nptr_t& n1, int param);
164  BitvectorOp(Op op, const nptr_t& n1, int p1, int p2);
165  // Binary op
166  BitvectorOp(Op op, const nptr_t& n1, const nptr_t& n2);
167  // Binary op with params (read-block)
168  BitvectorOp(Op op, const nptr_t& n1, const nptr_t& n2, int blocks,
169  endianness_t e);
170  // Ternary op
171  BitvectorOp(Op op, nptr_vec_t& args_);
172  // copy-constructor with a fresh set of args.
173  BitvectorOp(const BitvectorOp* other, nptr_vec_t& args_);
174  // destructors.
175  virtual ~BitvectorOp();
176 
177  // clone.
178  virtual Node* clone() const;
179 
180  // equality method.
181  virtual bool equal(const Node* that) const;
182 
183  // stream output.
184  virtual std::ostream& write(std::ostream& out) const;
185 
186  // number of operands.
187  virtual unsigned nArgs() const;
188 
189  // operand i.
190  virtual nptr_t arg(unsigned i) const;
191 
192  Op getOp() const { return op; }
193 
194  // number of params
195  unsigned nParams() const;
196 
197  // the ith param.
198  int param(unsigned i) const;
199 
200  friend class FuncReduction;
201 };
202 } // namespace ilasynth
203 #endif
static bool checkUnaryOpWidth(Op op, const nptr_t &n, int width)
Definition: bitvec.hpp:83
Definition: bitvec.hpp:94
Arity
Definition: bitvec.hpp:86
virtual nptr_t arg(unsigned i) const
Definition: bitvec.hpp:117
Definition: bitvec.hpp:119
Definition: bitvec.hpp:101
Definition: bitvec.hpp:86
Definition: bitvec.hpp:95
Definition: bitvec.hpp:86
virtual bool equal(const Node *that) const
virtual Node * clone() const
Definition: bitvec.hpp:104
Definition: bitvec.hpp:124
Definition: bitvec.hpp:115
enum ilasynth::BitvectorOp::Op op
Definition: bitvec.hpp:123
Definition: bitvec.hpp:118
Definition: bitvec.hpp:103
int param(unsigned i) const
std::string vstr() const
Definition: bitvec.hpp:78
Definition: type.hpp:11
static int checkNaryOpWidth(Op op, nptr_vec_t &args, int width)
Definition: bitvec.hpp:99
Definition: bitvec.hpp:98
static int getBinaryResultWidth(Op op, const nptr_t &n1, const nptr_t &n2)
std::vector< nptr_t > nptr_vec_t
Definition: node.hpp:27
virtual std::ostream & write(std::ostream &out) const
Definition: bitvec.hpp:93
nptr_vec_t args
Definition: bitvec.hpp:131
Definition: bitvec.hpp:121
boost::multiprecision::cpp_int mp_int_t
Definition: common.hpp:9
Definition: funcReduct.hpp:13
static bool isTernary(Op op)
Definition: bitvec.hpp:137
Definition: bitvec.hpp:97
Definition: bitvec.hpp:108
Definition: bitvec.hpp:120
static int getNaryResultWidth(Op op, nptr_vec_t &args)
Definition: bitvec.hpp:106
Definition: bitvec.hpp:113
Definition: bitvec.hpp:109
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
Op getOp() const
Definition: bitvec.hpp:192
Definition: bitvec.hpp:91
Definition: bitvec.hpp:111
virtual Node * clone() const
Definition: bitvec.hpp:107
virtual std::ostream & write(std::ostream &out) const
mp_int_t value
Definition: bitvec.hpp:56
virtual std::ostream & write(std::ostream &out) const
Definition: bitvec.hpp:102
static bool isNary(Op op)
Definition: bitvec.hpp:138
Definition: bitvec.hpp:96
static const std::string operatorNames[]
Definition: bitvec.hpp:127
static bool isBinary(Op op)
Definition: bitvec.hpp:136
Definition: abstraction.hpp:21
const mp_int_t & val() const
Definition: bitvec.hpp:72
Definition: bitvec.hpp:26
Definition: node.hpp:55
Definition: bitvec.hpp:38
virtual bool equal(const Node *that) const
BitvectorVar(const std::string &n, int width)
enum ilasynth::BitvectorOp::Arity arity
Definition: bitvec.hpp:86
static int checkBinaryOpWidth(Op op, const nptr_t &n1, const nptr_t &n2, int width)
BitvectorOp(Op op, const nptr_t &n1)
Definition: bitvec.hpp:105
Definition: bitvec.hpp:116
endianness_t
Definition: common.hpp:10
unsigned nParams() const
Definition: bitvec.hpp:86
virtual bool equal(const Node *that) const
virtual py::object getValue() const
std::vector< int > params
Definition: bitvec.hpp:132
Definition: bitvec.hpp:54
Definition: bitvec.hpp:112
Definition: bitvec.hpp:110
static int getUnaryResultWidth(Op op, const nptr_t &n)
virtual unsigned nArgs() const
Op
Definition: bitvec.hpp:89
Definition: bitvec.hpp:114
static bool isUnary(Op op)
Definition: bitvec.hpp:135
BitvectorConst(const mp_int_t &v, int width)
virtual Node * clone() const