ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
choice.hpp
Go to the documentation of this file.
1 #ifndef __AST_SYN_HPP_DEFINED__
2 #define __AST_SYN_HPP_DEFINED__
3 
5 #include <ilasynth/ast/bool.hpp>
6 #include <ilasynth/ast/mem.hpp>
7 #include <ilasynth/ast/node.hpp>
8 #include <ilasynth/common.hpp>
9 #include <ilasynth/type.hpp>
10 
11 namespace ilasynth {
12 class Abstraction;
13 
14 // ---------------------------------------------------------------------- //
15 struct Choice {
16  // the operands themselves.
18  // vector of names for boolean variables.
19  std::vector<std::string> choiceVars;
20  // constructor.
21  Choice(const std::string& name, const nptr_vec_t& args);
22  // kind of a copy constructor.
23  Choice(const Choice* that, const nptr_vec_t& args_);
24  // destructor.
25  ~Choice();
26 
27  // check types and return the result type.
28  static NodeType getChoiceType(const nptr_vec_t& args);
29 
30  // check equality.
31  bool equal(const Choice& that) const;
32 
33  // dump to stream.
34  std::ostream& write(std::ostream& out) const;
35 
36  // choice variable name.
37  const char* getChoiceVarName(unsigned i) const {
38  if ((i + 1) < args.size()) {
39  return choiceVars[i].c_str();
40  } else {
41  return NULL;
42  }
43  }
44 };
45 
46 // ---------------------------------------------------------------------- //
47 template <typename T> class ChoiceExpr : public T {
48 protected:
50 
51 public:
52  // constructor.
53  ChoiceExpr(const std::string& n_, const nptr_vec_t& args_)
54  : T(Choice::getChoiceType(args_)), choice(n_, args_) {
55  this->name = n_;
56  }
57 
58  // destructor
59  virtual ~ChoiceExpr() {}
60 
61  // clone.
62  virtual Node* clone() const {
63  return new ChoiceExpr(this->name, choice.args);
64  }
65 
66  // clone with new args.
67  ChoiceExpr* clone(const nptr_vec_t& args) const {
68  return new ChoiceExpr(this->name, args);
69  }
70 
71  // equal.
72  virtual bool equal(const Node* that_) const {
73  const ChoiceExpr<T>* that = dynamic_cast<const ChoiceExpr<T>*>(that_);
74  if (that == NULL)
75  return false;
76  else
77  return choice.equal(that->choice);
78  }
79 
80  // write to stream.
81  virtual std::ostream& write(std::ostream& out) const {
82  return choice.write(out);
83  }
84 
85  // number of arguments.
86  virtual unsigned nArgs() const { return choice.args.size(); }
87 
88  // return arg i.
89  virtual nptr_t arg(unsigned i) const {
90  if (i < choice.args.size()) {
91  return choice.args[i];
92  } else {
93  return NULL;
94  }
95  }
96 
97  // return the SMT variable name.
98  const char* getChoiceVarName(unsigned i) const {
99  return choice.getChoiceVarName(i);
100  }
101 };
102 
103 // ---------------------------------------------------------------------- //
107 
108 // ---------------------------------------------------------------------- //
109 class ReadSlice : public BitvectorChoice {
110 private:
111  // private constructor: called by the static function.
112  ReadSlice(const std::string& name, const nptr_vec_t& args, const nptr_t& bv,
113  int width, int incr);
114 
115 public:
116  // destructor.
117  virtual ~ReadSlice();
118 
119  // the bitvector.
121  // slice width.
122  int width;
123  // the increment between slices.
125 
126  // factory method.
127  static ReadSlice* createReadSlice(const std::string& name, const nptr_t& bv,
128  int width, int incr);
129 
130  // clone.
131  virtual Node* clone() const;
132 
133  // stream output
134  virtual std::ostream& write(std::ostream& out) const;
135 };
136 
137 // ---------------------------------------------------------------------- //
138 class WriteSlice : public BitvectorChoice {
139 private:
140  // private constructor: called by the static function.
141  WriteSlice(const std::string& name, const nptr_vec_t& args, const nptr_t& bv,
142  const nptr_t& wr, int incr);
143 
144 public:
145  // destructor.
146  virtual ~WriteSlice();
147  // the bitvector.
149  // the thing to replace it with.
151  // the increment for the choices of slice to write.
153 
154  // factory method.
155  static WriteSlice* createWriteSlice(const std::string& name, const nptr_t& bv,
156  const nptr_t& wr, int incr);
157 
158  // clone.
159  virtual Node* clone() const;
160 
161  // stream output
162  virtual std::ostream& write(std::ostream& out) const;
163 };
164 } // namespace ilasynth
165 
166 #endif
static ReadSlice * createReadSlice(const std::string &name, const nptr_t &bv, int width, int incr)
int width
Definition: choice.hpp:122
std::vector< std::string > choiceVars
Definition: choice.hpp:19
virtual Node * clone() const
Definition: choice.hpp:62
ChoiceExpr< BoolExpr > BoolChoice
Definition: choice.hpp:105
Choice choice
Definition: choice.hpp:49
nptr_t bitvec
Definition: choice.hpp:148
WriteSlice(const std::string &name, const nptr_vec_t &args, const nptr_t &bv, const nptr_t &wr, int incr)
Definition: choice.hpp:138
ChoiceExpr< MemExpr > MemChoice
Definition: choice.hpp:106
Definition: type.hpp:11
virtual std::ostream & write(std::ostream &out) const
nptr_t bitvec
Definition: choice.hpp:120
std::vector< nptr_t > nptr_vec_t
Definition: node.hpp:27
std::ostream & write(std::ostream &out) const
Definition: choice.hpp:109
Definition: choice.hpp:47
ReadSlice(const std::string &name, const nptr_vec_t &args, const nptr_t &bv, int width, int incr)
virtual std::ostream & write(std::ostream &out) const
Definition: choice.hpp:81
static NodeType getChoiceType(const nptr_vec_t &args)
nptr_t data
Definition: choice.hpp:150
virtual ~ChoiceExpr()
Definition: choice.hpp:59
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
virtual Node * clone() const
bool equal(const Choice &that) const
ChoiceExpr * clone(const nptr_vec_t &args) const
Definition: choice.hpp:67
Definition: abstraction.hpp:21
Definition: node.hpp:55
virtual Node * clone() const
ChoiceExpr(const std::string &n_, const nptr_vec_t &args_)
Definition: choice.hpp:53
const char * getChoiceVarName(unsigned i) const
Definition: choice.hpp:37
int increment
Definition: choice.hpp:152
virtual unsigned nArgs() const
Definition: choice.hpp:86
virtual bool equal(const Node *that_) const
Definition: choice.hpp:72
static WriteSlice * createWriteSlice(const std::string &name, const nptr_t &bv, const nptr_t &wr, int incr)
int increment
Definition: choice.hpp:124
nptr_vec_t args
Definition: choice.hpp:17
Choice(const std::string &name, const nptr_vec_t &args)
ChoiceExpr< BitvectorExpr > BitvectorChoice
Definition: choice.hpp:104
virtual nptr_t arg(unsigned i) const
Definition: choice.hpp:89
virtual std::ostream & write(std::ostream &out) const
const char * getChoiceVarName(unsigned i) const
Definition: choice.hpp:98
Definition: choice.hpp:15