ilang  1.1.4
ILAng: A Modeling and Verification Platform for SoCs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros
expr.h
Go to the documentation of this file.
1 
4 #ifndef ILANG_ILA_AST_EXPR_H__
5 #define ILANG_ILA_AST_EXPR_H__
6 
7 #include <memory>
8 #include <ostream>
9 #include <string>
10 #include <unordered_map>
11 #include <unordered_set>
12 #include <vector>
13 
14 #include <z3++.h>
15 #include <z3_api.h>
16 
17 #include <ilang/ila/ast/ast.h>
18 #include <ilang/ila/ast/sort.h>
19 
21 namespace ilang {
22 
25 class Expr : public Ast, public std::enable_shared_from_this<Expr> {
26 public:
28  typedef std::shared_ptr<Expr> ExprPtr;
30  typedef std::vector<ExprPtr> ExprPtrVec;
31 
32 protected:
34  typedef std::vector<z3::expr> Z3ExprVec;
35 
36 public:
37  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
39  Expr();
41  Expr(const std::string& name);
43  virtual ~Expr();
44 
45  // ------------------------- ACCESSORS/MUTATORS --------------------------- //
47  inline const SortPtr sort() const { return sort_; }
49  inline size_t arg_num() const { return args_.size(); }
51  inline ExprPtr arg(const size_t& i) const { return args_.at(i); }
53  inline size_t param_num() const { return params_.size(); }
55  inline int param(const size_t& i) const { return params_.at(i); }
56 
58  void set_sort(const SortPtr sort);
60  void set_args(const ExprPtrVec& args);
62  void set_params(const std::vector<int> params);
64  void replace_arg(const int& idx, const ExprPtr& arg);
66  void replace_arg(const ExprPtr& a, const ExprPtr& b);
67 
69  bool is_expr() const { return true; }
70 
72  virtual bool is_const() const { return false; }
74  virtual bool is_var() const { return false; }
76  virtual bool is_op() const { return false; }
77 
79  inline bool is_bool() const { return sort_->is_bool(); }
81  inline bool is_bv(const int& width = 0) const { return sort_->is_bv(width); }
83  inline bool is_mem() const { return sort_->is_mem(); }
84 
85  // ------------------------- METHODS -------------------------------------- //
87  virtual z3::expr GetZ3Expr(z3::context& z3_ctx, const Z3ExprVec& z3expr_vec,
88  const std::string& suffix = "") const = 0;
89 
91  virtual std::ostream& Print(std::ostream& out) const = 0;
92 
94  friend std::ostream& operator<<(std::ostream& out, const ExprPtr& expr) {
95  return expr->Print(out);
96  }
97 
100  template <class F> void DepthFirstVisit(F& func) {
101  for (size_t i = 0; i != arg_num(); i++) {
102  auto arg_i = this->arg(i);
103  arg_i->DepthFirstVisit<F>(func);
104  }
105  func(shared_from_this());
106  }
107 
110  template <class F> void DepthFirstVisitPrePost(F& func) {
111  // pre check
112  if (func.pre(shared_from_this())) { // break if return true
113  return;
114  }
115  // traverse child
116  for (size_t i = 0; i != arg_num(); i++) {
117  auto arg_i = this->arg(i);
118  arg_i->DepthFirstVisitPrePost<F>(func);
119  }
120  // post
121  func.post(shared_from_this());
122  }
123 
124 private:
125  // ------------------------- MEMBERS -------------------------------------- //
127  SortPtr sort_;
129  ExprPtrVec args_;
131  std::vector<int> params_;
132 
133  // ------------------------- HELPERS -------------------------------------- //
134 
135 }; // class Expr
136 
141 
144 class ExprHash {
145 public:
147  size_t operator()(const ExprPtr& expr) const { return expr->name().id(); }
148 }; // class ExprHash
149 
151 typedef std::unordered_map<const ExprPtr, const ExprPtr, ExprHash> ExprMap;
153 typedef std::unordered_set<ExprPtr, ExprHash> ExprSet;
154 
155 } // namespace ilang
156 
157 #endif // ILANG_ILA_AST_EXPR_H__
Expr()
Default constructor.
std::shared_ptr< Expr > ExprPtr
Pointer type for normal use of Expr.
Definition: expr.h:28
bool is_expr() const
Is type expr (object).
Definition: expr.h:69
Expr::ExprPtr ExprPtr
Pointer type for normal use of Expr.
Definition: expr.h:138
size_t param_num() const
Return the number of parameters.
Definition: expr.h:53
The class for expression, which is the basic type for variables, constraints, state update expression...
Definition: expr.h:25
ExprPtr arg(const size_t &i) const
Return the i-th argument.
Definition: expr.h:51
Sort::SortPtr SortPtr
Pointer type for storing/passing Sort.
Definition: sort.h:82
void set_params(const std::vector< int > params)
Set the parameters.
const SortPtr sort() const
Return the pointer of the sort.
Definition: expr.h:47
The function object for hashing Expr. The hash value is the id of the symbol, which is supposed to be...
Definition: expr.h:144
std::vector< z3::expr > Z3ExprVec
Vector type for z3 expression.
Definition: defines.h:19
std::unordered_set< ExprPtr, ExprHash > ExprSet
Type for storing a set of Expr.
Definition: expr.h:153
size_t operator()(const ExprPtr &expr) const
Function object for hashing.
Definition: expr.h:147
std::vector< ExprPtr > ExprPtrVec
Type for storing a set of Expr.
Definition: expr.h:30
bool is_bv(const int &width=0) const
Return true if this is a Bitvector expression.
Definition: expr.h:81
int param(const size_t &i) const
Return the i-th paramter.
Definition: expr.h:55
void DepthFirstVisitPrePost(F &func)
Templated visitor: visit each node in a depth-first order and apply the function object F pre/pose on...
Definition: expr.h:110
std::vector< z3::expr > Z3ExprVec
Vector type for z3 expression.
Definition: expr.h:34
virtual z3::expr GetZ3Expr(z3::context &z3_ctx, const Z3ExprVec &z3expr_vec, const std::string &suffix="") const =0
Return the z3 expression for the node.
void DepthFirstVisit(F &func)
Templated visitor: visit each node in a depth-first order and apply the function object F on it...
Definition: expr.h:100
The class for the Abstract Syntax Tree. An Ast object can be an expression or function definition (in...
Definition: ast.h:20
virtual bool is_op() const
Return true if this is an operation.
Definition: expr.h:76
std::unordered_map< const ExprPtr, const ExprPtr, ExprHash > ExprMap
Type for mapping between Expr.
Definition: expr.h:151
virtual bool is_var() const
Return true if this is a variable.
Definition: expr.h:74
Expr::ExprPtrVec ExprPtrVec
Type for storing a set of Expr.
Definition: expr.h:140
friend std::ostream & operator<<(std::ostream &out, const ExprPtr &expr)
Overload output stream operator for pointer.
Definition: expr.h:94
void replace_arg(const int &idx, const ExprPtr &arg)
Replace the i-th argument.
void set_args(const ExprPtrVec &args)
Set the arguments.
size_t arg_num() const
Retrun the number of argument (arity).
Definition: expr.h:49
void set_sort(const SortPtr sort)
Set the sort of the expression.
virtual bool is_const() const
Return true if this is a constant.
Definition: expr.h:72
bool is_mem() const
Return true if this is an Array expression.
Definition: expr.h:83
bool is_bool() const
Return true if this is a Boolean expression.
Definition: expr.h:79
virtual std::ostream & Print(std::ostream &out) const =0
Output to stream.
virtual ~Expr()
Default destructor.
const Symbol & name() const
Get the symbol (name).